View Javadoc

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