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