001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.teststeps.assertions;
014:
015: import org.apache.xmlbeans.XmlObject;
016:
017: import com.eviware.soapui.config.RequestAssertionConfig;
018: import com.eviware.soapui.impl.wsdl.WsdlInterface;
019: import com.eviware.soapui.impl.wsdl.WsdlOperation;
020: import com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange;
021: import com.eviware.soapui.impl.wsdl.support.assertions.Assertable;
022: import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlContext;
023: import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlValidator;
024: import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
025: import com.eviware.soapui.model.iface.SubmitContext;
026: import com.eviware.soapui.support.UISupport;
027: import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
028: import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
029:
030: /**
031: * Asserts that a request or response message complies with its related
032: * WSDL definition / XML Schema
033: *
034: * @author Ole.Matzura
035: */
036:
037: public class SchemaComplianceAssertion extends WsdlMessageAssertion
038: implements RequestAssertion, ResponseAssertion {
039: public static final String ID = "Schema Compliance";
040: private String definition;
041:
042: public SchemaComplianceAssertion(
043: RequestAssertionConfig assertionConfig,
044: Assertable assertable) {
045: super (assertionConfig, assertable, false, true);
046:
047: XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader(
048: getConfiguration());
049: definition = reader.readString("definition", null);
050: }
051:
052: protected String internalAssertResponse(
053: WsdlMessageExchange messageExchange, SubmitContext context)
054: throws AssertionException {
055: WsdlContext wsdlContext = getWsdlContext(messageExchange);
056: WsdlValidator validator = new WsdlValidator(wsdlContext);
057:
058: try {
059: AssertionError[] errors = validator.assertResponse(
060: messageExchange, false);
061: if (errors.length > 0)
062: throw new AssertionException(errors);
063: } catch (AssertionException e) {
064: throw e;
065: } catch (Exception e) {
066: throw new AssertionException(new AssertionError(e
067: .getMessage()));
068: }
069:
070: return "Schema compliance OK";
071: }
072:
073: private WsdlContext getWsdlContext(
074: WsdlMessageExchange messageExchange) {
075: WsdlOperation operation = messageExchange.getOperation();
076: WsdlContext wsdlContext = null;
077: if (definition == null
078: || definition.trim().length() == 0
079: || definition.equals(operation.getInterface()
080: .getDefinition())) {
081: wsdlContext = ((WsdlInterface) operation.getInterface())
082: .getWsdlContext();
083: } else {
084: wsdlContext = new WsdlContext(definition,
085: ((WsdlInterface) operation.getInterface())
086: .getSoapVersion(), null,
087: (WsdlInterface) operation.getInterface());
088: }
089: return wsdlContext;
090: }
091:
092: public boolean configure() {
093: String value = definition;
094:
095: WsdlInterface iface = getAssertable().getInterface();
096: String orgDef = iface == null ? null : iface.getDefinition();
097:
098: if (value == null || value.trim().length() == 0) {
099: value = orgDef;
100: }
101:
102: value = UISupport.prompt(
103: "Specify defintion url to validate by",
104: "Configure SchemaCompliance Assertion", value);
105:
106: if (value == null)
107: return false;
108:
109: if (value.trim().length() == 0 || value.equals(orgDef))
110: definition = "";
111: else
112: definition = value;
113:
114: setConfiguration(createConfiguration());
115: return true;
116: }
117:
118: protected XmlObject createConfiguration() {
119: XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
120: return builder.add("definition", definition).finish();
121: }
122:
123: protected String internalAssertRequest(
124: WsdlMessageExchange messageExchange, SubmitContext context)
125: throws AssertionException {
126: WsdlContext wsdlContext = getWsdlContext(messageExchange);
127: WsdlValidator validator = new WsdlValidator(wsdlContext);
128:
129: try {
130: AssertionError[] errors = validator.assertRequest(
131: messageExchange, false);
132: if (errors.length > 0)
133: throw new AssertionException(errors);
134: } catch (AssertionException e) {
135: throw e;
136: } catch (Exception e) {
137: throw new AssertionException(new AssertionError(e
138: .getMessage()));
139: }
140:
141: return "Schema compliance OK";
142: }
143: }
|