1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.loadtest.assertions;
14
15 import java.util.List;
16
17 import org.apache.log4j.Logger;
18 import org.apache.xmlbeans.XmlObject;
19
20 import com.eviware.soapui.config.LoadTestAssertionConfig;
21 import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
22 import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics;
23 import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics.Statistic;
24 import com.eviware.soapui.impl.wsdl.support.Configurable;
25 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
26 import com.eviware.soapui.model.testsuite.LoadTestRunContext;
27 import com.eviware.soapui.model.testsuite.LoadTestRunner;
28 import com.eviware.soapui.model.testsuite.TestCaseRunContext;
29 import com.eviware.soapui.model.testsuite.TestCaseRunner;
30 import com.eviware.soapui.model.testsuite.TestStep;
31 import com.eviware.soapui.model.testsuite.TestStepResult;
32 import com.eviware.soapui.support.UISupport;
33 import com.eviware.soapui.support.types.StringToStringMap;
34 import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
35 import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
36 import com.eviware.x.form.XForm;
37 import com.eviware.x.form.XFormDialog;
38 import com.eviware.x.form.XFormDialogBuilder;
39 import com.eviware.x.form.XFormFactory;
40 import com.eviware.x.form.XForm.FieldType;
41
42 /***
43 * LoadTestAssertion for asserting the maximum step time
44 *
45 * @author Ole.Matzura
46 */
47
48 public class TestStepMaxAssertion extends AbstractLoadTestAssertion implements Configurable
49 {
50 private static final String NAME_FIELD = "Name";
51 private static final String NAME_ELEMENT = "name";
52 private static final String MAX_VALUE_ELEMENT = "max-value";
53 private static final String MIN_REQUESTS_ELEMENT = "min-requests";
54 private static final String MAX_VALUE_FIELD = "Max Time";
55 private static final String MINIMUM_REQUESTS_FIELD = "Minimum Requests";
56 private static final String MAX_ERRORS_ELEMENT = "max-errors";
57 private static final String MAX_ERRORS_FIELD = "Max Errors";
58
59 private int minRequests;
60 private int maxValue;
61 private int maxErrors;
62 private XFormDialog dialog;
63 public static final String STEP_MAXIMUM_TYPE = "Step Maximum";
64 private final static Logger log = Logger.getLogger( TestStepMaxAssertion.class );
65
66 public TestStepMaxAssertion( LoadTestAssertionConfig assertionConfig, WsdlLoadTest loadTest )
67 {
68 super( assertionConfig, loadTest );
69
70 init( assertionConfig );
71 initIcon( "/max_loadtest_assertion.gif" );
72 }
73
74 private void init( LoadTestAssertionConfig assertionConfig )
75 {
76 XmlObject configuration = assertionConfig.getConfiguration();
77
78 XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( configuration );
79 setName( reader.readString( TestStepMaxAssertion.NAME_ELEMENT, "Step Maximum" ) );
80 minRequests = reader.readInt( TestStepMaxAssertion.MIN_REQUESTS_ELEMENT, 100 );
81 maxValue = reader.readInt( TestStepMaxAssertion.MAX_VALUE_ELEMENT, 1000 );
82 setTargetStep( reader.readString( TestStepMaxAssertion.TEST_STEP_ELEMENT, TestStepMaxAssertion.ANY_TEST_STEP ) );
83 maxErrors = reader.readInt( MAX_ERRORS_ELEMENT, -1 );
84 }
85
86 public String assertResult( LoadTestRunner loadTestRunner, LoadTestRunContext context, TestStepResult result,
87 TestCaseRunner testRunner, TestCaseRunContext runContext )
88 {
89 TestStep step = result.getTestStep();
90 if( targetStepMatches( step ) )
91 {
92 WsdlLoadTest loadTest = ( WsdlLoadTest )loadTestRunner.getLoadTest();
93 LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();
94
95 int index = step.getTestCase().getIndexOfTestStep( step );
96
97 long maximum = result.getTimeTaken();
98 if( statisticsModel.getStatistic( index, Statistic.COUNT ) > minRequests && maximum >= maxValue )
99 {
100 return returnErrorOrFail( "Time [" + maximum + "] exceeds limit [" + maxValue + "]", maxErrors,
101 loadTestRunner, context );
102 }
103 }
104
105 return null;
106 }
107
108 public String assertResults( LoadTestRunner loadTestRunner, LoadTestRunContext context, TestCaseRunner testRunner,
109 TestCaseRunContext runContext )
110 {
111 if( ALL_TEST_STEPS.equals( getTargetStep() ) )
112 {
113 WsdlLoadTest loadTest = ( WsdlLoadTest )loadTestRunner.getLoadTest();
114 LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();
115
116 long sum = 0;
117 List<TestStepResult> results = testRunner.getResults();
118 for( int c = 0; c < results.size(); c++ )
119 {
120 TestStepResult result = results.get( c );
121 if( result == null )
122 {
123 log.warn( "Result [" + c + "] is null in TestCase [" + testRunner.getTestCase().getName() + "]" );
124 continue;
125 }
126
127 sum += result.getTimeTaken();
128 }
129
130 if( statisticsModel.getStatistic( LoadTestStatistics.TOTAL, Statistic.COUNT ) >= minRequests
131 && sum >= maxValue )
132 {
133 return returnErrorOrFail( "Time [" + sum + "] exceeds limit [" + maxValue + "]", maxErrors, loadTestRunner,
134 context );
135 }
136 }
137
138 return null;
139 }
140
141 public String getDescription()
142 {
143 return "testStep: " + getTargetStep() + ", minRequests: " + minRequests + ", maxValue: " + maxValue
144 + ", maxErrors: " + maxErrors;
145 }
146
147 public boolean configure()
148 {
149 if( dialog == null )
150 {
151 buildDialog();
152 }
153
154 StringToStringMap values = new StringToStringMap();
155
156 values.put( TestStepMaxAssertion.NAME_FIELD, getName() );
157 values.put( TestStepMaxAssertion.MINIMUM_REQUESTS_FIELD, String.valueOf( minRequests ) );
158 values.put( TestStepMaxAssertion.MAX_VALUE_FIELD, String.valueOf( maxValue ) );
159 values.put( TestStepMaxAssertion.TEST_STEP_FIELD, getTargetStep() );
160 values.put( TestStepMaxAssertion.MAX_ERRORS_FIELD, String.valueOf( maxErrors ) );
161
162 dialog.setOptions( TestStepMaxAssertion.TEST_STEP_FIELD, getTargetStepOptions( true ) );
163 values = dialog.show( values );
164
165 if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
166 {
167 try
168 {
169 minRequests = Integer.parseInt( values.get( TestStepMaxAssertion.MINIMUM_REQUESTS_FIELD ) );
170 maxValue = Integer.parseInt( values.get( TestStepMaxAssertion.MAX_VALUE_FIELD ) );
171 maxErrors = Integer.parseInt( values.get( TestStepMaxAssertion.MAX_ERRORS_FIELD ) );
172 setTargetStep( values.get( TestStepMaxAssertion.TEST_STEP_FIELD ) );
173 setName( values.get( TestStepMaxAssertion.NAME_FIELD ) );
174 }
175 catch( Exception e )
176 {
177 UISupport.showErrorMessage( e.getMessage() );
178 }
179
180 updateConfiguration();
181
182 return true;
183 }
184
185 return false;
186 }
187
188 protected void updateConfiguration()
189 {
190 XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
191
192 builder.add( TestStepMaxAssertion.NAME_ELEMENT, getName() );
193 builder.add( TestStepMaxAssertion.MIN_REQUESTS_ELEMENT, minRequests );
194 builder.add( TestStepMaxAssertion.MAX_VALUE_ELEMENT, maxValue );
195 builder.add( TestStepMaxAssertion.TEST_STEP_ELEMENT, getTargetStep() );
196 builder.add( TestStepMaxAssertion.MAX_ERRORS_ELEMENT, maxErrors );
197
198 setConfiguration( builder.finish() );
199 }
200
201 private void buildDialog()
202 {
203 XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "TestStep Max Assertion" );
204 XForm form = builder.createForm( "Basic" );
205
206 form.addTextField( TestStepMaxAssertion.NAME_FIELD, "Name of this assertion", FieldType.TEXT );
207 form.addTextField( TestStepMaxAssertion.MINIMUM_REQUESTS_FIELD, "Minimum steps before asserting", FieldType.TEXT );
208 form.addTextField( TestStepMaxAssertion.MAX_VALUE_FIELD, "Maximum allowed step time", FieldType.TEXT );
209 form.addTextField( TestStepMaxAssertion.MAX_ERRORS_FIELD, "Maximum number of errors before failing",
210 FieldType.TEXT );
211 form.addComboBox( TestStepMaxAssertion.TEST_STEP_FIELD, new String[0], "TestStep to assert" );
212
213 dialog = builder.buildDialog( builder
214 .buildOkCancelHelpActions( HelpUrls.STEP_MAXIMUM_LOAD_TEST_ASSERTION_HELP_URL ),
215 "Specify options for this TestStep Max Assertion", UISupport.OPTIONS_ICON );
216 }
217 }