View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.basic;
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           SchemaType schemaType = representation.getSchemaType();
63  
64           boolean statusOk = representation.getStatus().isEmpty() || representation.getStatus().contains( messageExchange.getResponseStatusCode());
65  
66           if( schemaType != null && responseBodyElementName != null && statusOk )
67           {
68              try
69              {
70                 XmlObject xmlObject = schemaType.getTypeSystem().parse( messageExchange.getResponseContentAsXml(), schemaType, new XmlOptions() );
71  
72                 // create internal error list
73                 List<?> list = new ArrayList<Object>();
74  
75                 XmlOptions xmlOptions = new XmlOptions();
76                 xmlOptions.setErrorListener( list );
77                 xmlOptions.setValidateTreatLaxAsSkip();
78                 xmlObject.validate( xmlOptions );
79  
80                 for( Object o : list )
81                 {
82                    if( o instanceof XmlError )
83                       result.add( new AssertionError( (XmlError) o ) );
84                    else
85                       result.add( new AssertionError( o.toString() ) );
86                 }
87  
88                 asserted = true;
89              }
90              catch( XmlException e )
91              {
92                 SoapUI.logError( e );
93              }
94           }
95        }
96  
97        if( !asserted && result.isEmpty())
98        {
99           result.add( new AssertionError( "Missing matching representation for request with contentType [" +
100             messageExchange.getResponseContentType() + "]" ));
101       }
102 
103       return result.toArray( new AssertionError[result.size()] );
104    }
105 
106    private QName getResponseBodyElementName( RestMessageExchange messageExchange )
107    {
108       try
109       {
110          XmlObject xmlObject = XmlObject.Factory.parse( messageExchange.getResponseContentAsXml() );
111          Element docElement = ((Document) xmlObject.getDomNode()).getDocumentElement();
112 
113          return new QName( docElement.getNamespaceURI(), docElement.getLocalName() );
114       }
115       catch( XmlException e )
116       {
117          SoapUI.logError( e );
118       }
119 
120       return null;
121    }
122 }