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