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.iface;
    14
    15  import java.util.Arrays;
    16  import java.util.List;
    17
    18  import com.eviware.soapui.impl.wsdl.WsdlInterface;
    19  import com.eviware.soapui.impl.wsdl.WsdlOperation;
    20  import com.eviware.soapui.impl.wsdl.WsdlProject;
    21  import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
    22  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
    23  import com.eviware.soapui.impl.wsdl.panels.mock.WsdlMockServiceDesktopPanel;
    24  import com.eviware.soapui.model.support.ModelSupport;
    25  import com.eviware.soapui.support.UISupport;
    26  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
    27  import com.eviware.x.form.XFormDialog;
    28  import com.eviware.x.form.XFormOptionsField;
    29  import com.eviware.x.form.support.ADialogBuilder;
    30  import com.eviware.x.form.support.AField;
    31  import com.eviware.x.form.support.AForm;
    32  import com.eviware.x.form.support.AField.AFieldType;
    33
    34  /***
    35   * Generates a MockService for a specified Interface
    36   *
    37   * @author ole.matzura
    38   */
    39
    40  public class GenerateMockServiceAction extends AbstractSoapUIAction<WsdlInterface>
    41  {
    42  	private static final String CREATE_MOCKSUITE_OPTION = "<create>";
    43
    44  	public GenerateMockServiceAction()
    45  	{
    46  		super( "Generate MockService", "Generates a MockService containing all Operations in this Interface" );
    47  	}
    48
    49  	public void perform( WsdlInterface iface, Object param )
    50  	{
    51        generateMockService( iface );
    52  	}
    53
    54  	public WsdlMockService generateMockService( WsdlInterface iface )
    55  	{
    56  		XFormDialog dialog = ADialogBuilder.buildDialog( Form.class );
    57  		dialog.setBooleanValue( Form.ADD_ENDPOINT, true );
    58  		String[] names = ModelSupport.getNames( iface.getOperations() );
    59  		dialog.setOptions( Form.OPERATIONS, names );
    60  		XFormOptionsField operationsFormField = ( XFormOptionsField ) dialog.getFormField( Form.OPERATIONS );
    61  		operationsFormField.setSelectedOptions( names );
    62
    63  		WsdlProject project = ( WsdlProject ) iface.getProject();
    64  		String[] mockServices = ModelSupport.getNames( new String[] {CREATE_MOCKSUITE_OPTION}, project.getMockServices());
    65  		dialog.setOptions( Form.MOCKSERVICE, mockServices );
    66
    67  		dialog.setValue( Form.PATH, "/mock" + iface.getName() );
    68  		dialog.setValue( Form.PORT, "8088" );
    69
    70  		if( dialog.show() )
    71  		{
    72  			List<String> operations = Arrays.asList( operationsFormField.getSelectedOptions() );
    73  			if( operations.size() == 0 )
    74  			{
    75  				UISupport.showErrorMessage( "No Operations selected.." );
    76  				return null;
    77  			}
    78
    79  			String mockServiceName = dialog.getValue( Form.MOCKSERVICE );
    80  			WsdlMockService mockService = ( WsdlMockService ) project.getMockServiceByName( mockServiceName );
    81
    82  			if( mockService == null || mockServiceName.equals( CREATE_MOCKSUITE_OPTION ))
    83  			{
    84  				mockServiceName = UISupport.prompt( "Specify name of MockService to create", getName(), iface.getName() + " MockService" );
    85  				if( mockServiceName == null )
    86  					return null;
    87
    88  				mockService = ( WsdlMockService ) project.addNewMockService( mockServiceName );
    89  			}
    90
    91  			mockService.setPath( dialog.getValue( Form.PATH ) );
    92  			try
    93  			{
    94  				mockService.setPort( Integer.parseInt( dialog.getValue( Form.PORT )) );
    95  			}
    96  			catch( NumberFormatException e1 )
    97  			{
    98  			}
    99
    100 			for( int i = 0; i < iface.getOperationCount(); i++ )
    101 			{
    102 				WsdlOperation operation = ( WsdlOperation ) iface.getOperationAt( i );
    103 				if( !operations.contains( operation.getName() ))
    104 					continue;
    105
    106 				WsdlMockOperation mockOperation = ( WsdlMockOperation ) mockService.addNewMockOperation( operation );
    107 				if( mockOperation != null )
    108 					mockOperation.addNewMockResponse( "Response 1", true );
    109 			}
    110
    111 			if( dialog.getBooleanValue( Form.ADD_ENDPOINT ))
    112 			{
    113 				iface.addEndpoint( mockService.getLocalEndpoint() );
    114 			}
    115
    116 			WsdlMockServiceDesktopPanel desktopPanel = ( WsdlMockServiceDesktopPanel ) UISupport.showDesktopPanel( mockService );
    117
    118 			if( dialog.getBooleanValue( Form.START_MOCKSERVICE ))
    119 			{
    120 				desktopPanel.startMockService();
    121 			}
    122
    123 			return mockService;
    124 		}
    125
    126 		return null;
    127 	}
    128
    129 	@AForm( name="Generate MockService",
    130 				description = "Set options for generated MockOperations for this Interface")
    131 	private interface Form
    132 	{
    133 		@AField(name = "MockService", description = "The MockService to create or use", type = AFieldType.ENUMERATION )
    134 		public final static String MOCKSERVICE = "MockService";
    135
    136 		@AField( name = "Operations", description = "The Operations for which to Generate MockOperations", type = AFieldType.MULTILIST )
    137 		public final static String OPERATIONS = "Operations";
    138
    139 		@AField(name = "Path", description = "The URL path to mount on", type = AFieldType.STRING )
    140 		public final static String PATH = "Path";
    141
    142 		@AField(name = "Port", description = "The endpoint port to listen on", type = AFieldType.STRING )
    143 		public final static String PORT = "Port";
    144
    145 		@AField(name = "Add Endpoint", description = "Adds the MockServices endpoint to the mocked Interface",
    146 					type = AFieldType.BOOLEAN )
    147 		public final static String ADD_ENDPOINT = "Add Endpoint";
    148
    149 		@AField(name = "Start MockService", description = "Starts the MockService immediately",
    150 					type = AFieldType.BOOLEAN )
    151 		public final static String START_MOCKSERVICE = "Start MockService";
    152 	}
    153 }