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