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 java.awt.event.ActionEvent;
    16  import java.util.ArrayList;
    17  import java.util.List;
    18
    19  import com.eviware.soapui.config.DispatchStyleConfig;
    20  import com.eviware.soapui.impl.wsdl.WsdlInterface;
    21  import com.eviware.soapui.impl.wsdl.WsdlOperation;
    22  import com.eviware.soapui.impl.wsdl.WsdlProject;
    23  import com.eviware.soapui.impl.wsdl.actions.iface.AbstractSwingAction;
    24  import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
    25  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResult;
    26  import com.eviware.soapui.model.iface.Interface;
    27  import com.eviware.soapui.model.iface.Operation;
    28  import com.eviware.soapui.model.mock.MockResponse;
    29  import com.eviware.soapui.model.util.ModelItemNames;
    30  import com.eviware.soapui.support.UISupport;
    31  import com.eviware.soapui.support.action.swing.DefaultActionList;
    32  import com.eviware.soapui.support.xml.XmlUtils;
    33  import com.eviware.x.form.XFormDialog;
    34  import com.eviware.x.form.XFormField;
    35  import com.eviware.x.form.XFormFieldListener;
    36  import com.eviware.x.form.support.ADialogBuilder;
    37  import com.eviware.x.form.support.AField;
    38  import com.eviware.x.form.support.AForm;
    39  import com.eviware.x.form.support.AField.AFieldType;
    40
    41  /***
    42   * Displays the options for the specified WsdlMockOperation
    43   *
    44   * @author ole.matzura
    45   */
    46
    47  public class WsdlMockOperationOptionsAction extends AbstractSwingAction<WsdlMockOperation>
    48  {
    49  	private XFormDialog dialog;
    50  	private DefineNamespacesAction defineNamespacesAction;
    51  	private WsdlProject project;
    52
    53  	public WsdlMockOperationOptionsAction( WsdlMockOperation mockOperation )
    54  	{
    55  		super( "MockOperation Options", "Sets options for this MockOperation", mockOperation );
    56
    57  		project = mockOperation.getMockService().getProject();
    58  	}
    59
    60  	@Override
    61  	public void actionPerformed( ActionEvent arg0, WsdlMockOperation mockOperation )
    62  	{
    63  		if( dialog == null )
    64  		{
    65  			DefaultActionList actions = new DefaultActionList();
    66  			defineNamespacesAction = new DefineNamespacesAction( mockOperation );
    67  			actions.addAction( defineNamespacesAction  );
    68
    69  			dialog = ADialogBuilder.buildDialog( Form.class, actions );
    70  			dialog.getFormField( Form.DISPATCH_STYLE ).addFormFieldListener( new XFormFieldListener() {
    71
    72  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
    73  				{
    74  					boolean enabled = newValue.equals( "XPATH" ) || newValue.equals( "SCRIPT" );
    75
    76  					enableXPathFields( enabled );
    77  					defineNamespacesAction.setEnabled( enabled);
    78  				}} );
    79
    80  			dialog.getFormField( Form.INTERFACE ).addFormFieldListener( new XFormFieldListener() {
    81
    82  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
    83  				{
    84  					WsdlInterface iface = project.getInterfaceByName( newValue );
    85  					dialog.setOptions( Form.OPERATION,
    86  								new ModelItemNames<Operation>( iface.getOperations() ).getNames() );
    87  				}} );
    88  		}
    89
    90  		List<Interface> interfaces = new ArrayList<Interface>();
    91  		for( int c = 0; c < project.getInterfaceCount(); c++ )
    92  		{
    93  			if( project.getInterfaceAt( c ).getOperationCount() > 0 )
    94  				interfaces.add( project.getInterfaceAt( c ));
    95  		}
    96
    97  		dialog.setOptions( Form.INTERFACE, new ModelItemNames<Interface>( interfaces ).getNames() );
    98  		dialog.setOptions( Form.OPERATION,
    99  					new ModelItemNames<Operation>( interfaces.get( 0 ).getOperations() ).getNames() );
    100
    101 		dialog.setValue( Form.INTERFACE, mockOperation.getOperation().getInterface().getName() );
    102 		dialog.setValue( Form.OPERATION, mockOperation.getOperation().getName() );
    103
    104 		dialog.setOptions( Form.DEFAULT_RESPONSE,
    105 					new ModelItemNames<MockResponse>( mockOperation.getMockResponses() ).getNames() );
    106
    107 		dialog.setValue( Form.DISPATCH_STYLE, mockOperation.getDispatchStyle().toString() );
    108 		dialog.setValue( Form.DISPATCH_PATH, mockOperation.getDispatchPath() );
    109 		dialog.setValue( Form.DEFAULT_RESPONSE, mockOperation.getDefaultResponse() );
    110
    111 		enableXPathFields( dialog.getValue( Form.DISPATCH_STYLE ).equals( "XPATH" ) ||
    112 					dialog.getValue( Form.DISPATCH_STYLE ).equals( "SCRIPT" ));
    113
    114 		if( dialog.show() )
    115 		{
    116 			mockOperation.setDispatchStyle(
    117 						DispatchStyleConfig.Enum.forString( dialog.getValue( Form.DISPATCH_STYLE )) );
    118 			mockOperation.setDispatchPath( dialog.getValue( Form.DISPATCH_PATH ));
    119 			mockOperation.setDefaultResponse( dialog.getValue( Form.DEFAULT_RESPONSE ) );
    120
    121 			WsdlOperation operation = project.getInterfaceByName( dialog.getValue( Form.INTERFACE ) ).
    122 				getOperationByName( dialog.getValue( Form.OPERATION ) );
    123
    124 			if( operation != mockOperation.getOperation() )
    125 				mockOperation.setOperation( operation );
    126 		}
    127 	}
    128
    129 	private void enableXPathFields( boolean enabled )
    130 	{
    131 		dialog.getFormField( Form.DISPATCH_PATH ).setEnabled( enabled);
    132 		dialog.getFormField( Form.DEFAULT_RESPONSE ).setEnabled( enabled);
    133 		defineNamespacesAction.setEnabled( enabled );
    134 	}
    135
    136 	@AForm(description="Set options for this MockOperation", name="MockOperation Options" )
    137 	private class Form
    138 	{
    139 		@AField(description = "Specifies the operation to be mocked", name = "Operation", type = AFieldType.ENUMERATION)
    140 		public final static String OPERATION = "Operation";
    141
    142 		@AField(description = "Specifies the interface containing the operation to be mocked", name = "Interface", type = AFieldType.ENUMERATION)
    143 		public final static String INTERFACE = "Interface";
    144
    145 		@AField(description="How to dispatch requests to responses", name="Dispatch Style",
    146 					type=AFieldType.ENUMERATION, values= {"SEQUENCE", "RANDOM", "XPATH", "SCRIPT" })
    147 		public final static String DISPATCH_STYLE = "Dispatch Style";
    148
    149 		@AField(description="The XPath to use for selecting the corresponding response", name="Dispatch Path",
    150 					type=AFieldType.STRINGAREA)
    151 		public final static String DISPATCH_PATH = "Dispatch Path";
    152
    153 		@AField(description="Default response to use", name="Default Response",
    154 					type=AFieldType.ENUMERATION )
    155 		public final static String DEFAULT_RESPONSE = "Default Response";
    156 	}
    157
    158 	public class DefineNamespacesAction extends AbstractSwingAction<WsdlMockOperation>
    159 	{
    160 		public DefineNamespacesAction( WsdlMockOperation mockOperation )
    161 		{
    162 			super( "Define Namespaces", "Defines namespaces from last Mock Request", mockOperation );
    163 		}
    164
    165 		@Override
    166 		public void actionPerformed( ActionEvent arg0, WsdlMockOperation mockOperation )
    167 		{
    168 			WsdlMockResult result = mockOperation.getLastMockResult();
    169 			if( result == null || result.getMockRequest() == null )
    170 			{
    171 				UISupport.showErrorMessage( "Missing request to define from" );
    172 			}
    173 			else
    174 			{
    175 				try
    176 				{
    177 					String ns = XmlUtils.declareXPathNamespaces( result.getMockRequest().getRequestContent() );
    178 					if( ns != null )
    179 					{
    180 						ns += dialog.getValue( Form.DISPATCH_PATH );
    181 						dialog.setValue( Form.DISPATCH_PATH, ns );
    182 					}
    183 				}
    184 				catch( Exception e )
    185 				{
    186 					UISupport.showErrorMessage( e );
    187 				}
    188 			}
    189 		}
    190 	}
    191 }