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