001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.actions.iface;
014:
015: import java.util.Arrays;
016: import java.util.List;
017:
018: import com.eviware.soapui.impl.wsdl.WsdlInterface;
019: import com.eviware.soapui.impl.wsdl.WsdlOperation;
020: import com.eviware.soapui.impl.wsdl.WsdlProject;
021: import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
022: import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
023: import com.eviware.soapui.impl.wsdl.panels.mock.WsdlMockServiceDesktopPanel;
024: import com.eviware.soapui.model.support.ModelSupport;
025: import com.eviware.soapui.support.UISupport;
026: import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
027: import com.eviware.x.form.XFormDialog;
028: import com.eviware.x.form.XFormOptionsField;
029: import com.eviware.x.form.support.ADialogBuilder;
030: import com.eviware.x.form.support.AField;
031: import com.eviware.x.form.support.AForm;
032: import com.eviware.x.form.support.AField.AFieldType;
033:
034: /**
035: * Generates a MockService for a specified Interface
036: *
037: * @author ole.matzura
038: */
039:
040: public class GenerateMockServiceAction extends
041: AbstractSoapUIAction<WsdlInterface> {
042: private static final String CREATE_MOCKSUITE_OPTION = "<create>";
043:
044: public GenerateMockServiceAction() {
045: super ("Generate MockService",
046: "Generates a MockService containing all Operations in this Interface");
047: }
048:
049: public void perform(WsdlInterface iface, Object param) {
050: generateMockService(iface);
051: }
052:
053: public WsdlMockService generateMockService(WsdlInterface iface) {
054: XFormDialog dialog = ADialogBuilder.buildDialog(Form.class);
055: dialog.setBooleanValue(Form.ADD_ENDPOINT, true);
056: String[] names = ModelSupport.getNames(iface.getOperations());
057: dialog.setOptions(Form.OPERATIONS, names);
058: XFormOptionsField operationsFormField = (XFormOptionsField) dialog
059: .getFormField(Form.OPERATIONS);
060: operationsFormField.setSelectedOptions(names);
061:
062: WsdlProject project = (WsdlProject) iface.getProject();
063: String[] mockServices = ModelSupport.getNames(
064: new String[] { CREATE_MOCKSUITE_OPTION }, project
065: .getMockServices());
066: dialog.setOptions(Form.MOCKSERVICE, mockServices);
067:
068: dialog.setValue(Form.PATH, "/mock" + iface.getName());
069: dialog.setValue(Form.PORT, "8088");
070:
071: if (dialog.show()) {
072: List<String> operations = Arrays.asList(operationsFormField
073: .getSelectedOptions());
074: if (operations.size() == 0) {
075: UISupport.showErrorMessage("No Operations selected..");
076: return null;
077: }
078:
079: String mockServiceName = dialog.getValue(Form.MOCKSERVICE);
080: WsdlMockService mockService = (WsdlMockService) project
081: .getMockServiceByName(mockServiceName);
082:
083: if (mockService == null
084: || mockServiceName.equals(CREATE_MOCKSUITE_OPTION)) {
085: mockServiceName = UISupport.prompt(
086: "Specify name of MockService to create",
087: getName(), iface.getName() + " MockService");
088: if (mockServiceName == null)
089: return null;
090:
091: mockService = (WsdlMockService) project
092: .addNewMockService(mockServiceName);
093: }
094:
095: mockService.setPath(dialog.getValue(Form.PATH));
096: try {
097: mockService.setPort(Integer.parseInt(dialog
098: .getValue(Form.PORT)));
099: } catch (NumberFormatException e1) {
100: }
101:
102: for (int i = 0; i < iface.getOperationCount(); i++) {
103: WsdlOperation operation = (WsdlOperation) iface
104: .getOperationAt(i);
105: if (!operations.contains(operation.getName()))
106: continue;
107:
108: WsdlMockOperation mockOperation = (WsdlMockOperation) mockService
109: .addNewMockOperation(operation);
110: if (mockOperation != null)
111: mockOperation
112: .addNewMockResponse("Response 1", true);
113: }
114:
115: if (dialog.getBooleanValue(Form.ADD_ENDPOINT)) {
116: iface.addEndpoint(mockService.getLocalEndpoint());
117: }
118:
119: WsdlMockServiceDesktopPanel desktopPanel = (WsdlMockServiceDesktopPanel) UISupport
120: .showDesktopPanel(mockService);
121:
122: if (dialog.getBooleanValue(Form.START_MOCKSERVICE)) {
123: desktopPanel.startMockService();
124: }
125:
126: return mockService;
127: }
128:
129: return null;
130: }
131:
132: @AForm(name="Generate MockService",description="Set options for generated MockOperations for this Interface")
133: private interface Form {
134: @AField(name="MockService",description="The MockService to create or use",type=AFieldType.ENUMERATION)
135: public final static String MOCKSERVICE = "MockService";
136:
137: @AField(name="Operations",description="The Operations for which to Generate MockOperations",type=AFieldType.MULTILIST)
138: public final static String OPERATIONS = "Operations";
139:
140: @AField(name="Path",description="The URL path to mount on",type=AFieldType.STRING)
141: public final static String PATH = "Path";
142:
143: @AField(name="Port",description="The endpoint port to listen on",type=AFieldType.STRING)
144: public final static String PORT = "Port";
145:
146: @AField(name="Add Endpoint",description="Adds the MockServices endpoint to the mocked Interface",type=AFieldType.BOOLEAN)
147: public final static String ADD_ENDPOINT = "Add Endpoint";
148:
149: @AField(name="Start MockService",description="Starts the MockService immediately",type=AFieldType.BOOLEAN)
150: public final static String START_MOCKSERVICE = "Start MockService";
151: }
152: }
|