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;
14  
15  import com.eviware.soapui.impl.rest.RestRequest;
16  import com.eviware.soapui.impl.rest.support.MediaTypeHandler;
17  import com.eviware.soapui.impl.rest.support.MediaTypeHandlerRegistry;
18  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
19  import com.eviware.soapui.impl.wsdl.support.assertions.AssertedXPathsContainer;
20  import com.eviware.soapui.impl.wsdl.teststeps.actions.ShowMessageExchangeAction;
21  import com.eviware.soapui.model.ModelItem;
22  import com.eviware.soapui.model.iface.Attachment;
23  import com.eviware.soapui.model.iface.MessageExchange;
24  import com.eviware.soapui.model.iface.Operation;
25  import com.eviware.soapui.model.testsuite.AssertedXPath;
26  import com.eviware.soapui.model.testsuite.MessageExchangeTestStepResult;
27  import com.eviware.soapui.model.testsuite.ResponseAssertedMessageExchange;
28  import com.eviware.soapui.support.action.swing.ActionList;
29  import com.eviware.soapui.support.types.StringToStringMap;
30  import com.eviware.soapui.support.xml.XmlUtils;
31  
32  import java.io.PrintWriter;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  /***
37   * TestStepResult for a WsdlTestRequestStep
38   *
39   * @author ole.matzura
40   */
41  
42  public class RestRequestStepResult extends WsdlTestStepResult implements
43          ResponseAssertedMessageExchange, AssertedXPathsContainer, MessageExchangeTestStepResult
44  {
45     private String requestContent;
46     private HttpResponse response;
47     private String domain;
48     private String username;
49     private String endpoint;
50     private String encoding;
51     private String password;
52     private StringToStringMap properties;
53     private boolean addedAction;
54     private List<AssertedXPath> assertedXPaths;
55  
56     public RestRequestStepResult( HttpTestRequestStep step )
57     {
58        super( step );
59     }
60  
61     public RestRequestStepResult( RestTestRequestStep step )
62     {
63        super( step );
64     }
65  
66     public Operation getOperation()
67     {
68        return response.getRequest().getOperation();
69     }
70  
71     public ModelItem getModelItem()
72     {
73        if( response != null )
74           return response.getRequest();
75        else
76           return null;
77     }
78  
79     public String getRequestContent()
80     {
81        if( isDiscarded() )
82           return "<discarded>";
83  
84        return requestContent;
85     }
86  
87     public void setRequestContent( String requestContent )
88     {
89        this.requestContent = requestContent;
90     }
91  
92     public HttpResponse getResponse()
93     {
94        return response;
95     }
96  
97     @Override
98     public ActionList getActions()
99     {
100       if( !addedAction )
101       {
102          addAction( new ShowMessageExchangeAction( this, "TestStep" ), true );
103          addedAction = true;
104       }
105 
106       return super.getActions();
107    }
108 
109    public void setResponse( HttpResponse response )
110    {
111       this.response = response;
112    }
113 
114    public String getDomain()
115    {
116       return domain;
117    }
118 
119    public void setDomain( String domain )
120    {
121       this.domain = domain;
122       addProperty( "domain", domain );
123    }
124 
125    public void addProperty( String key, String value )
126    {
127       if( properties == null )
128          properties = new StringToStringMap();
129 
130       properties.put( key, value );
131    }
132 
133    public String getEncoding()
134    {
135       return encoding;
136    }
137 
138    public void setEncoding( String encoding )
139    {
140       this.encoding = encoding;
141       addProperty( "encoding", encoding );
142    }
143 
144    public String getEndpoint()
145    {
146       return endpoint;
147    }
148 
149    public void setEndpoint( String endpoint )
150    {
151       this.endpoint = endpoint;
152       addProperty( "endpoint", endpoint );
153    }
154 
155    public String getPassword()
156    {
157       return password;
158    }
159 
160    public void setPassword( String password )
161    {
162       this.password = password;
163       addProperty( "password", password );
164    }
165 
166    public String getUsername()
167    {
168       return username;
169    }
170 
171    public void setUsername( String username )
172    {
173       this.username = username;
174       addProperty( "username", username );
175    }
176 
177    public void discard()
178    {
179       super.discard();
180 
181       requestContent = null;
182       response = null;
183       properties = null;
184       assertedXPaths = null;
185    }
186 
187    public void writeTo( PrintWriter writer )
188    {
189       super.writeTo( writer );
190 
191       writer.println( "----------------------------------------------------" );
192       writer.println( "Encoding: " + getEncoding() );
193       writer.println( "Endpoint: " + getEndpoint() );
194       writer.println( "Username: " + getUsername() );
195       writer.println( "Password: " + getPassword() );
196       writer.println( "Domain: " + getDomain() );
197 
198       writer.println( "---------------- Request ---------------------------" );
199       if( requestContent != null )
200          writer.println( XmlUtils.prettyPrintXml( requestContent ) );
201       else
202          writer.println( "- missing request / garbage collected -" );
203 
204       if( response != null )
205       {
206          writer.println( "Request Headers: " + response.getRequestHeaders().toString() );
207       }
208 
209       writer.println( "---------------- Response --------------------------" );
210       if( response != null )
211       {
212          String respContent = response.getContentAsString();
213          if( respContent != null )
214             writer.println( XmlUtils.prettyPrintXml( respContent ) );
215 
216          writer.println( "Response Headers: " + response.getResponseHeaders().toString() );
217       }
218       else
219          writer.println( "- missing response / garbage collected -" );
220    }
221 
222    public StringToStringMap getProperties()
223    {
224       return properties;
225    }
226 
227    public String getProperty( String name )
228    {
229       return properties == null ? null : properties.get( name );
230    }
231 
232    public Attachment[] getRequestAttachments()
233    {
234       if( response == null || response.getRequest() == null )
235          return new Attachment[0];
236 
237       return response.getRequest().getAttachments();
238    }
239 
240    public StringToStringMap getRequestHeaders()
241    {
242       if( response == null )
243          return null;
244 
245       return response.getRequestHeaders();
246    }
247 
248    public Attachment[] getResponseAttachments()
249    {
250       if( response == null )
251          return new Attachment[0];
252 
253       return response.getAttachments();
254    }
255 
256    public String getResponseContent()
257    {
258       if( isDiscarded() )
259          return "<discarded>";
260 
261       if( response == null )
262          return "<missing response>";
263 
264       return response.getContentAsString();
265    }
266 
267    public String getRequestContentAsXml()
268    {
269       return XmlUtils.seemsToBeXml( requestContent ) ? requestContent : "<not-xml/>";
270    }
271 
272    public String getResponseContentAsXml()
273    {
274       if( response.getProperty( RestRequest.REST_XML_RESPONSE ) == null )
275       {
276          MediaTypeHandler typeHandler = MediaTypeHandlerRegistry.getTypeHandler( response.getContentType() );
277          if( typeHandler != null )
278             response.setProperty( RestRequest.REST_XML_RESPONSE, typeHandler.createXmlRepresentation( response ));
279       }
280 
281       return response.getProperty( RestRequest.REST_XML_RESPONSE );
282    }
283 
284    public StringToStringMap getResponseHeaders()
285    {
286       if( response == null )
287          return null;
288 
289       return response.getResponseHeaders();
290    }
291 
292    public long getTimestamp()
293    {
294       if( isDiscarded() || response == null )
295          return -1;
296 
297       return response.getTimestamp();
298    }
299 
300    public AssertedXPath[] getAssertedXPathsForResponse()
301    {
302       return assertedXPaths == null ? new AssertedXPath[0] :
303               assertedXPaths.toArray( new AssertedXPath[assertedXPaths.size()] );
304    }
305 
306    public void addAssertedXPath( AssertedXPath assertedXPath )
307    {
308       if( assertedXPaths == null )
309          assertedXPaths = new ArrayList<AssertedXPath>();
310 
311       assertedXPaths.add( assertedXPath );
312    }
313 
314    public MessageExchange[] getMessageExchanges()
315    {
316       return new MessageExchange[] { this };
317    }
318 
319    public byte[] getRawRequestData()
320    {
321       return response.getRawRequestData();
322    }
323 
324    public byte[] getRawResponseData()
325    {
326       return response.getRawResponseData();
327    }
328 
329    public Attachment[] getRequestAttachmentsForPart( String partName )
330    {
331       return null;
332    }
333 
334    public Attachment[] getResponseAttachmentsForPart( String partName )
335    {
336       return null;
337    }
338 
339    public boolean hasRawData()
340    {
341       return getRawResponseData() != null || getRawRequestData() != null;
342    }
343 
344    public boolean hasRequest( boolean b )
345    {
346       return true;
347    }
348 
349    public boolean hasResponse()
350    {
351       return response != null;
352    }
353 }