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.actions.iface;
14  
15  import java.util.Arrays;
16  import java.util.List;
17  
18  import com.eviware.soapui.impl.wsdl.WsdlInterface;
19  import com.eviware.soapui.impl.wsdl.WsdlOperation;
20  import com.eviware.soapui.impl.wsdl.WsdlProject;
21  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
22  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
23  import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory;
24  import com.eviware.soapui.model.support.ModelSupport;
25  import com.eviware.soapui.support.UISupport;
26  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
27  import com.eviware.x.form.XFormDialog;
28  import com.eviware.x.form.XFormOptionsField;
29  import com.eviware.x.form.support.ADialogBuilder;
30  import com.eviware.x.form.support.AField;
31  import com.eviware.x.form.support.AForm;
32  import com.eviware.x.form.support.AField.AFieldType;
33  
34  /***
35   * Generates a TestSuite for the specified Interface
36   *  
37   * @author ole.matzura
38   */
39  
40  public class GenerateTestSuiteAction extends AbstractSoapUIAction<WsdlInterface>
41  {
42  	public GenerateTestSuiteAction()
43  	{
44  		super( "Generate TestSuite", "Generates TestSuite with TestCase(s) for all Operations in this Interface" );
45  	}
46  
47  	public void perform( WsdlInterface target, Object param )
48  	{
49  		generateTestSuite( target );
50  	}
51  
52  	public WsdlTestSuite generateTestSuite( WsdlInterface iface )
53  	{
54  		XFormDialog dialog = ADialogBuilder.buildDialog( GenerateForm.class );
55  		dialog.setValue( GenerateForm.STYLE, "One TestCase for each Operation" );
56  		dialog.setValue( GenerateForm.REQUEST_CONTENT, "Create new empty requests" );
57  		String[] names = ModelSupport.getNames( iface.getOperations() );
58  		dialog.setOptions( GenerateForm.OPERATIONS, names );
59  		XFormOptionsField operationsFormField = ( XFormOptionsField ) dialog.getFormField( GenerateForm.OPERATIONS );
60  		operationsFormField.setSelectedOptions( names );
61  
62  		WsdlProject project = ( WsdlProject ) iface.getProject();
63  		String[] testSuites = ModelSupport.getNames( new String[] { "<create>" }, project.getTestSuites() );
64  		dialog.setOptions( GenerateForm.TESTSUITE, testSuites );
65  
66  		if( dialog.show() )
67  		{
68  			List<String> operations = Arrays.asList( operationsFormField.getSelectedOptions() );
69  			if( operations.size() == 0 )
70  			{
71  				UISupport.showErrorMessage( "No Operations selected.." );
72  				return null;
73  			}
74  			
75  			String testSuiteName = dialog.getValue( GenerateForm.TESTSUITE );
76  
77  			if( testSuiteName.equals( "<create>" ) )
78  				testSuiteName = UISupport.prompt( "Enter name of TestSuite to create", "Generate TestSuite", iface
79  							.getName()
80  							+ " TestSuite" );
81  
82  			if( testSuiteName != null && testSuiteName.trim().length() > 0 )
83  			{
84  				WsdlTestSuite testSuite = ( WsdlTestSuite ) project.getTestSuiteByName( testSuiteName );
85  
86  				if( testSuite == null )
87  				{
88  					testSuite = ( WsdlTestSuite ) project.addNewTestSuite( testSuiteName );
89  				}
90  
91  				int style = dialog.getValueIndex( GenerateForm.STYLE );
92  				boolean useExistingRequests = dialog.getValueIndex( GenerateForm.REQUEST_CONTENT ) == 0;
93  				boolean generateLoadTest = dialog.getBooleanValue( GenerateForm.GENERATE_LOADTEST );
94  				if( style == 0 )
95  				{
96  					generateMulipleTestCases( testSuite, iface, useExistingRequests, generateLoadTest, operations );
97  				}
98  				else if( style == 1 )
99  				{
100 					generateSingleTestCase( testSuite, iface, useExistingRequests, generateLoadTest, operations );
101 				}
102 
103 				return testSuite;
104 			}
105 		}
106 
107 		return null;
108 	}
109 
110 	private void generateSingleTestCase( WsdlTestSuite testSuite, WsdlInterface iface, boolean useExisting,
111 				boolean createLoadTest, List<String> operations )
112 	{
113 		WsdlTestCase testCase = testSuite.addNewTestCase( iface.getName() + " TestSuite" );
114 
115 		for( int i = 0; i < iface.getOperationCount(); i++ )
116 		{
117 			WsdlOperation operation = ( WsdlOperation ) iface.getOperationAt( i );
118 			if( !operations.contains( operation.getName() ))
119 				continue;
120 
121 			testCase.addTestStep( WsdlTestRequestStepFactory.createConfig( operation, operation.getName() ) );
122 		}
123 
124 		if( createLoadTest )
125 		{
126 			testCase.addNewLoadTest( "LoadTest 1" );
127 		}
128 
129 		UISupport.showDesktopPanel( testCase );
130 	}
131 
132 	private void generateMulipleTestCases( WsdlTestSuite testSuite, WsdlInterface iface, boolean useExisting,
133 				boolean createLoadTest, List<String> operations )
134 	{
135 		for( int i = 0; i < iface.getOperationCount(); i++ )
136 		{
137 			WsdlOperation operation = ( WsdlOperation ) iface.getOperationAt( i );
138 			if( !operations.contains( operation.getName() ))
139 				continue;
140 			
141 			WsdlTestCase testCase = testSuite.addNewTestCase( operation.getName() + " TestCase" );
142 
143 			if( useExisting && operation.getRequestCount() > 0 )
144 			{
145 				for( int x = 0; x < operation.getRequestCount(); x++ )
146 				{
147 					testCase.addTestStep( WsdlTestRequestStepFactory.createConfig( operation.getRequestAt( x ), operation
148 								.getName() ) );
149 				}
150 			}
151 			else
152 			{
153 				testCase.addTestStep( WsdlTestRequestStepFactory.createConfig( operation, operation.getName() ) );
154 			}
155 
156 			if( createLoadTest )
157 			{
158 				testCase.addNewLoadTest( "LoadTest 1" );
159 			}
160 		}
161 
162 		UISupport.showDesktopPanel( testSuite );
163 	}
164 
165 	@AForm( name = "Generate TestSuite", description = "Generates TestSuite with TestCase(s) for all Operations in this Interface" )
166 	private class GenerateForm
167 	{
168 		@AField( name = "TestSuite", description = "The TestSuite to create or use", type = AFieldType.ENUMERATION )
169 		public final static String TESTSUITE = "TestSuite";
170 
171 		@AField( name = "Style", description = "Select the style of TestCases to create", type = AFieldType.RADIOGROUP, values = {
172 					"One TestCase for each Operation", "Single TestCase with one Request for each Operation" } )
173 		public final static String STYLE = "Style";
174 
175 		@AField( name = "Request Content", description = "Select how to create Test Requests", type = AFieldType.RADIOGROUP, values = {
176 					"Use existing Requests in Interface", "Create new empty requests" } )
177 		public final static String REQUEST_CONTENT = "Request Content";
178 
179 		@AField( name = "Operations", description = "The Operations for which to Generate Tests", type = AFieldType.MULTILIST )
180 		public final static String OPERATIONS = "Operations";
181 
182 		@AField( name = "Generate LoadTest", description = "Generates a default LoadTest for each created TestCase", type = AFieldType.BOOLEAN )
183 		public final static String GENERATE_LOADTEST = "Generate LoadTest";
184 	}
185 }