View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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 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.TestRunContext;
29  import com.eviware.soapui.model.testsuite.TestRunner;
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, TestRunner testRunner, TestRunContext runContext)
87  	{
88  		TestStep step = result.getTestStep();
89  		if( targetStepMatches( step ) )
90  		{
91  			WsdlLoadTest loadTest = (WsdlLoadTest) loadTestRunner.getLoadTest();
92  			LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();
93  
94  			int index = step.getTestCase().getIndexOfTestStep( step );
95  			
96  			long maximum = result.getTimeTaken();
97  			if( statisticsModel.getStatistic( index, Statistic.COUNT ) > minRequests &&
98  				 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, TestRunner testRunner, TestRunContext runContext)
109 	{
110 		if( ALL_TEST_STEPS.equals( getTargetStep() ))
111 		{
112 			WsdlLoadTest loadTest = (WsdlLoadTest) loadTestRunner.getLoadTest();
113 			LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();
114 
115 			long sum = 0;
116 			List<TestStepResult> results = testRunner.getResults();
117 			for( int c = 0; c < results.size(); c++ )
118 			{
119 				TestStepResult result = results.get( c );
120 				if( result == null )
121 				{
122 					log.warn( "Result [" + c + "] is null in TestCase [" + testRunner.getTestCase().getName() + "]" );
123 					continue;
124 				}
125 				
126 				sum += result.getTimeTaken();
127 			}
128 			
129 			if( statisticsModel.getStatistic( LoadTestStatistics.TOTAL, Statistic.COUNT ) >= minRequests &&
130 				 sum >= maxValue )
131 			{
132 				return returnErrorOrFail( "Time [" + sum + "] exceeds limit [" + maxValue + "]", maxErrors, 
133 						loadTestRunner, context );
134 			}
135 		}
136 
137 		return null;
138 	}
139 	
140 	public String getDescription()
141 	{
142 		return "testStep: " + getTargetStep() + ", minRequests: " + minRequests + ", maxValue: " + maxValue + ", maxErrors: " + maxErrors;
143 	}
144 
145 	public boolean configure()
146 	{
147 		if( dialog == null )
148 		{
149 			buildDialog();
150 		}
151 		
152 		StringToStringMap values = new StringToStringMap();
153 		
154 		values.put( TestStepMaxAssertion.NAME_FIELD, getName() );
155 		values.put( TestStepMaxAssertion.MINIMUM_REQUESTS_FIELD, String.valueOf( minRequests ));
156 		values.put( TestStepMaxAssertion.MAX_VALUE_FIELD, String.valueOf( maxValue ));
157 		values.put( TestStepMaxAssertion.TEST_STEP_FIELD, getTargetStep() );
158 		values.put( TestStepMaxAssertion.MAX_ERRORS_FIELD, String.valueOf( maxErrors ));
159 		
160 		dialog.setOptions( TestStepMaxAssertion.TEST_STEP_FIELD, getTargetStepOptions( true ) );
161 		values = dialog.show( values );
162 		
163 		if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
164 		{
165 			try
166 			{
167 				minRequests = Integer.parseInt( values.get( TestStepMaxAssertion.MINIMUM_REQUESTS_FIELD ));
168 				maxValue = Integer.parseInt( values.get( TestStepMaxAssertion.MAX_VALUE_FIELD ));
169 				maxErrors = Integer.parseInt( values.get( TestStepMaxAssertion.MAX_ERRORS_FIELD ));
170 				setTargetStep( values.get( TestStepMaxAssertion.TEST_STEP_FIELD ));
171 				setName( values.get( TestStepMaxAssertion.NAME_FIELD ));
172 			}
173 			catch( Exception e )
174 			{
175 				UISupport.showErrorMessage( e.getMessage() );
176 			}
177 			
178 			updateConfiguration();
179 			
180 			return true;
181 		}
182 		
183 		return false;
184 	}
185 
186 	protected void updateConfiguration()
187 	{
188 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
189 		
190 		builder.add( TestStepMaxAssertion.NAME_ELEMENT, getName() );
191 		builder.add( TestStepMaxAssertion.MIN_REQUESTS_ELEMENT, minRequests );
192 		builder.add( TestStepMaxAssertion.MAX_VALUE_ELEMENT, maxValue );
193 		builder.add( TestStepMaxAssertion.TEST_STEP_ELEMENT, getTargetStep() );
194 		builder.add( TestStepMaxAssertion.MAX_ERRORS_ELEMENT, maxErrors );
195 		
196 		setConfiguration( builder.finish() );
197 	}
198 
199 	private void buildDialog()
200 	{
201 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "TestStep Max Assertion" );
202 	   XForm form = builder.createForm( "Basic" );
203 		
204 	   form.addTextField( TestStepMaxAssertion.NAME_FIELD, "Name of this assertion", FieldType.TEXT );
205 	   form.addTextField( TestStepMaxAssertion.MINIMUM_REQUESTS_FIELD, "Minimum steps before asserting", FieldType.TEXT );
206 	   form.addTextField( TestStepMaxAssertion.MAX_VALUE_FIELD, "Maximum allowed step time", FieldType.TEXT );
207 	   form.addTextField( TestStepMaxAssertion.MAX_ERRORS_FIELD, "Maximum number of errors before failing", FieldType.TEXT );
208 	   form.addComboBox( TestStepMaxAssertion.TEST_STEP_FIELD, new String[0], "TestStep to assert" );
209 	   
210 	   dialog = builder.buildDialog( builder.buildOkCancelHelpActions( HelpUrls.STEP_MAXIMUM_LOAD_TEST_ASSERTION_HELP_URL), 
211 			"Specify options for this TestStep Max Assertion", UISupport.OPTIONS_ICON );
212 	}
213 }