1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.teststeps.assertions;
14
15 import org.apache.xmlbeans.XmlObject;
16
17 import com.eviware.soapui.config.RequestAssertionConfig;
18 import com.eviware.soapui.impl.wsdl.WsdlInterface;
19 import com.eviware.soapui.impl.wsdl.WsdlOperation;
20 import com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange;
21 import com.eviware.soapui.impl.wsdl.support.assertions.Assertable;
22 import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlContext;
23 import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlValidator;
24 import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
25 import com.eviware.soapui.model.iface.SubmitContext;
26 import com.eviware.soapui.support.UISupport;
27 import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
28 import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
29
30 /***
31 * Asserts that a request or response message complies with its related
32 * WSDL definition / XML Schema
33 *
34 * @author Ole.Matzura
35 */
36
37 public class SchemaComplianceAssertion extends WsdlMessageAssertion implements RequestAssertion, ResponseAssertion
38 {
39 public static final String ID = "Schema Compliance";
40 private String definition;
41
42 public SchemaComplianceAssertion(RequestAssertionConfig assertionConfig, Assertable assertable)
43 {
44 super(assertionConfig, assertable,false, true);
45
46 XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( getConfiguration() );
47 definition = reader.readString( "definition", null );
48 }
49
50 protected String internalAssertResponse(WsdlMessageExchange messageExchange, SubmitContext context) throws AssertionException
51 {
52 WsdlContext wsdlContext = getWsdlContext( messageExchange );
53 WsdlValidator validator = new WsdlValidator( wsdlContext );
54
55 try
56 {
57 AssertionError[] errors = validator.assertResponse( messageExchange, false );
58 if (errors.length > 0)
59 throw new AssertionException(errors);
60 }
61 catch( AssertionException e )
62 {
63 throw e;
64 }
65 catch (Exception e)
66 {
67 throw new AssertionException( new AssertionError( e.getMessage() ));
68 }
69
70 return "Schema compliance OK";
71 }
72
73 private WsdlContext getWsdlContext( WsdlMessageExchange messageExchange )
74 {
75 WsdlOperation operation = messageExchange.getOperation();
76 WsdlContext wsdlContext = null;
77 if( definition == null || definition.trim().length() == 0 || definition.equals( operation.getInterface().getDefinition() ))
78 {
79 wsdlContext = ((WsdlInterface)operation.getInterface()).getWsdlContext();
80 }
81 else
82 {
83 wsdlContext = new WsdlContext( definition, ((WsdlInterface)operation.getInterface()).getSoapVersion(), null,
84 (WsdlInterface) operation.getInterface() );
85 }
86 return wsdlContext;
87 }
88
89 public boolean configure()
90 {
91 String value = definition;
92
93 WsdlInterface iface = getAssertable().getInterface();
94 String orgDef = iface == null ? null : iface.getDefinition();
95
96 if( value == null || value.trim().length() == 0 )
97 {
98 value = orgDef;
99 }
100
101 value = UISupport.prompt( "Specify defintion url to validate by", "Configure SchemaCompliance Assertion", value );
102
103 if( value == null ) return false;
104
105 if( value.trim().length() == 0 || value.equals( orgDef ))
106 definition = "";
107 else
108 definition = value;
109
110 setConfiguration( createConfiguration() );
111 return true;
112 }
113
114 protected XmlObject createConfiguration()
115 {
116 XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
117 return builder.add( "definition", definition ).finish();
118 }
119
120 protected String internalAssertRequest( WsdlMessageExchange messageExchange, SubmitContext context ) throws AssertionException
121 {
122 WsdlContext wsdlContext = getWsdlContext( messageExchange );
123 WsdlValidator validator = new WsdlValidator( wsdlContext );
124
125 try
126 {
127 AssertionError[] errors = validator.assertRequest( messageExchange, false );
128 if (errors.length > 0)
129 throw new AssertionException(errors);
130 }
131 catch( AssertionException e )
132 {
133 throw e;
134 }
135 catch (Exception e)
136 {
137 throw new AssertionException( new AssertionError( e.getMessage() ));
138 }
139
140 return "Schema compliance OK";
141 }
142 }