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