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