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.MockResponseConfig;
19 import com.eviware.soapui.config.MockResponseStepConfig;
20 import com.eviware.soapui.config.TestStepConfig;
21 import com.eviware.soapui.impl.WsdlInterfaceFactory;
22 import com.eviware.soapui.impl.wsdl.WsdlInterface;
23 import com.eviware.soapui.impl.wsdl.WsdlOperation;
24 import com.eviware.soapui.impl.wsdl.WsdlProject;
25 import com.eviware.soapui.impl.wsdl.WsdlRequest;
26 import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
27 import com.eviware.soapui.impl.wsdl.support.CompressedStringSupport;
28 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
29 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
30 import com.eviware.soapui.impl.wsdl.teststeps.WsdlMockResponseTestStep;
31 import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
32 import com.eviware.soapui.model.iface.Interface;
33 import com.eviware.soapui.model.iface.Operation;
34 import com.eviware.soapui.model.util.ModelItemNames;
35 import com.eviware.soapui.settings.WsdlSettings;
36 import com.eviware.soapui.support.UISupport;
37 import com.eviware.x.form.XFormDialog;
38 import com.eviware.x.form.XFormField;
39 import com.eviware.x.form.XFormFieldListener;
40 import com.eviware.x.form.support.ADialogBuilder;
41 import com.eviware.x.form.support.AField;
42 import com.eviware.x.form.support.AForm;
43 import com.eviware.x.form.support.AField.AFieldType;
44
45 /***
46 * Factory for creation TransferValue steps
47 *
48 * @author Ole.Matzura
49 */
50 public class WsdlMockResponseStepFactory extends WsdlTestStepFactory
51 {
52 public static final String MOCKRESPONSE_TYPE = "mockresponse";
53 private static XFormDialog dialog;
54 private static WsdlProject project;
55
56 public WsdlMockResponseStepFactory()
57 {
58 super( MOCKRESPONSE_TYPE, "Mock Response", "Waits for a request and returns the specified response",
59 "/mockResponseStep.gif" );
60 }
61
62 public WsdlTestStep buildTestStep( WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest )
63 {
64 return new WsdlMockResponseTestStep( testCase, config, forLoadTest );
65 }
66
67 public TestStepConfig createNewTestStep( WsdlTestCase testCase, String name )
68 {
69 ensureDialog();
70
71 return createFromDialog( testCase.getTestSuite().getProject(), name );
72 }
73
74 private static void ensureDialog()
75 {
76 if( dialog == null )
77 {
78 dialog = ADialogBuilder.buildDialog( CreateForm.class );
79 dialog.getFormField( CreateForm.INTERFACE ).addFormFieldListener( new XFormFieldListener()
80 {
81
82 public void valueChanged( XFormField sourceField, String newValue, String oldValue )
83 {
84 WsdlInterface iface = ( WsdlInterface )project.getInterfaceByName( newValue );
85 dialog.setOptions( CreateForm.OPERATION, new ModelItemNames<Operation>( iface.getOperationList() )
86 .getNames() );
87 }
88 } );
89
90 dialog.setBooleanValue( CreateForm.CREATE_RESPONSE, true );
91 dialog.setValue( CreateForm.PATH, "/" );
92 }
93 }
94
95 private static TestStepConfig createFromDialog( WsdlProject project, String name )
96 {
97 WsdlMockResponseStepFactory.project = project;
98
99 try
100 {
101 List<Interface> interfaces = new ArrayList<Interface>();
102 for( Interface iface : project.getInterfaces( WsdlInterfaceFactory.WSDL_TYPE ) )
103 {
104 if( iface.getOperationCount() > 0 )
105 interfaces.add( iface );
106 }
107
108 if( interfaces.isEmpty() )
109 {
110 UISupport.showErrorMessage( "Missing Interfaces/Operations to mock" );
111 return null;
112 }
113
114 dialog.setValue( CreateForm.NAME, name );
115 dialog.setOptions( CreateForm.INTERFACE, new ModelItemNames<Interface>( interfaces ).getNames() );
116 dialog.setOptions( CreateForm.OPERATION,
117 new ModelItemNames<Operation>( interfaces.get( 0 ).getOperationList() ).getNames() );
118
119 if( !dialog.show() )
120 return null;
121
122 TestStepConfig testStepConfig = TestStepConfig.Factory.newInstance();
123 testStepConfig.setType( MOCKRESPONSE_TYPE );
124 testStepConfig.setName( dialog.getValue( CreateForm.NAME ) );
125
126 MockResponseStepConfig config = MockResponseStepConfig.Factory.newInstance();
127 config.setInterface( dialog.getValue( CreateForm.INTERFACE ) );
128 config.setOperation( dialog.getValue( CreateForm.OPERATION ) );
129 config.setPort( dialog.getIntValue( CreateForm.PORT, 8080 ) );
130 config.setPath( dialog.getValue( CreateForm.PATH ) );
131 config.addNewResponse();
132 config.getResponse().addNewResponseContent();
133
134 if( dialog.getBooleanValue( CreateForm.CREATE_RESPONSE ) )
135 {
136 WsdlInterface iface = ( WsdlInterface )project.getInterfaceByName( config.getInterface() );
137 String response = iface.getOperationByName( config.getOperation() ).createResponse(
138 project.getSettings().getBoolean( WsdlSettings.XML_GENERATION_ALWAYS_INCLUDE_OPTIONAL_ELEMENTS ) );
139
140 CompressedStringSupport.setString( config.getResponse().getResponseContent(), response );
141 }
142
143 testStepConfig.addNewConfig().set( config );
144 return testStepConfig;
145 }
146 finally
147 {
148 WsdlMockResponseStepFactory.project = null;
149 }
150 }
151
152 @AForm( description = "Secify options for new MockResponse step", name = "New MockResponse Step", helpUrl = HelpUrls.CREATEMOCKRESPONSESTEP_HELP_URL, icon = UISupport.OPTIONS_ICON_PATH )
153 private class CreateForm
154 {
155 @AField( description = "The name of the MockResponse step", name = "Name", type = AFieldType.STRING )
156 public static final String NAME = "Name";
157
158 @AField( description = "Specifies the operation to be mocked", name = "Operation", type = AFieldType.ENUMERATION )
159 public final static String OPERATION = "Operation";
160
161 @AField( description = "Specifies the interface containing the operation to be mocked", name = "Interface", type = AFieldType.ENUMERATION )
162 public final static String INTERFACE = "Interface";
163
164 @AField( description = "Specifies if a mock response is to be created from the schema", name = "Create Response", type = AFieldType.BOOLEAN )
165 public final static String CREATE_RESPONSE = "Create Response";
166
167 @AField( description = "Specifies the port to listen on", name = "Port", type = AFieldType.INT )
168 public final static String PORT = "Port";
169
170 @AField( description = "Specifies the path to listen on", name = "Path" )
171 public final static String PATH = "Path";
172 }
173
174 public static TestStepConfig createConfig( WsdlOperation operation, boolean interactive )
175 {
176 return createConfig( operation, null, interactive );
177 }
178
179 public static TestStepConfig createConfig( WsdlRequest request, boolean interactive )
180 {
181 return createConfig( request.getOperation(), request, interactive );
182 }
183
184 public static TestStepConfig createConfig( WsdlOperation operation, WsdlRequest request, boolean interactive )
185 {
186 if( interactive )
187 {
188 ensureDialog();
189
190 dialog.setValue( CreateForm.INTERFACE, operation.getInterface().getName() );
191 dialog.setValue( CreateForm.OPERATION, operation.getName() );
192 dialog.setBooleanValue( CreateForm.CREATE_RESPONSE, request.getResponse() == null );
193
194 return createFromDialog( operation.getInterface().getProject(), request.getName() + " Response" );
195 }
196 else
197 {
198 TestStepConfig testStepConfig = TestStepConfig.Factory.newInstance();
199 testStepConfig.setType( MOCKRESPONSE_TYPE );
200 testStepConfig.setName( "Mock Response" );
201
202 MockResponseStepConfig config = MockResponseStepConfig.Factory.newInstance();
203 config.setInterface( operation.getInterface().getName() );
204 config.setOperation( operation.getName() );
205 MockResponseConfig response = config.addNewResponse();
206 response.addNewResponseContent();
207
208 if( request != null && request.getResponse() != null )
209 {
210 CompressedStringSupport.setString( response.getResponseContent(), request.getResponse()
211 .getContentAsString() );
212 }
213
214 testStepConfig.addNewConfig().set( config );
215
216 return testStepConfig;
217 }
218 }
219
220 public static TestStepConfig createNewTestStep( WsdlMockResponse mockResponse )
221 {
222 WsdlOperation operation = mockResponse.getMockOperation().getOperation();
223 if( operation == null )
224 {
225 UISupport.showErrorMessage( "Missing operation for this mock response" );
226 return null;
227 }
228
229 ensureDialog();
230
231 dialog.setValue( CreateForm.INTERFACE, operation.getInterface().getName() );
232 dialog.setValue( CreateForm.OPERATION, operation.getName() );
233 dialog.setBooleanValue( CreateForm.CREATE_RESPONSE, false );
234 dialog.setIntValue( CreateForm.PORT, mockResponse.getMockOperation().getMockService().getPort() );
235 dialog.setValue( CreateForm.PATH, mockResponse.getMockOperation().getMockService().getPath() );
236
237 return createFromDialog( operation.getInterface().getProject(), mockResponse.getMockOperation().getName() + " - "
238 + mockResponse.getName() );
239 }
240
241 public boolean canCreate()
242 {
243 return true;
244 }
245 }