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