View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.mockoperation;
14  
15  import com.eviware.soapui.impl.WsdlInterfaceFactory;
16  import com.eviware.soapui.impl.wsdl.WsdlInterface;
17  import com.eviware.soapui.impl.wsdl.WsdlOperation;
18  import com.eviware.soapui.impl.wsdl.WsdlProject;
19  import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
20  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
21  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
22  import com.eviware.soapui.model.support.ModelSupport;
23  import com.eviware.soapui.support.UISupport;
24  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
25  import com.eviware.soapui.support.xml.XmlUtils;
26  import com.eviware.x.form.XFormDialog;
27  import com.eviware.x.form.XFormField;
28  import com.eviware.x.form.XFormFieldListener;
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  /***
35   * Prompts to change the WsdlOperation of a WsdlMockOperation
36   * 
37   * @author Ole.Matzura
38   */
39  
40  public class ChangeMockOperationAction extends AbstractSoapUIAction<WsdlMockOperation>
41  {
42  	private XFormDialog dialog;
43  	private WsdlMockOperation testStep;
44  
45  	public ChangeMockOperationAction()
46  	{
47  		super( "Change Operation", "Changes the Interface Operation for this MockOperation" );
48  	}
49  
50  	public void perform( WsdlMockOperation target, Object param )
51  	{
52  		this.testStep = target;
53  
54  		if( dialog == null )
55  		{
56  			dialog = ADialogBuilder.buildDialog( Form.class );
57  			dialog.getFormField( Form.INTERFACE ).addFormFieldListener( new XFormFieldListener()
58  			{
59  
60  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
61  				{
62  					WsdlProject project = testStep.getMockService().getProject();
63  					dialog.setOptions( Form.OPERATION, ModelSupport.getNames( project.getInterfaceByName( newValue )
64  							.getOperationList() ) );
65  					WsdlOperation operation = testStep.getOperation();
66  					dialog.setValue( Form.OPERATION, operation == null ? "" : operation.getName() );
67  				}
68  			} );
69  
70  			dialog.getFormField( Form.RECREATE_REQUEST ).addFormFieldListener( new XFormFieldListener()
71  			{
72  
73  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
74  				{
75  					boolean enabled = Boolean.parseBoolean( newValue );
76  
77  					dialog.getFormField( Form.CREATE_OPTIONAL ).setEnabled( enabled );
78  					dialog.getFormField( Form.KEEP_EXISTING ).setEnabled( enabled );
79  				}
80  			} );
81  
82  			dialog.getFormField( Form.CREATE_OPTIONAL ).setEnabled( false );
83  			dialog.getFormField( Form.KEEP_EXISTING ).setEnabled( false );
84  		}
85  
86  		WsdlOperation operation = testStep.getOperation();
87  		WsdlProject project = testStep.getMockService().getProject();
88  		String[] interfaceNames = ModelSupport.getNames( project.getInterfaceList(),
89  				new ModelSupport.InterfaceTypeFilter( WsdlInterfaceFactory.WSDL_TYPE ) );
90  		dialog.setOptions( Form.INTERFACE, interfaceNames );
91  		dialog.setValue( Form.INTERFACE, operation == null ? interfaceNames[0] : operation.getInterface().getName() );
92  
93  		dialog.setOptions( Form.OPERATION, ModelSupport.getNames( project.getInterfaceByName(
94  				dialog.getValue( Form.INTERFACE ) ).getOperationList() ) );
95  		dialog.setValue( Form.OPERATION, operation == null ? null : operation.getName() );
96  		dialog.setValue( Form.NAME, target.getName() );
97  
98  		if( dialog.show() )
99  		{
100 			String ifaceName = dialog.getValue( Form.INTERFACE );
101 			String operationName = dialog.getValue( Form.OPERATION );
102 
103 			WsdlInterface iface = ( WsdlInterface )project.getInterfaceByName( ifaceName );
104 			operation = iface.getOperationByName( operationName );
105 			target.setOperation( operation );
106 
107 			String name = dialog.getValue( Form.NAME ).trim();
108 			if( name.length() > 0 && !target.getName().equals( name ) )
109 				target.setName( name );
110 
111 			if( dialog.getBooleanValue( Form.RECREATE_REQUEST ) )
112 			{
113 				String req = operation.createResponse( dialog.getBooleanValue( Form.CREATE_OPTIONAL ) );
114 				if( req == null )
115 				{
116 					UISupport.showErrorMessage( "Response creation failed" );
117 				}
118 				else
119 				{
120 					for( int c = 0; c < target.getMockResponseCount(); c++ )
121 					{
122 						String msg = req;
123 						WsdlMockResponse mockResponse = target.getMockResponseAt( c );
124 
125 						if( dialog.getBooleanValue( Form.KEEP_EXISTING ) )
126 						{
127 							msg = XmlUtils.transferValues( mockResponse.getResponseContent(), req );
128 						}
129 
130 						mockResponse.setResponseContent( msg );
131 					}
132 				}
133 			}
134 		}
135 	}
136 
137 	@AForm( description = "Specify Interface/Operation for MockOperation", name = "Change Operation", helpUrl = HelpUrls.CHANGEMOCKOPERATION_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
138 	protected interface Form
139 	{
140 		@AField( name = "Name", description = "The Name of the MockOperation", type = AFieldType.STRING )
141 		public final static String NAME = "Name";
142 
143 		@AField( name = "Interface", description = "The MockOperations Interface", type = AFieldType.ENUMERATION )
144 		public final static String INTERFACE = "Interface";
145 
146 		@AField( name = "Operation", description = "The MockOperations Operation", type = AFieldType.ENUMERATION )
147 		public final static String OPERATION = "Operation";
148 
149 		@AField( name = "Recreate Responses", description = "Recreates all MockResponses content from the new Operations Definition", type = AFieldType.BOOLEAN )
150 		public final static String RECREATE_REQUEST = "Recreate Responses";
151 
152 		@AField( name = "Create Optional", description = "Creates optional content when recreating the response", type = AFieldType.BOOLEAN )
153 		public final static String CREATE_OPTIONAL = "Create Optional";
154 
155 		@AField( name = "Keep Existing", description = "Tries to keep existing values when recreating the response", type = AFieldType.BOOLEAN )
156 		public final static String KEEP_EXISTING = "Keep Existing";
157 	}
158 }