View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
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.wsdl.WsdlContext;
22  import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlValidator;
23  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
24  import com.eviware.soapui.model.iface.SubmitContext;
25  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
26  import com.eviware.soapui.model.testsuite.Assertable;
27  import com.eviware.soapui.model.testsuite.AssertionError;
28  import com.eviware.soapui.model.testsuite.AssertionException;
29  import com.eviware.soapui.model.testsuite.RequestAssertion;
30  import com.eviware.soapui.model.testsuite.ResponseAssertion;
31  import com.eviware.soapui.support.UISupport;
32  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
33  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
34  
35  /***
36   * Asserts that a request or response message complies with its related 
37   * WSDL definition / XML Schema
38   * 
39   * @author Ole.Matzura
40   */
41  
42  public class SchemaComplianceAssertion extends WsdlMessageAssertion implements RequestAssertion, ResponseAssertion
43  {
44  	public static final String ID = "Schema Compliance";
45  	private String definition;
46  	
47     public SchemaComplianceAssertion(RequestAssertionConfig assertionConfig, Assertable assertable)
48     {
49        super(assertionConfig, assertable,false, true, false, true);
50        
51        XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( getConfiguration() );
52        definition = reader.readString( "definition", null );
53     }
54     
55  	protected String internalAssertResponse(WsdlMessageExchange messageExchange, SubmitContext context) throws AssertionException
56  	{
57  		WsdlContext wsdlContext = getWsdlContext( messageExchange, context );
58  		WsdlValidator validator = new WsdlValidator( wsdlContext );
59  		
60  		try
61  		{
62  			AssertionError[] errors = validator.assertResponse( messageExchange, false );
63  			if (errors.length > 0)
64  				throw new AssertionException(errors);
65  		}
66  		catch( AssertionException e )
67  		{
68  			throw e;
69  		}
70  		catch (Exception e)
71  		{
72  			throw new AssertionException( new AssertionError( e.getMessage() ));
73  		}
74  		
75  		return "Schema compliance OK";
76  	}
77  
78  	private WsdlContext getWsdlContext( WsdlMessageExchange messageExchange, SubmitContext context )
79  	{
80  		WsdlOperation operation = messageExchange.getOperation();
81  		WsdlContext wsdlContext = null;
82  		if( definition == null || definition.trim().length() == 0 || definition.equals( operation.getInterface().getDefinition() ))
83  		{
84  			wsdlContext = ((WsdlInterface)operation.getInterface()).getWsdlContext();
85  		}
86  		else
87  		{
88  			String def = PropertyExpansionUtils.expandProperties( context, definition );
89  			wsdlContext = new WsdlContext( def, ((WsdlInterface)operation.getInterface()).getSoapVersion(), null, 
90  					(WsdlInterface) operation.getInterface() );
91  		}
92  		return wsdlContext;
93  	}
94  	
95     public boolean configure()
96     {
97     	String value = definition;
98     	
99     	WsdlInterface iface = ( WsdlInterface ) getAssertable().getInterface();
100    	String orgDef = iface == null ? null : iface.getDefinition();
101    		
102    	if( value == null || value.trim().length() == 0 )
103    	{
104 			value = orgDef;
105    	}
106    	
107       value = UISupport.prompt( "Specify definition url to validate by", "Configure SchemaCompliance Assertion", value );
108       
109       if( value == null ) return false;
110       
111       if( value.trim().length() == 0 || value.equals( orgDef ))
112       	definition = "";
113       else
114       	definition = value;
115       
116       setConfiguration( createConfiguration() );
117       return true;
118    }
119 
120    protected XmlObject createConfiguration()
121    {
122    	XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
123    	return builder.add( "definition", definition ).finish();
124    }
125 
126 	protected String internalAssertRequest( WsdlMessageExchange messageExchange, SubmitContext context ) throws AssertionException
127 	{
128 		WsdlContext wsdlContext = getWsdlContext( messageExchange, context );
129 		WsdlValidator validator = new WsdlValidator( wsdlContext );
130 		
131 		try
132 		{
133 			AssertionError[] errors = validator.assertRequest( messageExchange, false );
134 			if (errors.length > 0)
135 				throw new AssertionException(errors);
136 		}
137 		catch( AssertionException e )
138 		{
139 			throw e;
140 		}
141 		catch (Exception e)
142 		{
143 			throw new AssertionException( new AssertionError( e.getMessage() ));
144 		}
145 		
146 		return "Schema compliance OK";
147 	}
148 }