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.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  		if( request.getConfig().isSetWsaConfig() )
89  			testRequestConfig.setWsaConfig( ( WsaConfigConfig )request.getConfig().getWsaConfig().copy() );
90  
91  		if( ( CredentialsConfig )request.getConfig().getCredentials() != null )
92  		{
93  			testRequestConfig.setCredentials( ( CredentialsConfig )request.getConfig().getCredentials().copy() );
94  		}
95  
96  		testRequestConfig.setWssPasswordType( request.getConfig().getWssPasswordType() );
97  
98  		TestStepConfig testStep = TestStepConfig.Factory.newInstance();
99  		testStep.setType( REQUEST_TYPE );
100 		testStep.setConfig( requestStepConfig );
101 
102 		return testStep;
103 	}
104 
105 	public static TestStepConfig createConfig( WsdlOperation operation, String stepName )
106 	{
107 		RequestStepConfig requestStepConfig = RequestStepConfig.Factory.newInstance();
108 
109 		requestStepConfig.setInterface( operation.getInterface().getName() );
110 		requestStepConfig.setOperation( operation.getName() );
111 
112 		WsdlRequestConfig testRequestConfig = requestStepConfig.addNewRequest();
113 		testRequestConfig.addNewWsaConfig();
114 
115 		testRequestConfig.setName( stepName );
116 		testRequestConfig.setEncoding( "UTF-8" );
117 		String[] endpoints = operation.getInterface().getEndpoints();
118 		if( endpoints.length > 0 )
119 			testRequestConfig.setEndpoint( endpoints[0] );
120 
121 		String requestContent = operation.createRequest( SoapUI.getSettings().getBoolean(
122 				WsdlSettings.XML_GENERATION_ALWAYS_INCLUDE_OPTIONAL_ELEMENTS ) );
123 		testRequestConfig.addNewRequest().setStringValue( requestContent );
124 
125 		// add ws-a action
126 		String defaultAction = WsdlUtils.getDefaultWsaAction( operation, false );
127 		if( StringUtils.hasContent( defaultAction ) )
128 			testRequestConfig.getWsaConfig().setAction( defaultAction );
129 
130 		TestStepConfig testStep = TestStepConfig.Factory.newInstance();
131 		testStep.setType( REQUEST_TYPE );
132 		testStep.setConfig( requestStepConfig );
133 
134 		return testStep;
135 	}
136 
137 	public TestStepConfig createNewTestStep( WsdlTestCase testCase, String name )
138 	{
139 		// build list of available interfaces / operations
140 		Project project = testCase.getTestSuite().getProject();
141 		List<String> options = new ArrayList<String>();
142 		List<Operation> operations = new ArrayList<Operation>();
143 
144 		for( int c = 0; c < project.getInterfaceCount(); c++ )
145 		{
146 			Interface iface = project.getInterfaceAt( c );
147 			for( int i = 0; i < iface.getOperationCount(); i++ )
148 			{
149 				options.add( iface.getName() + " -> " + iface.getOperationAt( i ).getName() );
150 				operations.add( iface.getOperationAt( i ) );
151 			}
152 		}
153 
154 		Object op = UISupport.prompt( "Select operation to invoke for request", "New TestRequest", options.toArray() );
155 		if( op != null )
156 		{
157 			int ix = options.indexOf( op );
158 			if( ix != -1 )
159 			{
160 				WsdlOperation operation = ( WsdlOperation )operations.get( ix );
161 
162 				if( dialog == null )
163 					buildDialog();
164 
165 				dialogValues.put( STEP_NAME, name );
166 				dialogValues = dialog.show( dialogValues );
167 				if( dialog.getReturnValue() != XFormDialog.OK_OPTION )
168 					return null;
169 
170 				return createNewTestStep( operation, dialogValues );
171 			}
172 		}
173 
174 		return null;
175 	}
176 
177 	public TestStepConfig createNewTestStep( WsdlOperation operation, StringToStringMap values )
178 	{
179 		String name;
180 		name = values.get( STEP_NAME );
181 
182 		String requestContent = operation.createRequest( values.getBoolean( CREATE_OPTIONAL_ELEMENTS_IN_REQUEST ) );
183 
184 		RequestStepConfig requestStepConfig = RequestStepConfig.Factory.newInstance();
185 
186 		requestStepConfig.setInterface( operation.getInterface().getName() );
187 		requestStepConfig.setOperation( operation.getName() );
188 
189 		WsdlRequestConfig testRequestConfig = requestStepConfig.addNewRequest();
190 
191 		testRequestConfig.setName( name );
192 		testRequestConfig.setEncoding( "UTF-8" );
193 		String[] endpoints = operation.getInterface().getEndpoints();
194 		if( endpoints.length > 0 )
195 			testRequestConfig.setEndpoint( endpoints[0] );
196 		testRequestConfig.addNewRequest().setStringValue( requestContent );
197 
198 		if( values.getBoolean( ADD_SOAP_RESPONSE_ASSERTION ) )
199 		{
200 			TestAssertionConfig assertionConfig = testRequestConfig.addNewAssertion();
201 			assertionConfig.setType( SoapResponseAssertion.ID );
202 		}
203 
204 		if( values.getBoolean( ADD_SCHEMA_ASSERTION ) )
205 		{
206 			TestAssertionConfig assertionConfig = testRequestConfig.addNewAssertion();
207 			assertionConfig.setType( SchemaComplianceAssertion.ID );
208 		}
209 
210 		if( values.getBoolean( ADD_SOAP_FAULT_ASSERTION ) )
211 		{
212 			TestAssertionConfig assertionConfig = testRequestConfig.addNewAssertion();
213 			assertionConfig.setType( NotSoapFaultAssertion.ID );
214 		}
215 
216 		TestStepConfig testStep = TestStepConfig.Factory.newInstance();
217 		testStep.setType( REQUEST_TYPE );
218 		testStep.setConfig( requestStepConfig );
219 		testStep.setName( name );
220 
221 		return testStep;
222 	}
223 
224 	public boolean canCreate()
225 	{
226 		return true;
227 	}
228 
229 	private void buildDialog()
230 	{
231 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Add Request to TestCase" );
232 		XForm mainForm = builder.createForm( "Basic" );
233 
234 		mainForm.addTextField( STEP_NAME, "Name of TestStep", XForm.FieldType.URL ).setWidth( 30 );
235 
236 		mainForm.addCheckBox( ADD_SOAP_RESPONSE_ASSERTION, "(adds validation that response is a SOAP message)" );
237 		mainForm.addCheckBox( ADD_SCHEMA_ASSERTION, "(adds validation that response complies with its schema)" );
238 		mainForm.addCheckBox( ADD_SOAP_FAULT_ASSERTION, "(adds validation that response is not a SOAP Fault)" );
239 		mainForm.addCheckBox( CREATE_OPTIONAL_ELEMENTS_IN_REQUEST, "(creates optional content i sample request)" );
240 
241 		dialog = builder.buildDialog( builder.buildOkCancelActions(),
242 				"Specify options for adding a new request to a TestCase", UISupport.OPTIONS_ICON );
243 
244 		dialogValues.put( ADD_SOAP_RESPONSE_ASSERTION, Boolean.TRUE.toString() );
245 	}
246 }