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