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.HttpRequestConfig;
23  import com.eviware.soapui.config.TestAssertionConfig;
24  import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
25  import com.eviware.soapui.impl.support.http.HttpRequest;
26  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
27  import com.eviware.soapui.impl.wsdl.support.assertions.AssertableConfig;
28  import com.eviware.soapui.impl.wsdl.support.assertions.AssertionsSupport;
29  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
30  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
31  import com.eviware.soapui.impl.wsdl.teststeps.assertions.TestAssertionRegistry.AssertableType;
32  import com.eviware.soapui.model.ModelItem;
33  import com.eviware.soapui.model.iface.Interface;
34  import com.eviware.soapui.model.iface.SubmitContext;
35  import com.eviware.soapui.model.testsuite.AssertionsListener;
36  import com.eviware.soapui.model.testsuite.TestAssertion;
37  import com.eviware.soapui.monitor.TestMonitor;
38  import com.eviware.soapui.support.UISupport;
39  
40  public class HttpTestRequest extends HttpRequest implements HttpTestRequestInterface<HttpRequestConfig>
41  {
42  	private final boolean forLoadTest;
43  	private HttpTestRequestStep testStep;
44  	private AssertionsSupport assertionsSupport;
45  	private HttpResponseMessageExchange messageExchange;
46  	private PropertyChangeNotifier notifier;
47  	private AssertionStatus currentStatus;
48  
49  	private ImageIcon validRequestIcon;
50  	private ImageIcon failedRequestIcon;
51  	private ImageIcon disabledRequestIcon;
52  	private ImageIcon unknownRequestIcon;
53  
54  	protected HttpTestRequest( HttpRequestConfig config, HttpTestRequestStep testStep, boolean forLoadTest )
55  	{
56  		super( config, forLoadTest );
57  		this.forLoadTest = forLoadTest;
58  
59  		setSettings( new XmlBeansSettingsImpl( this, testStep.getSettings(), config.getSettings() ) );
60  
61  		this.testStep = testStep;
62  
63  		initAssertions();
64  		initIcons();
65  	}
66  
67  	protected void initIcons()
68  	{
69  		validRequestIcon = UISupport.createImageIcon( "/valid_http_request.gif" );
70  		failedRequestIcon = UISupport.createImageIcon( "/invalid_http_request.gif" );
71  		unknownRequestIcon = UISupport.createImageIcon( "/unknown_http_request.gif" );
72  		disabledRequestIcon = UISupport.createImageIcon( "/disabled_http_request.gif" );
73  
74  		setIconAnimator( new RequestIconAnimator<HttpTestRequest>( this, "/http_request.gif", "/exec_http_request", 4,
75  				"gif" ) );
76  	}
77  
78  	private void initAssertions()
79  	{
80  		assertionsSupport = new AssertionsSupport( testStep, new AssertableConfig()
81  		{
82  			public TestAssertionConfig addNewAssertion()
83  			{
84  				return getConfig().addNewAssertion();
85  			}
86  
87  			public List<TestAssertionConfig> getAssertionList()
88  			{
89  				return getConfig().getAssertionList();
90  			}
91  
92  			public void removeAssertion( int ix )
93  			{
94  				getConfig().removeAssertion( ix );
95  			}
96  
97  			public TestAssertionConfig insertAssertion( TestAssertionConfig source, int ix )
98  			{
99  				TestAssertionConfig conf = getConfig().insertNewAssertion( ix );
100 				conf.set( source );
101 				return conf;
102 			}
103 		} );
104 	}
105 
106 	public int getAssertionCount()
107 	{
108 		return assertionsSupport.getAssertionCount();
109 	}
110 
111 	public WsdlMessageAssertion getAssertionAt( int c )
112 	{
113 		return assertionsSupport.getAssertionAt( c );
114 	}
115 
116 	public void setResponse( HttpResponse response, SubmitContext context )
117 	{
118 		super.setResponse( response, context );
119 		assertResponse( context );
120 	}
121 
122 	public void assertResponse( SubmitContext context )
123 	{
124 		if( notifier == null )
125 			notifier = new PropertyChangeNotifier();
126 
127 		messageExchange = getResponse() == null ? null : new HttpResponseMessageExchange( this );
128 
129 		if( messageExchange != null )
130 		{
131 			// assert!
132 			for( WsdlMessageAssertion assertion : assertionsSupport.getAssertionList() )
133 			{
134 				assertion.assertResponse( messageExchange, context );
135 			}
136 		}
137 
138 		notifier.notifyChange();
139 	}
140 
141 	@Override
142 	public ImageIcon getIcon()
143 	{
144 		if( forLoadTest || UISupport.isHeadless() )
145 			return null;
146 
147 		TestMonitor testMonitor = SoapUI.getTestMonitor();
148 		if( testMonitor != null && testMonitor.hasRunningLoadTest( testStep.getTestCase() ) )
149 			return disabledRequestIcon;
150 
151 		ImageIcon icon = getIconAnimator().getIcon();
152 		if( icon == getIconAnimator().getBaseIcon() )
153 		{
154 			AssertionStatus status = getAssertionStatus();
155 			if( status == AssertionStatus.VALID )
156 				return validRequestIcon;
157 			else if( status == AssertionStatus.FAILED )
158 				return failedRequestIcon;
159 			else if( status == AssertionStatus.UNKNOWN )
160 				return unknownRequestIcon;
161 		}
162 
163 		return icon;
164 	}
165 
166 	private class PropertyChangeNotifier
167 	{
168 		private AssertionStatus oldStatus;
169 		private ImageIcon oldIcon;
170 
171 		public PropertyChangeNotifier()
172 		{
173 			oldStatus = getAssertionStatus();
174 			oldIcon = getIcon();
175 		}
176 
177 		public void notifyChange()
178 		{
179 			AssertionStatus newStatus = getAssertionStatus();
180 			ImageIcon newIcon = getIcon();
181 
182 			if( oldStatus != newStatus )
183 				notifyPropertyChanged( STATUS_PROPERTY, oldStatus, newStatus );
184 
185 			if( oldIcon != newIcon )
186 				notifyPropertyChanged( ICON_PROPERTY, oldIcon, getIcon() );
187 
188 			oldStatus = newStatus;
189 			oldIcon = newIcon;
190 		}
191 	}
192 
193 	public WsdlMessageAssertion addAssertion( String assertionLabel )
194 	{
195 		PropertyChangeNotifier notifier = new PropertyChangeNotifier();
196 
197 		try
198 		{
199 			WsdlMessageAssertion assertion = assertionsSupport.addWsdlAssertion( assertionLabel );
200 			if( assertion == null )
201 				return null;
202 
203 			if( getResponse() != null )
204 			{
205 				assertion.assertResponse( new HttpResponseMessageExchange( this ), new WsdlTestRunContext( testStep ) );
206 				notifier.notifyChange();
207 			}
208 
209 			return assertion;
210 		}
211 		catch( Exception e )
212 		{
213 			SoapUI.logError( e );
214 			return null;
215 		}
216 	}
217 
218 	public void removeAssertion( TestAssertion assertion )
219 	{
220 		PropertyChangeNotifier notifier = new PropertyChangeNotifier();
221 
222 		try
223 		{
224 			assertionsSupport.removeAssertion( ( WsdlMessageAssertion )assertion );
225 
226 		}
227 		finally
228 		{
229 			( ( WsdlMessageAssertion )assertion ).release();
230 			notifier.notifyChange();
231 		}
232 	}
233 
234 	public TestAssertion moveAssertion( int ix, int offset )
235 	{
236 		PropertyChangeNotifier notifier = new PropertyChangeNotifier();
237 		WsdlMessageAssertion assertion = getAssertionAt( ix );
238 		try
239 		{
240 			return assertionsSupport.moveAssertion( ix, offset );
241 		}
242 		finally
243 		{
244 			( ( WsdlMessageAssertion )assertion ).release();
245 			notifier.notifyChange();
246 		}
247 	}
248 
249 	public AssertionStatus getAssertionStatus()
250 	{
251 		currentStatus = AssertionStatus.UNKNOWN;
252 
253 		if( messageExchange != null )
254 		{
255 			if( !messageExchange.hasResponse() && getOperation() != null && getOperation().isBidirectional() )
256 			{
257 				currentStatus = AssertionStatus.FAILED;
258 			}
259 		}
260 		else
261 			return currentStatus;
262 
263 		int cnt = getAssertionCount();
264 		if( cnt == 0 )
265 			return currentStatus;
266 
267 		for( int c = 0; c < cnt; c++ )
268 		{
269 			if( getAssertionAt( c ).getStatus() == AssertionStatus.FAILED )
270 			{
271 				currentStatus = AssertionStatus.FAILED;
272 				break;
273 			}
274 		}
275 
276 		if( currentStatus == AssertionStatus.UNKNOWN )
277 			currentStatus = AssertionStatus.VALID;
278 
279 		return currentStatus;
280 	}
281 
282 	public void addAssertionsListener( AssertionsListener listener )
283 	{
284 		assertionsSupport.addAssertionsListener( listener );
285 	}
286 
287 	public void removeAssertionsListener( AssertionsListener listener )
288 	{
289 		assertionsSupport.removeAssertionsListener( listener );
290 	}
291 
292 	public String getResponseContentAsString()
293 	{
294 		return getResponse() == null ? null : getResponse().getContentAsString();
295 	}
296 
297 	public String getAssertableContent()
298 	{
299 		return getResponseContentAsXml();
300 	}
301 
302 	public HttpTestRequestStep getTestStep()
303 	{
304 		return testStep;
305 	}
306 
307 	public TestAssertion cloneAssertion( TestAssertion source, String name )
308 	{
309 		return assertionsSupport.cloneAssertion( source, name );
310 	}
311 
312 	public List<TestAssertion> getAssertionList()
313 	{
314 		return new ArrayList<TestAssertion>( assertionsSupport.getAssertionList() );
315 	}
316 
317 	public WsdlMessageAssertion getAssertionByName( String name )
318 	{
319 		return assertionsSupport.getAssertionByName( name );
320 	}
321 
322 	public Map<String, TestAssertion> getAssertions()
323 	{
324 		return assertionsSupport.getAssertions();
325 	}
326 
327 	public String getDefaultAssertableContent()
328 	{
329 		return "";
330 	}
331 
332 	public AssertableType getAssertableType()
333 	{
334 		return AssertableType.RESPONSE;
335 	}
336 
337 	public Interface getInterface()
338 	{
339 		return null;
340 	}
341 
342 	public void updateConfig( HttpRequestConfig request )
343 	{
344 		super.updateConfig( request );
345 
346 		assertionsSupport.refresh();
347 	}
348 
349 	public WsdlTestCase getTestCase()
350 	{
351 		return testStep.getTestCase();
352 	}
353 
354 	public ModelItem getParent()
355 	{
356 		return getTestStep();
357 	}
358 
359 	public WsdlMessageAssertion importAssertion( WsdlMessageAssertion source, boolean overwrite, boolean createCopy )
360 	{
361 		return assertionsSupport.importAssertion( source, overwrite, createCopy );
362 	}
363 
364 }