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