View Javadoc

1   /*
2    *  soapUI Pro, copyright (C) 2007-2008 eviware software ab 
3    */
4   
5   package com.eviware.soapui.impl.wsdl.actions.mockresponse;
6   
7   import com.eviware.soapui.SoapUI;
8   import com.eviware.soapui.config.MockResponseStepConfig;
9   import com.eviware.soapui.config.TestStepConfig;
10  import com.eviware.soapui.impl.wsdl.actions.support.AbstractAddToTestCaseAction;
11  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
12  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
13  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
14  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
15  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMockResponseTestStep;
16  import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.SchemaComplianceAssertion;
17  import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlMockResponseStepFactory;
18  import com.eviware.soapui.support.UISupport;
19  import com.eviware.soapui.ui.desktop.SoapUIDesktop;
20  import com.eviware.x.form.XFormDialog;
21  import com.eviware.x.form.support.ADialogBuilder;
22  import com.eviware.x.form.support.AField;
23  import com.eviware.x.form.support.AField.AFieldType;
24  import com.eviware.x.form.support.AForm;
25  
26  public class AddMockResponseToTestCaseAction extends AbstractAddToTestCaseAction<WsdlMockResponse>
27  {
28     public final static String SOAPUI_ACTION_ID = "AddMockResponseToTestCaseAction";
29     private XFormDialog dialog;
30  
31     public AddMockResponseToTestCaseAction()
32     {
33        super( "Add to TestCase", "Adds this MockResponse to a TestCase" );
34     }
35  
36     public void perform( WsdlMockResponse mockResponse, Object param )
37     {
38        WsdlMockService mockService = mockResponse.getMockOperation().getMockService();
39        WsdlTestCase testCase = getTargetTestCase( mockService.getProject() );
40        if( testCase == null )
41           return;
42  
43        addMockResponseToTestCase( mockResponse, testCase, -1 );
44     }
45  
46     public void addMockResponseToTestCase( WsdlMockResponse mockResponse, WsdlTestCase testCase, int index )
47     {
48        if( mockResponse.getMockOperation().getOperation() == null )
49        {
50           UISupport.showErrorMessage( "Missing operation for this mock response" );
51           return;
52        }
53  
54        WsdlMockService mockService = mockResponse.getMockOperation().getMockService();
55  
56        if( dialog == null )
57           dialog = ADialogBuilder.buildDialog( Form.class );
58  
59        dialog.setValue( Form.STEP_NAME, mockResponse.getMockOperation().getName() );
60        dialog.setBooleanValue( Form.CLOSE_EDITOR, true );
61        dialog.setBooleanValue( Form.SHOW_TESTCASE, true );
62        dialog.setIntValue( Form.PORT, mockService.getPort() );
63        dialog.setValue( Form.PATH, mockService.getPath() );
64  
65        SoapUIDesktop desktop = SoapUI.getDesktop();
66        dialog.getFormField( Form.CLOSE_EDITOR ).setEnabled(
67                desktop != null && desktop.hasDesktopPanel( mockResponse ) );
68  
69        if( !dialog.show() )
70           return;
71  
72        TestStepConfig config = WsdlMockResponseStepFactory.createConfig( mockResponse.getMockOperation().getOperation(), false );
73        MockResponseStepConfig mockResponseStepConfig = ( (MockResponseStepConfig) config.getConfig() );
74  
75        config.setName( dialog.getValue( Form.STEP_NAME ) );
76        mockResponseStepConfig.setPath( dialog.getValue( Form.PATH ) );
77        mockResponseStepConfig.setPort( dialog.getIntValue( Form.PORT, mockService.getPort() ) );
78  
79        mockResponse.beforeSave();
80        mockResponseStepConfig.getResponse().set( mockResponse.getConfig() );
81  
82        WsdlMockResponseTestStep testStep = (WsdlMockResponseTestStep) testCase.insertTestStep( config, -1 );
83  
84        if( dialog.getBooleanValue( Form.ADD_SCHEMA_ASSERTION ) )
85           testStep.addAssertion( SchemaComplianceAssertion.ID );
86  
87        UISupport.selectAndShow( testStep );
88  
89        if( dialog.getBooleanValue( Form.CLOSE_EDITOR ) && desktop != null )
90        {
91           desktop.closeDesktopPanel( mockResponse );
92        }
93  
94        if( dialog.getBooleanValue( Form.SHOW_TESTCASE ) )
95        {
96           UISupport.selectAndShow( testCase );
97        }
98     }
99  
100    @AForm( name = "Add MockResponse to TestCase", description = "Options for adding this MockResponse to a " +
101            "TestCase", helpUrl = HelpUrls.ADDMOCKRESPONSETOTESTCASE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
102    private interface Form
103    {
104       @AField( name = "Name", description = "Unique name of MockResponse Step" )
105       public final static String STEP_NAME = "Name";
106 
107       @AField( name = "Path", description = "Path to listen on" )
108       public final static String PATH = "Path";
109 
110       @AField( name = "Port", description = "Port to listen on", type = AFieldType.INT )
111       public final static String PORT = "Port";
112 
113       @AField( name = "Add Schema Assertion", description = "Adds SchemaCompliance Assertion for request", type = AFieldType.BOOLEAN )
114       public final static String ADD_SCHEMA_ASSERTION = "Add Schema Assertion";
115 
116       @AField( name = "Close MockResponse Window", description = "Closes the MockResponse editor if visible", type = AFieldType.BOOLEAN )
117       public final static String CLOSE_EDITOR = "Close MockResponse Window";
118 
119       @AField( name = "Shows TestCase Editor", description = "Shows the target steps TestCase editor", type = AFieldType.BOOLEAN )
120       public final static String SHOW_TESTCASE = "Shows TestCase Editor";
121    }
122 }