View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.wadl.support;
14  
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  import javax.xml.namespace.QName;
19  
20  import org.apache.xmlbeans.SchemaType;
21  import org.apache.xmlbeans.XmlError;
22  import org.apache.xmlbeans.XmlException;
23  import org.apache.xmlbeans.XmlObject;
24  import org.apache.xmlbeans.XmlOptions;
25  import org.w3c.dom.Document;
26  import org.w3c.dom.Element;
27  
28  import com.eviware.soapui.SoapUI;
29  import com.eviware.soapui.impl.rest.RestRepresentation;
30  import com.eviware.soapui.impl.rest.RestRequestInterface;
31  import com.eviware.soapui.impl.wadl.WadlDefinitionContext;
32  import com.eviware.soapui.impl.wsdl.submit.RestMessageExchange;
33  import com.eviware.soapui.model.testsuite.AssertionError;
34  
35  public class WadlValidator
36  {
37  	public WadlValidator( WadlDefinitionContext context )
38  	{
39  	}
40  
41  	public AssertionError[] assertResponse( RestMessageExchange messageExchange )
42  	{
43  		RestRequestInterface restRequest = messageExchange.getRestRequest();
44  		if( restRequest != null )
45  		{
46  			if( messageExchange.getResponseStatusCode() >= 400 )
47  			{
48  				return assertResponse( messageExchange, RestRepresentation.Type.FAULT );
49  			}
50  			else
51  			{
52  				return assertResponse( messageExchange, RestRepresentation.Type.RESPONSE );
53  			}
54  		}
55  
56  		return new AssertionError[0];
57  	}
58  
59  	private AssertionError[] assertResponse( RestMessageExchange messageExchange, RestRepresentation.Type type )
60  	{
61  		List<AssertionError> result = new ArrayList<AssertionError>();
62  		QName responseBodyElementName = getResponseBodyElementName( messageExchange );
63  		RestRequestInterface restRequest = messageExchange.getRestRequest();
64  		boolean asserted = false;
65  
66  		for( RestRepresentation representation : restRequest.getRepresentations( type, messageExchange
67  				.getResponseContentType() ) )
68  		{
69  			if( representation.getStatus().isEmpty()
70  					|| representation.getStatus().contains( messageExchange.getResponseStatusCode() ) )
71  			{
72  				SchemaType schemaType = representation.getSchemaType();
73  				if( schemaType != null && representation.getElement().equals( responseBodyElementName ) )
74  				{
75  					try
76  					{
77  						XmlObject xmlObject = schemaType.getTypeSystem().parse( messageExchange.getResponseContentAsXml(),
78  								schemaType, new XmlOptions() );
79  
80  						// create internal error list
81  						List<?> list = new ArrayList<Object>();
82  
83  						XmlOptions xmlOptions = new XmlOptions();
84  						xmlOptions.setErrorListener( list );
85  						xmlOptions.setValidateTreatLaxAsSkip();
86  						xmlObject.validate( xmlOptions );
87  
88  						for( Object o : list )
89  						{
90  							if( o instanceof XmlError )
91  								result.add( new AssertionError( ( XmlError )o ) );
92  							else
93  								result.add( new AssertionError( o.toString() ) );
94  						}
95  
96  						asserted = true;
97  					}
98  					catch( XmlException e )
99  					{
100 						SoapUI.logError( e );
101 					}
102 				}
103 				else
104 				{
105 					asserted = true;
106 				}
107 			}
108 		}
109 
110 		if( !asserted && result.isEmpty() )
111 		{
112 			result.add( new AssertionError( "Missing matching representation for request with contentType ["
113 					+ messageExchange.getResponseContentType() + "]" ) );
114 		}
115 
116 		return result.toArray( new AssertionError[result.size()] );
117 	}
118 
119 	private QName getResponseBodyElementName( RestMessageExchange messageExchange )
120 	{
121 		try
122 		{
123 			XmlObject xmlObject = XmlObject.Factory.parse( messageExchange.getResponseContentAsXml() );
124 			Element docElement = ( ( Document )xmlObject.getDomNode() ).getDocumentElement();
125 
126 			return new QName( docElement.getNamespaceURI(), docElement.getLocalName() );
127 		}
128 		catch( XmlException e )
129 		{
130 			SoapUI.logError( e );
131 		}
132 
133 		return null;
134 	}
135 }