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.teststeps.registry;
14  
15  import com.eviware.soapui.config.RestMethodConfig;
16  import com.eviware.soapui.config.RestParametersConfig;
17  import com.eviware.soapui.config.RestRequestStepConfig;
18  import com.eviware.soapui.config.TestStepConfig;
19  import com.eviware.soapui.impl.rest.RestRequest;
20  import com.eviware.soapui.impl.rest.RestResource;
21  import com.eviware.soapui.impl.rest.RestService;
22  import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder;
23  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
24  import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep;
25  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
26  import com.eviware.soapui.model.iface.Interface;
27  import com.eviware.soapui.model.project.Project;
28  import com.eviware.soapui.support.UISupport;
29  import com.eviware.soapui.support.types.TupleList;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /***
35   * Factory for WsdlTestRequestSteps
36   *
37   * @author Ole.Matzura
38   */
39  
40  public class RestRequestStepFactory extends WsdlTestStepFactory
41  {
42     public static final String RESTREQUEST_TYPE = "restrequest";
43     public static final String STEP_NAME = "Name";
44  //   private XFormDialog dialog;
45  //   private StringToStringMap dialogValues = new StringToStringMap();
46  
47     public RestRequestStepFactory()
48     {
49        super( RESTREQUEST_TYPE, "REST Test Request", "Submits a REST-style Request and validates its response", "/rest_request.gif" );
50     }
51  
52     public WsdlTestStep buildTestStep( WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest )
53     {
54        return new RestTestRequestStep( testCase, config, forLoadTest );
55     }
56  
57     public static TestStepConfig createConfig( RestRequest request, String stepName )
58     {
59        RestRequestStepConfig requestStepConfig = RestRequestStepConfig.Factory.newInstance();
60  
61        requestStepConfig.setService( request.getOperation().getInterface().getName() );
62        requestStepConfig.setResourcePath( request.getOperation().getFullPath() );
63        requestStepConfig.addNewRestRequest().set( request.getConfig().copy() );
64  
65        TestStepConfig testStep = TestStepConfig.Factory.newInstance();
66        testStep.setType( RESTREQUEST_TYPE );
67        testStep.setConfig( requestStepConfig );
68        testStep.setName( stepName );
69  
70        return testStep;
71     }
72  
73     public TestStepConfig createNewTestStep( WsdlTestCase testCase, String name )
74     {
75        // build list of available interfaces / restResources
76        Project project = testCase.getTestSuite().getProject();
77        List<String> options = new ArrayList<String>();
78        TupleList<RestResource, RestRequest> restResources = new TupleList<RestResource, RestRequest>();
79  
80        for( int c = 0; c < project.getInterfaceCount(); c++ )
81        {
82           Interface iface = project.getInterfaceAt( c );
83           if( iface instanceof RestService )
84           {
85              List<RestResource> resources = ( (RestService) iface ).getAllResources();
86  
87              for( RestResource resource : resources )
88              {
89                 options.add( iface.getName() + " -> " + resource.getPath() );
90                 restResources.add( resource, null );
91  
92                 for( RestRequest request : resource.getRequests().values() )
93                 {
94                    restResources.add( resource, request );
95                    options.add( iface.getName() + " -> " + resource.getPath() + " -> " + request.getName() );
96                 }
97              }
98           }
99        }
100 
101       if( restResources.size() == 0 )
102       {
103          UISupport.showErrorMessage( "Missing REST Resources in project" );
104          return null;
105       }
106 
107       Object op = UISupport.prompt( "Select Resource to invoke for request", "New RestRequest", options.toArray() );
108       if( op != null )
109       {
110          int ix = options.indexOf( op );
111          if( ix != -1 )
112          {
113             TupleList<RestResource, RestRequest>.Tuple tuple = restResources.get( ix );
114 
115 //            if( dialog == null )
116 //               buildDialog();
117 //
118 //            dialogValues.put( STEP_NAME, name );
119 //            dialogValues = dialog.show( dialogValues );
120 //            if( dialog.getReturnValue() != XFormDialog.OK_OPTION )
121 //               return null;
122 
123             return tuple.getValue2() == null ? createNewTestStep( tuple.getValue1(), name )
124                     : createConfig( tuple.getValue2(), name );
125          }
126       }
127 
128       return null;
129    }
130 
131    public TestStepConfig createNewTestStep( RestResource resource, String name )
132    {
133       RestRequestStepConfig requestStepConfig = RestRequestStepConfig.Factory.newInstance();
134       RestMethodConfig testRequestConfig = requestStepConfig.addNewRestRequest();
135 
136       testRequestConfig.setName( name );
137       testRequestConfig.setEncoding( "UTF-8" );
138 
139       if( resource != null )
140       {
141          requestStepConfig.setService( resource.getInterface().getName() );
142          requestStepConfig.setResourcePath( resource.getPath() );
143 
144          String[] endpoints = resource.getInterface().getEndpoints();
145          if( endpoints.length > 0 )
146             testRequestConfig.setEndpoint( endpoints[0] );
147 
148          testRequestConfig.addNewRequest();
149          RestParametersConfig parametersConfig = testRequestConfig.addNewParameters();
150 
151          for( XmlBeansRestParamsTestPropertyHolder.RestParamProperty property : resource.getDefaultParams() )
152          {
153             parametersConfig.addNewParameter().set( property.getConfig() );
154          }
155       }
156 
157       TestStepConfig testStepConfig = TestStepConfig.Factory.newInstance();
158       testStepConfig.setType( RESTREQUEST_TYPE );
159       testStepConfig.setConfig( requestStepConfig );
160       testStepConfig.setName( name );
161 
162       return testStepConfig;
163    }
164 
165    public boolean canCreate()
166    {
167       return true;
168    }
169 
170 //   private void buildDialog()
171 //   {
172 //      XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Add REST Request to TestCase" );
173 //      XForm mainForm = builder.createForm( "Basic" );
174 //
175 //      mainForm.addTextField( STEP_NAME, "Name of TestStep", XForm.FieldType.URL ).setWidth( 30 );
176 //
177 //      dialog = builder.buildDialog( builder.buildOkCancelActions(),
178 //              "Specify options for adding a new REST Request to a TestCase", UISupport.OPTIONS_ICON );
179 //   }
180 }