View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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  	protected String internalAssertRequest( MessageExchange messageExchange, SubmitContext context )
60  			throws AssertionException
61  	{
62  		return null;
63  	}
64  
65  	protected String internalAssertResponse( MessageExchange messageExchange, SubmitContext context )
66  			throws AssertionException
67  	{
68  
69  		// assert!
70  		if( messageExchange.getTimeTaken() > Long.parseLong( SLA ) )
71  		{
72  			throw new AssertionException( new AssertionError( "Response did not meet SLA "
73  					+ messageExchange.getTimeTaken() + "/" + SLA ) );
74  		}
75  
76  		return "Response meets SLA";
77  	}
78  
79  	/***
80  	 * @see com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion#configure()
81  	 */
82  	public boolean configure()
83  	{
84  		String value = SLA;
85  
86  		if( value == null || value.trim().length() == 0 )
87  		{
88  			value = "200";
89  		}
90  
91  		value = UISupport.prompt( "Specify desired response time", "Configure Response SLA Assertion", value );
92  
93  		try
94  		{
95  			Long.parseLong( value );
96  			SLA = value;
97  
98  		}
99  		catch( Exception e )
100 		{
101 			return false;
102 		}
103 
104 		setConfiguration( createConfiguration() );
105 		return true;
106 	}
107 
108 	public String getSLA()
109 	{
110 		return SLA;
111 	}
112 
113 	public void setSLA( String sla )
114 	{
115 		SLA = sla;
116 		setConfiguration( createConfiguration() );
117 	}
118 
119 	/***
120 	 * @return XmlObject, our config chunk
121 	 */
122 	protected XmlObject createConfiguration()
123 	{
124 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
125 		return builder.add( "SLA", SLA ).finish();
126 	}
127 
128 	public static class Factory extends AbstractTestAssertionFactory
129 	{
130 		public Factory()
131 		{
132 			super( ResponseSLAAssertion.ID, ResponseSLAAssertion.LABEL, ResponseSLAAssertion.class );
133 		}
134 	}
135 }