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