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.assertions;
14  
15  import org.apache.xmlbeans.XmlObject;
16  
17  import com.eviware.soapui.config.RequestAssertionConfig;
18  import com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange;
19  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
20  import com.eviware.soapui.model.iface.SubmitContext;
21  import com.eviware.soapui.model.testsuite.Assertable;
22  import com.eviware.soapui.model.testsuite.AssertionError;
23  import com.eviware.soapui.model.testsuite.AssertionException;
24  import com.eviware.soapui.model.testsuite.ResponseAssertion;
25  import com.eviware.soapui.support.UISupport;
26  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
27  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
28  
29  /***
30   * Assertion for verifiying that responses occurred in the desired amount of
31   * time.
32   * 
33   * @author Cory Lewis cory.lewis@genworth.com
34   * 
35   * with help from
36   * @author Ole.Matzura
37   */
38  
39  public class ResponseSLAAssertion extends WsdlMessageAssertion implements ResponseAssertion
40  {
41  	public static final String ID = "Response SLA Assertion";
42  	private String SLA;
43  
44  	/***
45  	 * Constructor for our assertion.
46  	 * 
47  	 * @param assertionConfig
48  	 * @param modelItem
49  	 */
50  	public ResponseSLAAssertion( RequestAssertionConfig assertionConfig, Assertable modelItem )
51  	{
52  		super( assertionConfig, modelItem, false, true, false, false );
53  		XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( getConfiguration() );
54  		SLA = reader.readString( "SLA", "200" );
55  	}
56  
57  	/***
58  	 * @see com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion#internalAssertRequest(com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange,
59  	 *      com.eviware.soapui.model.iface.SubmitContext)
60  	 */
61  	protected String internalAssertRequest( WsdlMessageExchange messageExchange, SubmitContext context )
62  				throws AssertionException
63  	{
64  
65  		return null;
66  
67  	}
68  
69  	/***
70  	 * @see com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion#internalAssertResponse(com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange,
71  	 *      com.eviware.soapui.model.iface.SubmitContext)
72  	 */
73  	protected String internalAssertResponse( WsdlMessageExchange messageExchange, SubmitContext context )
74  				throws AssertionException
75  	{
76  
77  		// assert!
78  		if( messageExchange.getTimeTaken() > Long.parseLong( SLA ) )
79  		{
80  			throw new AssertionException( new AssertionError( "Response did not meet SLA "
81  						+ messageExchange.getTimeTaken() + "/" + SLA ) );
82  		}
83  
84  		return "Response meets SLA";
85  	}
86  
87  	/***
88  	 * @see com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion#configure()
89  	 */
90  	public boolean configure()
91  	{
92  		String value = SLA;
93  
94  		if( value == null || value.trim().length() == 0 )
95  		{
96  			value = "200";
97  		}
98  
99  		value = UISupport.prompt( "Specify desired response time", "Configure Response SLA Assertion", value );
100 
101 		try
102 		{
103 			Long.parseLong( value );
104 			SLA = value;
105 
106 		}
107 		catch( Exception e )
108 		{
109 			return false;
110 		}
111 
112 		setConfiguration( createConfiguration() );
113 		return true;
114 	}
115 
116 	
117 	
118 	public String getSLA()
119 	{
120 		return SLA;
121 	}
122 
123 	public void setSLA( String sla )
124 	{
125 		SLA = sla;
126 		setConfiguration( createConfiguration() );
127 	}
128 
129 	/***
130 	 * @return XmlObject, our config chunk
131 	 */
132 	protected XmlObject createConfiguration()
133 	{
134 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
135 		return builder.add( "SLA", SLA ).finish();
136 	}
137 }