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