View Javadoc

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