View Javadoc

1   /*
2    *  soapUI Pro, copyright (C) 2007-2008 eviware software ab 
3    */
4   
5   package com.eviware.soapui.impl.wsdl.actions.operation;
6   
7   import com.eviware.soapui.config.MockResponseStepConfig;
8   import com.eviware.soapui.config.TestStepConfig;
9   import com.eviware.soapui.impl.wsdl.WsdlOperation;
10  import com.eviware.soapui.impl.wsdl.actions.support.AbstractAddToTestCaseAction;
11  import com.eviware.soapui.impl.wsdl.support.CompressedStringSupport;
12  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
13  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
14  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMockResponseTestStep;
15  import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.SchemaComplianceAssertion;
16  import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlMockResponseStepFactory;
17  import com.eviware.soapui.settings.WsdlSettings;
18  import com.eviware.soapui.support.UISupport;
19  import com.eviware.x.form.XFormDialog;
20  import com.eviware.x.form.support.ADialogBuilder;
21  import com.eviware.x.form.support.AField;
22  import com.eviware.x.form.support.AField.AFieldType;
23  import com.eviware.x.form.support.AForm;
24  
25  public class AddOperationAsMockResponseStepAction extends AbstractAddToTestCaseAction<WsdlOperation>
26  {
27     private XFormDialog dialog;
28  
29     public AddOperationAsMockResponseStepAction()
30     {
31        super( "Add as MockResponse Step", "Creates a MockResponseStep for this Operation" );
32     }
33  
34     public void perform( WsdlOperation operation, Object param )
35     {
36        WsdlTestCase testCase = getTargetTestCase( operation.getInterface().getProject() );
37        if( testCase != null )
38           addMockResponse( testCase, operation );
39     }
40  
41     protected boolean addMockResponse( WsdlTestCase testCase, WsdlOperation operation )
42     {
43        if( dialog == null )
44        {
45           dialog = ADialogBuilder.buildDialog( Form.class );
46           dialog.setWidth( 450 );
47        }
48  
49        dialog.setValue( Form.STEP_NAME, operation.getName() );
50        dialog.setBooleanValue( Form.SHOW_TESTCASE, true );
51        dialog.setIntValue( Form.PORT, 8181 );
52        dialog.setValue( Form.PATH, "/" + operation.getName() );
53  
54        if( !dialog.show() )
55           return false;
56  
57        TestStepConfig config = WsdlMockResponseStepFactory.createConfig( operation, false );
58        MockResponseStepConfig mockResponseStepConfig = ( (MockResponseStepConfig) config.getConfig() );
59  
60        config.setName( dialog.getValue( Form.STEP_NAME ) );
61        mockResponseStepConfig.setPath( dialog.getValue( Form.PATH ) );
62        mockResponseStepConfig.setPort( dialog.getIntValue( Form.PORT, 8181 ) );
63  
64        String response = operation.createResponse( operation.getSettings().getBoolean( WsdlSettings.XML_GENERATION_ALWAYS_INCLUDE_OPTIONAL_ELEMENTS ) );
65        CompressedStringSupport.setString( mockResponseStepConfig.addNewResponse().addNewResponseContent(), response );
66  
67        WsdlMockResponseTestStep testStep = (WsdlMockResponseTestStep) testCase.addTestStep( config );
68  
69        if( dialog.getBooleanValue( Form.ADD_SCHEMA_ASSERTION ) )
70           testStep.addAssertion( SchemaComplianceAssertion.ID );
71  
72        UISupport.selectAndShow( testStep );
73  
74        if( dialog.getBooleanValue( Form.SHOW_TESTCASE ) )
75        {
76           UISupport.selectAndShow( testCase );
77        }
78  
79        return true;
80     }
81  
82     @AForm( name = "Add MockResponse to TestCase", description = "Options for adding this Operations Response to a " +
83             "TestCase using a default response message generated from the schema.",
84             helpUrl = HelpUrls.ADDMOCKOPERATIONASMOCKRESPONSESTEP_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
85     private interface Form
86     {
87        @AField( name = "Name", description = "Unique name of MockResponse Step" )
88        public final static String STEP_NAME = "Name";
89  
90        @AField( name = "Path", description = "Path to listen on" )
91        public final static String PATH = "Path";
92  
93        @AField( name = "Port", description = "Port to listen on", type = AFieldType.INT )
94        public final static String PORT = "Port";
95  
96        @AField( name = "Add Schema Assertion", description = "Adds SchemaCompliance Assertion for request", type = AFieldType.BOOLEAN )
97        public final static String ADD_SCHEMA_ASSERTION = "Add Schema Assertion";
98  
99        @AField( name = "Shows TestCase Editor", description = "Shows the target steps TestCase editor", type = AFieldType.BOOLEAN )
100       public final static String SHOW_TESTCASE = "Shows TestCase Editor";
101    }
102 }