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