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