View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.teststeps.actions;
14  
15  import com.eviware.soapui.impl.wsdl.WsdlOperation;
16  import com.eviware.soapui.impl.wsdl.WsdlProject;
17  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
18  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
19  import com.eviware.soapui.model.support.ModelSupport;
20  import com.eviware.soapui.support.UISupport;
21  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
22  import com.eviware.soapui.support.xml.XmlUtils;
23  import com.eviware.x.form.XFormDialog;
24  import com.eviware.x.form.XFormField;
25  import com.eviware.x.form.XFormFieldListener;
26  import com.eviware.x.form.support.ADialogBuilder;
27  import com.eviware.x.form.support.AField;
28  import com.eviware.x.form.support.AForm;
29  import com.eviware.x.form.support.AField.AFieldType;
30  
31  /***
32   * Prompts to change the WsdlOperation of a WsdlTestRequestStep
33   * 
34   * @author Ole.Matzura
35   */
36  
37  public class ChangeOperationAction extends AbstractSoapUIAction<WsdlTestRequestStep>
38  {
39  	private XFormDialog dialog;
40  	private WsdlTestRequestStep testStep;
41  
42  	public ChangeOperationAction()
43  	{
44  		super( "Change Operation", "Changes the Interface Operation for this Test Request" );
45  	}
46  
47  	public void perform( WsdlTestRequestStep target, Object param )
48  	{
49  		this.testStep = target;
50  		
51  		if( dialog == null )
52     	{
53  		   dialog = ADialogBuilder.buildDialog( Form.class );
54  		   dialog.getFormField( Form.INTERFACE ).addFormFieldListener( new XFormFieldListener() {
55  
56  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
57  				{
58  					WsdlProject project = testStep.getTestCase().getTestSuite().getProject();
59  					dialog.setOptions( Form.OPERATION, 
60  								ModelSupport.getNames( project.getInterfaceByName( newValue ).getOperations() ));
61  					dialog.setValue( Form.OPERATION, testStep.getOperationName() );
62  				}} );
63  		   
64  		   dialog.getFormField( Form.RECREATE_REQUEST ).addFormFieldListener( new XFormFieldListener() {
65  
66  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
67  				{
68  					boolean enabled = Boolean.parseBoolean( newValue );
69  					
70  					dialog.getFormField( Form.CREATE_OPTIONAL ).setEnabled( enabled );
71  					dialog.getFormField( Form.KEEP_EXISTING ).setEnabled( enabled );
72  				}} );
73  		   
74  		   dialog.getFormField( Form.CREATE_OPTIONAL ).setEnabled( false );
75  			dialog.getFormField( Form.KEEP_EXISTING ).setEnabled( false );
76     	}
77  		
78  		WsdlProject project = target.getTestCase().getTestSuite().getProject();
79  		dialog.setOptions( Form.INTERFACE, ModelSupport.getNames( project.getInterfaces() ));
80  		dialog.setValue( Form.INTERFACE, target.getInterfaceName() );
81  		
82  		dialog.setOptions( Form.OPERATION, 
83  					ModelSupport.getNames( project.getInterfaceByName( target.getInterfaceName() ).getOperations() ));
84  		dialog.setValue( Form.OPERATION, target.getOperationName() );
85  		dialog.setValue( Form.NAME, target.getName() );
86  		
87  		if( dialog.show() )
88  		{
89  			String ifaceName = dialog.getValue( Form.INTERFACE );
90  			String operationName = dialog.getValue( Form.OPERATION );
91  			
92  			WsdlOperation operation = project.getInterfaceByName( ifaceName ).getOperationByName( operationName );
93  			target.setOperation( operation );
94  			
95  			String name = dialog.getValue( Form.NAME ).trim();
96  			if( name.length() > 0 && !target.getName().equals( name ) )
97  				target.setName( name );
98  			
99  			if( dialog.getBooleanValue( Form.RECREATE_REQUEST ))
100 			{
101 				String req = operation.createRequest( dialog.getBooleanValue( Form.CREATE_OPTIONAL ) );
102 		      if( req == null )
103 		      {
104 		      	UISupport.showErrorMessage( "Request creation failed" );
105 		      	return;
106 		      }
107 		      
108 		      WsdlTestRequest request = target.getTestRequest();
109 	         if( dialog.getBooleanValue( Form.KEEP_EXISTING ))
110 	         {
111 	        		req = XmlUtils.transferValues( request.getRequestContent(), req );
112 	         }         	
113 		      
114 		      request.setRequestContent( req );
115 			}
116 		}
117 	}
118 
119 	@AForm( description = "Specify Interface/Operation for TestRequest", name = "Change Operation" )
120 	protected interface Form
121 	{
122 		@AField( name = "Name", description = "The Name of the TestRequests", type = AFieldType.STRING )
123 		public final static String NAME = "Name";
124 		
125 		@AField( name = "Interface", description = "The TestRequests' Interface", type = AFieldType.ENUMERATION )
126 		public final static String INTERFACE = "Interface";
127 
128 		@AField( name = "Operation", description = "The TestRequests' Operation", type = AFieldType.ENUMERATION )
129 		public final static String OPERATION = "Operation";
130 		
131 		@AField( name = "Recreate Request", description = "Recreates the request content from the new Operations Definition", type = AFieldType.BOOLEAN )
132 		public final static String RECREATE_REQUEST = "Recreate Request";
133 		
134 		@AField( name = "Create Optional", description = "Creates optional content when recreating the request", type = AFieldType.BOOLEAN )
135 		public final static String CREATE_OPTIONAL = "Create Optional";
136 		
137 		@AField( name = "Keep Existing", description = "Tries to keep existing values when recreating the request", type = AFieldType.BOOLEAN )
138 		public final static String KEEP_EXISTING = "Keep Existing";
139 	}
140 }