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.request;
    14
    15  import com.eviware.soapui.SoapUI;
    16  import com.eviware.soapui.impl.wsdl.WsdlProject;
    17  import com.eviware.soapui.impl.wsdl.WsdlRequest;
    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.impl.wsdl.submit.transports.http.WsdlResponse;
    22  import com.eviware.soapui.model.iface.Attachment;
    23  import com.eviware.soapui.model.support.ModelSupport;
    24  import com.eviware.soapui.support.UISupport;
    25  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
    26
    27  /***
    28   * Adds a WsdlRequest to a WsdlMockService, will create required WsdlMockOperation if neccessary
    29   *
    30   * @author ole.matzura
    31   */
    32
    33  public class AddRequestToMockServiceAction extends AbstractSoapUIAction<WsdlRequest>
    34  {
    35  	private static final String CREATE_MOCKSUITE_OPTION = "Create new..";
    36  	public static final String SOAPUI_ACTION_ID = "AddRequestToMockServiceAction";
    37
    38  	public AddRequestToMockServiceAction()
    39  	{
    40  		super( "Add to MockService", "Adds the current response to a MockService" );
    41  	}
    42
    43  	public void perform( WsdlRequest request, Object param )
    44  	{
    45  		String title = getName();
    46
    47  		if( request != null && request.getResponse() == null )
    48  		{
    49  			if( !UISupport.confirm( "Request is missing response, create default mock response instead?", title ))
    50  			{
    51  				return;
    52  			}
    53  		}
    54
    55  		WsdlMockService mockService = null;
    56  		WsdlMockOperation mockOperation = ( WsdlMockOperation ) param;
    57  		if( mockOperation != null )
    58  			mockService = mockOperation.getMockService();
    59
    60  		WsdlProject project = ( WsdlProject ) request.getOperation().getInterface().getProject();
    61
    62  		while( mockService == null )
    63  		{
    64  			if( project.getMockServiceCount() > 0 )
    65  			{
    66  				String[] mockServices = ModelSupport.getNames( project.getMockServices(), new String[] {CREATE_MOCKSUITE_OPTION});
    67
    68  				// prompt
    69  				String option = ( String ) UISupport.prompt( "Select MockService for MockOperation", title, mockServices );
    70  				if( option == null )
    71  					return;
    72
    73  				mockService = ( WsdlMockService ) project.getMockServiceByName( option );
    74  			}
    75
    76  			// create new mocksuite?
    77  			if( mockService == null )
    78  			{
    79  				String mockServiceName = UISupport.prompt( "Enter name of new MockService", title, "MockService " + (project.getMockServiceCount()+1) );
    80  				if( mockServiceName == null || mockServiceName.trim().length() == 0 )
    81  					return;
    82
    83  				mockService = ( WsdlMockService ) project.addNewMockService( mockServiceName );
    84  			}
    85
    86  			mockOperation = mockService.getMockOperation( request.getOperation() );
    87  			if( mockOperation != null)
    88  			{
    89  				Boolean retval = UISupport.confirmOrCancel( "MockService [" + mockService.getName() + "] already has a MockOperation for [" +
    90  							request.getOperation().getName() + "],\r\nShould MockResponse be added to this MockOperation instead",
    91  							"Add Request to MockService" );
    92
    93  				if( retval == null )
    94  					return;
    95
    96  				if( !retval.booleanValue())
    97  					mockService = null;
    98  			}
    99  		}
    100
    101 		// add mockoperation
    102 		if( mockOperation == null )
    103 			mockOperation = ( WsdlMockOperation ) mockService.addNewMockOperation( request.getOperation() );
    104
    105 		WsdlMockResponse mockResponse = mockOperation.addNewMockResponse( "Response " + (1+mockOperation.getMockResponseCount()), false );
    106
    107 		// add expected response if available
    108 		if( request != null && request.getResponse() != null )
    109 		{
    110 			WsdlResponse response = ( WsdlResponse ) request.getResponse();
    111 			mockResponse.setResponseContent( response.getContentAsString() );
    112
    113 			Attachment[] attachments = response.getAttachments();
    114 			for( Attachment attachment : attachments )
    115 			{
    116 				mockResponse.addAttachment( attachment );
    117 			}
    118
    119 			if( mockResponse.getResponseHeaders() != null && mockResponse.getResponseHeaders().size() > 0 &&
    120 						UISupport.confirm( "Add current Response HTTP Headers to MockResponse", title ))
    121 				mockResponse.setResponseHeaders( response.getResponseHeaders() );
    122 		}
    123 		else
    124 		{
    125 			mockResponse.setResponseContent( request.getOperation().createResponse( true ));
    126 		}
    127
    128 		if( UISupport.confirm( "Open MockResponse editor?", title ))
    129 		{
    130 			SoapUI.getDesktop().showDesktopPanel( mockResponse );
    131 		}
    132 	}
    133 }