1 package com.eviware.soapui.impl.wsdl.teststeps.registry;
2
3 import java.util.ArrayList;
4
5 import com.eviware.soapui.config.AsyncResponseStepConfig;
6 import com.eviware.soapui.config.MockResponseConfig;
7 import com.eviware.soapui.config.TestStepConfig;
8 import com.eviware.soapui.impl.WsdlInterfaceFactory;
9 import com.eviware.soapui.impl.support.AbstractInterface;
10 import com.eviware.soapui.impl.wsdl.WsdlInterface;
11 import com.eviware.soapui.impl.wsdl.WsdlOperation;
12 import com.eviware.soapui.impl.wsdl.WsdlProject;
13 import com.eviware.soapui.impl.wsdl.WsdlRequest;
14 import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
15 import com.eviware.soapui.impl.wsdl.support.CompressedStringSupport;
16 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
17 import com.eviware.soapui.impl.wsdl.teststeps.WsdlAsyncResponseTestStep;
18 import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
19 import com.eviware.soapui.model.iface.Interface;
20 import com.eviware.soapui.model.util.ModelItemNames;
21 import com.eviware.soapui.settings.WsdlSettings;
22 import com.eviware.soapui.support.UISupport;
23 import com.eviware.x.form.XFormDialog;
24 import com.eviware.x.form.XFormField;
25 import com.eviware.x.form.XFormFieldListener;
26 import com.eviware.x.form.support.ADialogBuilder;
27 import com.eviware.x.form.support.AField;
28 import com.eviware.x.form.support.AForm;
29 import com.eviware.x.form.support.AField.AFieldType;
30
31
32 public class AsyncResponseStepFactory extends WsdlTestStepFactory
33 {
34 public static final String ASYNC_RESPONSE_TYPE = "asyncresponse";
35
36 private static XFormDialog form;
37 private static WsdlProject project;
38
39 @AForm(name="New Asynchronous Response Step", description="Set options for the Asynchronous Response step")
40 private class CreateForm
41 {
42 @AField(name="Name", description="The name of the Asynchronous Response step", type=AFieldType.STRING)
43 public static final String NAME = "Name";
44
45 @AField(name="Operation", description="Specifies the operation to mock", type=AFieldType.ENUMERATION)
46 public static final String OPERATION = "Operation";
47
48 @AField(name="Interface", description="Specifies the interface containing the operation to be mocked", type=AFieldType.ENUMERATION)
49 public static final String INTERFACE = "Interface";
50
51 @AField(name="Create Response", description="Specifies if a mock response is to be created from the scema", type=AFieldType.BOOLEAN)
52 public static final String CREATE_RESPONSE = "Create Response";
53
54 @AField(name="Port", description="Specifies the port to listen on", type=AFieldType.STRING)
55 public static final String PORT = "Port";
56
57 @AField(name="Path", description="Specifies the path to listen on", type=AFieldType.STRING)
58 public static final String PATH = "Path";
59
60 @AField(name="Request Query", description="Specifies the XPath query to use when extracting information from the incoming request", type=AFieldType.STRING)
61 public static final String REQUEST_QUERY = "Request Query";
62
63 @AField(name="Matching Value", description="Specifies the value/property that must match the result of the request query", type=AFieldType.STRING)
64 public static final String MATCHING_VALUE = "Matching Value";
65 }
66
67 public AsyncResponseStepFactory()
68 {
69 super(ASYNC_RESPONSE_TYPE, "Asynchronous Response",
70 "Responds to an asynchronous call based on XPath expressions",
71 "/asyncResponseStep.gif");
72 }
73
74 public WsdlTestStep buildTestStep(
75 WsdlTestCase testcase, TestStepConfig config, boolean flag)
76 {
77 return new WsdlAsyncResponseTestStep(testcase, config, flag);
78 }
79
80 public TestStepConfig createNewTestStep(WsdlTestCase testCase, String name)
81 {
82 createForm();
83
84 return createConfig(testCase.getTestSuite().getProject(), name);
85 }
86
87 private static void createForm()
88 {
89 if (form == null)
90 {
91 form = ADialogBuilder.buildDialog(AsyncResponseStepFactory.CreateForm.class);
92 form.getFormField(CreateForm.INTERFACE).addFormFieldListener(new XFormFieldListener()
93 {
94 public void valueChanged(XFormField sourceField, String newValue, String oldValue)
95 {
96 WsdlInterface wsdlinterface = (WsdlInterface) project.getInterfaceByName(newValue);
97 form.setOptions(CreateForm.OPERATION,
98 (new ModelItemNames(wsdlinterface.getOperationList())).getNames());
99 }
100
101 });
102
103 form.setBooleanValue(CreateForm.CREATE_RESPONSE, true);
104 form.setValue(CreateForm.PATH, "/");
105 form.setValue(CreateForm.REQUEST_QUERY, "");
106 form.setValue(CreateForm.MATCHING_VALUE, "");
107 }
108 }
109
110 private static TestStepConfig createConfig(WsdlProject wsdlProject, String name)
111 {
112 project = wsdlProject;
113
114 ArrayList<WsdlInterface> arraylist = new ArrayList<WsdlInterface>();
115 for(int i = 0; i < project.getInterfaceCount(); i++)
116 {
117 AbstractInterface<?> iface = project.getInterfaceAt(i);
118 if( iface.getInterfaceType().equals( WsdlInterfaceFactory.WSDL_TYPE ) && iface.getOperationCount() > 0)
119 {
120 arraylist.add((WsdlInterface) iface);
121 }
122 }
123
124 if(arraylist.isEmpty())
125 {
126 UISupport.showErrorMessage("Missing Interfaces/Operations to mock");
127 return null;
128 }
129
130 form.setValue(CreateForm.NAME, name);
131 form.setOptions(CreateForm.INTERFACE, (new ModelItemNames<WsdlInterface>(arraylist)).getNames());
132 form.setOptions(CreateForm.OPERATION, (new ModelItemNames(arraylist.get(0).getOperationList())).getNames());
133
134 if(!form.show())
135 {
136 return null;
137 }
138
139 TestStepConfig testStepConfig;
140 (testStepConfig = TestStepConfig.Factory.newInstance()).setType(ASYNC_RESPONSE_TYPE);
141 testStepConfig.setName(form.getValue(CreateForm.NAME));
142
143 AsyncResponseStepConfig config = AsyncResponseStepConfig.Factory.newInstance();
144 config.setInterface(form.getValue(CreateForm.INTERFACE));
145 config.setOperation(form.getValue(CreateForm.OPERATION));
146 config.setPort(form.getIntValue(CreateForm.PORT, 8080));
147 config.setPath(form.getValue(CreateForm.PATH));
148 config.setRequestQuery(form.getValue(CreateForm.REQUEST_QUERY));
149 config.setMatchingValue(form.getValue(CreateForm.MATCHING_VALUE));
150 config.addNewResponse();
151 config.getResponse().addNewResponseContent();
152
153 if (form.getBooleanValue(CreateForm.CREATE_RESPONSE))
154 {
155 WsdlInterface iface = (WsdlInterface) project.getInterfaceByName(config.getInterface());
156 String content = iface.getOperationByName( config.getOperation()).createResponse(project.getSettings().getBoolean(
157 WsdlSettings.XML_GENERATION_ALWAYS_INCLUDE_OPTIONAL_ELEMENTS));
158 CompressedStringSupport.setString(config.getResponse().getResponseContent(), content);
159 }
160
161 testStepConfig.addNewConfig().set(config);
162
163 return testStepConfig;
164 }
165
166 public static TestStepConfig createConfig(WsdlOperation operation, boolean flag)
167 {
168 return createConfig(operation, null, flag);
169 }
170
171 public static TestStepConfig createConfig(WsdlRequest request, boolean flag)
172 {
173 return createConfig(request.getOperation(), request, flag);
174 }
175
176 public static TestStepConfig createConfig(
177 WsdlOperation operation, WsdlRequest request, boolean flag)
178 {
179 if (flag)
180 {
181 createForm();
182
183 form.setValue(CreateForm.INTERFACE, operation.getInterface().getName());
184 form.setValue(CreateForm.OPERATION, operation.getName());
185 form.setBooleanValue(CreateForm.CREATE_RESPONSE, request.getResponse() == null);
186
187 return createConfig(operation.getInterface().getProject(),
188 (new StringBuilder()).append(request.getName()).append(" Response").toString());
189 }
190
191 TestStepConfig testStepConfig = com.eviware.soapui.config.TestStepConfig.Factory.newInstance();
192 testStepConfig.setType(ASYNC_RESPONSE_TYPE);
193 testStepConfig.setName("Asynchronous Response");
194
195 AsyncResponseStepConfig config = AsyncResponseStepConfig.Factory.newInstance();
196 config.setInterface(operation.getInterface().getName());
197 config.setOperation(operation.getName());
198
199 MockResponseConfig mockResponseConfig = config.addNewResponse();
200 mockResponseConfig.addNewResponseContent();
201 if (request != null && request.getResponse() != null)
202 {
203 CompressedStringSupport.setString(mockResponseConfig.getResponseContent(), request.getResponse().getContentAsString());
204 }
205
206 testStepConfig.addNewConfig().set(config);
207
208 return testStepConfig;
209 }
210
211 public static TestStepConfig createNewTestStep(WsdlMockResponse response)
212 {
213 WsdlOperation wsdloperation;
214 if ((wsdloperation = response.getMockOperation().getOperation()) == null)
215 {
216 UISupport.showErrorMessage("Missing operation for this mock response");
217 return null;
218 }
219 else
220 {
221 createForm();
222
223 form.setValue(CreateForm.INTERFACE, wsdloperation.getInterface().getName());
224 form.setValue(CreateForm.OPERATION, wsdloperation.getName());
225 form.setBooleanValue(CreateForm.CREATE_RESPONSE, false);
226 form.setIntValue(CreateForm.PORT, response.getMockOperation().getMockService().getPort());
227 form.setValue(CreateForm.PATH, response.getMockOperation().getMockService().getPath());
228
229
230
231 return createConfig(wsdloperation.getInterface().getProject(),
232 (new StringBuilder()).append(
233 response.getMockOperation().getName()).append(
234 " - ").append(response.getName()).toString());
235 }
236 }
237
238 public boolean canCreate()
239 {
240 return true;
241 }
242 }