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  package com.eviware.soapui.integration.loadui;
13  
14  import java.io.File;
15  import java.util.ArrayList;
16  import java.util.HashMap;
17  import java.util.List;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.config.LoadTestLimitTypesConfig;
21  import com.eviware.soapui.impl.wsdl.loadtest.LoadTestAssertion;
22  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
23  import com.eviware.soapui.impl.wsdl.loadtest.assertions.TestStepAverageAssertion;
24  import com.eviware.soapui.impl.wsdl.loadtest.assertions.TestStepMaxAssertion;
25  import com.eviware.soapui.impl.wsdl.loadtest.assertions.TestStepTpsAssertion;
26  import com.eviware.soapui.impl.wsdl.loadtest.strategy.BurstLoadStrategy;
27  import com.eviware.soapui.impl.wsdl.loadtest.strategy.LoadStrategy;
28  import com.eviware.soapui.impl.wsdl.loadtest.strategy.SimpleLoadStrategy;
29  import com.eviware.soapui.impl.wsdl.loadtest.strategy.ThreadCountChangeLoadStrategy;
30  import com.eviware.soapui.impl.wsdl.loadtest.strategy.VarianceLoadStrategy;
31  
32  public class ContextMapping
33  {
34  	private static final String NOT_SELECTED = "-";
35  	private static final String TEST_CASE = "testCase";
36  	private static final String TEST_SUITE = "testSuite";
37  	private static final String PROJECT_FILE = "projectFile";
38  	public static final String MOCK_SERVICE = "mockService";
39  	public static final String PATH = "path";
40  	public static final String PORT = "port";
41  
42  	private static final String SOAPUI_RUNNER_LABEL = "soapuiSamplerLabel";
43  	public static final String LOADUI_TEST_CASE_NAME = "loaduiTestCaseName";
44  	public static final String LOADUI_PROJECT_NAME = "loaduiProjectName";
45  	private static final String SOAPUI_RUNNER_PROPERTIES = "SoapUISamplerComponent_properties";
46  	private static final String MOCKSERVICE_RUNNER_LABEL = "mockRunnerLabel";
47  	private static final String MOCKSERVICE_RUNNER_PROPERTIES = "MockServiceComponent_properties";
48  
49  	private static final String ASSERTION_LABEL = "assertionLabel";
50  	private static final String ASSERTION_TYPE = "assertionType";
51  	private static final String ASSERTION_PROPERTIES = "assertionProperties";
52  	private static final String ASSERTION = "Assertion";
53  	private static final String ASSERTION_CREATE_NEW = "assertionCreateNew";
54  
55  	private static final String STATISTICS_LABEL = "statisticsLabel";
56  	private static final String STATISTICS_TYPE = "statisticsType";
57  	private static final String STATISTICS_PROPERTIES = "statisticsProperties";
58  	private static final String STATISTICS = "Statistics";
59  	private static final String STATISTICS_CREATE_NEW = "statisticsCreateNew";
60  
61  	private static final String TRIGGER_LABEL = "triggerLabel";
62  	private static final String TRIGGER_TYPE = "triggerType";
63  	protected static final String TRIGGER_PROPERTIES = "triggerProperties";
64  	private static final String TRIGGER_CREATE_NEW = "triggerCreateNew";
65  
66  	private static final String DELAY_LABEL = "delayLabel";
67  	private static final String DELAY_TYPE = "delayType";
68  	private static final String DELAY_PROPERTIES = "delayProperties";
69  	private static final String DELAY = "Delay";
70  	private static final String DELAY_CREATE_NEW = "delayCreateNew";
71  
72  	protected static final String FIXED_LOAD_TRIGGER = "Fixed Load";
73  	private static final String VARIANCE_TRIGGER = "Variance";
74  	private static final String RAMP_TRIGGER = "Ramp";
75  
76  	private WsdlLoadTest loadTest;
77  	private String loadUIProject;
78  	private String loadUITestCase;
79  	private String loadUISoapUISampler;
80  	private String soapUIProjectPath;
81  	private String soapUITestSuite;
82  	private String soapUITestCase;
83  	protected String loadUITriggerType;
84  	private String soapUIMockService;
85  	private String mockServicePath;
86  	private String mockServicePort;
87  	private String loadUIMockServiceRunner;
88  
89  	HashMap<String, String> triggerProperties;
90  	HashMap<String, String> delayProperties;
91  	List<HashMap<String, String>> assertionPropertiesList;
92  	HashMap<String, String> statisticsProperties;
93  
94  	public ContextMapping( WsdlLoadTest loadTest, String loadUIProject, String loadUITestCase, String loadUISoapUISampler )
95  	{
96  		this.loadTest = loadTest;
97  		this.loadUIProject = loadUIProject;
98  		this.loadUITestCase = loadUITestCase;
99  		this.loadUISoapUISampler = loadUISoapUISampler;
100 	}
101 
102 	public ContextMapping( String soapUIProjectPath, String soapUITestSuite, String soapUITestCase,
103 			String loadUIProject, String loadUITestCase, String loadUISoapUISampler )
104 	{
105 		this.loadUIProject = loadUIProject;
106 		this.loadUITestCase = loadUITestCase;
107 		this.loadUISoapUISampler = loadUISoapUISampler;
108 		this.soapUIProjectPath = soapUIProjectPath;
109 		this.soapUITestSuite = soapUITestSuite;
110 		this.soapUITestCase = soapUITestCase;
111 
112 	}
113 
114 	public ContextMapping( String soapUIProjectPath, String soapUIMockService, String path, String port,
115 			String loadUIProject, String loadUITestCase, String loadUIMockServiceRunner )
116 	{
117 		this.loadUIProject = loadUIProject;
118 		this.loadUITestCase = loadUITestCase;
119 		this.loadUIMockServiceRunner = loadUIMockServiceRunner;
120 		this.soapUIProjectPath = soapUIProjectPath;
121 		this.soapUIMockService = soapUIMockService;
122 		this.mockServicePath = path;
123 		this.mockServicePort = port;
124 
125 	}
126 
127 	public static String createProperyValue( Class clazz, String value )
128 	{
129 		return clazz.getName() + "@" + value;
130 	}
131 
132 	public HashMap<String, Object> setCreateSoapUIRunnerContext( String generatorType, String analisysType )
133 	{
134 		HashMap<String, Object> context = new HashMap<String, Object>();
135 		HashMap<String, String> properties = new HashMap<String, String>();
136 
137 		properties.put( PROJECT_FILE, createProperyValue( File.class, soapUIProjectPath ) );
138 		properties.put( TEST_SUITE, createProperyValue( String.class, soapUITestSuite ) );
139 		properties.put( TEST_CASE, createProperyValue( String.class, soapUITestCase ) );
140 		context.put( LOADUI_PROJECT_NAME, loadUIProject );
141 		context.put( LOADUI_TEST_CASE_NAME, loadUITestCase );
142 		context.put( SOAPUI_RUNNER_LABEL, loadUISoapUISampler );
143 		context.put( SOAPUI_RUNNER_PROPERTIES, properties );
144 
145 		if( !NOT_SELECTED.equals( generatorType ) )
146 		{
147 			mapDefaultTriggerProperties( generatorType );
148 			context.put( TRIGGER_PROPERTIES, triggerProperties );
149 			context.put( TRIGGER_LABEL, loadUITriggerType );
150 			context.put( TRIGGER_TYPE, loadUITriggerType );
151 			context.put( TRIGGER_CREATE_NEW, new Boolean( true ) );
152 		}
153 		
154 		if( !NOT_SELECTED.equals( analisysType ) )
155 		{
156 			mapStatisticsProperties( null );
157 			context.put( STATISTICS_PROPERTIES, statisticsProperties );
158 			context.put( STATISTICS_LABEL, STATISTICS );
159 			context.put( STATISTICS_TYPE, STATISTICS );
160 			context.put( STATISTICS_CREATE_NEW, new Boolean( true ) );
161 		}
162 		return context;
163 	}
164 
165 	public HashMap<String, Object> setCreateMockServiceRunnerContext()
166 	{
167 		HashMap<String, Object> context = new HashMap<String, Object>();
168 		HashMap<String, String> properties = new HashMap<String, String>();
169 
170 		properties.put( PROJECT_FILE, createProperyValue( File.class, soapUIProjectPath ) );
171 		properties.put( MOCK_SERVICE, createProperyValue( String.class, soapUIMockService ) );
172 		properties.put( PATH, createProperyValue( String.class, mockServicePath ) );
173 		properties.put( PORT, createProperyValue( String.class, mockServicePort ) );
174 		context.put( LOADUI_PROJECT_NAME, loadUIProject );
175 		context.put( LOADUI_TEST_CASE_NAME, loadUITestCase );
176 		context.put( MOCKSERVICE_RUNNER_LABEL, loadUIMockServiceRunner );
177 		context.put( MOCKSERVICE_RUNNER_PROPERTIES, properties );
178 		return context;
179 	}
180 
181 	public HashMap<String, Object> setInitExportLoadTestToLoadUIContext()
182 	{
183 		HashMap<String, Object> context = new HashMap<String, Object>();
184 		HashMap<String, String> properties = new HashMap<String, String>();
185 
186 		properties.put( PROJECT_FILE, createProperyValue( File.class, loadTest.getTestCase().getTestSuite().getProject()
187 				.getPath() ) );
188 		properties.put( TEST_SUITE, createProperyValue( String.class, loadTest.getTestCase().getTestSuite().getName() ) );
189 		properties.put( TEST_CASE, createProperyValue( String.class, loadTest.getTestCase().getName() ) );
190 
191 		context.put( LOADUI_PROJECT_NAME, loadUIProject );
192 		context.put( LOADUI_TEST_CASE_NAME, loadUITestCase );
193 		context.put( SOAPUI_RUNNER_LABEL, loadUISoapUISampler );
194 
195 		context.put( SOAPUI_RUNNER_PROPERTIES, properties );
196 
197 		mapInitialTriggerProperties( loadTest );
198 		context.put( TRIGGER_PROPERTIES, triggerProperties );
199 		context.put( TRIGGER_LABEL, loadUITriggerType );
200 		context.put( TRIGGER_TYPE, loadUITriggerType );
201 		context.put( TRIGGER_CREATE_NEW, new Boolean( true ) );
202 
203 		if( delayProperties != null )
204 		{
205 			context.put( DELAY_PROPERTIES, delayProperties );
206 			context.put( DELAY_LABEL, DELAY );
207 			context.put( DELAY_TYPE, DELAY );
208 			context.put( DELAY_CREATE_NEW, new Boolean( true ) );
209 		}
210 		mapAssertionProperties( loadTest );
211 		for( int i = 0; i < assertionPropertiesList.size(); i++ )
212 		{
213 			context.put( ASSERTION_PROPERTIES + i, assertionPropertiesList.get( i ) );
214 			context.put( ASSERTION_LABEL + i, ASSERTION + i );
215 			context.put( ASSERTION_TYPE + i, ASSERTION );
216 			context.put( ASSERTION_CREATE_NEW + i, new Boolean( true ) );
217 
218 		}
219 		mapStatisticsProperties( loadTest );
220 		context.put( STATISTICS_PROPERTIES, statisticsProperties );
221 		context.put( STATISTICS_LABEL, STATISTICS );
222 		context.put( STATISTICS_TYPE, STATISTICS );
223 		context.put( STATISTICS_CREATE_NEW, new Boolean( true ) );
224 
225 		return context;
226 	}
227 
228 	protected void mapInitialTriggerProperties( WsdlLoadTest loadTest )
229 	{
230 		triggerProperties = new HashMap<String, String>();
231 		LoadStrategy loadStrategy = loadTest.getLoadStrategy();
232 		if( loadStrategy instanceof SimpleLoadStrategy )
233 		{
234 			SimpleLoadStrategy currentStrategy = ( SimpleLoadStrategy )loadStrategy;
235 			loadUITriggerType = FIXED_LOAD_TRIGGER;
236 			triggerProperties.put( "load", createProperyValue( Long.class, Long.toString( loadTest.getThreadCount() ) ) );
237 			long testDelay = currentStrategy.getTestDelay();
238 			if( testDelay > 0 )
239 			{
240 				delayProperties = new HashMap<String, String>();
241 				delayProperties.put( "delay", createProperyValue( Long.class, Long.toString( testDelay ) ) );
242 				int randomFactor = ( int )( currentStrategy.getRandomFactor() * 100 );
243 				delayProperties.put( "randomDelay", createProperyValue( Long.class, Integer.toString( randomFactor ) ) );
244 			}
245 			return;
246 		}
247 		if( loadStrategy instanceof VarianceLoadStrategy )
248 		{
249 			loadUITriggerType = VARIANCE_TRIGGER;
250 			VarianceLoadStrategy currentStrategy = ( VarianceLoadStrategy )loadStrategy;
251 			return;
252 		}
253 		if( loadStrategy instanceof ThreadCountChangeLoadStrategy )
254 		{
255 			loadUITriggerType = RAMP_TRIGGER;
256 			ThreadCountChangeLoadStrategy currentStrategy = ( ThreadCountChangeLoadStrategy )loadStrategy;
257 			return;
258 		}
259 		if( loadStrategy instanceof BurstLoadStrategy )
260 		{
261 			loadUITriggerType = FIXED_LOAD_TRIGGER;
262 			triggerProperties.put( "load", createProperyValue( Long.class, Long.toString( loadTest.getThreadCount() ) ) );
263 			return;
264 		}
265 	}
266 
267 	protected void mapDefaultTriggerProperties( String generatorType )
268 	{
269 		triggerProperties = new HashMap<String, String>();
270 		loadUITriggerType = generatorType;
271 	}
272 
273 	public HashMap<String, Object> setFinalExportLoadTestToLoadUIContext( HashMap<String, Object> createdItemContext )
274 	{
275 		mapExistingTriggerProperties( loadTest, createdItemContext );
276 		createdItemContext.put( TRIGGER_PROPERTIES, triggerProperties );
277 		return createdItemContext;
278 	}
279 
280 	// repopulates the properties with right values, if property is not set old
281 	// value it means leave the old value
282 	protected HashMap<String, String> mapExistingTriggerProperties( WsdlLoadTest loadTest,
283 			HashMap<String, Object> createdItemContext )
284 	{
285 		LoadStrategy loadStrategy = loadTest.getLoadStrategy();
286 		triggerProperties = ( HashMap<String, String> )createdItemContext.get( TRIGGER_PROPERTIES );
287 		if( loadStrategy instanceof VarianceLoadStrategy )
288 		{
289 			loadUITriggerType = VARIANCE_TRIGGER;
290 			VarianceLoadStrategy currentStrategy = ( VarianceLoadStrategy )loadStrategy;
291 			long rate = extractLongProperty( "rate" );
292 			triggerProperties.put( "shape", createProperyValue( String.class, "Sine-wave" ) );
293 			triggerProperties.put( "amplitude", createProperyValue( Long.class, Long.toString( ( ( long )( currentStrategy
294 					.getVariance() * rate ) ) ) ) );
295 			triggerProperties.put( "period", createProperyValue( Long.class, Long
296 					.toString( currentStrategy.getInterval() / 1000 ) ) );
297 		}
298 		if( loadStrategy instanceof ThreadCountChangeLoadStrategy )
299 		{
300 			loadUITriggerType = RAMP_TRIGGER;
301 			ThreadCountChangeLoadStrategy currentStrategy = ( ThreadCountChangeLoadStrategy )loadStrategy;
302 			long end = extractLongProperty( "end" );
303 			triggerProperties.put( "end", createProperyValue( Long.class, Long.toString( end
304 					* ( currentStrategy.getEndThreadCount() / currentStrategy.getStartThreadCount() ) ) ) );
305 			if( loadTest.getLimitType().equals( LoadTestLimitTypesConfig.TIME ) )
306 			{
307 				triggerProperties
308 						.put( "period", createProperyValue( Long.class, Long.toString( loadTest.getTestLimit() ) ) );
309 			}
310 		}
311 		return triggerProperties;
312 	}
313 
314 	public Long extractLongProperty( String propertyKey )
315 	{
316 		Long value = null;
317 		String[] parts = triggerProperties.get( propertyKey ).split( "@" );
318 		try
319 		{
320 			value = new Long( parts[1] );
321 		}
322 		catch( NumberFormatException e )
323 		{
324 			SoapUI.logError( e, "property " + propertyKey + "is not a Long" );
325 		}
326 		return value;
327 	}
328 
329 	private void mapAssertionProperties( WsdlLoadTest loadTest )
330 	{
331 		assertionPropertiesList = new ArrayList<HashMap<String, String>>();
332 		List<LoadTestAssertion> loadTestAssertions = loadTest.getAssertionList();
333 		for( LoadTestAssertion loadTestAssertion : loadTestAssertions )
334 		{
335 			if( loadTestAssertion.getTargetStep().equals( LoadTestAssertion.ALL_TEST_STEPS ) )
336 			{
337 				HashMap<String, String> asrtProperties = new HashMap<String, String>();
338 				if( loadTestAssertion instanceof TestStepTpsAssertion )
339 				{
340 					asrtProperties.put( "value", createProperyValue( String.class, "Tps" ) );
341 				}
342 				if( loadTestAssertion instanceof TestStepMaxAssertion )
343 				{
344 					asrtProperties.put( "value", createProperyValue( String.class, "Max" ) );
345 				}
346 				if( loadTestAssertion instanceof TestStepAverageAssertion )
347 				{
348 					asrtProperties.put( "value", createProperyValue( String.class, "Avg" ) );
349 				}
350 				assertionPropertiesList.add( asrtProperties );
351 			}
352 		}
353 
354 	}
355 
356 	private void mapStatisticsProperties( WsdlLoadTest loadTest )
357 	{
358 		statisticsProperties = new HashMap<String, String>();
359 	}
360 
361 	public boolean isFinalTriggerMappingNeeded()
362 	{
363 		if( loadUITriggerType.equals( VARIANCE_TRIGGER ) || loadUITriggerType.equals( RAMP_TRIGGER ) )
364 			return true;
365 		else
366 			return false;
367 	}
368 
369 }