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