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.panels.mockoperation.actions;
014:
015: import java.awt.Dimension;
016: import java.io.File;
017: import java.io.IOException;
018: import java.net.MalformedURLException;
019: import java.net.URL;
020: import java.util.Calendar;
021:
022: import org.wsI.testing.x2003.x03.common.AddStyleSheet;
023: import org.wsI.testing.x2003.x03.log.Environment;
024: import org.wsI.testing.x2003.x03.log.HttpMessageEntry;
025: import org.wsI.testing.x2003.x03.log.Implementation;
026: import org.wsI.testing.x2003.x03.log.Log;
027: import org.wsI.testing.x2003.x03.log.LogDocument;
028: import org.wsI.testing.x2003.x03.log.MessageEntry;
029: import org.wsI.testing.x2003.x03.log.Monitor;
030: import org.wsI.testing.x2003.x03.log.NameVersionPair;
031: import org.wsI.testing.x2003.x03.log.TcpMessageType;
032: import org.wsI.testing.x2004.x07.analyzerConfig.AssertionResults;
033: import org.wsI.testing.x2004.x07.analyzerConfig.Configuration;
034: import org.wsI.testing.x2004.x07.analyzerConfig.ConfigurationDocument;
035: import org.wsI.testing.x2004.x07.analyzerConfig.LogFile;
036: import org.wsI.testing.x2004.x07.analyzerConfig.ReportFile;
037: import org.wsI.testing.x2004.x07.analyzerConfig.LogFile.CorrelationType;
038:
039: import com.eviware.soapui.SoapUI;
040: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
041: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
042: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
043: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.RunnerContext;
044: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
045: import com.eviware.soapui.impl.wsdl.actions.iface.tools.wsi.WSIReportPanel;
046: import com.eviware.soapui.impl.wsdl.mock.WsdlMockRequest;
047: import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
048: import com.eviware.soapui.model.mock.MockService;
049: import com.eviware.soapui.model.settings.Settings;
050: import com.eviware.soapui.settings.WSISettings;
051: import com.eviware.soapui.support.UISupport;
052: import com.eviware.soapui.support.types.StringToStringMap;
053: import com.eviware.soapui.ui.support.DefaultDesktopPanel;
054:
055: /**
056: * Validates the current request/response exchange of a WsdlMockResponse with the WS-I tools
057: *
058: * @author Ole.Matzura
059: */
060:
061: public class WSIValidateResponseAction extends
062: AbstractToolsAction<WsdlMockResponse> {
063: private String configFile;
064: private File logFile;
065:
066: public WSIValidateResponseAction() {
067: super ("Check WS-I Compliance",
068: "Validates the current request/response againt the WS-I Basic Profile");
069: // putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "alt W" ));
070:
071: // setEnabled( request != null && request.getMockResult() != null );
072: }
073:
074: protected void generate(StringToStringMap values,
075: ToolHost toolHost, WsdlMockResponse modelItem)
076: throws Exception {
077: if (modelItem.getMockResult() == null) {
078: UISupport
079: .showErrorMessage("Request/Response required for WS-I validations");
080: return;
081: }
082:
083: String wsiDir = SoapUI.getSettings().getString(
084: WSISettings.WSI_LOCATION, null);
085: if (wsiDir == null) {
086: UISupport
087: .showErrorMessage("WSI Test Tools directory must be set in global preferences");
088: return;
089: }
090:
091: if (modelItem.getAttachmentCount() > 0) {
092: if (!UISupport.confirm(
093: "Response contains attachments which is not supported by "
094: + "validation tools, validate anyway?",
095: "Validation Warning"))
096: return;
097: }
098:
099: ProcessBuilder builder = new ProcessBuilder();
100:
101: File reportFile = File.createTempFile("wsi-report", ".xml");
102:
103: ArgumentBuilder args = buildArgs(reportFile, modelItem);
104: builder.command(args.getArgs());
105: builder.directory(new File(wsiDir + File.separatorChar + "java"
106: + File.separatorChar + "bin"));
107:
108: toolHost.run(new WSIProcessToolRunner(builder, reportFile,
109: modelItem));
110: }
111:
112: private ArgumentBuilder buildArgs(File reportFile,
113: WsdlMockResponse modelItem) throws Exception {
114: File logFile = buildLog(modelItem);
115: File file = buildConfig(reportFile, logFile, modelItem);
116: Settings settings = modelItem.getSettings();
117:
118: ArgumentBuilder builder = new ArgumentBuilder(
119: new StringToStringMap());
120: builder.startScript("Analyzer", ".bat", ".sh");
121:
122: builder.addArgs("-config", file.getAbsolutePath());
123:
124: // add this to command-line due to bug in wsi-tools (?)
125: if (settings.getBoolean(WSISettings.ASSERTION_DESCRIPTION))
126: builder.addArgs("-assertionDescription", "true");
127:
128: return builder;
129: }
130:
131: private File buildLog(WsdlMockResponse modelItem) throws Exception {
132: LogDocument logDoc = LogDocument.Factory.newInstance();
133: Log log = logDoc.addNewLog();
134: log.setTimestamp(Calendar.getInstance());
135:
136: addMonitorConfig(log);
137: addMessageConfig(log, modelItem);
138:
139: logFile = File.createTempFile("wsi-analyzer-log", ".xml");
140: logDoc.save(logFile);
141: return logFile;
142: }
143:
144: private File buildConfig(File reportFile, File logFile,
145: WsdlMockResponse modelItem) throws IOException {
146: Settings settings = modelItem.getSettings();
147:
148: ConfigurationDocument configDoc = ConfigurationDocument.Factory
149: .newInstance();
150: Configuration config = configDoc.addNewConfiguration();
151:
152: config.setVerbose(settings.getBoolean(WSISettings.VERBOSE));
153: AssertionResults results = config.addNewAssertionResults();
154: results.setType(AssertionResults.Type.Enum.forString(settings
155: .getString(WSISettings.RESULTS_TYPE,
156: AssertionResults.Type.ONLY_FAILED.toString())));
157:
158: results.setMessageEntry(settings
159: .getBoolean(WSISettings.MESSAGE_ENTRY));
160: results.setFailureMessage(settings
161: .getBoolean(WSISettings.FAILURE_MESSAGE));
162: results.setAssertionDescription(settings
163: .getBoolean(WSISettings.ASSERTION_DESCRIPTION));
164:
165: ReportFile report = config.addNewReportFile();
166: report.setLocation(reportFile.getAbsolutePath());
167: report.setReplace(true);
168:
169: AddStyleSheet stylesheet = report.addNewAddStyleSheet();
170: stylesheet
171: .setHref(".\\..\\common\\Profiles\\SSBP10_BP11_TAD.xml");
172: stylesheet.setType("text/xsl");
173: stylesheet.setAlternate(false);
174:
175: config
176: .setTestAssertionsFile("../../common/profiles/SSBP10_BP11_TAD.xml");
177:
178: LogFile logFileConfig = config.addNewLogFile();
179: logFileConfig.setStringValue(logFile.getAbsolutePath());
180: logFileConfig.setCorrelationType(CorrelationType.ENDPOINT);
181:
182: /*
183: WsdlInterface iface = (WsdlInterface) modelItem.getOperation().getInterface();
184:
185: WsdlReferenceConfig wsdlRef = config.addNewWsdlReference();
186: wsdlRef.setWsdlURI( iface.getDefinition() );
187: WsdlElementReferenceConfig wsdlElement = wsdlRef.addNewWsdlElement();
188: wsdlElement.setType( WsdlElementTypeConfig.BINDING );
189: wsdlElement.setStringValue( iface.getBindingName().getLocalPart() );
190: wsdlElement.setNamespace( iface.getBindingName().getNamespaceURI() );
191: wsdlRef.setServiceLocation( modelItem.getEndpoint() );
192: */
193:
194: configFile = configDoc.toString();
195:
196: File file = File.createTempFile("wsi-analyzer-config", ".xml");
197:
198: configDoc.save(file);
199: return file;
200: }
201:
202: private void addMessageConfig(Log log, WsdlMockResponse modelItem)
203: throws MalformedURLException {
204: HttpMessageEntry requestMessage = HttpMessageEntry.Factory
205: .newInstance();
206: WsdlMockRequest mockRequest = modelItem.getMockResult()
207: .getMockRequest();
208: requestMessage.addNewMessageContent().setStringValue(
209: mockRequest.getRequestContent());
210: requestMessage.setConversationID("1");
211: requestMessage.setTimestamp(Calendar.getInstance());
212: requestMessage.setID("1");
213: MockService mockService = modelItem.getMockOperation()
214: .getMockService();
215: URL endpoint = new URL("http://127.0.0.1:"
216: + mockService.getPort() + mockService.getPath());
217: requestMessage.setSenderHostAndPort("localhost");
218:
219: if (endpoint.getPort() > 0)
220: requestMessage.setReceiverHostAndPort(endpoint.getHost()
221: + ":" + endpoint.getPort());
222: else
223: requestMessage.setReceiverHostAndPort(endpoint.getHost());
224:
225: requestMessage.setType(TcpMessageType.REQUEST);
226:
227: HttpMessageEntry responseMessage = HttpMessageEntry.Factory
228: .newInstance();
229: responseMessage.addNewMessageContent().setStringValue(
230: modelItem.getMockResult().getResponseContent());
231: responseMessage.setConversationID("1");
232: responseMessage.setType(TcpMessageType.RESPONSE);
233: responseMessage.setTimestamp(Calendar.getInstance());
234: responseMessage.setID("2");
235: responseMessage.setSenderHostAndPort(requestMessage
236: .getReceiverHostAndPort());
237: responseMessage.setReceiverHostAndPort(requestMessage
238: .getSenderHostAndPort());
239:
240: String requestHeaders = buildHttpHeadersString(mockRequest
241: .getRequestHeaders());
242: requestMessage.setHttpHeaders("POST " + mockRequest.getPath()
243: + " " + mockRequest.getProtocol() + "\r\n"
244: + requestHeaders);
245:
246: responseMessage.setHttpHeaders("HTTP/1.1 200 OK"
247: + buildHttpHeadersString(modelItem.getMockResult()
248: .getResponseHeaders()));
249:
250: log.setMessageEntryArray(new MessageEntry[] { requestMessage,
251: responseMessage });
252: }
253:
254: private void addMonitorConfig(Log log) throws Exception {
255: Monitor monitor = log.addNewMonitor();
256:
257: monitor.setVersion("1.5");
258: monitor.setReleaseDate(Calendar.getInstance());
259:
260: org.wsI.testing.x2003.x03.monitorConfig.Configuration conf = monitor
261: .addNewConfiguration();
262: conf.setCleanupTimeoutSeconds(0);
263: conf.setLogDuration(0);
264:
265: org.wsI.testing.x2003.x03.monitorConfig.LogFile logFileConf = conf
266: .addNewLogFile();
267: logFileConf.setLocation("report.xml");
268: logFileConf.setReplace(true);
269:
270: /*
271: ArrayOfRedirectConfig mintConf = conf.addNewManInTheMiddle();
272: RedirectConfig redirect = mintConf.addNewRedirect();
273: redirect.setListenPort( 9999 );
274: redirect.setMaxConnections( 10 );
275: redirect.setReadTimeoutSeconds( 10 );
276:
277: URL endpoint = new URL( modelItem.getEndpoint());
278: if( endpoint.getPort() > 0 )
279: redirect.setSchemeAndHostPort( endpoint.getHost() + ":" + endpoint.getPort());
280: else
281: redirect.setSchemeAndHostPort( endpoint.getHost() );
282: */
283:
284: Environment env = monitor.addNewEnvironment();
285: NameVersionPair osConf = env.addNewOperatingSystem();
286: osConf.setName("Windows");
287: osConf.setVersion("2003");
288:
289: NameVersionPair rtConf = env.addNewRuntime();
290: rtConf.setName("java");
291: rtConf.setVersion("1.5");
292:
293: NameVersionPair xpConf = env.addNewXmlParser();
294: xpConf.setName("xmlbeans");
295: xpConf.setVersion("2.2.0");
296:
297: Implementation implConf = monitor.addNewImplementer();
298: implConf.setName("soapui");
299: implConf.setLocation("here");
300: }
301:
302: private String buildHttpHeadersString(
303: StringToStringMap requestHeaders) {
304: StringBuffer buffer = new StringBuffer();
305:
306: if (requestHeaders.containsKey("#status#")) {
307: buffer.append(requestHeaders.get("#status#"))
308: .append("\r\n");
309: }
310:
311: for (String header : requestHeaders.keySet()) {
312: if (!header.equals("#status#"))
313: buffer.append(header).append(": ").append(
314: requestHeaders.get(header)).append("\r\n");
315: }
316:
317: return buffer.toString();
318: }
319:
320: private class WSIProcessToolRunner extends ProcessToolRunner {
321: private final File reportFile;
322: private final WsdlMockResponse modelItem;
323:
324: public WSIProcessToolRunner(ProcessBuilder builder,
325: File reportFile, WsdlMockResponse modelItem) {
326: super (builder, "WSI Message Validation", modelItem);
327: this .reportFile = reportFile;
328: this .modelItem = modelItem;
329: }
330:
331: public String getDescription() {
332: return "Running WSI Analysis tools..";
333: }
334:
335: protected void afterRun(int exitCode, RunnerContext context) {
336: try {
337: if (exitCode == 0
338: && context.getStatus() == RunnerContext.RunnerStatus.FINISHED) {
339: WSIReportPanel panel = new WSIReportPanel(
340: reportFile, configFile, logFile);
341: panel.setPreferredSize(new Dimension(600, 400));
342:
343: UISupport
344: .showDesktopPanel(new DefaultDesktopPanel(
345: "WS-I Report",
346: "WS-I Report for validation of messages in MockResponse ["
347: + modelItem.getName() + "]",
348: panel));
349: }
350: } catch (Exception e) {
351: UISupport.showErrorMessage(e);
352: }
353: }
354:
355: public boolean showLog() {
356: return modelItem.getSettings().getBoolean(
357: WSISettings.SHOW_LOG);
358: }
359: }
360: }
|