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.panels.resource.RestParamsTable;
21  import com.eviware.soapui.impl.rest.support.RestUtils;
22  import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder;
23  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
24  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
25  import com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStep;
26  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
27  import com.eviware.soapui.support.MessageSupport;
28  import com.eviware.soapui.support.StringUtils;
29  import com.eviware.soapui.support.UISupport;
30  import com.eviware.x.form.XFormDialog;
31  import com.eviware.x.form.XFormOptionsField;
32  import com.eviware.x.form.support.ADialogBuilder;
33  import com.eviware.x.form.support.AField;
34  import com.eviware.x.form.support.AForm;
35  import com.eviware.x.form.validators.RequiredValidator;
36  
37  import javax.swing.*;
38  import java.awt.event.ActionEvent;
39  
40  /***
41   * Factory for WsdlTestRequestSteps
42   *
43   * @author Ole.Matzura
44   */
45  
46  public class HttpRequestStepFactory extends WsdlTestStepFactory
47  {
48     public static final String HTTPREQUEST_TYPE = "httprequest";
49     private XFormDialog dialog;
50     public static final MessageSupport messages = MessageSupport.getMessages( HttpRequestStepFactory.class );
51     private XmlBeansRestParamsTestPropertyHolder params;
52     private RestParamsTable paramsTable;
53  
54     public HttpRequestStepFactory()
55     {
56        super( HTTPREQUEST_TYPE, "HTTP Test Request", "Submits a HTTP Request and validates its response", "/http_request.gif" );
57     }
58  
59     public WsdlTestStep buildTestStep( WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest )
60     {
61        return new HttpTestRequestStep( testCase, config, forLoadTest );
62     }
63  
64     public TestStepConfig createNewTestStep( WsdlTestCase testCase, String name )
65     {
66        if( dialog == null )
67        {
68           buildDialog();
69        }
70        else
71        {
72           dialog.setValue( Form.ENDPOINT, "" );
73        }
74  
75        params = new XmlBeansRestParamsTestPropertyHolder( testCase,
76                RestParametersConfig.Factory.newInstance() );
77  
78        paramsTable = new RestParamsTable( params, false );
79        dialog.getFormField( Form.PARAMSTABLE ).setProperty( "component", paramsTable );
80        dialog.setValue( Form.STEPNAME, name );
81  
82        if( dialog.show() )
83        {
84           RestRequestStepConfig testStepConfig = RestRequestStepConfig.Factory.newInstance();
85           RestMethodConfig requestConfig = testStepConfig.addNewRestRequest();
86           requestConfig.setFullPath( dialog.getValue( Form.ENDPOINT ) );
87           requestConfig.setEndpoint( dialog.getValue( Form.ENDPOINT ) );
88           requestConfig.setMethod( dialog.getValue( Form.HTTPMETHOD ) );
89  
90           new XmlBeansRestParamsTestPropertyHolder( testCase,
91                   requestConfig.addNewParameters() ).addParameters( params );
92  
93           TestStepConfig testStep = TestStepConfig.Factory.newInstance();
94           testStep.setType( HTTPREQUEST_TYPE );
95           testStep.setConfig( testStepConfig );
96           testStep.setName( dialog.getValue( Form.STEPNAME ) );
97  
98           return testStep;
99        }
100       else
101       {
102          return null;
103       }
104    }
105 
106    public boolean canCreate()
107    {
108       return true;
109    }
110 
111    private void buildDialog()
112    {
113       dialog = ADialogBuilder.buildDialog( Form.class );
114       dialog.getFormField( Form.STEPNAME ).addFormFieldValidator( new RequiredValidator() );
115       dialog.getFormField( Form.EXTRACTPARAMS ).setProperty( "action", new ExtractParamsAction() );
116       ( (XFormOptionsField) dialog.getFormField( Form.HTTPMETHOD ) ).setOptions( new Object[]{
117               RestRequest.RequestMethod.GET,
118               RestRequest.RequestMethod.POST,
119               RestRequest.RequestMethod.PUT,
120               RestRequest.RequestMethod.DELETE,
121               RestRequest.RequestMethod.HEAD
122       } );
123    }
124 
125    @AForm( name = "Form.Title", description = "Form.Description", helpUrl = HelpUrls.NEWRESTSERVICE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
126    public interface Form
127    {
128       @AField( description = "Form.TestStepName.Description", type = AField.AFieldType.STRING )
129       public final static String STEPNAME = messages.get( "Form.TestStepName.Label" );
130 
131       @AField( description = "Form.Endpoint.Description", type = AField.AFieldType.STRING )
132       public final static String ENDPOINT = messages.get( "Form.Endpoint.Label" );
133 
134       @AField( description = "Form.ExtractParams.Description", type = AField.AFieldType.ACTION )
135       public final static String EXTRACTPARAMS = messages.get( "Form.ExtractParams.Label" );
136 
137       @AField( description = "Form.ParamsTable.Description", type = AField.AFieldType.COMPONENT )
138       public final static String PARAMSTABLE = messages.get( "Form.ParamsTable.Label" );
139 
140       @AField( description = "Form.HttpMethod.Description", type = AField.AFieldType.ENUMERATION )
141       public final static String HTTPMETHOD = messages.get( "Form.HttpMethod.Label" );
142    }
143 
144    private class ExtractParamsAction extends AbstractAction
145    {
146       public ExtractParamsAction()
147       {
148          super( "Extract Params" );
149       }
150 
151       public void actionPerformed( ActionEvent e )
152       {
153          try
154          {
155             String path = RestUtils.extractParams( dialog.getValue( Form.ENDPOINT ), params, true );
156             dialog.setValue( Form.ENDPOINT, path );
157 
158             if( StringUtils.isNullOrEmpty( dialog.getValue( Form.STEPNAME ) ) )
159             {
160                setNameFromPath( path );
161             }
162 
163             paramsTable.refresh();
164          }
165          catch( Exception e1 )
166          {
167             UISupport.showInfoMessage( "No parameters to extract!" );
168          }
169       }
170 
171       private void setNameFromPath( String path )
172       {
173          String[] items = path.split( "/" );
174 
175          if( items.length > 0 )
176          {
177             dialog.setValue( Form.STEPNAME, items[items.length - 1] );
178          }
179       }
180    }
181 
182 }