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.request;
014:
015: import com.eviware.soapui.SoapUI;
016: import com.eviware.soapui.impl.wsdl.WsdlProject;
017: import com.eviware.soapui.impl.wsdl.WsdlRequest;
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.impl.wsdl.submit.transports.http.WsdlResponse;
022: import com.eviware.soapui.model.iface.Attachment;
023: import com.eviware.soapui.model.support.ModelSupport;
024: import com.eviware.soapui.support.UISupport;
025: import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
026:
027: /**
028: * Adds a WsdlRequest to a WsdlMockService, will create required WsdlMockOperation if neccessary
029: *
030: * @author ole.matzura
031: */
032:
033: public class AddRequestToMockServiceAction extends
034: AbstractSoapUIAction<WsdlRequest> {
035: private static final String CREATE_MOCKSUITE_OPTION = "Create new..";
036: public static final String SOAPUI_ACTION_ID = "AddRequestToMockServiceAction";
037:
038: public AddRequestToMockServiceAction() {
039: super ("Add to MockService",
040: "Adds the current response to a MockService");
041: }
042:
043: public void perform(WsdlRequest request, Object param) {
044: String title = getName();
045:
046: if (request != null && request.getResponse() == null) {
047: if (!UISupport
048: .confirm(
049: "Request is missing response, create default mock response instead?",
050: title)) {
051: return;
052: }
053: }
054:
055: WsdlMockService mockService = null;
056: WsdlMockOperation mockOperation = (WsdlMockOperation) param;
057: if (mockOperation != null)
058: mockService = mockOperation.getMockService();
059:
060: WsdlProject project = (WsdlProject) request.getOperation()
061: .getInterface().getProject();
062:
063: while (mockService == null) {
064: if (project.getMockServiceCount() > 0) {
065: String[] mockServices = ModelSupport.getNames(project
066: .getMockServices(),
067: new String[] { CREATE_MOCKSUITE_OPTION });
068:
069: // prompt
070: String option = (String) UISupport.prompt(
071: "Select MockService for MockOperation", title,
072: mockServices);
073: if (option == null)
074: return;
075:
076: mockService = (WsdlMockService) project
077: .getMockServiceByName(option);
078: }
079:
080: // create new mocksuite?
081: if (mockService == null) {
082: String mockServiceName = UISupport.prompt(
083: "Enter name of new MockService", title,
084: "MockService "
085: + (project.getMockServiceCount() + 1));
086: if (mockServiceName == null
087: || mockServiceName.trim().length() == 0)
088: return;
089:
090: mockService = (WsdlMockService) project
091: .addNewMockService(mockServiceName);
092: }
093:
094: mockOperation = mockService.getMockOperation(request
095: .getOperation());
096: if (mockOperation != null) {
097: Boolean retval = UISupport
098: .confirmOrCancel(
099: "MockService ["
100: + mockService.getName()
101: + "] already has a MockOperation for ["
102: + request.getOperation()
103: .getName()
104: + "],\r\nShould MockResponse be added to this MockOperation instead",
105: "Add Request to MockService");
106:
107: if (retval == null)
108: return;
109:
110: if (!retval.booleanValue())
111: mockService = null;
112: }
113: }
114:
115: // add mockoperation
116: if (mockOperation == null)
117: mockOperation = (WsdlMockOperation) mockService
118: .addNewMockOperation(request.getOperation());
119:
120: WsdlMockResponse mockResponse = mockOperation
121: .addNewMockResponse("Response "
122: + (1 + mockOperation.getMockResponseCount()),
123: false);
124:
125: // add expected response if available
126: if (request != null && request.getResponse() != null) {
127: WsdlResponse response = (WsdlResponse) request
128: .getResponse();
129: mockResponse.setResponseContent(response
130: .getContentAsString());
131:
132: Attachment[] attachments = response.getAttachments();
133: for (Attachment attachment : attachments) {
134: mockResponse.addAttachment(attachment);
135: }
136:
137: if (mockResponse.getResponseHeaders() != null
138: && mockResponse.getResponseHeaders().size() > 0
139: && UISupport
140: .confirm(
141: "Add current Response HTTP Headers to MockResponse",
142: title))
143: mockResponse.setResponseHeaders(response
144: .getResponseHeaders());
145: } else {
146: mockResponse.setResponseContent(request.getOperation()
147: .createResponse(true));
148: }
149:
150: if (UISupport.confirm("Open MockResponse editor?", title)) {
151: SoapUI.getDesktop().showDesktopPanel(mockResponse);
152: }
153: }
154: }
|