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