View Javadoc

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