View Javadoc

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