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.operation;
    14
    15  import com.eviware.soapui.SoapUI;
    16  import com.eviware.soapui.impl.wsdl.WsdlOperation;
    17  import com.eviware.soapui.impl.wsdl.WsdlProject;
    18  import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
    19  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
    20  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
    21  import com.eviware.soapui.model.support.ModelSupport;
    22  import com.eviware.soapui.support.UISupport;
    23  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
    24
    25  /***
    26   * Creates a WsdlMockOperation for the specified WsdlOperation
    27   *
    28   * @author ole.matzura
    29   */
    30
    31  public class AddOperationToMockServiceAction extends AbstractSoapUIAction<WsdlOperation>
    32  {
    33  	private static final String CREATE_MOCKSUITE_OPTION = "Create new..";
    34  	public static final String SOAPUI_ACTION_ID = "AddOperationToMockServiceAction";
    35
    36  	public AddOperationToMockServiceAction()
    37  	{
    38  		super( "Add to MockService", "Add this operation to a MockService" );
    39  	}
    40
    41  	public void perform( WsdlOperation operation, Object param )
    42  	{
    43  		String title = getName();
    44
    45  		WsdlMockService mockService = null;
    46  		WsdlProject project = ( WsdlProject ) operation.getInterface().getProject();
    47
    48  		while( mockService == null )
    49  		{
    50  			if( project.getMockServiceCount() > 0 )
    51  			{
    52  				String[] mockServices = ModelSupport.getNames( project.getMockServices(), new String[] {CREATE_MOCKSUITE_OPTION});
    53
    54  				// prompt
    55  				String option = ( String ) UISupport.prompt( "Select MockService for MockOperation", title, mockServices );
    56  				if( option == null )
    57  					return;
    58
    59  				mockService = ( WsdlMockService ) project.getMockServiceByName( option );
    60  			}
    61
    62  			// create new mocksuite?
    63  			if( mockService == null )
    64  			{
    65  				String mockServiceName = UISupport.prompt( "Enter name of new MockService", title, "MockService " + (project.getMockServiceCount()+1) );
    66  				if( mockServiceName == null || mockServiceName.trim().length() == 0 )
    67  					return;
    68
    69  				mockService = ( WsdlMockService ) project.addNewMockService( mockServiceName );
    70  			}
    71
    72  			if( mockService.hasMockOperation( operation ))
    73  			{
    74  				UISupport.showErrorMessage( "MockService [" + mockService.getName() + "] already has a MockOperation for [" +
    75  							operation.getName() + "], please select another MockService" );
    76  				mockService = null;
    77  			}
    78  		}
    79
    80  		// add mockoperation
    81  		addOperationToMockService( operation, mockService );
    82  	}
    83
    84  	public boolean addOperationToMockService( WsdlOperation operation, WsdlMockService mockService )
    85  	{
    86  		if( mockService.hasMockOperation( operation ))
    87  		{
    88  			UISupport.showErrorMessage( "MockService [" + mockService.getName() + "] already has a MockOperation for [" +
    89  						operation.getName() + "]" );
    90  			return false;
    91  		}
    92
    93  		WsdlMockOperation mockOperation = ( WsdlMockOperation ) mockService.addNewMockOperation( operation );
    94  		WsdlMockResponse mockResponse = mockOperation.addNewMockResponse( "Response 1", false );
    95
    96  		if( !operation.isOneWay() )
    97  			mockResponse.setResponseContent( operation.createResponse( true ));
    98
    99  		if( UISupport.confirm( "Open MockResponse editor?", getName() ))
    100 		{
    101 			SoapUI.getDesktop().showDesktopPanel( mockResponse );
    102 		}
    103
    104 		return true;
    105 	}
    106 }