View Javadoc

1   /*
2    * soapUI, copyright (C) 2004-2009 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  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  		List<Interface> interfaces = new ArrayList<Interface>();
100 		for( Interface iface : project.getInterfaces( WsdlInterfaceFactory.WSDL_TYPE ) )
101 		{
102 			if( iface.getOperationCount() > 0 )
103 				interfaces.add( iface );
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, new ModelItemNames<Operation>( interfaces.get( 0 ).getOperationList() )
115 				.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", helpUrl = HelpUrls.CREATEMOCKRESPONSESTEP_HELP_URL, icon = UISupport.OPTIONS_ICON_PATH )
147 	private class CreateForm
148 	{
149 		@AField( description = "The name of the MockResponse step", name = "Name", type = AFieldType.STRING )
150 		public static final String NAME = "Name";
151 
152 		@AField( description = "Specifies the operation to be mocked", name = "Operation", type = AFieldType.ENUMERATION )
153 		public final static String OPERATION = "Operation";
154 
155 		@AField( description = "Specifies the interface containing the operation to be mocked", name = "Interface", type = AFieldType.ENUMERATION )
156 		public final static String INTERFACE = "Interface";
157 
158 		@AField( description = "Specifies if a mock response is to be created from the schema", name = "Create Response", type = AFieldType.BOOLEAN )
159 		public final static String CREATE_RESPONSE = "Create Response";
160 
161 		@AField( description = "Specifies the port to listen on", name = "Port", type = AFieldType.INT )
162 		public final static String PORT = "Port";
163 
164 		@AField( description = "Specifies the path to listen on", name = "Path" )
165 		public final static String PATH = "Path";
166 	}
167 
168 	public static TestStepConfig createConfig( WsdlOperation operation, boolean interactive )
169 	{
170 		return createConfig( operation, null, interactive );
171 	}
172 
173 	public static TestStepConfig createConfig( WsdlRequest request, boolean interactive )
174 	{
175 		return createConfig( request.getOperation(), request, interactive );
176 	}
177 
178 	public static TestStepConfig createConfig( WsdlOperation operation, WsdlRequest request, boolean interactive )
179 	{
180 		if( interactive )
181 		{
182 			ensureDialog();
183 
184 			dialog.setValue( CreateForm.INTERFACE, operation.getInterface().getName() );
185 			dialog.setValue( CreateForm.OPERATION, operation.getName() );
186 			dialog.setBooleanValue( CreateForm.CREATE_RESPONSE, request.getResponse() == null );
187 
188 			return createFromDialog( operation.getInterface().getProject(), request.getName() + " Response" );
189 		}
190 		else
191 		{
192 			TestStepConfig testStepConfig = TestStepConfig.Factory.newInstance();
193 			testStepConfig.setType( MOCKRESPONSE_TYPE );
194 			testStepConfig.setName( "Mock Response" );
195 
196 			MockResponseStepConfig config = MockResponseStepConfig.Factory.newInstance();
197 			config.setInterface( operation.getInterface().getName() );
198 			config.setOperation( operation.getName() );
199 			MockResponseConfig response = config.addNewResponse();
200 			response.addNewResponseContent();
201 
202 			if( request != null && request.getResponse() != null )
203 			{
204 				CompressedStringSupport.setString( response.getResponseContent(), request.getResponse()
205 						.getContentAsString() );
206 			}
207 
208 			testStepConfig.addNewConfig().set( config );
209 
210 			return testStepConfig;
211 		}
212 	}
213 
214 	public static TestStepConfig createNewTestStep( WsdlMockResponse mockResponse )
215 	{
216 		WsdlOperation operation = mockResponse.getMockOperation().getOperation();
217 		if( operation == null )
218 		{
219 			UISupport.showErrorMessage( "Missing operation for this mock response" );
220 			return null;
221 		}
222 
223 		ensureDialog();
224 
225 		dialog.setValue( CreateForm.INTERFACE, operation.getInterface().getName() );
226 		dialog.setValue( CreateForm.OPERATION, operation.getName() );
227 		dialog.setBooleanValue( CreateForm.CREATE_RESPONSE, false );
228 		dialog.setIntValue( CreateForm.PORT, mockResponse.getMockOperation().getMockService().getPort() );
229 		dialog.setValue( CreateForm.PATH, mockResponse.getMockOperation().getMockService().getPath() );
230 
231 		return createFromDialog( operation.getInterface().getProject(), mockResponse.getMockOperation().getName() + " - "
232 				+ mockResponse.getName() );
233 	}
234 
235 	public boolean canCreate()
236 	{
237 		return true;
238 	}
239 }