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