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