View Javadoc

1   /*
2    *  soapui, copyright (C) 2005 Ole Matzura / eviware.com 
3    *
4    *  SoapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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 javax.swing.JOptionPane;
16  
17  import org.apache.log4j.Logger;
18  import org.apache.xmlbeans.XmlCursor;
19  import org.apache.xmlbeans.XmlObject;
20  
21  import com.eviware.soapui.SoapUI;
22  import com.eviware.soapui.config.RequestAssertionConfig;
23  import com.eviware.soapui.impl.wsdl.WsdlInterface;
24  import com.eviware.soapui.impl.wsdl.WsdlOperation;
25  import com.eviware.soapui.impl.wsdl.support.WsdlContext;
26  import com.eviware.soapui.impl.wsdl.support.WsdlValidator;
27  import com.eviware.soapui.impl.wsdl.teststeps.WsdlAssertion;
28  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
29  import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError;
30  
31  /***
32   * Asserts that a request or response message complies with its related 
33   * WSDL definition / XML Schema
34   * 
35   * @author Ole.Matzura
36   */
37  
38  public class SchemaComplianceAssertion extends WsdlAssertion
39  {
40  	private final static Logger log = Logger.getLogger( SchemaComplianceAssertion.class );
41  	private String definition;
42  	
43     public SchemaComplianceAssertion(RequestAssertionConfig assertionConfig, WsdlTestRequest request)
44     {
45        super(assertionConfig, request);
46        
47        XmlObject[] paths = getConfiguration().selectPath( "$this/definition" );
48        if( paths.length != 1 ) return;
49           
50        definition = paths[0].newCursor().getTextValue();
51     }
52     
53  	protected String assertRequest(WsdlTestRequest request) throws AssertionException
54  	{
55  		WsdlOperation operation = (WsdlOperation) request.getOperation();
56  		WsdlContext wsdlContext = null;
57  		if( definition == null || definition.trim().length() == 0)
58  		{
59  			wsdlContext = ((WsdlInterface)operation.getInterface()).getWsdlContext();
60  		}
61  		else
62  		{
63  			wsdlContext = new WsdlContext( definition );
64  		}
65  		
66  		WsdlValidator validator = new WsdlValidator( wsdlContext );
67  		
68  		try
69  		{
70  			AssertionError[] errors = validator.assertResponse(request
71  					.getResponseContent(), operation.getBindingOperationName());
72  			if (errors.length > 0)
73  				throw new AssertionException(errors);
74  		}
75  		catch( AssertionException e )
76  		{
77  			throw e;
78  		}
79  		catch (Exception e)
80  		{
81  			throw new AssertionException( new AssertionError( e.getMessage() ));
82  		}
83  		
84  		return "Schema compliance OK";
85  	}
86  	
87     public void configure()
88     {
89     	String value = definition;
90     	String orgDef = getRequest().getOperation().getInterface().getDefinition();
91     	if( value == null || value.trim().length() == 0 )
92     	{
93  			value = orgDef;
94     	}
95     	
96        value = (String) JOptionPane.showInputDialog( SoapUI.getInstance().getFrame(), 
97        		"Specify defintion url to validate by", 
98        		"Configure SchemaCompliance Assertion", JOptionPane.QUESTION_MESSAGE, null, 
99        		null, value );
100       
101       if( value == null ) return;
102       
103       if( value.trim().length() == 0 || value.equals( orgDef ))
104       	definition = "";
105       else
106       	definition = value;
107       
108       setConfiguration( createConfiguration() );
109    }
110 
111    protected XmlObject createConfiguration()
112    {
113       XmlObject config = XmlObject.Factory.newInstance();
114       XmlCursor cursor = config.newCursor();
115       cursor.toNextToken();
116       cursor.beginElement( "definition" );
117       cursor.insertChars( definition );
118       cursor.dispose();
119       return config;
120    }
121 
122    public boolean isConfigurable()
123    {
124       return true;
125    }
126 }