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