View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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 java.awt.event.ActionEvent;
16  
17  import com.eviware.soapui.SoapUI;
18  import com.eviware.soapui.impl.wsdl.WsdlOperation;
19  import com.eviware.soapui.impl.wsdl.WsdlProject;
20  import com.eviware.soapui.impl.wsdl.WsdlRequest;
21  import com.eviware.soapui.impl.wsdl.actions.iface.AbstractSwingAction;
22  import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
23  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
24  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
25  import com.eviware.soapui.impl.wsdl.submit.transports.http.WsdlResponse;
26  import com.eviware.soapui.model.iface.Attachment;
27  import com.eviware.soapui.model.support.ModelSupport;
28  import com.eviware.soapui.support.UISupport;
29  
30  public class AddToMockServiceAction extends AbstractSwingAction<WsdlOperation>
31  {
32  	private static final String CREATE_MOCKSUITE_OPTION = "Create new..";
33  	private final WsdlRequest request;
34  
35  	public AddToMockServiceAction( WsdlOperation operation, WsdlRequest request )
36  	{
37  		super( "Add to MockService", request == null ? "Add this operation to a MockService" :
38  			"Adds the current response to a MockService", "/addToMockService.gif", operation );
39  
40  		this.request = request;
41  	}
42  
43  	public AddToMockServiceAction( WsdlOperation operation )
44  	{
45  		this( operation, null );
46  	}
47  	
48  	public AddToMockServiceAction( WsdlRequest request )
49  	{
50  		this( ( WsdlOperation ) request.getOperation(), request );
51  	}
52  	
53  	@Override
54  	public void actionPerformed( ActionEvent arg0, WsdlOperation operation )
55  	{
56  		String title = getName();
57  
58  		if( request != null && request.getResponse() == null )
59  		{
60  			if( !UISupport.confirm( "Request is missing response, create default mock response instead?", title ))
61  			{
62  				return;
63  			}
64  		}
65  
66  		WsdlMockService mockService = null;
67  		WsdlProject project = ( WsdlProject ) operation.getInterface().getProject();
68  		
69  		while( mockService == null )
70  		{
71  			if( project.getMockServiceCount() > 0 )
72  			{
73  				String[] mockServices = ModelSupport.getNames( project.getMockServices(), new String[] {CREATE_MOCKSUITE_OPTION});
74  	
75  				// prompt
76  				String option = ( String ) UISupport.prompt( "Select MockService for MockOperation", title, mockServices );
77  				if( option == null )
78  					return;
79  				
80  				mockService = ( WsdlMockService ) project.getMockServiceByName( option );
81  			}
82  	
83  			// create new mocksuite?
84  			if( mockService == null )
85  			{
86  				String mockServiceName = UISupport.prompt( "Enter name of new MockService", title, "MockService " + (project.getMockServiceCount()+1) );
87  				if( mockServiceName == null || mockServiceName.trim().length() == 0 )
88  					return;
89  				
90  				mockService = ( WsdlMockService ) project.addNewMockService( mockServiceName );
91  			}
92  			
93  			if( mockService.hasMockOperation( operation ))
94  			{
95  				UISupport.showErrorMessage( "MockService [" + mockService.getName() + "] already has a MockOperation for [" +  
96  							operation.getName() + "], please select another MockService" );
97  				mockService = null;
98  			}
99  		}
100 		
101 		// add mockoperation
102 		WsdlMockOperation mockOperation = ( WsdlMockOperation ) mockService.addNewMockOperation( operation );
103 		WsdlMockResponse mockResponse = mockOperation.addNewMockResponse( "Response 1", false );
104 		
105 		// add expected response if available
106 		if( request != null && request.getResponse() != null )
107 		{
108 			WsdlResponse response = ( WsdlResponse ) request.getResponse();
109 			mockResponse.setResponseContent( response.getContentAsString() );
110 			
111 			Attachment[] attachments = response.getAttachments();
112 			for( Attachment attachment : attachments )
113 			{
114 				mockResponse.addAttachment( attachment );
115 			}
116 			
117 			mockResponse.setResponseHeaders( response.getResponseHeaders() );
118 		}
119 		else
120 		{
121 			mockResponse.setResponseContent( operation.createResponse( true ));
122 		}
123 		
124 		if( UISupport.confirm( "Open MockResponse editor?", title ))
125 		{
126 			SoapUI.getDesktop().showDesktopPanel( mockResponse );
127 		}
128 	}
129 }