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