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.util.Collections;
16  import java.util.List;
17  
18  import javax.swing.ImageIcon;
19  
20  import org.apache.xmlbeans.XmlObject;
21  
22  import com.eviware.soapui.config.RequestAssertionConfig;
23  import com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange;
24  import com.eviware.soapui.impl.wsdl.teststeps.assertions.WsdlAssertionRegistry;
25  import com.eviware.soapui.model.ModelItem;
26  import com.eviware.soapui.model.iface.MessageExchange;
27  import com.eviware.soapui.model.iface.SubmitContext;
28  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
29  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContainer;
30  import com.eviware.soapui.model.settings.Settings;
31  import com.eviware.soapui.model.support.AbstractModelItem;
32  import com.eviware.soapui.model.support.ModelSupport;
33  import com.eviware.soapui.model.testsuite.Assertable;
34  import com.eviware.soapui.model.testsuite.AssertionError;
35  import com.eviware.soapui.model.testsuite.AssertionException;
36  import com.eviware.soapui.model.testsuite.TestAssertion;
37  import com.eviware.soapui.model.testsuite.Assertable.AssertionStatus;
38  import com.eviware.soapui.support.UISupport;
39  
40  /***
41   * Base class for WsdlAssertions
42   * 
43   * @author Ole.Matzura
44   */
45  
46  public abstract class WsdlMessageAssertion extends AbstractModelItem implements PropertyExpansionContainer, TestAssertion
47  {
48  	private RequestAssertionConfig assertionConfig;
49     private final Assertable assertable;
50     private AssertionStatus assertionStatus = AssertionStatus.UNKNOWN;
51     private com.eviware.soapui.model.testsuite.AssertionError [] assertionErrors;
52     private ImageIcon validIcon;
53     private ImageIcon failedIcon;
54     private ImageIcon unknownIcon;
55     
56     private final boolean cloneable;
57  	private final boolean configurable;
58  	private final boolean allowMultiple;
59  	private final boolean requiresResponseContent;  
60     
61     protected WsdlMessageAssertion(RequestAssertionConfig assertionConfig, Assertable modelItem, 
62     			boolean cloneable, boolean configurable, boolean multiple, boolean requiresResponseContent )
63     {
64        this.assertionConfig = assertionConfig;
65        this.assertable = modelItem;
66  		this.cloneable = cloneable;
67  		this.configurable = configurable;
68  		this.allowMultiple = multiple;
69  		this.requiresResponseContent = requiresResponseContent;
70        
71        validIcon = UISupport.createImageIcon("/valid_assertion.gif");
72        failedIcon = UISupport.createImageIcon("/failed_assertion.gif");
73        unknownIcon = UISupport.createImageIcon("/unknown_assertion.gif");      
74     }
75     
76     public XmlObject getConfiguration()
77     {
78        if( null == assertionConfig.getConfiguration())
79        {
80           assertionConfig.addNewConfiguration();
81        }
82        
83        return assertionConfig.getConfiguration();
84     }
85  
86     public void setConfiguration( XmlObject configuration )
87     {
88  	   XmlObject oldConfig = assertionConfig.getConfiguration();
89        assertionConfig.setConfiguration( configuration );
90        notifyPropertyChanged( TestAssertion.CONFIGURATION_PROPERTY, oldConfig, configuration );
91     }
92     
93     /* (non-Javadoc)
94  	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getName()
95  	 */
96     public String getName()
97     {
98        return assertionConfig.isSetName() ? assertionConfig.getName() : 
99        	WsdlAssertionRegistry.getInstance().getAssertionNameForType( assertionConfig.getType());
100    }
101 
102    /* (non-Javadoc)
103 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getStatus()
104 	 */
105    public AssertionStatus getStatus()
106    {
107       return isDisabled() ? AssertionStatus.UNKNOWN : assertionStatus;
108    }
109 
110    /* (non-Javadoc)
111 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getErrors()
112 	 */
113    public AssertionError[] getErrors()
114    {
115       return isDisabled() ? null : assertionErrors;
116    }
117 
118    /* (non-Javadoc)
119 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#isAllowMultiple()
120 	 */
121    public boolean isAllowMultiple()
122    {
123    	return allowMultiple;
124    }
125    
126    public AssertionStatus assertResponse( MessageExchange messageExchange, SubmitContext context)
127    {
128       AssertionStatus oldStatus = assertionStatus;
129       AssertionError[] oldErrors = getErrors();
130       ImageIcon oldIcon = getIcon();
131       
132       if( isDisabled() )
133       {
134       	assertionStatus = AssertionStatus.UNKNOWN;
135       	assertionErrors = null;
136       }
137       else if( !messageExchange.hasResponse() && requiresResponseContent )
138       {
139    		assertionStatus = AssertionStatus.FAILED;
140    		assertionErrors = new com.eviware.soapui.model.testsuite.AssertionError[] 
141    	                    { new com.eviware.soapui.model.testsuite.AssertionError("null/empty response") };
142       }
143       else
144       {
145 	      try
146 	      {
147 	      	internalAssertResponse( ( WsdlMessageExchange ) messageExchange, context );
148 	         assertionStatus = AssertionStatus.VALID;
149 	         assertionErrors = null;
150 	      }
151 	      catch ( AssertionException e )
152 	      {
153 	      	assertionStatus = AssertionStatus.FAILED;
154 	      	assertionErrors = e.getErrors();
155 	      }
156 	      catch (Throwable e)
157 	      {
158 	         assertionStatus = AssertionStatus.FAILED;
159 	         assertionErrors = new com.eviware.soapui.model.testsuite.AssertionError[] 
160                            { new com.eviware.soapui.model.testsuite.AssertionError( e.getMessage() )};
161 	      }
162       }
163       
164       notifyPropertyChanged( STATUS_PROPERTY, oldStatus, assertionStatus );
165       notifyPropertyChanged( ERRORS_PROPERTY, oldErrors, assertionErrors );
166       notifyPropertyChanged( ICON_PROPERTY, oldIcon, getIcon() );
167       
168       return assertionStatus;
169    }
170 
171    protected abstract String internalAssertResponse(WsdlMessageExchange messageExchange, SubmitContext context ) throws AssertionException;
172    
173    public AssertionStatus assertRequest( MessageExchange messageExchange, SubmitContext context)
174    {
175       AssertionStatus oldStatus = assertionStatus;
176       ImageIcon oldIcon = getIcon();
177       
178       if( !messageExchange.hasRequest( true ) )
179       {
180       	assertionStatus = AssertionStatus.FAILED;
181       	assertionErrors = new com.eviware.soapui.model.testsuite.AssertionError[] 
182       	                    { new com.eviware.soapui.model.testsuite.AssertionError("null/empty request") };
183       }
184       else
185       {
186 	      try
187 	      {
188 	      	internalAssertRequest( ( WsdlMessageExchange ) messageExchange, context );
189 	         assertionStatus = AssertionStatus.VALID;
190 	         assertionErrors = null;
191 	      }
192 	      catch ( AssertionException e )
193 	      {
194 	      	assertionStatus = AssertionStatus.FAILED;
195 	      	assertionErrors = e.getErrors();
196 	      }
197 	      catch (Throwable e)
198 	      {
199 	         assertionStatus = AssertionStatus.FAILED;
200 	         assertionErrors = new com.eviware.soapui.model.testsuite.AssertionError[] 
201                            { new com.eviware.soapui.model.testsuite.AssertionError( e.getMessage() )};
202 	      }
203       }
204       
205       notifyPropertyChanged( STATUS_PROPERTY, oldStatus, assertionStatus );
206       notifyPropertyChanged( ICON_PROPERTY, oldIcon, getIcon() );
207       
208       return assertionStatus;
209    }
210    
211    protected abstract String internalAssertRequest(WsdlMessageExchange messageExchange, SubmitContext context ) throws AssertionException;
212 
213    /* (non-Javadoc)
214 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#isConfigurable()
215 	 */
216    public boolean isConfigurable()
217    {
218       return configurable;
219    }
220    
221    /* (non-Javadoc)
222 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#isClonable()
223 	 */
224    public boolean isClonable()
225    {
226       return cloneable;
227    }
228    
229    /* (non-Javadoc)
230 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#configure()
231 	 */
232    public boolean configure()
233    {
234    	return true;
235    }
236 
237    /* (non-Javadoc)
238 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getDescription()
239 	 */
240    public String getDescription()
241 	{
242 		return getConfig().getDescription();
243 	}
244 
245 	/* (non-Javadoc)
246 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getIcon()
247 	 */
248 	public ImageIcon getIcon()
249    {
250       switch( getStatus() )
251       {
252          case FAILED : return failedIcon; 
253          case UNKNOWN : return unknownIcon; 
254          case VALID : return validIcon;
255       }
256       
257       return null;
258    }
259 
260 	public void updateConfig(RequestAssertionConfig config)
261 	{
262 		this.assertionConfig = config;
263 	}
264 
265 	public RequestAssertionConfig getConfig()
266 	{
267 		return assertionConfig;
268 	}
269 	
270 	public Settings getSettings()
271 	{
272 		return assertable.getModelItem().getSettings();
273 	}
274 
275 	public void release()
276 	{
277 	}
278 	
279 	/* (non-Javadoc)
280 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getAssertable()
281 	 */
282 	public Assertable getAssertable()
283 	{
284 		return assertable;
285 	}
286 	
287 	public String getId()
288 	{
289 		if( !assertionConfig.isSetId())
290 			assertionConfig.setId( ModelSupport.generateModelItemID() );
291 		
292 		return assertionConfig.getId();
293 	}
294 	
295 	@SuppressWarnings("unchecked")
296    public List<? extends ModelItem> getChildren()
297 	{
298 	   return Collections.EMPTY_LIST;
299 	}
300 
301 	public PropertyExpansion[] getPropertyExpansions()
302 	{
303 		return null;
304 	}
305 	
306 	public void setName( String name )
307 	{
308 		String oldLabel = getLabel();
309    	
310 		String old = getName();
311       assertionConfig.setName( name );
312       notifyPropertyChanged( NAME_PROPERTY, old, name );
313 		
314 		String label = getLabel();
315 		if( !oldLabel.equals( label ))
316 		{
317 			notifyPropertyChanged( LABEL_PROPERTY, oldLabel, label );
318 		}
319 	}
320 
321 	/* (non-Javadoc)
322 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getLabel()
323 	 */
324 	public String getLabel()
325 	{
326 		String name = getName();
327 		if( isDisabled() )
328 			return name + " (disabled)";
329 		else
330 			return name;
331 	}
332 
333 	/* (non-Javadoc)
334 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#isDisabled()
335 	 */
336 	public boolean isDisabled()
337 	{
338 		return getConfig().getDisabled();
339 	}
340 	
341 	public void setDisabled( boolean disabled )
342 	{
343 		String oldLabel = getLabel();
344 		
345 		boolean oldDisabled = isDisabled();
346 		if( oldDisabled == disabled )
347 			return;
348 		
349 		if( disabled )
350 		{
351 			getConfig().setDisabled( disabled );
352 		}
353 		else if( getConfig().isSetDisabled() )
354 		{
355 			getConfig().unsetDisabled();
356 		}
357 		
358 		String label = getLabel();
359 		if( !oldLabel.equals( label ))
360 		{
361 			notifyPropertyChanged( LABEL_PROPERTY, oldLabel, label );
362 		}
363 		
364 		notifyPropertyChanged( DISABLED_PROPERTY, oldDisabled, disabled );
365 	}
366 
367 	public ModelItem getParent()
368 	{
369 		return assertable.getModelItem();
370 	}
371 	
372 	public boolean isValid()
373 	{
374 		return getStatus() == AssertionStatus.VALID;
375 	}
376 
377 	public boolean isFailed()
378 	{
379 		return getStatus() == AssertionStatus.FAILED;
380 	}
381 }