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.request;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.config.CompressedStringConfig;
17  import com.eviware.soapui.config.MockResponseStepConfig;
18  import com.eviware.soapui.config.TestStepConfig;
19  import com.eviware.soapui.impl.wsdl.WsdlOperation;
20  import com.eviware.soapui.impl.wsdl.WsdlRequest;
21  import com.eviware.soapui.impl.wsdl.actions.support.AbstractAddToTestCaseAction;
22  import com.eviware.soapui.impl.wsdl.support.CompressedStringSupport;
23  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
24  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
25  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMockResponseTestStep;
26  import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.SchemaComplianceAssertion;
27  import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlMockResponseStepFactory;
28  import com.eviware.soapui.settings.WsdlSettings;
29  import com.eviware.soapui.support.UISupport;
30  import com.eviware.soapui.ui.desktop.SoapUIDesktop;
31  import com.eviware.x.form.XFormDialog;
32  import com.eviware.x.form.support.ADialogBuilder;
33  import com.eviware.x.form.support.AField;
34  import com.eviware.x.form.support.AForm;
35  import com.eviware.x.form.support.AField.AFieldType;
36  
37  public class AddRequestAsMockResponseStepAction extends AbstractAddToTestCaseAction<WsdlRequest>
38  {
39  	public static final String SOAPUI_ACTION_ID = "AddRequestAsMockResponseStepAction";
40  	private XFormDialog dialog;
41  
42  	public AddRequestAsMockResponseStepAction()
43  	{
44  		super( "Add as MockResponse Step", "Creates a MockResponseStep from this Request" );
45  	}
46  
47  	public void perform( WsdlRequest request, Object param )
48  	{
49  		WsdlTestCase testCase = getTargetTestCase( request.getOperation().getInterface().getProject() );
50  		if( testCase != null )
51  			addMockResponse( testCase, request );
52  	}
53  
54  	protected boolean addMockResponse( WsdlTestCase testCase, WsdlRequest request )
55  	{
56  		String title = getName();
57  		boolean create = false;
58  
59  		if( dialog == null )
60  			dialog = ADialogBuilder.buildDialog( Form.class );
61  
62  		WsdlOperation operation = request.getOperation();
63  		dialog.setValue( Form.STEP_NAME, operation.getName() );
64  		dialog.setBooleanValue( Form.CLOSE_REQUEST, true );
65  		dialog.setBooleanValue( Form.SHOW_TESTCASE, true );
66  		dialog.setIntValue( Form.PORT, 8181 );
67  		dialog.setValue( Form.PATH, "/" + operation.getName() );
68  
69  		SoapUIDesktop desktop = SoapUI.getDesktop();
70  		dialog.getFormField( Form.CLOSE_REQUEST ).setEnabled( desktop != null && desktop.hasDesktopPanel( request ) );
71  
72  		if( !dialog.show() )
73  			return false;
74  
75  		TestStepConfig config = WsdlMockResponseStepFactory.createConfig( operation, request, false );
76  		MockResponseStepConfig mockResponseStepConfig = ( ( MockResponseStepConfig )config.getConfig() );
77  
78  		config.setName( dialog.getValue( Form.STEP_NAME ) );
79  		mockResponseStepConfig.setPath( dialog.getValue( Form.PATH ) );
80  		mockResponseStepConfig.setPort( dialog.getIntValue( Form.PORT, 8181 ) );
81  		CompressedStringConfig responseContent = mockResponseStepConfig.getResponse().getResponseContent();
82  
83  		if( request.getResponse() == null && !request.getOperation().isOneWay() )
84  		{
85  			create = UISupport.confirm( "Request is missing response, create default mock response instead?", title );
86  		}
87  
88  		if( create )
89  		{
90  			String response = operation.createResponse( operation.getSettings().getBoolean(
91  					WsdlSettings.XML_GENERATION_ALWAYS_INCLUDE_OPTIONAL_ELEMENTS ) );
92  			CompressedStringSupport.setString( responseContent, response );
93  		}
94  		else if( request.getResponse() != null )
95  		{
96  			String response = request.getResponse().getContentAsString();
97  			CompressedStringSupport.setString( responseContent, response );
98  		}
99  
100 		WsdlMockResponseTestStep testStep = ( WsdlMockResponseTestStep )testCase.addTestStep( config );
101 
102 		if( dialog.getBooleanValue( Form.ADD_SCHEMA_ASSERTION ) )
103 			testStep.addAssertion( SchemaComplianceAssertion.ID );
104 
105 		UISupport.selectAndShow( testStep );
106 
107 		if( dialog.getBooleanValue( Form.CLOSE_REQUEST ) && desktop != null )
108 		{
109 			desktop.closeDesktopPanel( request );
110 		}
111 
112 		if( dialog.getBooleanValue( Form.SHOW_TESTCASE ) )
113 		{
114 			UISupport.selectAndShow( testCase );
115 		}
116 
117 		return true;
118 	}
119 
120 	@AForm( name = "Add MockResponse to TestCase", description = "Options for adding this requests response to a TestCase", helpUrl = HelpUrls.ADDREQUESTASMOCKRESPONSESTEP_HELP_URL, icon = UISupport.OPTIONS_ICON_PATH )
121 	private interface Form
122 	{
123 		@AField( name = "Name", description = "Unique name of MockResponse Step" )
124 		public final static String STEP_NAME = "Name";
125 
126 		@AField( name = "Path", description = "Path to listen on" )
127 		public final static String PATH = "Path";
128 
129 		@AField( name = "Port", description = "Port to listen on", type = AFieldType.INT )
130 		public final static String PORT = "Port";
131 
132 		@AField( name = "Add Schema Assertion", description = "Adds SchemaCompliance Assertion for request", type = AFieldType.BOOLEAN )
133 		public final static String ADD_SCHEMA_ASSERTION = "Add Schema Assertion";
134 
135 		@AField( name = "Close Request Window", description = "Closes the request editor if visible", type = AFieldType.BOOLEAN )
136 		public final static String CLOSE_REQUEST = "Close Request Window";
137 
138 		@AField( name = "Shows TestCase Editor", description = "Shows the target steps TestCase editor", type = AFieldType.BOOLEAN )
139 		public final static String SHOW_TESTCASE = "Shows TestCase Editor";
140 	}
141 }