1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.teststeps;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Map;
18
19 import javax.swing.ImageIcon;
20
21 import com.eviware.soapui.SoapUI;
22 import com.eviware.soapui.config.RestRequestConfig;
23 import com.eviware.soapui.config.TestAssertionConfig;
24 import com.eviware.soapui.impl.rest.RestMethod;
25 import com.eviware.soapui.impl.rest.RestRequest;
26 import com.eviware.soapui.impl.rest.RestResource;
27 import com.eviware.soapui.impl.rest.RestService;
28 import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
29 import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
30 import com.eviware.soapui.impl.wsdl.support.assertions.AssertableConfig;
31 import com.eviware.soapui.impl.wsdl.support.assertions.AssertionsSupport;
32 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
33 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
34 import com.eviware.soapui.impl.wsdl.teststeps.assertions.TestAssertionRegistry.AssertableType;
35 import com.eviware.soapui.model.ModelItem;
36 import com.eviware.soapui.model.iface.Submit;
37 import com.eviware.soapui.model.iface.SubmitContext;
38 import com.eviware.soapui.model.testsuite.AssertionsListener;
39 import com.eviware.soapui.model.testsuite.TestAssertion;
40 import com.eviware.soapui.monitor.TestMonitor;
41 import com.eviware.soapui.support.UISupport;
42 import com.eviware.soapui.support.resolver.ResolveContext;
43
44 public class RestTestRequest extends RestRequest implements RestTestRequestInterface
45 {
46 private ImageIcon validRequestIcon;
47 private ImageIcon failedRequestIcon;
48 private ImageIcon disabledRequestIcon;
49 private ImageIcon unknownRequestIcon;
50
51 private AssertionStatus currentStatus;
52 private RestTestRequestStep testStep;
53
54 private AssertionsSupport assertionsSupport;
55 private RestResponseMessageExchange messageExchange;
56 private final boolean forLoadTest;
57 private PropertyChangeNotifier notifier;
58 private RestResource restResource;
59 private RestMethod restMethod;
60
61 public RestTestRequest( RestMethod method, RestRequestConfig callConfig, RestTestRequestStep testStep,
62 boolean forLoadTest )
63 {
64 super( method, callConfig, forLoadTest );
65 this.forLoadTest = forLoadTest;
66
67
68
69 setSettings( new XmlBeansSettingsImpl( this, testStep.getSettings(), callConfig.getSettings() ) );
70
71 this.testStep = testStep;
72
73 initAssertions();
74 initIcons();
75 }
76
77 public ModelItem getParent()
78 {
79 return getTestStep();
80 }
81
82 public WsdlTestCase getTestCase()
83 {
84 return testStep.getTestCase();
85 }
86
87 protected void initIcons()
88 {
89 validRequestIcon = UISupport.createImageIcon( "/valid_rest_request.gif" );
90 failedRequestIcon = UISupport.createImageIcon( "/invalid_rest_request.gif" );
91 unknownRequestIcon = UISupport.createImageIcon( "/unknown_rest_request.gif" );
92 disabledRequestIcon = UISupport.createImageIcon( "/disabled_rest_request.gif" );
93
94
95
96 setIconAnimator( new TestRequestIconAnimator( this ) );
97 }
98
99 private void initAssertions()
100 {
101 assertionsSupport = new AssertionsSupport( testStep, new AssertableConfig()
102 {
103 public TestAssertionConfig addNewAssertion()
104 {
105 return getConfig().addNewAssertion();
106 }
107
108 public List<TestAssertionConfig> getAssertionList()
109 {
110 return getConfig().getAssertionList();
111 }
112
113 public void removeAssertion( int ix )
114 {
115 getConfig().removeAssertion( ix );
116 }
117
118 public TestAssertionConfig insertAssertion( TestAssertionConfig source, int ix )
119 {
120 TestAssertionConfig conf = getConfig().insertNewAssertion( ix );
121 conf.set( source );
122 return conf;
123 }
124
125 } );
126 }
127
128 public int getAssertionCount()
129 {
130 return assertionsSupport.getAssertionCount();
131 }
132
133 public WsdlMessageAssertion getAssertionAt( int c )
134 {
135 return assertionsSupport.getAssertionAt( c );
136 }
137
138 public void setResponse( HttpResponse response, SubmitContext context )
139 {
140 super.setResponse( response, context );
141 assertResponse( context );
142 }
143
144 public void assertResponse( SubmitContext context )
145 {
146 if( notifier == null )
147 notifier = new PropertyChangeNotifier();
148
149 messageExchange = getResponse() == null ? null :new RestResponseMessageExchange( this );
150
151 if( messageExchange != null )
152 {
153
154 for( WsdlMessageAssertion assertion : assertionsSupport.getAssertionList() )
155 {
156 assertion.assertResponse( messageExchange, context );
157 }
158 }
159
160 notifier.notifyChange();
161 }
162
163 private class PropertyChangeNotifier
164 {
165 private AssertionStatus oldStatus;
166 private ImageIcon oldIcon;
167
168 public PropertyChangeNotifier()
169 {
170 oldStatus = getAssertionStatus();
171 oldIcon = getIcon();
172 }
173
174 public void notifyChange()
175 {
176 AssertionStatus newStatus = getAssertionStatus();
177 ImageIcon newIcon = getIcon();
178
179 if( oldStatus != newStatus )
180 notifyPropertyChanged( STATUS_PROPERTY, oldStatus, newStatus );
181
182 if( oldIcon != newIcon )
183 notifyPropertyChanged( ICON_PROPERTY, oldIcon, getIcon() );
184
185 oldStatus = newStatus;
186 oldIcon = newIcon;
187 }
188 }
189
190 public WsdlMessageAssertion addAssertion( String assertionLabel )
191 {
192 PropertyChangeNotifier notifier = new PropertyChangeNotifier();
193
194 try
195 {
196 WsdlMessageAssertion assertion = assertionsSupport.addWsdlAssertion( assertionLabel );
197 if( assertion == null )
198 return null;
199
200 if( getResponse() != null )
201 {
202 assertion.assertResponse( new RestResponseMessageExchange( this ), new WsdlTestRunContext( testStep ) );
203 notifier.notifyChange();
204 }
205
206 return assertion;
207 }
208 catch( Exception e )
209 {
210 SoapUI.logError( e );
211 return null;
212 }
213 }
214
215 public void removeAssertion( TestAssertion assertion )
216 {
217 PropertyChangeNotifier notifier = new PropertyChangeNotifier();
218
219 try
220 {
221 assertionsSupport.removeAssertion( ( WsdlMessageAssertion )assertion );
222
223 }
224 finally
225 {
226 ( ( WsdlMessageAssertion )assertion ).release();
227 notifier.notifyChange();
228 }
229 }
230
231 public TestAssertion moveAssertion( int ix, int offset )
232 {
233 PropertyChangeNotifier notifier = new PropertyChangeNotifier();
234 WsdlMessageAssertion assertion = getAssertionAt( ix );
235 try
236 {
237 return assertionsSupport.moveAssertion( ix, offset );
238 }
239 finally
240 {
241 ( ( WsdlMessageAssertion )assertion ).release();
242 notifier.notifyChange();
243 }
244 }
245
246 public AssertionStatus getAssertionStatus()
247 {
248 currentStatus = AssertionStatus.UNKNOWN;
249
250 if( messageExchange != null )
251 {
252 if( !messageExchange.hasResponse() && getOperation() != null && getOperation().isBidirectional() )
253 {
254 currentStatus = AssertionStatus.FAILED;
255 }
256 }
257 else
258 return currentStatus;
259
260 int cnt = getAssertionCount();
261 if( cnt == 0 )
262 return currentStatus;
263
264 for( int c = 0; c < cnt; c++ )
265 {
266 if( getAssertionAt( c ).getStatus() == AssertionStatus.FAILED )
267 {
268 currentStatus = AssertionStatus.FAILED;
269 break;
270 }
271 }
272
273 if( currentStatus == AssertionStatus.UNKNOWN )
274 currentStatus = AssertionStatus.VALID;
275
276 return currentStatus;
277 }
278
279 @Override
280 public ImageIcon getIcon()
281 {
282 if( forLoadTest || UISupport.isHeadless() )
283 return null;
284
285 TestMonitor testMonitor = SoapUI.getTestMonitor();
286 if( testMonitor != null && testMonitor.hasRunningLoadTest( testStep.getTestCase() ) )
287 return disabledRequestIcon;
288
289 ImageIcon icon = getIconAnimator().getIcon();
290 if( icon == getIconAnimator().getBaseIcon() )
291 {
292 AssertionStatus status = getAssertionStatus();
293 if( status == AssertionStatus.VALID )
294 return validRequestIcon;
295 else if( status == AssertionStatus.FAILED )
296 return failedRequestIcon;
297 else if( status == AssertionStatus.UNKNOWN )
298 return unknownRequestIcon;
299 }
300
301 return icon;
302 }
303
304 public void addAssertionsListener( AssertionsListener listener )
305 {
306 assertionsSupport.addAssertionsListener( listener );
307 }
308
309 public void removeAssertionsListener( AssertionsListener listener )
310 {
311 assertionsSupport.removeAssertionsListener( listener );
312 }
313
314 /***
315 * Called when a testrequest is moved in a testcase
316 */
317
318 public void updateConfig( RestRequestConfig request )
319 {
320 super.updateConfig( request );
321
322 assertionsSupport.refresh();
323 }
324
325 @Override
326 public void release()
327 {
328 super.release();
329 assertionsSupport.release();
330 }
331
332 public String getAssertableContent()
333 {
334 return getResponseContentAsXml();
335 }
336
337 public RestTestRequestStep getTestStep()
338 {
339 return testStep;
340 }
341
342 public RestService getInterface()
343 {
344 return getOperation() == null ? null : getOperation().getInterface();
345 }
346
347 @Override
348 public RestResource getOperation()
349 {
350 return testStep instanceof RestTestRequestStepInterface ? ( ( RestTestRequestStepInterface )testStep )
351 .getResource() : null;
352 }
353
354 protected static class TestRequestIconAnimator extends RequestIconAnimator<RestTestRequest>
355 {
356 public TestRequestIconAnimator( RestTestRequestInterface modelItem )
357 {
358 super( ( RestTestRequest )modelItem, "/rest_request.gif", "/exec_rest_request", 4, "gif" );
359 }
360
361 @Override
362 public boolean beforeSubmit( Submit submit, SubmitContext context )
363 {
364 if( SoapUI.getTestMonitor() != null && SoapUI.getTestMonitor().hasRunningLoadTest( getTarget().getTestCase() ) )
365 return true;
366
367 return super.beforeSubmit( submit, context );
368 }
369
370 @Override
371 public void afterSubmit( Submit submit, SubmitContext context )
372 {
373 if( submit.getRequest() == getTarget() )
374 stop();
375 }
376 }
377
378 public AssertableType getAssertableType()
379 {
380 return AssertableType.RESPONSE;
381 }
382
383 public TestAssertion cloneAssertion( TestAssertion source, String name )
384 {
385 return assertionsSupport.cloneAssertion( source, name );
386 }
387
388 public WsdlMessageAssertion importAssertion( WsdlMessageAssertion source, boolean overwrite, boolean createCopy )
389 {
390 return assertionsSupport.importAssertion( source, overwrite, createCopy );
391 }
392
393 public List<TestAssertion> getAssertionList()
394 {
395 return new ArrayList<TestAssertion>( assertionsSupport.getAssertionList() );
396 }
397
398 public WsdlMessageAssertion getAssertionByName( String name )
399 {
400 return assertionsSupport.getAssertionByName( name );
401 }
402
403 public ModelItem getModelItem()
404 {
405 return testStep;
406 }
407
408 public Map<String, TestAssertion> getAssertions()
409 {
410 return assertionsSupport.getAssertions();
411 }
412
413 public String getDefaultAssertableContent()
414 {
415 return "";
416 }
417
418 public String getResponseContentAsString()
419 {
420 return getResponse() == null ? null : getResponse().getContentAsString();
421 }
422
423 public void setPath( String fullPath )
424 {
425 super.setPath( fullPath );
426
427 if( getOperation() == null )
428 {
429 setEndpoint( fullPath );
430 }
431 }
432
433 public void setResource( RestResource restResource )
434 {
435 if( this.restResource != null )
436 this.restResource.removePropertyChangeListener( this );
437
438 this.restResource = restResource;
439
440 restResource.addPropertyChangeListener( this );
441 }
442
443 public void setRestMethod( RestMethod restMethod )
444 {
445 if( this.restMethod != null )
446 this.restMethod.removePropertyChangeListener( this );
447
448 this.restMethod = restMethod;
449
450 restMethod.addPropertyChangeListener( this );
451 }
452
453 public RestResource getResource()
454 {
455 return restResource;
456 }
457
458 public RestMethod getRestMethod()
459 {
460 return restMethod;
461 }
462
463 public String getRestMethodName()
464 {
465 return restMethod.getName();
466 }
467
468 public void resolve( ResolveContext<?> context )
469 {
470 super.resolve( context );
471 assertionsSupport.resolve( context );
472 }
473
474 public String getServiceName()
475 {
476 return testStep instanceof RestTestRequestStepInterface ? ( ( RestTestRequestStepInterface )testStep )
477 .getService() : null;
478 }
479
480 }