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