View Javadoc

1    /*
2    *  soapUI, copyright (C) 2004-2007 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.awt.event.ActionEvent;
16  
17  import javax.swing.AbstractAction;
18  import javax.swing.Action;
19  import javax.swing.ImageIcon;
20  
21  import org.apache.xmlbeans.XmlObject;
22  
23  import com.eviware.soapui.config.RequestAssertionConfig;
24  import com.eviware.soapui.impl.wsdl.panels.support.assertions.Assertable;
25  import com.eviware.soapui.impl.wsdl.panels.support.assertions.Assertable.AssertionStatus;
26  import com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange;
27  import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError;
28  import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionException;
29  import com.eviware.soapui.impl.wsdl.teststeps.assertions.WsdlAssertionRegistry;
30  import com.eviware.soapui.model.iface.SubmitContext;
31  import com.eviware.soapui.model.settings.Settings;
32  import com.eviware.soapui.model.support.AbstractModelItem;
33  import com.eviware.soapui.support.UISupport;
34  
35  /***
36   * Base class for WsdlAssertions
37   * 
38   * @author Ole.Matzura
39   */
40  
41  public abstract class WsdlMessageAssertion extends AbstractModelItem
42  {
43  	private RequestAssertionConfig assertionConfig;
44     private final Assertable assertable;
45     private AssertionStatus assertionStatus = AssertionStatus.UNKNOWN;
46     private com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError [] assertionErrors;
47     private ImageIcon validIcon;
48     private ImageIcon failedIcon;
49     private ImageIcon unknownIcon;
50     
51     public final static String STATUS_PROPERTY = WsdlMessageAssertion.class.getName() + "@status";  
52     public final static String CONFIGURATION_PROPERTY = WsdlMessageAssertion.class.getName() + "@configuration";  
53     
54     public WsdlMessageAssertion(RequestAssertionConfig assertionConfig, Assertable modelItem)
55     {
56        this.assertionConfig = assertionConfig;
57        this.assertable = modelItem;
58        
59        addAction( new RenameAssertionAction() );
60        addAction( new RemoveAssertionAction() );
61        if( isConfigurable() )
62           addAction( new ConfigureAssertionAction() );
63        
64        validIcon = UISupport.createImageIcon("/valid_assertion.gif");
65        failedIcon = UISupport.createImageIcon("/failed_assertion.gif");
66        unknownIcon = UISupport.createImageIcon("/unknown_assertion.gif");      
67     }
68     
69     protected XmlObject getConfiguration()
70     {
71        if( null == assertionConfig.getConfiguration())
72        {
73           assertionConfig.addNewConfiguration();
74        }
75        
76        return assertionConfig.getConfiguration();
77     }
78  
79     protected void setConfiguration( XmlObject configuration )
80     {
81  	   XmlObject oldConfig = assertionConfig.getConfiguration();
82        assertionConfig.setConfiguration( configuration );
83        notifyPropertyChanged( WsdlMessageAssertion.CONFIGURATION_PROPERTY, oldConfig, configuration );
84     }
85     
86     public String getName()
87     {
88        return assertionConfig.isSetName() ? assertionConfig.getName() : 
89        	WsdlAssertionRegistry.getInstance().getAssertionNameForType( assertionConfig.getType());
90     }
91  
92     public void setName(String name)
93     {
94        String old = getName();
95        assertionConfig.setName( name );
96        notifyPropertyChanged( NAME_PROPERTY, old, name );
97     }
98     
99     public class RenameAssertionAction extends AbstractAction
100    {
101       public RenameAssertionAction()
102       {
103          super( "Rename" );
104          putValue( Action.SHORT_DESCRIPTION, "Renames this assertion" );
105       }
106       
107       public void actionPerformed(ActionEvent e)
108 		{
109          String name = UISupport.prompt("Specify name for this assertion", "Rename Assertion", WsdlMessageAssertion.this.getName() );
110          if( name == null || name.equals( WsdlMessageAssertion.this.getName() )) return;
111          
112          setName( name );
113       }
114    }
115    
116    public class RemoveAssertionAction extends AbstractAction
117    {
118       public RemoveAssertionAction()
119       {
120          super( "Remove" );
121          putValue( Action.SHORT_DESCRIPTION, "Removes this assertion from its request" );
122       }
123       
124       public void actionPerformed(ActionEvent e)
125 		{
126          if( UISupport.confirm( "Remove assertion [" + WsdlMessageAssertion.this.getName() + "] from [" + 
127                assertable.getTestStep().getName() + "]", "Remove Assertion" ))
128          {
129             assertable.removeAssertion( WsdlMessageAssertion.this );
130          }
131       }
132    }
133    
134    public class ConfigureAssertionAction extends AbstractAction
135    {
136       public ConfigureAssertionAction()
137       {
138          super( "Configure" );
139          putValue( Action.SHORT_DESCRIPTION, "Configures this assertion" );
140       }
141       
142       public void actionPerformed(ActionEvent e)
143 		{
144          configure();
145       }
146    }
147 
148    public AssertionStatus getStatus()
149    {
150       return assertionStatus;
151    }
152 
153    public AssertionError[] getErrors()
154    {
155       return assertionErrors;
156    }
157 
158    public AssertionStatus assertResponse( WsdlMessageExchange messageExchange, SubmitContext context)
159    {
160       AssertionStatus oldStatus = assertionStatus;
161       ImageIcon oldIcon = getIcon();
162       
163       if( !messageExchange.hasResponse() )
164       {
165       	if( messageExchange.getOperation().isOneWay() )
166       	{
167       		assertionStatus = AssertionStatus.VALID;
168 	         assertionErrors = null;
169       	}
170       	else
171       	{
172       		assertionStatus = AssertionStatus.FAILED;
173       		assertionErrors = new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[] 
174       	                    { new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError("null/empty response") };
175       	}
176       }
177       else
178       {
179 	      try
180 	      {
181 	      	internalAssertResponse( messageExchange, context );
182 	         assertionStatus = AssertionStatus.VALID;
183 	         assertionErrors = null;
184 	      }
185 	      catch ( AssertionException e )
186 	      {
187 	      	assertionStatus = AssertionStatus.FAILED;
188 	      	assertionErrors = e.getErrors();
189 	      }
190 	      catch (Throwable e)
191 	      {
192 	         assertionStatus = AssertionStatus.FAILED;
193 	         assertionErrors = new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[] 
194                            { new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError( e.getMessage() )};
195 	      }
196       }
197       
198       notifyPropertyChanged( STATUS_PROPERTY, oldStatus, assertionStatus );
199       notifyPropertyChanged( ICON_PROPERTY, oldIcon, getIcon() );
200       
201       return assertionStatus;
202    }
203 
204    protected abstract String internalAssertResponse(WsdlMessageExchange messageExchange, SubmitContext context ) throws AssertionException;
205    
206    public AssertionStatus assertRequest( WsdlMessageExchange messageExchange, SubmitContext context)
207    {
208       AssertionStatus oldStatus = assertionStatus;
209       ImageIcon oldIcon = getIcon();
210       
211       if( !messageExchange.hasRequest( true ) )
212       {
213       	assertionStatus = AssertionStatus.FAILED;
214       	assertionErrors = new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[] 
215       	                    { new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError("null/empty response") };
216       }
217       else
218       {
219 	      try
220 	      {
221 	      	internalAssertRequest( messageExchange, context );
222 	         assertionStatus = AssertionStatus.VALID;
223 	         assertionErrors = null;
224 	      }
225 	      catch ( AssertionException e )
226 	      {
227 	      	assertionStatus = AssertionStatus.FAILED;
228 	      	assertionErrors = e.getErrors();
229 	      }
230 	      catch (Throwable e)
231 	      {
232 	         assertionStatus = AssertionStatus.FAILED;
233 	         assertionErrors = new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[] 
234                            { new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError( e.getMessage() )};
235 	      }
236       }
237       
238       notifyPropertyChanged( STATUS_PROPERTY, oldStatus, assertionStatus );
239       notifyPropertyChanged( ICON_PROPERTY, oldIcon, getIcon() );
240       
241       return assertionStatus;
242    }
243    
244    protected abstract String internalAssertRequest(WsdlMessageExchange messageExchange, SubmitContext context ) throws AssertionException;
245 
246    public boolean isConfigurable()
247    {
248       return false;
249    }
250    
251    public boolean configure()
252    {
253    	return true;
254    }
255 
256    public ImageIcon getIcon()
257    {
258       switch( getStatus() )
259       {
260          case FAILED : return failedIcon; 
261          case UNKNOWN : return unknownIcon; 
262          case VALID : return validIcon;
263       }
264       
265       return null;
266    }
267 
268 	public void updateConfig(RequestAssertionConfig config)
269 	{
270 		this.assertionConfig = config;
271 	}
272 
273 	public Settings getSettings()
274 	{
275 		return assertable.getTestStep().getSettings();
276 	}
277 
278 	public void release()
279 	{
280 	}
281 	
282 	public Assertable getAssertable()
283 	{
284 		return assertable;
285 	}
286 }
287 
288