View Javadoc

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