View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.TestRunContext;
26  import com.eviware.soapui.model.testsuite.TestRunner;
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, TestRunner testRunner, TestRunContext runContext)
83  	{
84  		TestStep step = result.getTestStep();
85  		if( targetStepMatches( step ))
86  		{
87  			WsdlLoadTest loadTest = (WsdlLoadTest) loadTestRunner.getLoadTest();
88  			LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();
89  
90  			int index = step.getTestCase().getIndexOfTestStep( step );
91  			
92  			long tps = statisticsModel.getStatistic( index, Statistic.TPS );
93  			if( statisticsModel.getStatistic( index, Statistic.COUNT ) >= minRequests &&
94  				 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, TestRunner testRunner, TestRunContext runContext)
105 	{
106 		if( ALL_TEST_STEPS.equals( getTargetStep()) )
107 		{
108 			WsdlLoadTest loadTest = (WsdlLoadTest) loadTestRunner.getLoadTest();
109 			LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();
110 
111 			long tps = statisticsModel.getStatistic( LoadTestStatistics.TOTAL, Statistic.TPS );
112 			
113 			if( statisticsModel.getStatistic( LoadTestStatistics.TOTAL, Statistic.COUNT ) > minRequests &&
114 				 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 + ", maxErrors: " + maxErrors;
127 	}
128 
129 	public boolean configure()
130 	{
131 		if( dialog == null )
132 		{
133 			buildDialog();
134 		}
135 		
136 		StringToStringMap values = new StringToStringMap();
137 		
138 		values.put( TestStepTpsAssertion.NAME_FIELD, getName() );
139 		values.put( TestStepTpsAssertion.MINIMUM_REQUESTS_FIELD, String.valueOf( minRequests ));
140 		values.put( TestStepTpsAssertion.MIN_VALUE_FIELD, String.valueOf( minValue ));
141 		values.put( TestStepTpsAssertion.TEST_STEP_FIELD, getTargetStep() );
142 		values.put( TestStepTpsAssertion.MAX_ERRORS_FIELD, String.valueOf( maxErrors ));
143 		
144 		dialog.setOptions( TestStepTpsAssertion.TEST_STEP_FIELD, getTargetStepOptions( true ) );
145 		values = dialog.show( values );
146 		
147 		if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
148 		{
149 			try
150 			{
151 				minRequests = Integer.parseInt( values.get( TestStepTpsAssertion.MINIMUM_REQUESTS_FIELD ));
152 				minValue = Integer.parseInt( values.get( TestStepTpsAssertion.MIN_VALUE_FIELD ));
153 				maxErrors = Integer.parseInt( values.get( TestStepTpsAssertion.MAX_ERRORS_FIELD ));
154 				setTargetStep( values.get( TestStepTpsAssertion.TEST_STEP_FIELD ));
155 				setName( values.get( TestStepTpsAssertion.NAME_FIELD ));
156 			}
157 			catch( Exception e )
158 			{
159 				UISupport.showErrorMessage( e.getMessage() );
160 			}
161 			
162 			updateConfiguration();
163 			
164 			return true;
165 		}
166 		
167 		return false;
168 	}
169 
170 	protected void updateConfiguration()
171 	{
172 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
173 		
174 		builder.add( TestStepTpsAssertion.NAME_ELEMENT, getName() );
175 		builder.add( TestStepTpsAssertion.MIN_REQUESTS_ELEMENT, minRequests );
176 		builder.add( TestStepTpsAssertion.MIN_VALUE_ELEMENT, minValue );
177 		builder.add( TestStepTpsAssertion.TEST_STEP_ELEMENT, getTargetStep() );
178 		builder.add( TestStepTpsAssertion.MAX_ERRORS_ELEMENT, maxErrors );
179 		
180 		setConfiguration( builder.finish() );
181 	}
182 
183 	private void buildDialog()
184 	{
185 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "TestStep TPS Assertion" );
186 	   XForm form = builder.createForm( "Basic" );
187 		
188 	   form.addTextField( TestStepTpsAssertion.NAME_FIELD, "Name of this assertion", FieldType.TEXT );
189 	   form.addTextField( TestStepTpsAssertion.MINIMUM_REQUESTS_FIELD, "Minimum steps before asserting", FieldType.TEXT );
190 	   form.addTextField( TestStepTpsAssertion.MIN_VALUE_FIELD, "Minimum required step TPS", FieldType.TEXT );
191 	   form.addTextField( TestStepTpsAssertion.MAX_ERRORS_FIELD, "Maximum number of errors before failing", FieldType.TEXT );
192 	   form.addComboBox( TestStepTpsAssertion.TEST_STEP_FIELD, new String[0], "TestStep to assert" );
193 	   
194 	   dialog = builder.buildDialog( builder.buildOkCancelHelpActions( HelpUrls.STEP_TPS_LOAD_TEST_ASSERTION_HELP_URL), 
195 			"Specify options for this TestStep TPS Assertion", UISupport.OPTIONS_ICON );
196 	}
197 }