View Javadoc

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