1
2
3
4
5
6
7
8
9
10
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.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.getMockServiceList());
69 dialog.setOptions( Form.MOCKSERVICE, mockServices );
70
71 dialog.setValue( Form.PATH, "/mock" + iface.getName() );
72 dialog.setValue( Form.PORT, "8088" );
73
74 if( dialog.show() )
75 {
76 List<String> operations = Arrays.asList( operationsFormField.getSelectedOptions() );
77 if( operations.size() == 0 )
78 {
79 UISupport.showErrorMessage( "No Operations selected.." );
80 return null;
81 }
82
83 String mockServiceName = dialog.getValue( Form.MOCKSERVICE );
84 WsdlMockService mockService = ( WsdlMockService ) project.getMockServiceByName( mockServiceName );
85
86 if( mockService == null || mockServiceName.equals( CREATE_MOCKSUITE_OPTION ))
87 {
88 mockServiceName = UISupport.prompt( "Specify name of MockService to create", getName(), iface.getName() + " MockService" );
89 if( mockServiceName == null )
90 return null;
91
92 mockService = ( WsdlMockService ) project.addNewMockService( mockServiceName );
93 }
94
95 mockService.setPath( dialog.getValue( Form.PATH ) );
96 try
97 {
98 mockService.setPort( Integer.parseInt( dialog.getValue( Form.PORT )) );
99 }
100 catch( NumberFormatException e1 )
101 {
102 }
103
104 for( int i = 0; i < iface.getOperationCount(); i++ )
105 {
106 WsdlOperation operation = ( WsdlOperation ) iface.getOperationAt( i );
107 if( !operations.contains( operation.getName() ))
108 continue;
109
110 WsdlMockOperation mockOperation = ( WsdlMockOperation ) mockService.addNewMockOperation( operation );
111 if( mockOperation != null )
112 mockOperation.addNewMockResponse( "Response 1", true );
113 }
114
115 if( dialog.getBooleanValue( Form.ADD_ENDPOINT ))
116 {
117 iface.addEndpoint( mockService.getLocalEndpoint() );
118 }
119
120 if( !atCreation )
121 {
122 WsdlMockServiceDesktopPanel desktopPanel = ( WsdlMockServiceDesktopPanel ) UISupport.showDesktopPanel( mockService );
123
124 if( dialog.getBooleanValue( Form.START_MOCKSERVICE ))
125 {
126 desktopPanel.startMockService();
127 SoapUI.getDesktop().minimize( desktopPanel );
128 }
129 }
130
131 return mockService;
132 }
133
134 return null;
135 }
136
137 @AForm( name="Generate MockService", description = "Set options for generated MockOperations for this Interface",
138 helpUrl=HelpUrls.GENERATE_MOCKSERVICE_HELP_URL, icon=UISupport.TOOL_ICON_PATH )
139 private interface Form
140 {
141 @AField(name = "MockService", description = "The MockService to create or use", type = AFieldType.ENUMERATION )
142 public final static String MOCKSERVICE = "MockService";
143
144 @AField( name = "Operations", description = "The Operations for which to Generate MockOperations", type = AFieldType.MULTILIST )
145 public final static String OPERATIONS = "Operations";
146
147 @AField(name = "Path", description = "The URL path to mount on", type = AFieldType.STRING )
148 public final static String PATH = "Path";
149
150 @AField(name = "Port", description = "The endpoint port to listen on", type = AFieldType.STRING )
151 public final static String PORT = "Port";
152
153 @AField(name = "Add Endpoint", description = "Adds the MockServices endpoint to the mocked Interface",
154 type = AFieldType.BOOLEAN )
155 public final static String ADD_ENDPOINT = "Add Endpoint";
156
157 @AField(name = "Start MockService", description = "Starts the MockService immediately",
158 type = AFieldType.BOOLEAN )
159 public final static String START_MOCKSERVICE = "Start MockService";
160 }
161 }