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.operation;
014:
015: import com.eviware.soapui.SoapUI;
016: import com.eviware.soapui.impl.wsdl.WsdlOperation;
017: import com.eviware.soapui.impl.wsdl.WsdlProject;
018: import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
019: import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
020: import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
021: import com.eviware.soapui.model.support.ModelSupport;
022: import com.eviware.soapui.support.UISupport;
023: import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
024:
025: /**
026: * Creates a WsdlMockOperation for the specified WsdlOperation
027: *
028: * @author ole.matzura
029: */
030:
031: public class AddOperationToMockServiceAction extends
032: AbstractSoapUIAction<WsdlOperation> {
033: private static final String CREATE_MOCKSUITE_OPTION = "Create new..";
034: public static final String SOAPUI_ACTION_ID = "AddOperationToMockServiceAction";
035:
036: public AddOperationToMockServiceAction() {
037: super ("Add to MockService",
038: "Add this operation to a MockService");
039: }
040:
041: public void perform(WsdlOperation operation, Object param) {
042: String title = getName();
043:
044: WsdlMockService mockService = null;
045: WsdlProject project = (WsdlProject) operation.getInterface()
046: .getProject();
047:
048: while (mockService == null) {
049: if (project.getMockServiceCount() > 0) {
050: String[] mockServices = ModelSupport.getNames(project
051: .getMockServices(),
052: new String[] { CREATE_MOCKSUITE_OPTION });
053:
054: // prompt
055: String option = (String) UISupport.prompt(
056: "Select MockService for MockOperation", title,
057: mockServices);
058: if (option == null)
059: return;
060:
061: mockService = (WsdlMockService) project
062: .getMockServiceByName(option);
063: }
064:
065: // create new mocksuite?
066: if (mockService == null) {
067: String mockServiceName = UISupport.prompt(
068: "Enter name of new MockService", title,
069: "MockService "
070: + (project.getMockServiceCount() + 1));
071: if (mockServiceName == null
072: || mockServiceName.trim().length() == 0)
073: return;
074:
075: mockService = (WsdlMockService) project
076: .addNewMockService(mockServiceName);
077: }
078:
079: if (mockService.hasMockOperation(operation)) {
080: UISupport.showErrorMessage("MockService ["
081: + mockService.getName()
082: + "] already has a MockOperation for ["
083: + operation.getName()
084: + "], please select another MockService");
085: mockService = null;
086: }
087: }
088:
089: // add mockoperation
090: addOperationToMockService(operation, mockService);
091: }
092:
093: public boolean addOperationToMockService(WsdlOperation operation,
094: WsdlMockService mockService) {
095: if (mockService.hasMockOperation(operation)) {
096: UISupport.showErrorMessage("MockService ["
097: + mockService.getName()
098: + "] already has a MockOperation for ["
099: + operation.getName() + "]");
100: return false;
101: }
102:
103: WsdlMockOperation mockOperation = (WsdlMockOperation) mockService
104: .addNewMockOperation(operation);
105: WsdlMockResponse mockResponse = mockOperation
106: .addNewMockResponse("Response 1", false);
107:
108: mockResponse.setResponseContent(operation.createResponse(true));
109:
110: if (UISupport.confirm("Open MockResponse editor?", getName())) {
111: SoapUI.getDesktop().showDesktopPanel(mockResponse);
112: }
113:
114: return true;
115: }
116: }
|