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.setMethod( dialog.getValue( Form.HTTPMETHOD ) );
88  
89           new XmlBeansRestParamsTestPropertyHolder( testCase,
90                   requestConfig.addNewParameters() ).addParameters( params );
91  
92           TestStepConfig testStep = TestStepConfig.Factory.newInstance();
93           testStep.setType( HTTPREQUEST_TYPE );
94           testStep.setConfig( testStepConfig );
95           testStep.setName( dialog.getValue( Form.STEPNAME ) );
96  
97           return testStep;
98        }
99        else
100       {
101          return null;
102       }
103    }
104 
105    public boolean canCreate()
106    {
107       return true;
108    }
109 
110    private void buildDialog()
111    {
112       dialog = ADialogBuilder.buildDialog( Form.class );
113       dialog.getFormField( Form.STEPNAME ).addFormFieldValidator( new RequiredValidator() );
114       dialog.getFormField( Form.EXTRACTPARAMS ).setProperty( "action", new ExtractParamsAction() );
115       ( (XFormOptionsField) dialog.getFormField( Form.HTTPMETHOD ) ).setOptions( new Object[]{
116               RestRequest.RequestMethod.GET,
117               RestRequest.RequestMethod.POST,
118               RestRequest.RequestMethod.PUT,
119               RestRequest.RequestMethod.DELETE,
120               RestRequest.RequestMethod.HEAD
121       } );
122    }
123 
124    @AForm( name = "Form.Title", description = "Form.Description", helpUrl = HelpUrls.NEWRESTSERVICE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
125    public interface Form
126    {
127       @AField( description = "Form.TestStepName.Description", type = AField.AFieldType.STRING )
128       public final static String STEPNAME = messages.get( "Form.TestStepName.Label" );
129 
130       @AField( description = "Form.Endpoint.Description", type = AField.AFieldType.STRING )
131       public final static String ENDPOINT = messages.get( "Form.Endpoint.Label" );
132 
133       @AField( description = "Form.ExtractParams.Description", type = AField.AFieldType.ACTION )
134       public final static String EXTRACTPARAMS = messages.get( "Form.ExtractParams.Label" );
135 
136       @AField( description = "Form.ParamsTable.Description", type = AField.AFieldType.COMPONENT )
137       public final static String PARAMSTABLE = messages.get( "Form.ParamsTable.Label" );
138 
139       @AField( description = "Form.HttpMethod.Description", type = AField.AFieldType.ENUMERATION )
140       public final static String HTTPMETHOD = messages.get( "Form.HttpMethod.Label" );
141    }
142 
143    private class ExtractParamsAction extends AbstractAction
144    {
145       public ExtractParamsAction()
146       {
147          super( "Extract Params" );
148       }
149 
150       public void actionPerformed( ActionEvent e )
151       {
152          try
153          {
154             String path = RestUtils.extractParams( dialog.getValue( Form.ENDPOINT ), params, true );
155             dialog.setValue( Form.ENDPOINT, path );
156 
157             if( StringUtils.isNullOrEmpty( dialog.getValue( Form.STEPNAME ) ) )
158             {
159                setNameFromPath( path );
160             }
161 
162             paramsTable.refresh();
163          }
164          catch( Exception e1 )
165          {
166             UISupport.showInfoMessage( "No parameters to extract!" );
167          }
168       }
169 
170       private void setNameFromPath( String path )
171       {
172          String[] items = path.split( "/" );
173 
174          if( items.length > 0 )
175          {
176             dialog.setValue( Form.STEPNAME, items[items.length - 1] );
177          }
178       }
179    }
180 
181 }