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.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  		String name = dialogValues.get( STEP_NAME );
81  		
82  		WsdlTestRequestStep testStep = (WsdlTestRequestStep) testCase.insertTestStep( 
83  				WsdlTestRequestStepFactory.createConfig( mockResponse.getMockOperation().getOperation(), name ), position );
84  
85  		if( enabled )
86  		{
87  			if( dialogValues.getBoolean( ADD_SOAP_RESPONSE_ASSERTION ))
88  				testStep.getTestRequest().addAssertion( SoapResponseAssertion.ID );
89  			
90  			if( dialogValues.getBoolean( ADD_SCHEMA_ASSERTION ))
91  				testStep.getTestRequest().addAssertion( SchemaComplianceAssertion.ID );
92  			
93  			if( dialogValues.getBoolean( ADD_SOAP_FAULT_ASSERTION ))
94  				testStep.getTestRequest().addAssertion( NotSoapFaultAssertion.LABEL );
95  		}
96  		
97  		testStep.getTestRequest().setEndpoint( mockResponse.getMockOperation().getMockService().getLocalEndpoint() );
98  		
99  		UISupport.selectAndShow( testStep );
100 		
101 		return testStep;
102 	}
103 	
104 	private void buildDialog()
105 	{
106 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder("Create TestRequest");
107 		XForm mainForm = builder.createForm( "Basic" );
108 		
109 		mainForm.addTextField( STEP_NAME, "Name of TestRequest Step", XForm.FieldType.URL ).setWidth( 30 );
110 
111 		mainForm.addCheckBox( ADD_SOAP_RESPONSE_ASSERTION, "(adds validation that response is a SOAP message)" );
112 		mainForm.addCheckBox( ADD_SCHEMA_ASSERTION, "(adds validation that response complies with its schema)" );
113 		mainForm.addCheckBox( ADD_SOAP_FAULT_ASSERTION, "(adds validation that response is not a SOAP Fault)" );
114 		
115 		dialog = builder.buildDialog( builder.buildOkCancelActions(), 
116       		"Specify options for creating the TestRequest", UISupport.OPTIONS_ICON );		
117 		
118 		dialogValues.put( ADD_SOAP_RESPONSE_ASSERTION, Boolean.TRUE.toString() );
119 	}
120 }
121