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.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20
21 import javax.swing.ImageIcon;
22
23 import com.eviware.soapui.SoapUI;
24 import com.eviware.soapui.config.AttachmentConfig;
25 import com.eviware.soapui.config.CallConfig;
26 import com.eviware.soapui.config.RequestAssertionConfig;
27 import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
28 import com.eviware.soapui.impl.wsdl.WsdlInterface;
29 import com.eviware.soapui.impl.wsdl.WsdlOperation;
30 import com.eviware.soapui.impl.wsdl.WsdlRequest;
31 import com.eviware.soapui.impl.wsdl.submit.transports.http.WsdlResponse;
32 import com.eviware.soapui.impl.wsdl.support.assertions.AssertionsSupport;
33 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
34 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
35 import com.eviware.soapui.impl.wsdl.teststeps.assertions.WsdlAssertionRegistry;
36 import com.eviware.soapui.impl.wsdl.teststeps.assertions.WsdlAssertionRegistry.AssertableType;
37 import com.eviware.soapui.model.ModelItem;
38 import com.eviware.soapui.model.iface.Submit;
39 import com.eviware.soapui.model.iface.SubmitContext;
40 import com.eviware.soapui.model.testsuite.Assertable;
41 import com.eviware.soapui.model.testsuite.AssertionsListener;
42 import com.eviware.soapui.model.testsuite.TestAssertion;
43 import com.eviware.soapui.monitor.TestMonitor;
44 import com.eviware.soapui.support.UISupport;
45
46 /***
47 * WsdlRequest extension that adds WsdlAssertions
48 *
49 * @author Ole.Matzura
50 */
51
52 public class WsdlTestRequest extends WsdlRequest implements Assertable
53 {
54 public static final String RESPONSE_PROPERTY = WsdlTestRequest.class.getName() + "@response";
55 public static final String STATUS_PROPERTY = WsdlTestRequest.class.getName() + "@status";
56
57 private static ImageIcon validRequestIcon;
58 private static ImageIcon failedRequestIcon;
59 private static ImageIcon disabledRequestIcon;
60 private static ImageIcon unknownRequestIcon;
61
62 private AssertionStatus currentStatus;
63 private final WsdlTestRequestStep testStep;
64
65 private AssertionsSupport assertionsSupport;
66 private WsdlResponseMessageExchange messageExchange;
67 private final boolean forLoadTest;
68 private PropertyChangeNotifier notifier;
69
70 public WsdlTestRequest( WsdlOperation operation, CallConfig callConfig, WsdlTestRequestStep testStep,
71 boolean forLoadTest )
72 {
73 super( operation, callConfig, forLoadTest );
74 this.forLoadTest = forLoadTest;
75
76 setSettings( new XmlBeansSettingsImpl( this, testStep.getSettings(), callConfig.getSettings() ) );
77
78 this.testStep = testStep;
79
80 initAssertions();
81 initIcons();
82 }
83
84 public WsdlTestCase getTestCase()
85 {
86 return testStep.getTestCase();
87 }
88
89 protected void initIcons()
90 {
91 if( validRequestIcon == null )
92 validRequestIcon = UISupport.createImageIcon( "/valid_request.gif" );
93
94 if( failedRequestIcon == null )
95 failedRequestIcon = UISupport.createImageIcon( "/invalid_request.gif" );
96
97 if( unknownRequestIcon == null )
98 unknownRequestIcon = UISupport.createImageIcon( "/unknown_request.gif" );
99
100 if( disabledRequestIcon == null )
101 disabledRequestIcon = UISupport.createImageIcon( "/disabled_request.gif" );
102 }
103
104 @Override
105 protected RequestIconAnimator initIconAnimator()
106 {
107 return new TestRequestIconAnimator();
108 }
109
110 private void initAssertions()
111 {
112 assertionsSupport = new AssertionsSupport( testStep, getConfig().getAssertionList() );
113 }
114
115 public int getAssertionCount()
116 {
117 return assertionsSupport.getAssertionCount();
118 }
119
120 public WsdlMessageAssertion getAssertionAt( int c )
121 {
122 return assertionsSupport.getAssertionAt( c );
123 }
124
125 @Override
126 public void setResponse( WsdlResponse response, SubmitContext context )
127 {
128 super.setResponse( response, context );
129
130 assertResponse( context );
131 }
132
133 public void assertResponse( SubmitContext context )
134 {
135 if( notifier == null )
136 notifier = new PropertyChangeNotifier();
137
138 messageExchange = new WsdlResponseMessageExchange( this );
139
140
141 for( Iterator<WsdlMessageAssertion> iter = assertionsSupport.iterator(); iter.hasNext(); )
142 {
143 WsdlMessageAssertion assertion = iter.next();
144 assertion.assertResponse( messageExchange, context );
145 }
146
147 notifier.notifyChange();
148 }
149
150 private class PropertyChangeNotifier
151 {
152 private AssertionStatus oldStatus;
153 private ImageIcon oldIcon;
154
155 public PropertyChangeNotifier()
156 {
157 oldStatus = getAssertionStatus();
158 oldIcon = getIcon();
159 }
160
161 public void notifyChange()
162 {
163 AssertionStatus newStatus = getAssertionStatus();
164 ImageIcon newIcon = getIcon();
165
166 if( oldStatus != newStatus )
167 notifyPropertyChanged( STATUS_PROPERTY, oldStatus, newStatus );
168
169 if( oldIcon != newIcon )
170 notifyPropertyChanged( ICON_PROPERTY, oldIcon, getIcon() );
171
172 oldStatus = newStatus;
173 oldIcon = newIcon;
174 }
175 }
176
177 public WsdlMessageAssertion addAssertion( String assertionLabel )
178 {
179 PropertyChangeNotifier notifier = new PropertyChangeNotifier();
180
181 try
182 {
183 RequestAssertionConfig assertionConfig = getConfig().addNewAssertion();
184 assertionConfig.setType( WsdlAssertionRegistry.getInstance().getAssertionTypeForName( assertionLabel ) );
185
186 WsdlMessageAssertion assertion = assertionsSupport.addWsdlAssertion( assertionConfig );
187 assertionsSupport.fireAssertionAdded( assertion );
188
189 if( getResponse() != null )
190 {
191 assertion.assertResponse( new WsdlResponseMessageExchange( this ), new WsdlTestRunContext( testStep ) );
192 notifier.notifyChange();
193 }
194
195 return assertion;
196 }
197 catch( Exception e )
198 {
199 SoapUI.logError( e );
200 return null;
201 }
202 }
203
204 public void removeAssertion( TestAssertion assertion )
205 {
206 PropertyChangeNotifier notifier = new PropertyChangeNotifier();
207
208 try
209 {
210 int ix = assertionsSupport.removeAssertion( ( WsdlMessageAssertion ) assertion );
211 getConfig().removeAssertion( ix );
212 }
213 finally
214 {
215 ((WsdlMessageAssertion)assertion).release();
216 notifier.notifyChange();
217 }
218 }
219
220 public AssertionStatus getAssertionStatus()
221 {
222 currentStatus = AssertionStatus.UNKNOWN;
223
224 if( messageExchange != null )
225 {
226 if( !messageExchange.hasResponse() && !getOperation().isOneWay() )
227 {
228 currentStatus = AssertionStatus.FAILED;
229 }
230 }
231 else
232 return currentStatus;
233
234 int cnt = getAssertionCount();
235 if( cnt == 0 )
236 return currentStatus;
237
238 for( int c = 0; c < cnt; c++ )
239 {
240 if( getAssertionAt( c ).getStatus() == AssertionStatus.FAILED )
241 {
242 currentStatus = AssertionStatus.FAILED;
243 break;
244 }
245 }
246
247 if( currentStatus == AssertionStatus.UNKNOWN )
248 currentStatus = AssertionStatus.VALID;
249
250 return currentStatus;
251 }
252
253 @Override
254 public ImageIcon getIcon()
255 {
256 if( forLoadTest )
257 return null;
258
259 TestMonitor testMonitor = SoapUI.getTestMonitor();
260 if( testMonitor != null && testMonitor.hasRunningLoadTest( testStep.getTestCase() ) )
261 return disabledRequestIcon;
262
263 ImageIcon icon = getIconAnimator().getIcon();
264 if( icon == getIconAnimator().getBaseIcon() )
265 {
266 AssertionStatus status = getAssertionStatus();
267 if( status == AssertionStatus.VALID )
268 return validRequestIcon;
269 else if( status == AssertionStatus.FAILED )
270 return failedRequestIcon;
271 else if( status == AssertionStatus.UNKNOWN )
272 return unknownRequestIcon;
273 }
274
275 return icon;
276 }
277
278 public void addAssertionsListener( AssertionsListener listener )
279 {
280 assertionsSupport.addAssertionsListener( listener );
281 }
282
283 public void removeAssertionsListener( AssertionsListener listener )
284 {
285 assertionsSupport.removeAssertionsListener( listener );
286 }
287
288 /***
289 * Called when a testrequest is moved in a testcase
290 */
291
292 @Override
293 public void updateConfig( CallConfig request )
294 {
295 super.updateConfig( request );
296
297 assertionsSupport.updateConfig( getConfig().getAssertionList() );
298
299 List<AttachmentConfig> attachmentConfigs = getConfig().getAttachmentList();
300 for( int i = 0; i < attachmentConfigs.size(); i++ )
301 {
302 AttachmentConfig config = attachmentConfigs.get( i );
303 attachments.get( i ).updateConfig( config );
304 }
305 }
306
307 @Override
308 public void release()
309 {
310 super.release();
311 assertionsSupport.release();
312 }
313
314 public String getAssertableContent()
315 {
316 return getResponse() == null ? null : getResponse().getContentAsString();
317 }
318
319 public WsdlTestRequestStep getTestStep()
320 {
321 return testStep;
322 }
323
324 public WsdlInterface getInterface()
325 {
326 return getOperation().getInterface();
327 }
328
329 protected class TestRequestIconAnimator extends RequestIconAnimator
330 {
331 @Override
332 public boolean beforeSubmit( Submit submit, SubmitContext context )
333 {
334 if( SoapUI.getTestMonitor() != null && SoapUI.getTestMonitor().hasRunningLoadTest( getTestCase() ) )
335 return true;
336
337 return super.beforeSubmit( submit, context );
338 }
339
340 @Override
341 public void afterSubmit( Submit submit, SubmitContext context )
342 {
343 if( submit.getRequest() == getTarget() )
344 stop();
345 }
346 }
347
348 public AssertableType getAssertableType()
349 {
350 return AssertableType.RESPONSE;
351 }
352
353 public String getInterfaceName()
354 {
355 return testStep.getInterfaceName();
356 }
357
358 public String getOperationName()
359 {
360 return testStep.getOperationName();
361 }
362
363 public TestAssertion cloneAssertion( TestAssertion source, String name )
364 {
365 RequestAssertionConfig conf = getConfig().addNewAssertion();
366 conf.set( ((WsdlMessageAssertion)source).getConfig() );
367 conf.setName( name );
368
369 WsdlMessageAssertion result = assertionsSupport.addWsdlAssertion( conf );
370 assertionsSupport.fireAssertionAdded( result );
371 return result;
372 }
373
374 public WsdlMessageAssertion importAssertion( WsdlMessageAssertion source, boolean overwrite, boolean createCopy )
375 {
376 RequestAssertionConfig conf = getConfig().addNewAssertion();
377 conf.set( source.getConfig() );
378 if( createCopy && conf.isSetId() )
379 conf.unsetId();
380
381 if( !source.isAllowMultiple() )
382 {
383 List<WsdlMessageAssertion> existing = assertionsSupport.getAssertionsOfType( source.getClass() );
384 if( !existing.isEmpty() && !overwrite )
385 return null;
386
387 while( !existing.isEmpty() )
388 {
389 removeAssertion( existing.remove( 0 ) );
390 }
391 }
392
393 WsdlMessageAssertion result = assertionsSupport.addWsdlAssertion( conf );
394 assertionsSupport.fireAssertionAdded( result );
395 return result;
396 }
397
398 public List<TestAssertion> getAssertionList()
399 {
400 return new ArrayList<TestAssertion>( assertionsSupport.getAssertionList() );
401 }
402
403 public WsdlMessageAssertion getAssertionByName( String name )
404 {
405 return assertionsSupport.getAssertionByName( name );
406 }
407
408 public ModelItem getModelItem()
409 {
410 return testStep;
411 }
412
413 public Map<String,TestAssertion> getAssertions()
414 {
415 Map<String,TestAssertion> result = new HashMap<String, TestAssertion>();
416
417 for( TestAssertion assertion : getAssertionList() )
418 result.put( assertion.getName(), assertion );
419
420 return result;
421 }
422 }