View Javadoc

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