1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package com.eviware.soapui.impl.rest.actions.service;
26
27 import java.util.Arrays;
28 import java.util.List;
29
30 import com.eviware.soapui.impl.rest.RestRequest;
31 import com.eviware.soapui.impl.rest.RestResource;
32 import com.eviware.soapui.impl.rest.RestService;
33 import com.eviware.soapui.impl.wsdl.WsdlProject;
34 import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
35 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
36 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
37 import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory;
38 import com.eviware.soapui.model.support.ModelSupport;
39 import com.eviware.soapui.support.StringUtils;
40 import com.eviware.soapui.support.UISupport;
41 import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
42 import com.eviware.soapui.support.types.StringList;
43 import com.eviware.x.form.XFormDialog;
44 import com.eviware.x.form.XFormOptionsField;
45 import com.eviware.x.form.support.ADialogBuilder;
46 import com.eviware.x.form.support.AField;
47 import com.eviware.x.form.support.AForm;
48 import com.eviware.x.form.support.AField.AFieldType;
49
50 /***
51 * Generates a TestSuite for the specified Interface
52 *
53 * @author ole.matzura
54 */
55
56 public class GenerateRestTestSuiteAction extends AbstractSoapUIAction<RestService>
57 {
58 public GenerateRestTestSuiteAction()
59 {
60 super( "Generate TestSuite", "Generates TestSuite with TestCase(s) for all Resources in this Service" );
61 }
62
63 public void perform( RestService target, Object param )
64 {
65 generateTestSuite( target, false );
66 }
67
68 public WsdlTestSuite generateTestSuite( RestService service, boolean atCreation )
69 {
70 XFormDialog dialog = ADialogBuilder.buildDialog( GenerateForm.class );
71 dialog.setValue( GenerateForm.STYLE, "One TestCase for each Resource" );
72
73 StringList paths = new StringList();
74 for( RestResource resource : service.getAllResources() )
75 paths.add( resource.getName() + ": " + resource.getFullPath( false ) );
76
77 dialog.setOptions( GenerateForm.RESOURCES, paths.toStringArray() );
78 XFormOptionsField operationsFormField = ( XFormOptionsField )dialog.getFormField( GenerateForm.RESOURCES );
79 operationsFormField.setSelectedOptions( paths.toStringArray() );
80
81 WsdlProject project = service.getProject();
82 String[] testSuites = ModelSupport.getNames( new String[] { "<create>" }, project.getTestSuiteList() );
83 dialog.setOptions( GenerateForm.TESTSUITE, testSuites );
84
85 if( dialog.show() )
86 {
87 List<String> resources = Arrays.asList( StringUtils.toStringArray( operationsFormField.getSelectedOptions() ) );
88 if( resources.size() == 0 )
89 {
90 UISupport.showErrorMessage( "No Resources selected.." );
91 return null;
92 }
93
94 String testSuiteName = dialog.getValue( GenerateForm.TESTSUITE );
95
96 if( testSuiteName.equals( "<create>" ) )
97 testSuiteName = UISupport.prompt( "Enter name of TestSuite to create", "Generate TestSuite", service
98 .getName()
99 + " TestSuite" );
100
101 if( testSuiteName != null && testSuiteName.trim().length() > 0 )
102 {
103 WsdlTestSuite testSuite = project.getTestSuiteByName( testSuiteName );
104
105 if( testSuite == null )
106 {
107 testSuite = project.addNewTestSuite( testSuiteName );
108 testSuite.setDescription( "TestSuite generated for REST Service [" + service.getName() + "]" );
109 }
110
111 int style = dialog.getValueIndex( GenerateForm.STYLE );
112 boolean generateLoadTest = dialog.getBooleanValue( GenerateForm.GENERATE_LOADTEST );
113 if( style == 0 )
114 {
115 generateMulipleTestCases( testSuite, service, generateLoadTest, resources );
116 }
117 else if( style == 1 )
118 {
119 generateSingleTestCase( testSuite, service, generateLoadTest, resources );
120 }
121
122 if( !atCreation )
123 {
124 UISupport.showDesktopPanel( testSuite );
125 }
126
127 return testSuite;
128 }
129 }
130
131 return null;
132 }
133
134 private void generateSingleTestCase( WsdlTestSuite testSuite, RestService service, boolean createLoadTest,
135 List<String> resources )
136 {
137 WsdlTestCase testCase = testSuite.addNewTestCase( service.getName() + " TestSuite" );
138
139 for( RestResource resource : service.getAllResources() )
140 {
141 if( !resources.contains( resource.getName() + ": " + resource.getFullPath( false ) ) )
142 continue;
143
144 if( resource.getRequestCount() > 0 )
145 {
146 for( int x = 0; x < resource.getRequestCount(); x++ )
147 {
148 testCase.addTestStep( RestRequestStepFactory.createConfig( resource.getRequestAt( x ), resource
149 .getName() ) );
150 }
151 }
152 }
153
154 if( createLoadTest )
155 {
156 testCase.addNewLoadTest( "LoadTest 1" );
157 }
158 }
159
160 private void generateMulipleTestCases( WsdlTestSuite testSuite, RestService service, boolean createLoadTest,
161 List<String> resources )
162 {
163 for( RestResource resource : service.getAllResources() )
164 {
165 if( !resources.contains( resource.getName() + ": " + resource.getFullPath( false ) ) )
166 continue;
167
168 WsdlTestCase testCase = testSuite.addNewTestCase( resource.getName() + " TestCase" );
169 testCase.setDescription( "TestCase generated for REST Resource [" + resource.getName() + "] located at ["
170 + resource.getFullPath( false ) + "]" );
171
172 if( resource.getRequestCount() > 0 )
173 {
174 for( int x = 0; x < resource.getRequestCount(); x++ )
175 {
176 RestRequest request = resource.getRequestAt( x );
177 testCase.addTestStep( RestRequestStepFactory.createConfig( request, request.getName() ) );
178 }
179 }
180
181 if( createLoadTest )
182 {
183 testCase.addNewLoadTest( "LoadTest 1" );
184 }
185 }
186 }
187
188 @AForm( name = "Generate TestSuite", description = "Generates TestSuite with TestCase(s) for all Resources in this Service", helpUrl = HelpUrls.GENERATE_TESTSUITE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
189 private class GenerateForm
190 {
191 @AField( name = "TestSuite", description = "The TestSuite to create or use", type = AFieldType.ENUMERATION )
192 public final static String TESTSUITE = "TestSuite";
193
194 @AField( name = "Style", description = "Select the style of TestCases to create", type = AFieldType.RADIOGROUP, values = {
195 "One TestCase for each Resource", "Single TestCase with one Request for each Method" } )
196 public final static String STYLE = "Style";
197
198 @AField( name = "Resources", description = "The Resources for which to Generate Tests", type = AFieldType.MULTILIST )
199 public final static String RESOURCES = "Resources";
200
201 @AField( name = "Generate LoadTest", description = "Generates a default LoadTest for each created TestCase", type = AFieldType.BOOLEAN )
202 public final static String GENERATE_LOADTEST = "Generate LoadTest";
203 }
204 }