View Javadoc

1   /*
2    * soapUI, copyright (C) 2004-2008 eviware.com
3    *
4    * soapUI is free software; you can redistribute it and/or modify it under the
5    * terms of version 2.1 of the GNU Lesser General Public License as published by
6    * the Free Software Foundation.
7    *
8    * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9    * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   * See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  /*
14   *  soapUI Pro, copyright (C) 2007-2008 eviware software ab 
15   */
16  
17  package com.eviware.soapui.impl.wsdl.teststeps.registry;
18  
19  import com.eviware.soapui.config.MockResponseConfig;
20  import com.eviware.soapui.config.MockResponseStepConfig;
21  import com.eviware.soapui.config.TestStepConfig;
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.AField.AFieldType;
43  import com.eviware.x.form.support.AForm;
44  
45  import java.util.ArrayList;
46  import java.util.List;
47  
48  /***
49   * Factory for creation TransferValue steps
50   *
51   * @author Ole.Matzura
52   */
53  public class WsdlMockResponseStepFactory extends WsdlTestStepFactory
54  {
55  	public static final String MOCKRESPONSE_TYPE = "mockresponse";
56  	private static XFormDialog dialog;
57  	private static WsdlProject project;
58  
59  	public WsdlMockResponseStepFactory()
60  	{
61  		super( MOCKRESPONSE_TYPE, "Mock Response", "Waits for a request and returns the specified response", "/mockResponseStep.gif" );
62  	}
63  
64  	public WsdlTestStep buildTestStep(WsdlTestCase testCase,	TestStepConfig config, boolean forLoadTest )
65  	{
66  		return new WsdlMockResponseTestStep( testCase, config, forLoadTest );
67  	}
68  
69  	public TestStepConfig createNewTestStep(WsdlTestCase testCase, String name )
70  	{
71  		ensureDialog();
72  
73  		return createFromDialog( testCase.getTestSuite().getProject(), name );
74  	}
75  
76  	private static void ensureDialog()
77  	{
78  		if( dialog == null )
79  		{
80  			dialog = ADialogBuilder.buildDialog( CreateForm.class );
81  			dialog.getFormField( CreateForm.INTERFACE ).addFormFieldListener( new XFormFieldListener() {
82  
83  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
84  				{
85  					WsdlInterface iface = (WsdlInterface) project.getInterfaceByName( newValue );
86  					dialog.setOptions( CreateForm.OPERATION,
87  								new ModelItemNames<Operation>( iface.getOperationList() ).getNames() );
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  		List<Interface> interfaces = new ArrayList<Interface>();
100 		for( int c = 0; c < project.getInterfaceCount(); c++ )
101 		{
102 			if( project.getInterfaceAt( c ).getOperationCount() > 0 )
103 				interfaces.add( project.getInterfaceAt( c ));
104 		}
105 
106 		if( interfaces.isEmpty())
107 		{
108 			UISupport.showErrorMessage( "Missing Interfaces/Operations to mock" );
109 			return null;
110 		}
111 
112 		dialog.setValue( CreateForm.NAME, name );
113 		dialog.setOptions( CreateForm.INTERFACE, new ModelItemNames<Interface>( interfaces ).getNames() );
114 		dialog.setOptions( CreateForm.OPERATION,
115 					new ModelItemNames<Operation>( interfaces.get( 0 ).getOperationList() ).getNames() );
116 
117 		if( !dialog.show() )
118 			return null;
119 
120 		TestStepConfig testStepConfig = TestStepConfig.Factory.newInstance();
121       testStepConfig.setType( MOCKRESPONSE_TYPE );
122       testStepConfig.setName( dialog.getValue( CreateForm.NAME ) );
123 
124       MockResponseStepConfig config = MockResponseStepConfig.Factory.newInstance();
125       config.setInterface( dialog.getValue( CreateForm.INTERFACE ) );
126       config.setOperation( dialog.getValue( CreateForm.OPERATION ) );
127       config.setPort( dialog.getIntValue( CreateForm.PORT, 8080 ) );
128       config.setPath( dialog.getValue( CreateForm.PATH ) );
129       config.addNewResponse();
130       config.getResponse().addNewResponseContent();
131 
132       if( dialog.getBooleanValue( CreateForm.CREATE_RESPONSE ))
133       {
134       	WsdlInterface iface = (WsdlInterface) project.getInterfaceByName( config.getInterface() );
135 			String response = iface.getOperationByName( config.getOperation() ).createResponse(
136       					project.getSettings().getBoolean( WsdlSettings.XML_GENERATION_ALWAYS_INCLUDE_OPTIONAL_ELEMENTS ) );
137 
138       	CompressedStringSupport.setString( config.getResponse().getResponseContent(), response );
139       }
140 
141       testStepConfig.addNewConfig().set( config );
142 
143       return testStepConfig;
144 	}
145 
146 	@AForm(description = "Secify options for new MockResponse step", name = "New MockResponse Step",
147 				helpUrl= HelpUrls.CREATEMOCKRESPONSESTEP_HELP_URL, icon=UISupport.OPTIONS_ICON_PATH)
148 	private class CreateForm
149 	{
150 		@AField(description = "The name of the MockResponse step", name = "Name", type = AFieldType.STRING)
151 		public static final String NAME = "Name";
152 
153 		@AField(description = "Specifies the operation to be mocked", name = "Operation", type = AFieldType.ENUMERATION)
154 		public final static String OPERATION = "Operation";
155 
156 		@AField(description = "Specifies the interface containing the operation to be mocked", name = "Interface", type = AFieldType.ENUMERATION)
157 		public final static String INTERFACE = "Interface";
158 
159 		@AField(description = "Specifies if a mock response is to be created from the schema",
160 					name = "Create Response", type = AFieldType.BOOLEAN )
161 		public final static String CREATE_RESPONSE = "Create Response";
162 
163 		@AField(description = "Specifies the port to listen on", name = "Port", type = AFieldType.INT)
164 		public final static String PORT = "Port";
165 
166 		@AField(description = "Specifies the path to listen on", name = "Path")
167 		public final static String PATH = "Path";
168 	}
169 
170 	public static TestStepConfig createConfig( WsdlOperation operation, boolean interactive )
171 	{
172 		return createConfig( operation, null, interactive );
173 	}
174 
175 	public static TestStepConfig createConfig( WsdlRequest request, boolean interactive )
176 	{
177 		return createConfig( request.getOperation(), request, interactive );
178 	}
179 
180 	public static TestStepConfig createConfig( WsdlOperation operation, WsdlRequest request, boolean interactive )
181 	{
182 		if( interactive )
183 		{
184 			ensureDialog();
185 
186 			dialog.setValue( CreateForm.INTERFACE, operation.getInterface().getName() );
187 			dialog.setValue( CreateForm.OPERATION, operation.getName() );
188 			dialog.setBooleanValue( CreateForm.CREATE_RESPONSE, request.getResponse() == null );
189 
190 			return createFromDialog( operation.getInterface().getProject(), request.getName() + " Response" );
191 		}
192 		else
193 		{
194 			TestStepConfig testStepConfig = TestStepConfig.Factory.newInstance();
195 	      testStepConfig.setType( MOCKRESPONSE_TYPE );
196 	      testStepConfig.setName( "Mock Response" );
197 
198 	      MockResponseStepConfig config = MockResponseStepConfig.Factory.newInstance();
199 	      config.setInterface( operation.getInterface().getName() );
200 	      config.setOperation( operation.getName() );
201 	      MockResponseConfig response = config.addNewResponse();
202 	      response.addNewResponseContent();
203 
204 	      if( request != null && request.getResponse() != null )
205 	      {
206 	       	CompressedStringSupport.setString( response.getResponseContent(), request.getResponse().getContentAsString() );
207 	      }
208 
209 	      testStepConfig.addNewConfig().set( config );
210 
211 	      return testStepConfig;
212 		}
213 	}
214 
215 	public static TestStepConfig createNewTestStep( WsdlMockResponse mockResponse)
216 	{
217 		WsdlOperation operation = mockResponse.getMockOperation().getOperation();
218 		if( operation == null )
219 		{
220 			UISupport.showErrorMessage( "Missing operation for this mock response" );
221 			return null;
222 		}
223 
224 		ensureDialog();
225 
226 		dialog.setValue( CreateForm.INTERFACE, operation.getInterface().getName() );
227 		dialog.setValue( CreateForm.OPERATION, operation.getName() );
228 		dialog.setBooleanValue( CreateForm.CREATE_RESPONSE, false );
229 		dialog.setIntValue( CreateForm.PORT, mockResponse.getMockOperation().getMockService().getPort() );
230 		dialog.setValue( CreateForm.PATH, mockResponse.getMockOperation().getMockService().getPath() );
231 
232 		return createFromDialog( operation.getInterface().getProject(),
233 					mockResponse.getMockOperation().getName() + " - " + mockResponse.getName() );
234 	}
235 
236 	public boolean canCreate()
237 	{
238 		return true;
239 	}
240 }