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