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.util.ArrayList;
16 import java.util.List;
17
18 import com.eviware.soapui.config.RestRequestConfig;
19 import com.eviware.soapui.config.RestRequestStepConfig;
20 import com.eviware.soapui.config.StringToStringMapConfig;
21 import com.eviware.soapui.config.TestStepConfig;
22 import com.eviware.soapui.config.StringToStringMapConfig.Entry;
23 import com.eviware.soapui.impl.rest.RestMethod;
24 import com.eviware.soapui.impl.rest.RestRequest;
25 import com.eviware.soapui.impl.rest.RestResource;
26 import com.eviware.soapui.impl.rest.RestService;
27 import com.eviware.soapui.impl.rest.support.RestParamProperty;
28 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
29 import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep;
30 import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
31 import com.eviware.soapui.model.iface.Interface;
32 import com.eviware.soapui.model.project.Project;
33 import com.eviware.soapui.support.StringUtils;
34 import com.eviware.soapui.support.UISupport;
35 import com.eviware.soapui.support.types.TupleList;
36
37 /***
38 * Factory for WsdlTestRequestSteps
39 *
40 * @author Ole.Matzura
41 */
42
43 public class RestRequestStepFactory extends WsdlTestStepFactory
44 {
45 public static final String RESTREQUEST_TYPE = "restrequest";
46 public static final String STEP_NAME = "Name";
47
48
49
50
51 public RestRequestStepFactory()
52 {
53 super( RESTREQUEST_TYPE, "REST Test Request", "Submits a REST-style Request and validates its response",
54 "/rest_request.gif" );
55 }
56
57 public static class ItemDeletedException extends Exception
58 {
59
60 }
61
62 public WsdlTestStep buildTestStep( WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest )
63 {
64 try
65 {
66 return new RestTestRequestStep( testCase, config, forLoadTest );
67 }
68 catch( ItemDeletedException e )
69 {
70 return null;
71 }
72 }
73
74 public static TestStepConfig createConfig( RestRequest request, String stepName )
75 {
76 RestRequestStepConfig requestStepConfig = RestRequestStepConfig.Factory.newInstance();
77
78 requestStepConfig.setService( request.getOperation().getInterface().getName() );
79 requestStepConfig.setResourcePath( request.getOperation().getFullPath() );
80 requestStepConfig.setMethodName( request.getRestMethod().getName() );
81 requestStepConfig.addNewRestRequest().set( request.getConfig().copy() );
82
83 TestStepConfig testStep = TestStepConfig.Factory.newInstance();
84 testStep.setType( RESTREQUEST_TYPE );
85 testStep.setConfig( requestStepConfig );
86 testStep.setName( stepName );
87
88 return testStep;
89 }
90
91 public TestStepConfig createNewTestStep( WsdlTestCase testCase, String name )
92 {
93
94 Project project = testCase.getTestSuite().getProject();
95 List<String> options = new ArrayList<String>();
96 TupleList<RestMethod, RestRequest> restMethods = new TupleList<RestMethod, RestRequest>();
97
98 for( int c = 0; c < project.getInterfaceCount(); c++ )
99 {
100 Interface iface = project.getInterfaceAt( c );
101 if( iface instanceof RestService )
102 {
103 List<RestResource> resources = ( ( RestService )iface ).getAllResources();
104
105 for( RestResource resource : resources )
106 {
107
108
109
110 for( RestMethod method : resource.getRestMethodList() )
111 {
112 String methodStr = iface.getName() + " -> " + resource.getPath() + " -> " + method.getName();
113 restMethods.add( method, null );
114 options.add( methodStr );
115
116 for( RestRequest request : method.getRequestList() )
117 {
118 restMethods.add( method, request );
119 options.add( methodStr + " -> " + request.getName() );
120 }
121 }
122 }
123 }
124 }
125
126 if( restMethods.size() == 0 )
127 {
128 UISupport.showErrorMessage( "Missing REST Methods in project" );
129 return null;
130 }
131
132 Object op = UISupport.prompt( "Select REST method to invoke for request", "New RestRequest", options.toArray() );
133 if( op != null )
134 {
135 int ix = options.indexOf( op );
136 if( ix != -1 )
137 {
138 TupleList<RestMethod, RestRequest>.Tuple tuple = restMethods.get( ix );
139
140
141
142
143
144
145
146
147
148 return tuple.getValue2() == null ? createNewTestStep( tuple.getValue1(), name ) : createConfig( tuple
149 .getValue2(), name );
150 }
151 }
152
153 return null;
154 }
155
156 public TestStepConfig createNewTestStep( RestMethod restMethod, String name )
157 {
158 RestRequestStepConfig requestStepConfig = RestRequestStepConfig.Factory.newInstance();
159 RestRequestConfig testRequestConfig = requestStepConfig.addNewRestRequest();
160
161 testRequestConfig.setName( name );
162 testRequestConfig.setEncoding( "UTF-8" );
163
164 if( restMethod != null )
165 {
166 requestStepConfig.setService( restMethod.getInterface().getName() );
167 requestStepConfig.setMethodName( restMethod.getName() );
168 requestStepConfig.setResourcePath( restMethod.getOperation().getFullPath() );
169
170 String[] endpoints = restMethod.getInterface().getEndpoints();
171 if( endpoints.length > 0 )
172 testRequestConfig.setEndpoint( endpoints[0] );
173
174 testRequestConfig.addNewRequest();
175 StringToStringMapConfig parametersConfig = testRequestConfig.addNewParameters();
176
177 for( RestParamProperty property : restMethod.getDefaultParams() )
178 {
179 if( StringUtils.hasContent( property.getDefaultValue() ) )
180 {
181 Entry entry = parametersConfig.addNewEntry();
182 entry.setKey( property.getName() );
183 entry.setValue( property.getDefaultValue() );
184 }
185 }
186 }
187
188 TestStepConfig testStepConfig = TestStepConfig.Factory.newInstance();
189 testStepConfig.setType( RESTREQUEST_TYPE );
190 testStepConfig.setConfig( requestStepConfig );
191 testStepConfig.setName( name );
192
193 return testStepConfig;
194 }
195
196 public boolean canCreate()
197 {
198 return true;
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214 }