View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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 com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.config.RestMethodConfig;
17  import com.eviware.soapui.config.TestAssertionConfig;
18  import com.eviware.soapui.impl.rest.RestRequest;
19  import com.eviware.soapui.impl.rest.RestResource;
20  import com.eviware.soapui.impl.rest.RestService;
21  import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
22  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
23  import com.eviware.soapui.impl.wsdl.support.assertions.AssertableConfig;
24  import com.eviware.soapui.impl.wsdl.support.assertions.AssertionsSupport;
25  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
26  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
27  import com.eviware.soapui.impl.wsdl.teststeps.assertions.TestAssertionRegistry.AssertableType;
28  import com.eviware.soapui.model.ModelItem;
29  import com.eviware.soapui.model.iface.Submit;
30  import com.eviware.soapui.model.iface.SubmitContext;
31  import com.eviware.soapui.model.testsuite.Assertable;
32  import com.eviware.soapui.model.testsuite.AssertionsListener;
33  import com.eviware.soapui.model.testsuite.TestAssertion;
34  import com.eviware.soapui.monitor.TestMonitor;
35  import com.eviware.soapui.support.Tools;
36  import com.eviware.soapui.support.UISupport;
37  import com.eviware.soapui.support.resolver.ResolveContext;
38  
39  import javax.swing.*;
40  import java.net.MalformedURLException;
41  import java.net.URL;
42  import java.util.ArrayList;
43  import java.util.List;
44  import java.util.Map;
45  
46  public class RestTestRequest extends RestRequest implements Assertable, TestRequest
47  {
48     public static final String RESPONSE_PROPERTY = RestTestRequest.class.getName() + "@response";
49     public static final String STATUS_PROPERTY = RestTestRequest.class.getName() + "@status";
50  
51     private ImageIcon validRequestIcon;
52     private ImageIcon failedRequestIcon;
53     private ImageIcon disabledRequestIcon;
54     private ImageIcon unknownRequestIcon;
55  
56     private AssertionStatus currentStatus;
57     private HttpTestRequestStep testStep;
58  
59     private AssertionsSupport assertionsSupport;
60     private RestResponseMessageExchange messageExchange;
61     private final boolean forLoadTest;
62     private PropertyChangeNotifier notifier;
63     private RestResource restResource;
64  
65     public RestTestRequest( RestResource resource, RestMethodConfig callConfig, HttpTestRequestStep testStep,
66                             boolean forLoadTest )
67     {
68        super( resource, callConfig, forLoadTest );
69        this.forLoadTest = forLoadTest;
70  
71        setSettings( new XmlBeansSettingsImpl( this, testStep.getSettings(), callConfig.getSettings() ) );
72  
73        this.testStep = testStep;
74  
75        initAssertions();
76        initIcons();
77     }
78  
79     public ModelItem getParent()
80     {
81        return getTestStep();
82     }
83  
84     public WsdlTestCase getTestCase()
85     {
86        return testStep.getTestCase();
87     }
88  
89     protected void initIcons()
90     {
91        boolean isRest = getTestStep() instanceof RestTestRequestStep;
92  
93        validRequestIcon = !isRest ? UISupport.createImageIcon( "/valid_http_request.gif" ) :
94                UISupport.createImageIcon( "/valid_rest_request.gif" );
95  
96        failedRequestIcon = !isRest ? UISupport.createImageIcon( "/invalid_http_request.gif" ) :
97                UISupport.createImageIcon( "/invalid_rest_request.gif" );
98  
99        unknownRequestIcon = !isRest ? UISupport.createImageIcon( "/unknown_http_request.gif" ) :
100               UISupport.createImageIcon( "/unknown_rest_request.gif" );
101 
102       disabledRequestIcon = !isRest ? UISupport.createImageIcon( "/disabled_http_request.gif" ) :
103               UISupport.createImageIcon( "/disabled_rest_request.gif" );
104 
105       setIconAnimator( new TestRequestIconAnimator( this ) );
106    }
107 
108    private void initAssertions()
109    {
110       assertionsSupport = new AssertionsSupport( testStep, new AssertableConfig()
111       {
112 
113          public TestAssertionConfig addNewAssertion()
114          {
115             return getConfig().addNewAssertion();
116          }
117 
118          public List<TestAssertionConfig> getAssertionList()
119          {
120             return getConfig().getAssertionList();
121          }
122 
123          public void removeAssertion( int ix )
124          {
125             getConfig().removeAssertion( ix );
126          }
127       } );
128    }
129 
130    public int getAssertionCount()
131    {
132       return assertionsSupport.getAssertionCount();
133    }
134 
135    public WsdlMessageAssertion getAssertionAt( int c )
136    {
137       return assertionsSupport.getAssertionAt( c );
138    }
139 
140    public void setResponse( HttpResponse response, SubmitContext context )
141    {
142       HttpResponse oldResponse = getResponse();
143       super.setResponse( response, context );
144 
145       if( response != oldResponse )
146          assertResponse( context );
147    }
148 
149    public void assertResponse( SubmitContext context )
150    {
151       if( notifier == null )
152          notifier = new PropertyChangeNotifier();
153 
154       messageExchange = new RestResponseMessageExchange( this );
155 
156       // assert!
157       for( WsdlMessageAssertion assertion : assertionsSupport.getAssertionList() )
158       {
159          assertion.assertResponse( messageExchange, context );
160       }
161 
162       notifier.notifyChange();
163    }
164 
165    private class PropertyChangeNotifier
166    {
167       private AssertionStatus oldStatus;
168       private ImageIcon oldIcon;
169 
170       public PropertyChangeNotifier()
171       {
172          oldStatus = getAssertionStatus();
173          oldIcon = getIcon();
174       }
175 
176       public void notifyChange()
177       {
178          AssertionStatus newStatus = getAssertionStatus();
179          ImageIcon newIcon = getIcon();
180 
181          if( oldStatus != newStatus )
182             notifyPropertyChanged( STATUS_PROPERTY, oldStatus, newStatus );
183 
184          if( oldIcon != newIcon )
185             notifyPropertyChanged( ICON_PROPERTY, oldIcon, getIcon() );
186 
187          oldStatus = newStatus;
188          oldIcon = newIcon;
189       }
190    }
191 
192    public WsdlMessageAssertion addAssertion( String assertionLabel )
193    {
194       PropertyChangeNotifier notifier = new PropertyChangeNotifier();
195 
196       try
197       {
198          WsdlMessageAssertion assertion = assertionsSupport.addWsdlAssertion( assertionLabel );
199          if( assertion == null )
200             return null;
201 
202          if( getResponse() != null )
203          {
204             assertion.assertResponse( new RestResponseMessageExchange( this ), new WsdlTestRunContext( testStep ) );
205             notifier.notifyChange();
206          }
207 
208          return assertion;
209       }
210       catch( Exception e )
211       {
212          SoapUI.logError( e );
213          return null;
214       }
215    }
216 
217    public void removeAssertion( TestAssertion assertion )
218    {
219       PropertyChangeNotifier notifier = new PropertyChangeNotifier();
220 
221       try
222       {
223          assertionsSupport.removeAssertion( (WsdlMessageAssertion) assertion );
224 
225       }
226       finally
227       {
228          ((WsdlMessageAssertion) assertion).release();
229          notifier.notifyChange();
230       }
231    }
232 
233    public AssertionStatus getAssertionStatus()
234    {
235       currentStatus = AssertionStatus.UNKNOWN;
236 
237       if( messageExchange != null )
238       {
239          if( !messageExchange.hasResponse() &&
240                  getOperation().isBidirectional() )
241          {
242             currentStatus = AssertionStatus.FAILED;
243          }
244       }
245       else
246          return currentStatus;
247 
248       int cnt = getAssertionCount();
249       if( cnt == 0 )
250          return currentStatus;
251 
252       for( int c = 0; c < cnt; c++ )
253       {
254          if( getAssertionAt( c ).getStatus() == AssertionStatus.FAILED )
255          {
256             currentStatus = AssertionStatus.FAILED;
257             break;
258          }
259       }
260 
261       if( currentStatus == AssertionStatus.UNKNOWN )
262          currentStatus = AssertionStatus.VALID;
263 
264       return currentStatus;
265    }
266 
267    @Override
268    public ImageIcon getIcon()
269    {
270       if( forLoadTest || UISupport.isHeadless() )
271          return null;
272 
273       TestMonitor testMonitor = SoapUI.getTestMonitor();
274       if( testMonitor != null && testMonitor.hasRunningLoadTest( testStep.getTestCase() ) )
275          return disabledRequestIcon;
276 
277       ImageIcon icon = getIconAnimator().getIcon();
278       if( icon == getIconAnimator().getBaseIcon() )
279       {
280          AssertionStatus status = getAssertionStatus();
281          if( status == AssertionStatus.VALID )
282             return validRequestIcon;
283          else if( status == AssertionStatus.FAILED )
284             return failedRequestIcon;
285          else if( status == AssertionStatus.UNKNOWN )
286             return unknownRequestIcon;
287       }
288 
289       return icon;
290    }
291 
292    public void addAssertionsListener( AssertionsListener listener )
293    {
294       assertionsSupport.addAssertionsListener( listener );
295    }
296 
297    public void removeAssertionsListener( AssertionsListener listener )
298    {
299       assertionsSupport.removeAssertionsListener( listener );
300    }
301 
302    /***
303     * Called when a testrequest is moved in a testcase
304     */
305 
306    public void updateConfig( RestMethodConfig request )
307    {
308       super.updateConfig( request );
309 
310       assertionsSupport.refresh();
311    }
312 
313    @Override
314    public void release()
315    {
316       super.release();
317       assertionsSupport.release();
318    }
319 
320    public String getAssertableContent()
321    {
322       return getResponseContentAsXml();
323    }
324 
325    public HttpTestRequestStep getTestStep()
326    {
327       return testStep;
328    }
329 
330    public RestService getInterface()
331    {
332       return getOperation() == null ? null : getOperation().getInterface();
333    }
334 
335    @Override
336    public RestResource getOperation()
337    {
338       return testStep instanceof RestTestRequestStep ? ((RestTestRequestStep) testStep).getResource() : null;
339    }
340 
341    protected static class TestRequestIconAnimator extends RequestIconAnimator<RestTestRequest>
342    {
343       public TestRequestIconAnimator( RestTestRequest modelItem )
344       {
345          super( modelItem,
346                  modelItem.getTestStep() instanceof RestTestRequestStep ? "/rest_request.gif" : "/http_request.gif",
347                  modelItem.getTestStep() instanceof RestTestRequestStep ? "/exec_rest_request" : "/exec_http_request", 4, "gif" );
348       }
349 
350       @Override
351       public boolean beforeSubmit( Submit submit, SubmitContext context )
352       {
353          if( SoapUI.getTestMonitor() != null && SoapUI.getTestMonitor().hasRunningLoadTest( getTarget().getTestCase() ) )
354             return true;
355 
356          return super.beforeSubmit( submit, context );
357       }
358 
359       @Override
360       public void afterSubmit( Submit submit, SubmitContext context )
361       {
362          if( submit.getRequest() == getTarget() )
363             stop();
364       }
365    }
366 
367    public AssertableType getAssertableType()
368    {
369       return AssertableType.RESPONSE;
370    }
371 
372    public TestAssertion cloneAssertion( TestAssertion source, String name )
373    {
374       return assertionsSupport.cloneAssertion( source, name );
375    }
376 
377    public WsdlMessageAssertion importAssertion( WsdlMessageAssertion source, boolean overwrite, boolean createCopy )
378    {
379       return assertionsSupport.importAssertion( source, overwrite, createCopy );
380    }
381 
382    public List<TestAssertion> getAssertionList()
383    {
384       return new ArrayList<TestAssertion>( assertionsSupport.getAssertionList() );
385    }
386 
387    public WsdlMessageAssertion getAssertionByName( String name )
388    {
389       return assertionsSupport.getAssertionByName( name );
390    }
391 
392    public ModelItem getModelItem()
393    {
394       return testStep;
395    }
396 
397    public Map<String, TestAssertion> getAssertions()
398    {
399       return assertionsSupport.getAssertions();
400    }
401 
402    public String getDefaultAssertableContent()
403    {
404       return "";
405    }
406 
407    public String getResponseContentAsString()
408    {
409       return getResponse() == null ? null : getResponse().getContentAsString();
410    }
411 
412    public void setPath( String fullPath )
413    {
414       super.setPath( fullPath );
415 
416       if( getOperation() == null )
417       {
418          try
419          {
420             setEndpoint( Tools.getEndpointFromUrl( new URL( fullPath ) ) );
421          }
422          catch( MalformedURLException e )
423          {
424 
425          }
426       }
427    }
428 
429    public void setResource( RestResource restResource )
430    {
431       if( this.restResource != null )
432          this.restResource.removePropertyChangeListener( this );
433 
434       this.restResource = restResource;
435 
436       restResource.addPropertyChangeListener( this );
437    }
438 
439    public RestResource getResource()
440    {
441       return restResource;
442    }
443 
444    public void resolve( ResolveContext context )
445    {
446       super.resolve( context );
447       assertionsSupport.resolve( context );
448    }
449 
450    public String getServiceName()
451    {
452       return testStep instanceof RestTestRequestStep ? ((RestTestRequestStep)testStep).getService() : null;
453    }
454 }