1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.teststeps.registry;
14
15 import java.awt.event.ActionEvent;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Map;
19
20 import javax.swing.AbstractAction;
21
22 import com.eviware.soapui.config.HttpRequestConfig;
23 import com.eviware.soapui.config.RestParameterConfig;
24 import com.eviware.soapui.config.RestParametersConfig;
25 import com.eviware.soapui.config.TestStepConfig;
26 import com.eviware.soapui.impl.rest.RestRequestInterface;
27 import com.eviware.soapui.impl.rest.panels.resource.RestParamsTable;
28 import com.eviware.soapui.impl.rest.support.RestUtils;
29 import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder;
30 import com.eviware.soapui.impl.support.HttpUtils;
31 import com.eviware.soapui.impl.wsdl.monitor.WsdlMonitorMessageExchange;
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.HttpTestRequestStep;
35 import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
36 import com.eviware.soapui.support.MessageSupport;
37 import com.eviware.soapui.support.StringUtils;
38 import com.eviware.soapui.support.UISupport;
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.AForm;
44 import com.eviware.x.form.validators.RequiredValidator;
45
46 /***
47 * Factory for WsdlTestRequestSteps
48 *
49 * @author Ole.Matzura
50 */
51
52 public class HttpRequestStepFactory extends WsdlTestStepFactory
53 {
54 private static final String HTTPREQUEST = "httprequest";
55 public static final String HTTPREQUEST_TYPE = HTTPREQUEST;
56 private XFormDialog dialog;
57 public static final MessageSupport messages = MessageSupport.getMessages( HttpRequestStepFactory.class );
58 private XmlBeansRestParamsTestPropertyHolder params;
59 private RestParamsTable paramsTable;
60
61 public HttpRequestStepFactory()
62 {
63 super( HTTPREQUEST_TYPE, "HTTP Test Request", "Submits a HTTP Request and validates its response",
64 "/http_request.gif" );
65 }
66
67 public WsdlTestStep buildTestStep( WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest )
68 {
69 return new HttpTestRequestStep( testCase, config, forLoadTest );
70 }
71
72 public TestStepConfig createNewTestStep( WsdlTestCase testCase, String name )
73 {
74 if( dialog == null )
75 {
76 buildDialog();
77 }
78 else
79 {
80 dialog.setValue( Form.ENDPOINT, "" );
81 }
82
83 params = new XmlBeansRestParamsTestPropertyHolder( testCase, RestParametersConfig.Factory.newInstance() );
84
85 paramsTable = new RestParamsTable( params, false );
86 dialog.getFormField( Form.PARAMSTABLE ).setProperty( "component", paramsTable );
87 dialog.setValue( Form.STEPNAME, name );
88
89 try
90 {
91 if( dialog.show() )
92 {
93 HttpRequestConfig httpRequest = HttpRequestConfig.Factory.newInstance();
94 httpRequest.setEndpoint( HttpUtils.ensureEndpointStartsWithProtocol( dialog.getValue( Form.ENDPOINT ) ) );
95 httpRequest.setMethod( dialog.getValue( Form.HTTPMETHOD ) );
96 XmlBeansRestParamsTestPropertyHolder tempParams = new XmlBeansRestParamsTestPropertyHolder( testCase,
97 httpRequest.addNewParameters() );
98 tempParams.addParameters( params );
99 tempParams.release();
100
101 TestStepConfig testStep = TestStepConfig.Factory.newInstance();
102 testStep.setType( HTTPREQUEST_TYPE );
103 testStep.setConfig( httpRequest );
104 testStep.setName( dialog.getValue( Form.STEPNAME ) );
105
106 return testStep;
107 }
108 else
109 {
110 return null;
111 }
112 }
113 finally
114 {
115 paramsTable.release();
116 paramsTable = null;
117 params = null;
118 dialog.getFormField( Form.PARAMSTABLE ).setProperty( "component", paramsTable );
119 }
120 }
121
122 public TestStepConfig createNewTestStep( WsdlTestCase testCase, String name, String endpoint, String method )
123 {
124 RestParametersConfig restParamConf = RestParametersConfig.Factory.newInstance();
125 params = new XmlBeansRestParamsTestPropertyHolder( testCase, restParamConf );
126
127 HttpRequestConfig httpRequest = HttpRequestConfig.Factory.newInstance();
128 httpRequest.setMethod( method );
129
130 String path = RestUtils.extractParams( endpoint, params, true );
131 endpoint = path;
132
133 XmlBeansRestParamsTestPropertyHolder tempParams = new XmlBeansRestParamsTestPropertyHolder( testCase, httpRequest
134 .addNewParameters() );
135 tempParams.addParameters( params );
136
137 httpRequest.setEndpoint( HttpUtils.ensureEndpointStartsWithProtocol( endpoint ) );
138
139 TestStepConfig testStep = TestStepConfig.Factory.newInstance();
140 testStep.setType( HTTPREQUEST_TYPE );
141 testStep.setConfig( httpRequest );
142 testStep.setName( name );
143
144 return testStep;
145 }
146
147 public boolean canCreate()
148 {
149 return true;
150 }
151
152 private void buildDialog()
153 {
154 dialog = ADialogBuilder.buildDialog( Form.class );
155 dialog.getFormField( Form.STEPNAME ).addFormFieldValidator( new RequiredValidator() );
156 dialog.getFormField( Form.EXTRACTPARAMS ).setProperty( "action", new ExtractParamsAction() );
157 ( ( XFormOptionsField )dialog.getFormField( Form.HTTPMETHOD ) ).setOptions( RestRequestInterface.RequestMethod
158 .getMethods() );
159 }
160
161 @AForm( name = "Form.Title", description = "Form.Description", helpUrl = HelpUrls.NEWRESTSERVICE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
162 public interface Form
163 {
164 @AField( description = "Form.TestStepName.Description", type = AField.AFieldType.STRING )
165 public final static String STEPNAME = messages.get( "Form.TestStepName.Label" );
166
167 @AField( description = "Form.Endpoint.Description", type = AField.AFieldType.STRING )
168 public final static String ENDPOINT = messages.get( "Form.Endpoint.Label" );
169
170 @AField( description = "Form.ExtractParams.Description", type = AField.AFieldType.ACTION )
171 public final static String EXTRACTPARAMS = messages.get( "Form.ExtractParams.Label" );
172
173 @AField( description = "Form.ParamsTable.Description", type = AField.AFieldType.COMPONENT )
174 public final static String PARAMSTABLE = messages.get( "Form.ParamsTable.Label" );
175
176 @AField( description = "Form.HttpMethod.Description", type = AField.AFieldType.ENUMERATION )
177 public final static String HTTPMETHOD = messages.get( "Form.HttpMethod.Label" );
178 }
179
180 private class ExtractParamsAction extends AbstractAction
181 {
182 public ExtractParamsAction()
183 {
184 super( "Extract Params" );
185 }
186
187 public void actionPerformed( ActionEvent e )
188 {
189 try
190 {
191 String path = RestUtils.extractParams( dialog.getValue( Form.ENDPOINT ), params, true );
192 dialog.setValue( Form.ENDPOINT, path );
193
194 if( StringUtils.isNullOrEmpty( dialog.getValue( Form.STEPNAME ) ) )
195 {
196 setNameFromPath( path );
197 }
198
199 paramsTable.refresh();
200 }
201 catch( Exception e1 )
202 {
203 UISupport.showInfoMessage( "No parameters to extract!" );
204 }
205 }
206
207 private void setNameFromPath( String path )
208 {
209 String[] items = path.split( "/" );
210
211 if( items.length > 0 )
212 {
213 dialog.setValue( Form.STEPNAME, items[items.length - 1] );
214 }
215 }
216 }
217
218 public TestStepConfig createConfig( WsdlMonitorMessageExchange me, String stepName )
219 {
220 HttpRequestConfig testRequestConfig = HttpRequestConfig.Factory.newInstance();
221
222 testRequestConfig.setName( stepName );
223 testRequestConfig.setEncoding( "UTF-8" );
224 testRequestConfig.setEndpoint( me.getEndpoint() );
225 testRequestConfig.setMethod( me.getRequestMethod() );
226
227
228 RestParametersConfig parametersConfig = testRequestConfig.addNewParameters();
229 Map<String, String> parametersMap = me.getHttpRequestParameters();
230 List<RestParameterConfig> parameterConfigList = new ArrayList<RestParameterConfig>();
231 for( String name : parametersMap.keySet() )
232 {
233 RestParameterConfig parameterConf = RestParameterConfig.Factory.newInstance();
234 parameterConf.setName( name );
235 parameterConf.setValue( parametersMap.get( name ) );
236 parameterConfigList.add( parameterConf );
237 }
238 parametersConfig.setParameterArray( parameterConfigList.toArray( new RestParameterConfig[parametersMap.size()] ) );
239 testRequestConfig.setParameters( parametersConfig );
240
241
242
243
244 TestStepConfig testStep = TestStepConfig.Factory.newInstance();
245 testStep.setType( HTTPREQUEST );
246 testStep.setConfig( testRequestConfig );
247 testStep.setName( stepName );
248 return testStep;
249 }
250
251 }