View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
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.AttachmentConfig;
23  import com.eviware.soapui.config.TestAssertionConfig;
24  import com.eviware.soapui.config.WsdlRequestConfig;
25  import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
26  import com.eviware.soapui.impl.wsdl.WsdlInterface;
27  import com.eviware.soapui.impl.wsdl.WsdlOperation;
28  import com.eviware.soapui.impl.wsdl.WsdlRequest;
29  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
30  import com.eviware.soapui.impl.wsdl.submit.transports.jms.JMSResponse;
31  import com.eviware.soapui.impl.wsdl.support.assertions.AssertableConfig;
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.TestAssertionRegistry.AssertableType;
36  import com.eviware.soapui.model.ModelItem;
37  import com.eviware.soapui.model.iface.Submit;
38  import com.eviware.soapui.model.iface.SubmitContext;
39  import com.eviware.soapui.model.testsuite.Assertable;
40  import com.eviware.soapui.model.testsuite.AssertionsListener;
41  import com.eviware.soapui.model.testsuite.TestAssertion;
42  import com.eviware.soapui.monitor.TestMonitor;
43  import com.eviware.soapui.support.UISupport;
44  import com.eviware.soapui.support.resolver.ResolveContext;
45  
46  /***
47   * WsdlRequest extension that adds WsdlAssertions
48   * 
49   * @author Ole.Matzura
50   */
51  
52  public class WsdlTestRequest extends WsdlRequest implements Assertable, TestRequest
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, WsdlRequestConfig 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  	public ModelItem getParent()
90  	{
91  		return getTestStep();
92  	}
93  
94  	protected void initIcons()
95  	{
96  		if (validRequestIcon == null)
97  			validRequestIcon = UISupport.createImageIcon("/valid_request.gif");
98  
99  		if (failedRequestIcon == null)
100 			failedRequestIcon = UISupport.createImageIcon("/invalid_request.gif");
101 
102 		if (unknownRequestIcon == null)
103 			unknownRequestIcon = UISupport.createImageIcon("/unknown_request.gif");
104 
105 		if (disabledRequestIcon == null)
106 			disabledRequestIcon = UISupport.createImageIcon("/disabled_request.gif");
107 	}
108 
109 	@Override
110 	protected RequestIconAnimator<?> initIconAnimator()
111 	{
112 		return new TestRequestIconAnimator(this);
113 	}
114 
115 	private void initAssertions()
116 	{
117 		assertionsSupport = new AssertionsSupport(testStep, new AssertableConfig()
118 		{
119 
120 			public TestAssertionConfig addNewAssertion()
121 			{
122 				return getConfig().addNewAssertion();
123 			}
124 
125 			public List<TestAssertionConfig> getAssertionList()
126 			{
127 				return getConfig().getAssertionList();
128 			}
129 
130 			public void removeAssertion(int ix)
131 			{
132 				getConfig().removeAssertion(ix);
133 			}
134 
135 			public TestAssertionConfig insertAssertion(TestAssertionConfig source, int ix)
136 			{
137 				TestAssertionConfig conf = getConfig().insertNewAssertion(ix);
138 				conf.set(source);
139 				return conf;
140 			}
141 		});
142 	}
143 
144 	public int getAssertionCount()
145 	{
146 		return assertionsSupport.getAssertionCount();
147 	}
148 
149 	public WsdlMessageAssertion getAssertionAt(int c)
150 	{
151 		return assertionsSupport.getAssertionAt(c);
152 	}
153 
154 	public void setResponse(HttpResponse response, SubmitContext context)
155 	{
156 		super.setResponse(response, context);
157 		assertResponse(context);
158 	}
159 
160 	public void assertResponse(SubmitContext context)
161 	{
162 		if (notifier == null)
163 			notifier = new PropertyChangeNotifier();
164 
165 		if (getResponse() instanceof JMSResponse)
166 		{
167 			messageExchange = getResponse() == null ? null : new WsdlResponseMessageExchange(this){
168 				@Override
169 				public boolean hasResponse()
170 				{//JMS tweak
171 					String responseContent = getResponseContent();
172 					return responseContent != null ;
173 				}
174 			};
175 		}
176 		else
177 		{
178 			messageExchange = getResponse() == null ? null : new WsdlResponseMessageExchange(this);
179 		}
180 
181 		if (messageExchange != null)
182 		{
183 			// assert!
184 			for (WsdlMessageAssertion assertion : assertionsSupport.getAssertionList())
185 			{
186 				assertion.assertResponse(messageExchange, context);
187 			}
188 		}
189 
190 		notifier.notifyChange();
191 	}
192 
193 	private class PropertyChangeNotifier
194 	{
195 		private AssertionStatus oldStatus;
196 		private ImageIcon oldIcon;
197 
198 		public PropertyChangeNotifier()
199 		{
200 			oldStatus = getAssertionStatus();
201 			oldIcon = getIcon();
202 		}
203 
204 		public void notifyChange()
205 		{
206 			AssertionStatus newStatus = getAssertionStatus();
207 			ImageIcon newIcon = getIcon();
208 
209 			if (oldStatus != newStatus)
210 				notifyPropertyChanged(STATUS_PROPERTY, oldStatus, newStatus);
211 
212 			if (oldIcon != newIcon)
213 				notifyPropertyChanged(ICON_PROPERTY, oldIcon, getIcon());
214 
215 			oldStatus = newStatus;
216 			oldIcon = newIcon;
217 		}
218 	}
219 
220 	public WsdlMessageAssertion addAssertion(String assertionLabel)
221 	{
222 		PropertyChangeNotifier notifier = new PropertyChangeNotifier();
223 
224 		try
225 		{
226 			WsdlMessageAssertion assertion = assertionsSupport.addWsdlAssertion(assertionLabel);
227 			if (assertion == null)
228 				return null;
229 
230 			if (getResponse() != null)
231 			{
232 				assertion.assertResponse(new WsdlResponseMessageExchange(this), new WsdlTestRunContext(testStep));
233 				notifier.notifyChange();
234 			}
235 
236 			return assertion;
237 		}
238 		catch (Exception e)
239 		{
240 			SoapUI.logError(e);
241 			return null;
242 		}
243 	}
244 
245 	public void removeAssertion(TestAssertion assertion)
246 	{
247 		PropertyChangeNotifier notifier = new PropertyChangeNotifier();
248 
249 		try
250 		{
251 			assertionsSupport.removeAssertion((WsdlMessageAssertion) assertion);
252 
253 		}
254 		finally
255 		{
256 			((WsdlMessageAssertion) assertion).release();
257 			notifier.notifyChange();
258 		}
259 	}
260 
261 	public TestAssertion moveAssertion(int ix, int offset)
262 	{
263 		WsdlMessageAssertion assertion = getAssertionAt(ix);
264 		PropertyChangeNotifier notifier = new PropertyChangeNotifier();
265 
266 		try
267 		{
268 			return assertionsSupport.moveAssertion(ix, offset);
269 		}
270 		finally
271 		{
272 			((WsdlMessageAssertion) assertion).release();
273 			notifier.notifyChange();
274 		}
275 	}
276 
277 	public AssertionStatus getAssertionStatus()
278 	{
279 		currentStatus = AssertionStatus.UNKNOWN;
280 
281 		if (messageExchange != null)
282 		{
283 			/*
284 			 * if( !messageExchange.hasResponse() &&
285 			 * getOperation().isBidirectional() && !isWsaEnabled() ) {
286 			 * currentStatus = AssertionStatus.FAILED; }
287 			 */
288 		}
289 		else
290 			return currentStatus;
291 
292 		int cnt = getAssertionCount();
293 		if (cnt == 0)
294 			return currentStatus;
295 
296 		boolean hasEnabled = false;
297 
298 		for (int c = 0; c < cnt; c++)
299 		{
300 			if (!getAssertionAt(c).isDisabled())
301 				hasEnabled = true;
302 
303 			if (getAssertionAt(c).getStatus() == AssertionStatus.FAILED)
304 			{
305 				currentStatus = AssertionStatus.FAILED;
306 				break;
307 			}
308 		}
309 
310 		if (currentStatus == AssertionStatus.UNKNOWN && hasEnabled)
311 			currentStatus = AssertionStatus.VALID;
312 
313 		return currentStatus;
314 	}
315 
316 	@Override
317 	public ImageIcon getIcon()
318 	{
319 		if (forLoadTest || UISupport.isHeadless())
320 			return null;
321 
322 		TestMonitor testMonitor = SoapUI.getTestMonitor();
323 		if (testMonitor != null && testMonitor.hasRunningLoadTest(testStep.getTestCase()))
324 			return disabledRequestIcon;
325 
326 		ImageIcon icon = getIconAnimator().getIcon();
327 		if (icon == getIconAnimator().getBaseIcon())
328 		{
329 			AssertionStatus status = getAssertionStatus();
330 			if (status == AssertionStatus.VALID)
331 				return validRequestIcon;
332 			else if (status == AssertionStatus.FAILED)
333 				return failedRequestIcon;
334 			else if (status == AssertionStatus.UNKNOWN)
335 				return unknownRequestIcon;
336 		}
337 
338 		return icon;
339 	}
340 
341 	public void addAssertionsListener(AssertionsListener listener)
342 	{
343 		assertionsSupport.addAssertionsListener(listener);
344 	}
345 
346 	public void removeAssertionsListener(AssertionsListener listener)
347 	{
348 		assertionsSupport.removeAssertionsListener(listener);
349 	}
350 
351 	/***
352 	 * Called when a testrequest is moved in a testcase
353 	 */
354 
355 	@Override
356 	public void updateConfig(WsdlRequestConfig request)
357 	{
358 		super.updateConfig(request);
359 
360 		assertionsSupport.refresh();
361 
362 		List<AttachmentConfig> attachmentConfigs = getConfig().getAttachmentList();
363 		for (int i = 0; i < attachmentConfigs.size(); i++)
364 		{
365 			AttachmentConfig config = attachmentConfigs.get(i);
366 			getAttachmentsList().get(i).updateConfig(config);
367 		}
368 
369 	}
370 
371 	@Override
372 	public void release()
373 	{
374 		super.release();
375 		assertionsSupport.release();
376 	}
377 
378 	public String getAssertableContent()
379 	{
380 		return getResponse() == null ? null : getResponse().getContentAsString();
381 	}
382 
383 	public WsdlTestRequestStep getTestStep()
384 	{
385 		return testStep;
386 	}
387 
388 	public WsdlInterface getInterface()
389 	{
390 		return getOperation().getInterface();
391 	}
392 
393 	protected static class TestRequestIconAnimator extends RequestIconAnimator<WsdlTestRequest>
394 	{
395 		public TestRequestIconAnimator(WsdlTestRequest modelItem)
396 		{
397 			super(modelItem, "/request.gif", "/exec_request", 4, "gif");
398 		}
399 
400 		@Override
401 		public boolean beforeSubmit(Submit submit, SubmitContext context)
402 		{
403 			if (SoapUI.getTestMonitor() != null && SoapUI.getTestMonitor().hasRunningLoadTest(getTarget().getTestCase()))
404 				return true;
405 
406 			return super.beforeSubmit(submit, context);
407 		}
408 
409 		@Override
410 		public void afterSubmit(Submit submit, SubmitContext context)
411 		{
412 			if (submit.getRequest() == getTarget())
413 				stop();
414 		}
415 	}
416 
417 	public AssertableType getAssertableType()
418 	{
419 		return AssertableType.BOTH;
420 	}
421 
422 	public String getInterfaceName()
423 	{
424 		return testStep.getInterfaceName();
425 	}
426 
427 	public String getOperationName()
428 	{
429 		return testStep.getOperationName();
430 	}
431 
432 	public TestAssertion cloneAssertion(TestAssertion source, String name)
433 	{
434 		return assertionsSupport.cloneAssertion(source, name);
435 	}
436 
437 	public WsdlMessageAssertion importAssertion(WsdlMessageAssertion source, boolean overwrite, boolean createCopy)
438 	{
439 		return assertionsSupport.importAssertion(source, overwrite, createCopy);
440 	}
441 
442 	public List<TestAssertion> getAssertionList()
443 	{
444 		return new ArrayList<TestAssertion>(assertionsSupport.getAssertionList());
445 	}
446 
447 	public WsdlMessageAssertion getAssertionByName(String name)
448 	{
449 		return assertionsSupport.getAssertionByName(name);
450 	}
451 
452 	public ModelItem getModelItem()
453 	{
454 		return testStep;
455 	}
456 
457 	public Map<String, TestAssertion> getAssertions()
458 	{
459 		return assertionsSupport.getAssertions();
460 	}
461 
462 	public String getDefaultAssertableContent()
463 	{
464 		return getOperation().createResponse(true);
465 	}
466 
467 	public void resolve(ResolveContext<?> context)
468 	{
469 		super.resolve(context);
470 
471 		assertionsSupport.resolve(context);
472 	}
473 }