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