View Javadoc

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