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