View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.actions.mockresponse;
14  
15  import com.eviware.soapui.impl.wsdl.WsdlProject;
16  import com.eviware.soapui.impl.wsdl.actions.support.AbstractAddToTestCaseAction;
17  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
18  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
19  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
20  import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.SchemaComplianceAssertion;
21  import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.NotSoapFaultAssertion;
22  import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.SoapResponseAssertion;
23  import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory;
24  import com.eviware.soapui.model.support.ModelSupport;
25  import com.eviware.soapui.support.UISupport;
26  import com.eviware.soapui.support.types.StringToStringMap;
27  import com.eviware.x.form.XForm;
28  import com.eviware.x.form.XFormDialog;
29  import com.eviware.x.form.XFormDialogBuilder;
30  import com.eviware.x.form.XFormFactory;
31  
32  /***
33   * Adds a WsdlRequest to a WsdlTestCase as a WsdlTestRequestStep
34   * 
35   * @author Ole.Matzura
36   */
37  
38  public class CreateTestRequestForMockResponseAction extends AbstractAddToTestCaseAction<WsdlMockResponse>
39  {
40  	public static final String SOAPUI_ACTION_ID = "CreateTestRequestForMockResponseAction";
41  
42  	private static final String STEP_NAME = "Name";
43  	private static final String ADD_SOAP_FAULT_ASSERTION = "Add Not SOAP Fault Assertion";
44  	private static final String ADD_SOAP_RESPONSE_ASSERTION = "Add SOAP Response Assertion";
45  	private static final String ADD_SCHEMA_ASSERTION = "Add Schema Assertion";
46  
47  	private XFormDialog dialog;
48  	private StringToStringMap dialogValues = new StringToStringMap();
49  
50  	public CreateTestRequestForMockResponseAction()
51  	{
52  		super( "Create TestRequest", "Creates a TestRequest for this MockResponse in a TestCase" );
53  	}
54  
55  	public void perform( WsdlMockResponse mockResponse, Object param )
56  	{
57  		WsdlProject project = ( WsdlProject )ModelSupport.getModelItemProject( mockResponse );
58  
59  		WsdlTestCase testCase = getTargetTestCase( project );
60  		if( testCase != null )
61  			addRequest( testCase, mockResponse, -1 );
62  	}
63  
64  	public WsdlTestRequestStep addRequest( WsdlTestCase testCase, WsdlMockResponse mockResponse, int position )
65  	{
66  		if( dialog == null )
67  			buildDialog();
68  
69  		dialogValues.put( STEP_NAME, mockResponse.getMockOperation().getName() + " - " + mockResponse.getName() );
70  
71  		boolean enabled = mockResponse.getMockOperation().getOperation().isBidirectional();
72  		dialog.getFormField( ADD_SCHEMA_ASSERTION ).setEnabled( enabled );
73  		dialog.getFormField( ADD_SOAP_FAULT_ASSERTION ).setEnabled( enabled );
74  		dialog.getFormField( ADD_SOAP_RESPONSE_ASSERTION ).setEnabled( enabled );
75  
76  		dialogValues = dialog.show( dialogValues );
77  		if( dialog.getReturnValue() != XFormDialog.OK_OPTION )
78  			return null;
79  		;
80  
81  		String name = dialogValues.get( STEP_NAME );
82  
83  		WsdlTestRequestStep testStep = ( WsdlTestRequestStep )testCase.insertTestStep( WsdlTestRequestStepFactory
84  				.createConfig( mockResponse.getMockOperation().getOperation(), name ), position );
85  
86  		if( testStep == null )
87  			return null;
88  
89  		if( enabled )
90  		{
91  			if( dialogValues.getBoolean( ADD_SOAP_RESPONSE_ASSERTION ) )
92  				testStep.getTestRequest().addAssertion( SoapResponseAssertion.ID );
93  
94  			if( dialogValues.getBoolean( ADD_SCHEMA_ASSERTION ) )
95  				testStep.getTestRequest().addAssertion( SchemaComplianceAssertion.ID );
96  
97  			if( dialogValues.getBoolean( ADD_SOAP_FAULT_ASSERTION ) )
98  				testStep.getTestRequest().addAssertion( NotSoapFaultAssertion.LABEL );
99  		}
100 
101 		testStep.getTestRequest().setEndpoint( mockResponse.getMockOperation().getMockService().getLocalEndpoint() );
102 
103 		UISupport.selectAndShow( testStep );
104 
105 		return testStep;
106 	}
107 
108 	private void buildDialog()
109 	{
110 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Create TestRequest" );
111 		XForm mainForm = builder.createForm( "Basic" );
112 
113 		mainForm.addTextField( STEP_NAME, "Name of TestRequest Step", XForm.FieldType.URL ).setWidth( 30 );
114 
115 		mainForm.addCheckBox( ADD_SOAP_RESPONSE_ASSERTION, "(adds validation that response is a SOAP message)" );
116 		mainForm.addCheckBox( ADD_SCHEMA_ASSERTION, "(adds validation that response complies with its schema)" );
117 		mainForm.addCheckBox( ADD_SOAP_FAULT_ASSERTION, "(adds validation that response is not a SOAP Fault)" );
118 
119 		dialog = builder.buildDialog( builder.buildOkCancelActions(), "Specify options for creating the TestRequest",
120 				UISupport.OPTIONS_ICON );
121 
122 		dialogValues.put( ADD_SOAP_RESPONSE_ASSERTION, Boolean.TRUE.toString() );
123 	}
124 }