1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.teststeps.assertions.basic;
14
15 import com.eviware.soapui.config.TestAssertionConfig;
16 import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
17 import com.eviware.soapui.impl.wsdl.teststeps.assertions.AbstractTestAssertionFactory;
18 import com.eviware.soapui.model.iface.MessageExchange;
19 import com.eviware.soapui.model.iface.SubmitContext;
20 import com.eviware.soapui.model.testsuite.Assertable;
21 import com.eviware.soapui.model.testsuite.AssertionError;
22 import com.eviware.soapui.model.testsuite.AssertionException;
23 import com.eviware.soapui.model.testsuite.ResponseAssertion;
24 import com.eviware.soapui.support.UISupport;
25 import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
26 import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
27 import org.apache.xmlbeans.XmlObject;
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 public static final String LABEL = "Response SLA";
43 private String SLA;
44
45 /***
46 * Constructor for our assertion.
47 *
48 * @param assertionConfig
49 * @param modelItem
50 */
51 public ResponseSLAAssertion( TestAssertionConfig assertionConfig, Assertable modelItem )
52 {
53 super( assertionConfig, modelItem, false, true, false, false );
54 XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( getConfiguration() );
55 SLA = reader.readString( "SLA", "200" );
56 }
57
58 protected String internalAssertRequest( MessageExchange messageExchange, SubmitContext context )
59 throws AssertionException
60 {
61 return null;
62 }
63
64 protected String internalAssertResponse( MessageExchange messageExchange, SubmitContext context )
65 throws AssertionException
66 {
67
68
69 if( messageExchange.getTimeTaken() > Long.parseLong( SLA ) )
70 {
71 throw new AssertionException( new AssertionError( "Response did not meet SLA "
72 + messageExchange.getTimeTaken() + "/" + SLA ) );
73 }
74
75 return "Response meets SLA";
76 }
77
78 /***
79 * @see com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion#configure()
80 */
81 public boolean configure()
82 {
83 String value = SLA;
84
85 if( value == null || value.trim().length() == 0 )
86 {
87 value = "200";
88 }
89
90 value = UISupport.prompt( "Specify desired response time", "Configure Response SLA Assertion", value );
91
92 try
93 {
94 Long.parseLong( value );
95 SLA = value;
96
97 }
98 catch( Exception e )
99 {
100 return false;
101 }
102
103 setConfiguration( createConfiguration() );
104 return true;
105 }
106
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 }