View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.SoapUI;
19  import com.eviware.soapui.config.CredentialsConfig;
20  import com.eviware.soapui.config.RequestStepConfig;
21  import com.eviware.soapui.config.TestAssertionConfig;
22  import com.eviware.soapui.config.TestStepConfig;
23  import com.eviware.soapui.config.WsaConfigConfig;
24  import com.eviware.soapui.config.WsdlRequestConfig;
25  import com.eviware.soapui.impl.wsdl.WsdlOperation;
26  import com.eviware.soapui.impl.wsdl.WsdlRequest;
27  import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlUtils;
28  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
29  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
30  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
31  import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.SchemaComplianceAssertion;
32  import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.NotSoapFaultAssertion;
33  import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.SoapResponseAssertion;
34  import com.eviware.soapui.model.iface.Interface;
35  import com.eviware.soapui.model.iface.Operation;
36  import com.eviware.soapui.model.project.Project;
37  import com.eviware.soapui.settings.WsdlSettings;
38  import com.eviware.soapui.support.StringUtils;
39  import com.eviware.soapui.support.UISupport;
40  import com.eviware.soapui.support.types.StringToStringMap;
41  import com.eviware.x.form.XForm;
42  import com.eviware.x.form.XFormDialog;
43  import com.eviware.x.form.XFormDialogBuilder;
44  import com.eviware.x.form.XFormFactory;
45  
46  /***
47   * Factory for WsdlTestRequestSteps
48   * 
49   * @author Ole.Matzura
50   */
51  
52  public class WsdlTestRequestStepFactory extends WsdlTestStepFactory
53  {
54  	public static final String REQUEST_TYPE = "request";
55  	private static final String CREATE_OPTIONAL_ELEMENTS_IN_REQUEST = "Create optional elements";
56  	private static final String ADD_SOAP_RESPONSE_ASSERTION = "Add SOAP Response Assertion";
57  	private static final String ADD_SOAP_FAULT_ASSERTION = "Add Not SOAP Fault Assertion";
58  	private static final String ADD_SCHEMA_ASSERTION = "Add Schema Assertion";
59  	public static final String STEP_NAME = "Name";
60  	private XFormDialog dialog;
61  	private StringToStringMap dialogValues = new StringToStringMap();
62  
63  	public WsdlTestRequestStepFactory()
64  	{
65  		super( REQUEST_TYPE, "Test Request", "Submits a request and validates its response", "/request.gif" );
66  	}
67  
68  	public WsdlTestStep buildTestStep( WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest )
69  	{
70  		return new WsdlTestRequestStep( testCase, config, forLoadTest );
71  	}
72  
73  	public static TestStepConfig createConfig( WsdlRequest request, String stepName )
74  	{
75  		RequestStepConfig requestStepConfig = RequestStepConfig.Factory.newInstance();
76  
77  		requestStepConfig.setInterface( request.getOperation().getInterface().getName() );
78  		requestStepConfig.setOperation( request.getOperation().getName() );
79  
80  		WsdlRequestConfig testRequestConfig = requestStepConfig.addNewRequest();
81  
82  		testRequestConfig.setName( stepName );
83  		testRequestConfig.setEncoding( request.getEncoding() );
84  		testRequestConfig.setEndpoint( request.getEndpoint() );
85  		testRequestConfig.addNewRequest().setStringValue( request.getRequestContent() );
86  		testRequestConfig.setOutgoingWss( request.getOutgoingWss() );
87  		testRequestConfig.setIncomingWss( request.getIncomingWss() );
88  		testRequestConfig.setTimeout(request.getTimeout());
89  		
90  		if( request.getConfig().isSetWsaConfig() )
91  			testRequestConfig.setWsaConfig( ( WsaConfigConfig )request.getConfig().getWsaConfig().copy() );
92  
93  		if( ( CredentialsConfig )request.getConfig().getCredentials() != null )
94  		{
95  			testRequestConfig.setCredentials( ( CredentialsConfig )request.getConfig().getCredentials().copy() );
96  		}
97  
98  		testRequestConfig.setWssPasswordType( request.getConfig().getWssPasswordType() );
99  
100 		TestStepConfig testStep = TestStepConfig.Factory.newInstance();
101 		testStep.setType( REQUEST_TYPE );
102 		testStep.setConfig( requestStepConfig );
103 
104 		return testStep;
105 	}
106 
107 	public static TestStepConfig createConfig( WsdlOperation operation, String stepName )
108 	{
109 		RequestStepConfig requestStepConfig = RequestStepConfig.Factory.newInstance();
110 
111 		requestStepConfig.setInterface( operation.getInterface().getName() );
112 		requestStepConfig.setOperation( operation.getName() );
113 
114 		WsdlRequestConfig testRequestConfig = requestStepConfig.addNewRequest();
115 		testRequestConfig.addNewWsaConfig();
116 
117 		testRequestConfig.setName( stepName );
118 		testRequestConfig.setEncoding( "UTF-8" );
119 		String[] endpoints = operation.getInterface().getEndpoints();
120 		if( endpoints.length > 0 )
121 			testRequestConfig.setEndpoint( endpoints[0] );
122 
123 		String requestContent = operation.createRequest( SoapUI.getSettings().getBoolean(
124 				WsdlSettings.XML_GENERATION_ALWAYS_INCLUDE_OPTIONAL_ELEMENTS ) );
125 		testRequestConfig.addNewRequest().setStringValue( requestContent );
126 
127 		// add ws-a action
128 		String defaultAction = WsdlUtils.getDefaultWsaAction( operation, false );
129 		if( StringUtils.hasContent( defaultAction ) )
130 			testRequestConfig.getWsaConfig().setAction( defaultAction );
131 
132 		TestStepConfig testStep = TestStepConfig.Factory.newInstance();
133 		testStep.setType( REQUEST_TYPE );
134 		testStep.setConfig( requestStepConfig );
135 
136 		return testStep;
137 	}
138 
139 	public TestStepConfig createNewTestStep( WsdlTestCase testCase, String name )
140 	{
141 		// build list of available interfaces / operations
142 		Project project = testCase.getTestSuite().getProject();
143 		List<String> options = new ArrayList<String>();
144 		List<Operation> operations = new ArrayList<Operation>();
145 
146 		for( int c = 0; c < project.getInterfaceCount(); c++ )
147 		{
148 			Interface iface = project.getInterfaceAt( c );
149 			for( int i = 0; i < iface.getOperationCount(); i++ )
150 			{
151 				options.add( iface.getName() + " -> " + iface.getOperationAt( i ).getName() );
152 				operations.add( iface.getOperationAt( i ) );
153 			}
154 		}
155 
156 		Object op = UISupport.prompt( "Select operation to invoke for request", "New TestRequest", options.toArray() );
157 		if( op != null )
158 		{
159 			int ix = options.indexOf( op );
160 			if( ix != -1 )
161 			{
162 				WsdlOperation operation = ( WsdlOperation )operations.get( ix );
163 
164 				if( dialog == null )
165 					buildDialog();
166 
167 				dialogValues.put( STEP_NAME, name );
168 				dialogValues = dialog.show( dialogValues );
169 				if( dialog.getReturnValue() != XFormDialog.OK_OPTION )
170 					return null;
171 
172 				return createNewTestStep( operation, dialogValues );
173 			}
174 		}
175 
176 		return null;
177 	}
178 
179 	public TestStepConfig createNewTestStep( WsdlOperation operation, StringToStringMap values )
180 	{
181 		String name;
182 		name = values.get( STEP_NAME );
183 
184 		String requestContent = operation.createRequest( values.getBoolean( CREATE_OPTIONAL_ELEMENTS_IN_REQUEST ) );
185 
186 		RequestStepConfig requestStepConfig = RequestStepConfig.Factory.newInstance();
187 
188 		requestStepConfig.setInterface( operation.getInterface().getName() );
189 		requestStepConfig.setOperation( operation.getName() );
190 
191 		WsdlRequestConfig testRequestConfig = requestStepConfig.addNewRequest();
192 
193 		testRequestConfig.setName( name );
194 		testRequestConfig.setEncoding( "UTF-8" );
195 		String[] endpoints = operation.getInterface().getEndpoints();
196 		if( endpoints.length > 0 )
197 			testRequestConfig.setEndpoint( endpoints[0] );
198 		testRequestConfig.addNewRequest().setStringValue( requestContent );
199 
200 		if( values.getBoolean( ADD_SOAP_RESPONSE_ASSERTION ) )
201 		{
202 			TestAssertionConfig assertionConfig = testRequestConfig.addNewAssertion();
203 			assertionConfig.setType( SoapResponseAssertion.ID );
204 		}
205 
206 		if( values.getBoolean( ADD_SCHEMA_ASSERTION ) )
207 		{
208 			TestAssertionConfig assertionConfig = testRequestConfig.addNewAssertion();
209 			assertionConfig.setType( SchemaComplianceAssertion.ID );
210 		}
211 
212 		if( values.getBoolean( ADD_SOAP_FAULT_ASSERTION ) )
213 		{
214 			TestAssertionConfig assertionConfig = testRequestConfig.addNewAssertion();
215 			assertionConfig.setType( NotSoapFaultAssertion.ID );
216 		}
217 
218 		TestStepConfig testStep = TestStepConfig.Factory.newInstance();
219 		testStep.setType( REQUEST_TYPE );
220 		testStep.setConfig( requestStepConfig );
221 		testStep.setName( name );
222 
223 		return testStep;
224 	}
225 
226 	public boolean canCreate()
227 	{
228 		return true;
229 	}
230 
231 	private void buildDialog()
232 	{
233 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Add Request to TestCase" );
234 		XForm mainForm = builder.createForm( "Basic" );
235 
236 		mainForm.addTextField( STEP_NAME, "Name of TestStep", XForm.FieldType.URL ).setWidth( 30 );
237 
238 		mainForm.addCheckBox( ADD_SOAP_RESPONSE_ASSERTION, "(adds validation that response is a SOAP message)" );
239 		mainForm.addCheckBox( ADD_SCHEMA_ASSERTION, "(adds validation that response complies with its schema)" );
240 		mainForm.addCheckBox( ADD_SOAP_FAULT_ASSERTION, "(adds validation that response is not a SOAP Fault)" );
241 		mainForm.addCheckBox( CREATE_OPTIONAL_ELEMENTS_IN_REQUEST, "(creates optional content in sample request)" );
242 
243 		dialog = builder.buildDialog( builder.buildOkCancelActions(),
244 				"Specify options for adding a new request to a TestCase", UISupport.OPTIONS_ICON );
245 
246 		dialogValues.put( ADD_SOAP_RESPONSE_ASSERTION, Boolean.TRUE.toString() );
247 	}
248 }