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.WsdlSubmitContext;
31 import com.eviware.soapui.impl.wsdl.actions.request.DeleteRequestAction;
32 import com.eviware.soapui.impl.wsdl.actions.request.RenameRequestAction;
33 import com.eviware.soapui.impl.wsdl.panels.support.assertions.Assertable;
34 import com.eviware.soapui.impl.wsdl.panels.support.assertions.AssertionsListener;
35 import com.eviware.soapui.impl.wsdl.submit.transports.http.WsdlResponse;
36 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
37 import com.eviware.soapui.impl.wsdl.teststeps.actions.AddAssertionAction;
38 import com.eviware.soapui.impl.wsdl.teststeps.assertions.WsdlAssertionRegistry;
39 import com.eviware.soapui.impl.wsdl.teststeps.assertions.WsdlAssertionRegistry.AssertionType;
40 import com.eviware.soapui.model.iface.Submit;
41 import com.eviware.soapui.model.iface.SubmitContext;
42 import com.eviware.soapui.monitor.TestMonitor;
43 import com.eviware.soapui.support.UISupport;
44
45 /***
46 * WsdlRequest extension that adds WsdlAssertions
47 *
48 * @author Ole.Matzura
49 */
50
51 public class WsdlTestRequest extends WsdlRequest implements PropertyChangeListener, Assertable
52 {
53 public static final String RESPONSE_PROPERTY = WsdlTestRequest.class.getName() + "@response";
54 public static final String STATUS_PROPERTY = WsdlTestRequest.class.getName() + "@status";
55
56 private ImageIcon validRequestIcon;
57 private ImageIcon failedRequestIcon;
58 private ImageIcon disabledRequestIcon;
59 private ImageIcon unknownRequestIcon;
60 private AssertionStatus currentStatus;
61 private final WsdlTestRequestStep testStep;
62
63 private AssertionsSupport assertionsSupport;
64 private WsdlResponseMessageExchange messageExchange;
65
66 public WsdlTestRequest( WsdlOperation operation, CallConfig callConfig, WsdlTestRequestStep testStep )
67 {
68 super( operation, callConfig );
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 validRequestIcon = UISupport.createImageIcon("/valid_request.gif");
86 failedRequestIcon = UISupport.createImageIcon("/invalid_request.gif");
87 unknownRequestIcon = UISupport.createImageIcon("/unknown_request.gif");
88 disabledRequestIcon = UISupport.createImageIcon("/disabled_request.gif");
89 }
90
91 protected RequestIconAnimator initIconAnimator()
92 {
93 return new TestRequestIconAnimator();
94 }
95
96 protected void initActions()
97 {
98 addAction( new RenameRequestAction( this ));
99 addAction( new DeleteRequestAction( this ));
100 addAction( new AddAssertionAction( this ));
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 WsdlSubmitContext( testStep ) );
178 notifier.notifyChange();
179 }
180
181 return assertion;
182 }
183 catch (Exception e)
184 {
185 e.printStackTrace();
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 TestMonitor testMonitor = SoapUI.getTestMonitor();
241 if( testMonitor != null && testMonitor.hasRunningLoadTest( testStep.getTestCase()) )
242 return disabledRequestIcon;
243
244 ImageIcon icon = getIconAnimator().getIcon();
245 if( icon == getIconAnimator().getBaseIcon() )
246 {
247 AssertionStatus status = getAssertionStatus();
248 if( status == AssertionStatus.VALID ) return validRequestIcon;
249 else if( status == AssertionStatus.FAILED ) return failedRequestIcon;
250 else if( status == AssertionStatus.UNKNOWN ) return unknownRequestIcon;
251 }
252
253 return icon;
254 }
255
256 public void propertyChange(PropertyChangeEvent evt)
257 {
258 if( evt.getPropertyName().equals( WsdlMessageAssertion.CONFIGURATION_PROPERTY ))
259 assertResponse( new WsdlSubmitContext( testStep ));
260 }
261
262 public void addAssertionsListener( AssertionsListener listener )
263 {
264 assertionsSupport.addAssertionsListener( listener );
265 }
266
267 public void removeAssertionsListener( AssertionsListener listener )
268 {
269 assertionsSupport.removeAssertionsListener( listener );
270 }
271
272 /***
273 * Called when a testrequest is moved in a testcase
274 */
275
276 public void updateConfig(CallConfig request)
277 {
278 super.updateConfig(request);
279
280 assertionsSupport.updateConfig( getConfig().getAssertionList() );
281
282 List<AttachmentConfig> attachmentConfigs = getConfig().getAttachmentList();
283 for( int i = 0; i < attachmentConfigs.size(); i++ )
284 {
285 AttachmentConfig config = attachmentConfigs.get( i );
286 attachments.get( i ).updateConfig( config );
287 }
288 }
289
290 public void release()
291 {
292 super.release();
293 assertionsSupport.release();
294 }
295
296 public String getAssertableContent()
297 {
298 return getResponse() == null ? null : getResponse().getContentAsString();
299 }
300
301 public WsdlTestRequestStep getTestStep()
302 {
303 return testStep;
304 }
305
306 public WsdlInterface getInterface()
307 {
308 return getOperation().getInterface();
309 }
310
311 protected class TestRequestIconAnimator extends RequestIconAnimator
312 {
313 public boolean beforeSubmit(Submit submit, SubmitContext context)
314 {
315 if( SoapUI.getTestMonitor() != null && SoapUI.getTestMonitor().hasRunningLoadTest( getTestCase() ))
316 return true;
317
318 return super.beforeSubmit( submit, context );
319 }
320
321 public void afterSubmit(Submit submit, SubmitContext context)
322 {
323 if( submit.getRequest() == getTarget() )
324 stop();
325 }
326 }
327
328 public AssertionType getAssertionType()
329 {
330 return AssertionType.RESPONSE;
331 }
332 }