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.io.IOException;
16  import java.util.ArrayList;
17  import java.util.HashMap;
18  import java.util.List;
19  
20  import com.eviware.soapui.SoapUI;
21  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
22  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
23  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
24  import com.eviware.soapui.integration.impl.CajoClient;
25  
26  public class IntegrationUtils
27  {
28  
29  	private static final String NOT_SELECTED = "-";
30  	public static final String CREATE_NEW_OPTION = "<Create New>";
31  	public static final String CREATE_ON_PROJECT_LEVEL = "<Project Level>";
32  
33  	public static final int ADD_TO_PROJECT_LEVEL = 0;
34  	public static final int ADD_TO_SINGLE_TESTCASE = 1;
35  	public static final int ADD_TO_SEPARATE_TESTCASES = 2;
36  
37  	public static final String LOADU_INFO_DIALOG_TITLE = "Target loadUI items info";
38  
39  	
40  	public static String getIntegrationPort(String appName, String whichAppPort, String defaultPort )
41  	{
42  		String cajoPort = SoapUI.getSettings().getString( whichAppPort, defaultPort );
43  		try
44  		{
45  			Integer.parseInt( cajoPort );
46  		}
47  		catch( NumberFormatException nfe )
48  		{
49  			cajoPort = defaultPort;
50  			SoapUI.getSettings().setString( whichAppPort, cajoPort );
51  			SoapUI.log( appName+" integration port was reset to default value "+defaultPort+", because its value was not correct!" );
52  		}
53  		return cajoPort;
54  	}
55  	
56  	
57  	
58  	
59  	public static List<String> getProjectsNames()
60  	{
61  		List<String> testCaseNames = new ArrayList<String>();
62  		try
63  		{
64  			testCaseNames = ( List<String> )CajoClient.getInstance().invoke( "getProjetcs", null );
65  		}
66  		catch( Exception e )
67  		{
68  			SoapUI.log.error( "Error while invoking cajo server in loadui ", e );
69  		}
70  
71  		return testCaseNames;
72  	}
73  
74  	public static List<String> getTestCasesNames()
75  	{
76  		List<String> testCaseNames = new ArrayList<String>();
77  		try
78  		{
79  			testCaseNames = ( List<String> )CajoClient.getInstance().invoke( "getTestCases", null );
80  		}
81  		catch( Exception e )
82  		{
83  			SoapUI.log.error( "Error while invoking cajo server in loadui ", e );
84  		}
85  
86  		return testCaseNames;
87  	}
88  
89  	public static List<String> getSoapUISamplersNames( String projectName, String testCaseName )
90  	{
91  		List<String> soapUISamplersNames = new ArrayList<String>();
92  		try
93  		{
94  			soapUISamplersNames = ( List<String> )CajoClient.getInstance().invoke( "getSoapUIRunners",
95  					new String[] { projectName, testCaseName } );
96  		}
97  		catch( Exception e )
98  		{
99  			SoapUI.log.error( "Error while invoking cajo server in loadui ", e );
100 		}
101 
102 		return soapUISamplersNames;
103 	}
104 
105 	public static List<String> getMockServiceRunnersNames( String projectName, String testCaseName )
106 	{
107 		List<String> mockServiceRunnersNames = new ArrayList<String>();
108 		try
109 		{
110 			mockServiceRunnersNames = ( List<String> )CajoClient.getInstance().invoke( "getMockServiceRunners",
111 					new String[] { projectName, testCaseName } );
112 		}
113 		catch( Exception e )
114 		{
115 			SoapUI.log.error( "Error while invoking cajo server in loadui ", e );
116 		}
117 
118 		return mockServiceRunnersNames;
119 	}
120 
121 	public static boolean isProjectOpened( String projectName )
122 	{
123 		boolean isOpened = false;
124 		try
125 		{
126 			isOpened = ( ( Boolean )CajoClient.getInstance().invoke( "isProjectOpened", new String[] { projectName } ) )
127 					.booleanValue();
128 		}
129 		catch( Exception e )
130 		{
131 			SoapUI.log.error( "Error while invoking cajo server in loadui ", e );
132 		}
133 		return isOpened;
134 	}
135 
136 	public static String getOpenedProjectName()
137 	{
138 		String projectName = "";
139 		try
140 		{
141 			projectName = ( ( String )CajoClient.getInstance().invoke( "getOpenedProjectName", null ) );
142 		}
143 		catch( Exception e )
144 		{
145 			SoapUI.log.error( "Error while invoking cajo server in loadui ", e );
146 		}
147 		return projectName;
148 	}
149 
150 	public static void bringLoadUIToFront()
151 	{
152 		try
153 		{
154 			CajoClient.getInstance().invoke( "bringToFront", null );
155 		}
156 		catch( Exception e )
157 		{
158 			SoapUI.log.error( "Error while invoking cajo server in loadui ", e );
159 		}
160 	}
161 
162 	public static void removeLoadUILoadedProject( File projectFile )
163 	{
164 		try
165 		{
166 			CajoClient.getInstance().invoke( "removeLoadedSoapUIProject", projectFile );
167 		}
168 		catch( Exception e )
169 		{
170 			SoapUI.log.error( "Error while invoking cajo server in loadui ", e );
171 		}
172 	}
173 
174 	public static HashMap<String, String> createSoapUIRunner( String soapUIProjectPath, String soapUITestSuite,
175 			String soapUITestCase, String loadUIProject, String loadUITestCase, String loadUISoapUISampler,
176 			String generatorType, String analisysType ) throws IOException
177 	{
178 		HashMap<String, String> samplerSettings = new HashMap<String, String>();
179 		try
180 		{
181 			SoapUI.log( "createSoapUISampler for soapUIProjectPath=\"" + soapUIProjectPath + "\", soapUITestSuite=\""
182 					+ soapUITestSuite + "\", soapUITestCase=\"" + soapUITestCase + "\", loadUIProject=\"" + loadUIProject
183 					+ "\", loadUITestCase=\"" + loadUITestCase + ", \"loadUISoapUISampler=\"" + loadUISoapUISampler + "\"" );
184 
185 			HashMap<String, Object> context = new ContextMapping( soapUIProjectPath, soapUITestSuite, soapUITestCase,
186 					loadUIProject, loadUITestCase, loadUISoapUISampler ).setCreateSoapUIRunnerContext( generatorType,
187 					analisysType );
188 			samplerSettings = ( HashMap<String, String> )CajoClient.getInstance().invoke( "createSoapUIRunner", context );
189 			bringLoadUIToFront();
190 		}
191 		catch( IOException e )
192 		{
193 			throw e;
194 		}
195 		catch( Exception e )
196 		{
197 			SoapUI.log.error( "Error while invoking cajo server in loadui ", e );
198 		}
199 		return samplerSettings;
200 	}
201 
202 	public static HashMap<String, String> createMockServiceRunner( String soapUIProjectPath, String soapUIMockService,
203 			String path, String port, String loadUIProject, String loadUITestCase, String mockServiceRunner )
204 			throws IOException
205 	{
206 		HashMap<String, String> mockServiceSettings = new HashMap<String, String>();
207 		try
208 		{
209 			SoapUI.log( "createMockRunner for soapUIProjectPath=\"" + soapUIProjectPath + "\", soapUIMockService=\""
210 					+ soapUIMockService + "\", path=\"" + path + "\", port=\"" + port + "\", loadUIProject=\""
211 					+ loadUIProject + "\", loadUITestCase=\"" + loadUITestCase + ", \"loadUIMockRunner=\""
212 					+ mockServiceRunner + "\"" );
213 
214 			HashMap<String, Object> context = new ContextMapping( soapUIProjectPath, soapUIMockService, "", "",
215 					loadUIProject, loadUITestCase, mockServiceRunner ).setCreateMockServiceRunnerContext();
216 			mockServiceSettings = ( HashMap<String, String> )CajoClient.getInstance().invoke( "createMockServiceRunner",
217 					context );
218 			bringLoadUIToFront();
219 		}
220 		catch( IOException e )
221 		{
222 			throw e;
223 		}
224 		catch( Exception e )
225 		{
226 			SoapUI.log.error( "Error while invoking cajo server in loadui ", e );
227 		}
228 		return mockServiceSettings;
229 	}
230 
231 	public static HashMap<String, Object> exportLoadTestToLoadUI( WsdlLoadTest loadTest, String loadUIProject,
232 			String loadUITestCase, String loadUISoapUISampler ) throws IOException
233 	{
234 		HashMap<String, Object> contextSettings = new HashMap<String, Object>();
235 		try
236 		{
237 			ContextMapping contextMapping = new ContextMapping( loadTest, loadUIProject, loadUITestCase,
238 					loadUISoapUISampler );
239 			HashMap<String, Object> context = contextMapping.setInitExportLoadTestToLoadUIContext();
240 			contextSettings = ( HashMap<String, Object> )CajoClient.getInstance().invoke( "exportSoapUILoadTestToLoadUI",
241 					context );
242 			if( contextMapping.isFinalTriggerMappingNeeded() )
243 			{
244 				// export once more to set final values whose setting use
245 				// previously
246 				// set default values
247 				CajoClient.getInstance().invoke( "exportSoapUILoadTestToLoadUI",
248 						contextMapping.setFinalExportLoadTestToLoadUIContext( contextSettings ) );
249 			}
250 			bringLoadUIToFront();
251 		}
252 		catch( IOException e )
253 		{
254 			throw e;
255 		}
256 		catch( Exception e )
257 		{
258 			SoapUI.log.error( "Error while invoking cajo server in loadui ", e );
259 		}
260 		return contextSettings;
261 	}
262 
263 	public static HashMap<String, String> exportMultipleLoadTestToLoadUI( WsdlTestCase testCase, String[] loadTests,
264 			String loadUIProject ) throws IOException
265 	{
266 		HashMap<String, String> samplerSettings = new HashMap<String, String>();
267 		try
268 		{
269 			String loadTestName = loadTests[0];
270 			WsdlLoadTest loadTest = ( WsdlLoadTest )testCase.getLoadTestByName( loadTestName );
271 			HashMap<String, Object> firstSamplerSettings = exportLoadTestToLoadUI( loadTest, loadUIProject,
272 					CREATE_NEW_OPTION, CREATE_NEW_OPTION );
273 			// String loadUITestCaseAddedTo = ( String )firstSamplerSettings.get(
274 			// ContextMapping.LOADUI_TEST_CASE_NAME );
275 			String loadUIProjectAddedTo = ( String )firstSamplerSettings.get( ContextMapping.LOADUI_PROJECT_NAME );
276 			for( int i = 1; i < loadTests.length; i++ )
277 			{
278 				loadTestName = loadTests[i];
279 				loadTest = ( WsdlLoadTest )testCase.getLoadTestByName( loadTestName );
280 				exportLoadTestToLoadUI( loadTest, loadUIProjectAddedTo, CREATE_NEW_OPTION, CREATE_NEW_OPTION );
281 			}
282 			bringLoadUIToFront();
283 		}
284 		catch( IOException e )
285 		{
286 			throw e;
287 		}
288 		catch( Exception e )
289 		{
290 			SoapUI.log.error( "Error while invoking cajo server in loadui ", e );
291 		}
292 		return samplerSettings;
293 	}
294 
295 	public static HashMap<String, String> exportMultipleLoadTestToLoadUI( WsdlTestSuite testSuite, String[] loadTests,
296 			String loadUIProject ) throws IOException
297 	{
298 		HashMap<String, String> samplerSettings = new HashMap<String, String>();
299 		try
300 		{
301 			String compositeLoadTestName = loadTests[0];
302 			String[] names = compositeLoadTestName.split( " - " );
303 			String testCaseName = names[0];
304 			String loadTestName = names[1];
305 			WsdlTestCase testCase = testSuite.getTestCaseByName( testCaseName );
306 			HashMap<String, Object> firstSamplerSettings = new HashMap<String, Object>();
307 			// String loadUITestCaseAddedTo = "";
308 			String loadUIProjectAddedTo = "";
309 			if( testCase != null )
310 			{
311 				WsdlLoadTest loadTest = ( WsdlLoadTest )testCase.getLoadTestByName( loadTestName );
312 				if( loadTest != null )
313 				{
314 					firstSamplerSettings = exportLoadTestToLoadUI( loadTest, loadUIProject, CREATE_NEW_OPTION,
315 							CREATE_NEW_OPTION );
316 					// loadUITestCaseAddedTo = ( String )firstSamplerSettings.get(
317 					// ContextMapping.LOADUI_TEST_CASE_NAME );
318 					loadUIProjectAddedTo = ( String )firstSamplerSettings.get( ContextMapping.LOADUI_PROJECT_NAME );
319 				}
320 
321 			}
322 
323 			for( int i = 1; i < loadTests.length; i++ )
324 			{
325 				compositeLoadTestName = loadTests[i];
326 				loadTestName = loadTests[i];
327 				WsdlLoadTest loadTest = ( WsdlLoadTest )testCase.getLoadTestByName( loadTestName );
328 				testCaseName = names[0];
329 				loadTestName = names[1];
330 				testCase = testSuite.getTestCaseByName( testCaseName );
331 				if( testCase != null )
332 				{
333 					loadTest = ( WsdlLoadTest )testCase.getLoadTestByName( loadTestName );
334 					if( loadTest != null )
335 					{
336 						exportLoadTestToLoadUI( loadTest, loadUIProjectAddedTo, CREATE_NEW_OPTION, CREATE_NEW_OPTION );
337 					}
338 
339 				}
340 			}
341 			bringLoadUIToFront();
342 		}
343 		catch( IOException e )
344 		{
345 			throw e;
346 		}
347 		catch( Exception e )
348 		{
349 			SoapUI.log.error( "Error while invoking cajo server in loadui ", e );
350 		}
351 		return samplerSettings;
352 	}
353 
354 	public static void generateTestSuiteLoadTests( String soapUIProjectPath, String soapUITestSuite,
355 			String[] soapUITestCases, String loadUIProject, int levelToAdd ) throws IOException
356 	{
357 
358 		String firstTestCase = soapUITestCases[0];
359 		HashMap<String, String> firstSamplerSettings = new HashMap<String, String>();
360 		String loadUITestCaseAddedTo = "";
361 		String loadUIProjectAddedTo = "";
362 		switch( levelToAdd )
363 		{
364 		case ADD_TO_PROJECT_LEVEL :
365 			firstSamplerSettings = createSoapUIRunner( soapUIProjectPath, soapUITestSuite, firstTestCase, loadUIProject,
366 					null, CREATE_NEW_OPTION, NOT_SELECTED, NOT_SELECTED );
367 			loadUIProjectAddedTo = firstSamplerSettings.get( ContextMapping.LOADUI_PROJECT_NAME );
368 			for( int i = 1; i < soapUITestCases.length; i++ )
369 			{
370 				String testCase = soapUITestCases[i];
371 				createSoapUIRunner( soapUIProjectPath, soapUITestSuite, testCase, loadUIProjectAddedTo, null,
372 						CREATE_NEW_OPTION, NOT_SELECTED, NOT_SELECTED );
373 			}
374 			break;
375 
376 		case ADD_TO_SINGLE_TESTCASE :
377 			firstSamplerSettings = createSoapUIRunner( soapUIProjectPath, soapUITestSuite, firstTestCase, loadUIProject,
378 					CREATE_NEW_OPTION, CREATE_NEW_OPTION, NOT_SELECTED, NOT_SELECTED );
379 			loadUITestCaseAddedTo = firstSamplerSettings.get( ContextMapping.LOADUI_TEST_CASE_NAME );
380 			loadUIProjectAddedTo = firstSamplerSettings.get( ContextMapping.LOADUI_PROJECT_NAME );
381 			for( int i = 1; i < soapUITestCases.length; i++ )
382 			{
383 				String testCase = soapUITestCases[i];
384 				createSoapUIRunner( soapUIProjectPath, soapUITestSuite, testCase, loadUIProjectAddedTo,
385 						loadUITestCaseAddedTo, CREATE_NEW_OPTION, NOT_SELECTED, NOT_SELECTED );
386 
387 			}
388 			break;
389 		case ADD_TO_SEPARATE_TESTCASES :
390 			firstSamplerSettings = createSoapUIRunner( soapUIProjectPath, soapUITestSuite, firstTestCase, loadUIProject,
391 					CREATE_NEW_OPTION, CREATE_NEW_OPTION, NOT_SELECTED, NOT_SELECTED );
392 			loadUIProjectAddedTo = firstSamplerSettings.get( ContextMapping.LOADUI_PROJECT_NAME );
393 			for( int i = 1; i < soapUITestCases.length; i++ )
394 			{
395 				String testCase = soapUITestCases[i];
396 				createSoapUIRunner( soapUIProjectPath, soapUITestSuite, testCase, loadUIProjectAddedTo, CREATE_NEW_OPTION,
397 						CREATE_NEW_OPTION, NOT_SELECTED, NOT_SELECTED );
398 			}
399 			break;
400 		}
401 	}
402 
403 	public static void closeOpenedLoadUIProject()
404 	{
405 		try
406 		{
407 			CajoClient.getInstance().invoke( "closeOpenedProject", null );
408 		}
409 		catch( Exception e )
410 		{
411 			SoapUI.log.error( "Error while invoking cajo server in loadui ", e );
412 		}
413 	}
414 
415 	public static String[] getAvailableProjects()
416 	{
417 		List<String> availableProjects = getProjectsNames();
418 		availableProjects.add( CREATE_NEW_OPTION );
419 		String[] names = new String[availableProjects.size()];
420 		for( int c = 0; c < availableProjects.size(); c++ )
421 		{
422 			names[c] = availableProjects.get( c );
423 		}
424 
425 		return names;
426 	}
427 
428 	/*
429 	 * if project with projectName is opened adds all its testcases to list in
430 	 * any case adds {CREATE_ON_PROJECT_LEVEL, CREATE_NEW_OPTION}
431 	 */
432 	public static String[] getAvailableTestCases( String projectName )
433 	{
434 		List<String> availableTestCases = new ArrayList<String>();
435 		if( !projectName.equals( CREATE_NEW_OPTION ) && isProjectOpened( projectName ) )
436 		{
437 			availableTestCases.addAll( getTestCasesNames() );
438 		}
439 		availableTestCases.add( CREATE_ON_PROJECT_LEVEL );
440 		availableTestCases.add( CREATE_NEW_OPTION );
441 		String[] names = new String[availableTestCases.size()];
442 		for( int c = 0; c < availableTestCases.size(); c++ )
443 		{
444 			names[c] = availableTestCases.get( c );
445 		}
446 
447 		return names;
448 	}
449 
450 	public static String[] getAvailableRunners( String projectName, String testCaseName )
451 	{
452 		List<String> availableSamplers = new ArrayList<String>();
453 		if( !projectName.equals( CREATE_NEW_OPTION ) && isProjectOpened( projectName ) )
454 		{
455 			availableSamplers.addAll( getSoapUISamplersNames( projectName, testCaseName ) );
456 		}
457 
458 		availableSamplers.add( CREATE_NEW_OPTION );
459 		String[] names = new String[availableSamplers.size()];
460 		for( int c = 0; c < availableSamplers.size(); c++ )
461 		{
462 			names[c] = availableSamplers.get( c );
463 		}
464 
465 		return names;
466 	}
467 
468 	public static String[] getAvailableMockServiceRunners( String projectName, String testCaseName )
469 	{
470 		List<String> availableMockServiceRunners = new ArrayList<String>();
471 		if( !projectName.equals( CREATE_NEW_OPTION ) && isProjectOpened( projectName ) )
472 		{
473 			availableMockServiceRunners.addAll( getMockServiceRunnersNames( projectName, testCaseName ) );
474 		}
475 
476 		availableMockServiceRunners.add( CREATE_NEW_OPTION );
477 		String[] names = new String[availableMockServiceRunners.size()];
478 		for( int c = 0; c < availableMockServiceRunners.size(); c++ )
479 		{
480 			names[c] = availableMockServiceRunners.get( c );
481 		}
482 
483 		return names;
484 	}
485 
486 }