View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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 com.eviware.soapui.impl.wsdl.WsdlInterface;
16  import com.eviware.soapui.impl.wsdl.WsdlOperation;
17  import com.eviware.soapui.impl.wsdl.WsdlProject;
18  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
19  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
20  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
21  import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory;
22  import com.eviware.soapui.model.support.ModelSupport;
23  import com.eviware.soapui.support.UISupport;
24  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
25  import com.eviware.x.form.XFormDialog;
26  import com.eviware.x.form.XFormOptionsField;
27  import com.eviware.x.form.support.ADialogBuilder;
28  import com.eviware.x.form.support.AField;
29  import com.eviware.x.form.support.AField.AFieldType;
30  import com.eviware.x.form.support.AForm;
31  
32  import java.util.Arrays;
33  import java.util.List;
34  
35  /***
36   * Generates a TestSuite for the specified Interface
37   *  
38   * @author ole.matzura
39   */
40  
41  public class GenerateWsdlTestSuiteAction extends AbstractSoapUIAction<WsdlInterface>
42  {
43  	public GenerateWsdlTestSuiteAction()
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, false );
51  	}
52  
53  	public WsdlTestSuite generateTestSuite( WsdlInterface iface, boolean atCreation )
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.getOperationList() );
59  		dialog.setOptions( GenerateForm.OPERATIONS, names );
60  		XFormOptionsField operationsFormField = ( XFormOptionsField ) dialog.getFormField( GenerateForm.OPERATIONS );
61  		operationsFormField.setSelectedOptions( names );
62  
63  		WsdlProject project = iface.getProject();
64  		String[] testSuites = ModelSupport.getNames( new String[] { "<create>" }, project.getTestSuiteList() );
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 = project.getTestSuiteByName( testSuiteName );
86  
87  				if( testSuite == null )
88  				{
89  					testSuite = 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 				if( !atCreation )
105 				{
106 					UISupport.showDesktopPanel( testSuite );
107 				}
108 				
109 				return testSuite;
110 			}
111 		}
112 
113 		return null;
114 	}
115 
116 	private void generateSingleTestCase( WsdlTestSuite testSuite, WsdlInterface iface, boolean useExisting,
117 				boolean createLoadTest, List<String> operations )
118 	{
119 		WsdlTestCase testCase = testSuite.addNewTestCase( iface.getName() + " TestSuite" );
120 
121 		for( int i = 0; i < iface.getOperationCount(); i++ )
122 		{
123 			WsdlOperation operation = iface.getOperationAt( i );
124 			if( !operations.contains( operation.getName() ))
125 				continue;
126 
127 			if( useExisting && operation.getRequestCount() > 0 )
128 			{
129 				for( int x = 0; x < operation.getRequestCount(); x++ )
130 				{
131 					testCase.addTestStep( WsdlTestRequestStepFactory.createConfig( operation.getRequestAt( x ), operation
132 								.getName() ) );
133 				}
134 			}
135 			else
136 			{
137 				testCase.addTestStep( WsdlTestRequestStepFactory.createConfig( operation, operation.getName() ) );
138 			}
139 		}
140 
141 		if( createLoadTest )
142 		{
143 			testCase.addNewLoadTest( "LoadTest 1" );
144 		}
145 	}
146 
147 	private void generateMulipleTestCases( WsdlTestSuite testSuite, WsdlInterface iface, boolean useExisting,
148 				boolean createLoadTest, List<String> operations )
149 	{
150 		for( int i = 0; i < iface.getOperationCount(); i++ )
151 		{
152 			WsdlOperation operation = iface.getOperationAt( i );
153 			if( !operations.contains( operation.getName() ))
154 				continue;
155 			
156 			WsdlTestCase testCase = testSuite.addNewTestCase( operation.getName() + " TestCase" );
157 
158 			if( useExisting && operation.getRequestCount() > 0 )
159 			{
160 				for( int x = 0; x < operation.getRequestCount(); x++ )
161 				{
162 					testCase.addTestStep( WsdlTestRequestStepFactory.createConfig( operation.getRequestAt( x ), operation
163 								.getName() ) );
164 				}
165 			}
166 			else
167 			{
168 				testCase.addTestStep( WsdlTestRequestStepFactory.createConfig( operation, operation.getName() ) );
169 			}
170 
171 			if( createLoadTest )
172 			{
173 				testCase.addNewLoadTest( "LoadTest 1" );
174 			}
175 		}
176 	}
177 
178 	@AForm( name = "Generate TestSuite", description = "Generates TestSuite with TestCase(s) for all Operations in this Interface",
179 				helpUrl=HelpUrls.GENERATE_TESTSUITE_HELP_URL, icon=UISupport.TOOL_ICON_PATH )
180 	private class GenerateForm
181 	{
182 		@AField( name = "TestSuite", description = "The TestSuite to create or use", type = AFieldType.ENUMERATION )
183 		public final static String TESTSUITE = "TestSuite";
184 
185 		@AField( name = "Style", description = "Select the style of TestCases to create", type = AFieldType.RADIOGROUP, values = {
186 					"One TestCase for each Operation", "Single TestCase with one Request for each Operation" } )
187 		public final static String STYLE = "Style";
188 
189 		@AField( name = "Request Content", description = "Select how to create Test Requests", type = AFieldType.RADIOGROUP, values = {
190 					"Use existing Requests in Interface", "Create new empty requests" } )
191 		public final static String REQUEST_CONTENT = "Request Content";
192 
193 		@AField( name = "Operations", description = "The Operations for which to Generate Tests", type = AFieldType.MULTILIST )
194 		public final static String OPERATIONS = "Operations";
195 
196 		@AField( name = "Generate LoadTest", description = "Generates a default LoadTest for each created TestCase", type = AFieldType.BOOLEAN )
197 		public final static String GENERATE_LOADTEST = "Generate LoadTest";
198 	}
199 }