001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.jaxws.provider;
020:
021: import java.io.ByteArrayInputStream;
022: import java.util.Iterator;
023:
024: import javax.xml.namespace.QName;
025: import javax.xml.soap.AttachmentPart;
026: import javax.xml.soap.DetailEntry;
027: import javax.xml.soap.MessageFactory;
028: import javax.xml.soap.Node;
029: import javax.xml.soap.SOAPBody;
030: import javax.xml.soap.SOAPConstants;
031: import javax.xml.soap.SOAPElement;
032: import javax.xml.soap.SOAPFault;
033: import javax.xml.soap.SOAPMessage;
034: import javax.xml.transform.stream.StreamSource;
035: import javax.xml.ws.Binding;
036: import javax.xml.ws.Dispatch;
037: import javax.xml.ws.Service;
038: import javax.xml.ws.soap.SOAPBinding;
039: import javax.xml.ws.soap.SOAPFaultException;
040:
041: import org.apache.axis2.jaxws.provider.soapmsg.SoapMessageProvider;
042: import org.apache.axis2.jaxws.TestLogger;
043:
044: /**
045: * Tests Dispatch<SOAPMessage> client and a Provider<SOAPMessage> service.
046: * The client and service interaction tests various xml and attachment scenarios
047: *
048: */
049: public class SoapMessageProviderTests extends ProviderTestCase {
050:
051: private String endpointUrl = "http://localhost:8080/axis2/services/SoapMessageProviderService";
052: private QName serviceName = new QName("http://ws.apache.org/axis2",
053: "SoapMessageProviderService");
054:
055: private String reqMsgStart = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
056: + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>";;
057:
058: private String reqMsgEnd = "</soap:Body></soap:Envelope>";
059:
060: private String XML_INVOKE = "<ns2:invokeOp xmlns:ns2=\"http://org.test.soapmessage\"><invoke_str>"
061: + SoapMessageProvider.XML_REQUEST
062: + "</invoke_str></ns2:invokeOp>";
063: private String EMPTYBODY_INVOKE = "<ns2:invokeOp xmlns:ns2=\"http://org.test.soapmessage\"><invoke_str>"
064: + SoapMessageProvider.XML_EMPTYBODY_REQUEST
065: + "</invoke_str></ns2:invokeOp>";
066: private String ATTACHMENT_INVOKE = "<ns2:invokeOp xmlns:ns2=\"http://org.test.soapmessage\"><invoke_str>"
067: + SoapMessageProvider.XML_ATTACHMENT_REQUEST
068: + "</invoke_str></ns2:invokeOp>";
069: private String MTOM_INVOKE = "<ns2:invokeOp xmlns:ns2=\"http://org.test.soapmessage\"><invoke_str>"
070: + SoapMessageProvider.XML_MTOM_REQUEST
071: + "</invoke_str>"
072: + SoapMessageProvider.MTOM_REF + "</ns2:invokeOp>";
073: private String SWAREF_INVOKE = "<ns2:invokeOp xmlns:ns2=\"http://org.test.soapmessage\"><invoke_str>"
074: + SoapMessageProvider.XML_SWAREF_REQUEST
075: + "</invoke_str>"
076: + SoapMessageProvider.SWAREF_REF + "</ns2:invokeOp>";
077: private String XML_FAULT_INVOKE = "<ns2:invokeOp xmlns:ns2=\"http://org.test.soapmessage\"><invoke_str>"
078: + SoapMessageProvider.XML_FAULT_REQUEST
079: + "</invoke_str></ns2:invokeOp>";
080: private String XML_WSE_INVOKE = "<ns2:invokeOp xmlns:ns2=\"http://org.test.soapmessage\"><invoke_str>"
081: + SoapMessageProvider.XML_WSE_REQUEST
082: + "</invoke_str></ns2:invokeOp>";
083:
084: protected void setUp() throws Exception {
085: super .setUp();
086: }
087:
088: protected void tearDown() throws Exception {
089: super .tearDown();
090: }
091:
092: public SoapMessageProviderTests(String name) {
093: super (name);
094: }
095:
096: /**
097: * Sends an SOAPMessage containing only xml data to the web service.
098: * Receives a response containing just xml data.
099: */
100: public void testProviderSourceXMLOnly() {
101: try {
102: // Create the dispatch
103: Dispatch<SOAPMessage> dispatch = createDispatch();
104:
105: // Create the SOAPMessage
106: String msg = reqMsgStart + XML_INVOKE + reqMsgEnd;
107: MessageFactory factory = MessageFactory.newInstance();
108: SOAPMessage request = factory.createMessage(null,
109: new ByteArrayInputStream(msg.getBytes()));
110:
111: // Test the transport headers by sending a content description
112: request
113: .setContentDescription(SoapMessageProvider.XML_REQUEST);
114:
115: // Dispatch
116: TestLogger.logger
117: .debug(">> Invoking SourceMessageProviderDispatch");
118: SOAPMessage response = dispatch.invoke(request);
119:
120: // Check for valid content description
121: assertNotNull(response.getContentDescription());
122: assertEquals(SoapMessageProvider.XML_RESPONSE, response
123: .getContentDescription());
124:
125: // Check assertions and get the data element
126: SOAPElement dataElement = assertResponseXML(response,
127: SoapMessageProvider.XML_RESPONSE);
128:
129: assertTrue(countAttachments(response) == 0);
130:
131: // Print out the response
132: TestLogger.logger.debug(">> Response ["
133: + response.toString() + "]");
134: response.writeTo(System.out);
135:
136: } catch (Exception e) {
137: e.printStackTrace();
138: fail("Caught exception " + e);
139: }
140:
141: }
142:
143: /**
144: * Sends an SOAPMessage containing only xml data to the web service.
145: * Receives a response containing an empty body
146: */
147: public void testProviderSourceXMLEmptyBody() {
148: try {
149: // Create the dispatch
150: Dispatch<SOAPMessage> dispatch = createDispatch();
151:
152: // Create the SOAPMessage
153: String msg = reqMsgStart + EMPTYBODY_INVOKE + reqMsgEnd;
154: MessageFactory factory = MessageFactory.newInstance();
155: SOAPMessage request = factory.createMessage(null,
156: new ByteArrayInputStream(msg.getBytes()));
157:
158: // Test the transport headers by sending a content description
159: request
160: .setContentDescription(SoapMessageProvider.XML_EMPTYBODY_REQUEST);
161:
162: // Dispatch
163: TestLogger.logger
164: .debug(">> Invoking SourceMessageProviderDispatch");
165: SOAPMessage response = dispatch.invoke(request);
166:
167: // Check assertions
168: assertTrue(response != null);
169: assertTrue(response.getSOAPBody() != null);
170: assertTrue(response.getSOAPBody().getFirstChild() == null); // There should be nothing in the body
171:
172: assertTrue(countAttachments(response) == 0);
173:
174: // Print out the response
175: TestLogger.logger.debug(">> Response ["
176: + response.toString() + "]");
177: response.writeTo(System.out);
178:
179: } catch (Exception e) {
180: e.printStackTrace();
181: fail("Caught exception " + e);
182: }
183:
184: }
185:
186: /**
187: * Sends an SOAPMessage containing only xml data
188: * Provider will throw a Fault
189: */
190: public void testProviderSOAPFault() throws Exception {
191:
192: // Create the dispatch
193: Dispatch<SOAPMessage> dispatch = createDispatch();
194:
195: // Create the SOAPMessage
196: String msg = reqMsgStart + XML_FAULT_INVOKE + reqMsgEnd;
197: MessageFactory factory = MessageFactory.newInstance();
198: SOAPMessage request = factory.createMessage(null,
199: new ByteArrayInputStream(msg.getBytes()));
200:
201: // Test the transport headers by sending a content description
202: request
203: .setContentDescription(SoapMessageProvider.XML_FAULT_REQUEST);
204:
205: try {
206: // Dispatch
207: TestLogger.logger
208: .debug(">> Invoking SourceMessageProviderDispatch");
209: SOAPMessage response = dispatch.invoke(request);
210: assertTrue("Expected failure", false);
211: } catch (SOAPFaultException e) {
212: // Okay
213: SOAPFault fault = e.getFault();
214: assertTrue(fault != null);
215: assertTrue(fault.getFaultString().equals("sample fault"));
216: QName expectedFaultCode = new QName(
217: SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE, "Client");
218: assertTrue(fault.getFaultCodeAsQName().equals(
219: expectedFaultCode));
220: assertTrue(fault.getDetail() != null);
221: DetailEntry de = (DetailEntry) fault.getDetail()
222: .getDetailEntries().next();
223: assertTrue(de != null);
224: assertTrue(de.getLocalName().equals("detailEntry"));
225: assertTrue(de.getValue().equals("sample detail"));
226: assertTrue(fault.getFaultActor().equals("sample actor"));
227: }
228: }
229:
230: /**
231: * Sends an SOAPMessage containing only xml data
232: * Provider will throw a generic WebServicesException
233: */
234: public void testProviderWebServiceException() throws Exception {
235:
236: // Create the dispatch
237: Dispatch<SOAPMessage> dispatch = createDispatch();
238:
239: // Create the SOAPMessage
240: String msg = reqMsgStart + XML_WSE_INVOKE + reqMsgEnd;
241: MessageFactory factory = MessageFactory.newInstance();
242: SOAPMessage request = factory.createMessage(null,
243: new ByteArrayInputStream(msg.getBytes()));
244:
245: // Test the transport headers by sending a content description
246: request
247: .setContentDescription(SoapMessageProvider.XML_WSE_REQUEST);
248:
249: try {
250: // Dispatch
251: TestLogger.logger
252: .debug(">> Invoking SourceMessageProviderDispatch");
253: SOAPMessage response = dispatch.invoke(request);
254: assertTrue("Expected failure", false);
255: } catch (SOAPFaultException e) {
256: // Okay...SOAPFaultException should be thrown
257: SOAPFault fault = e.getFault();
258: assertTrue(fault != null);
259: assertTrue(fault.getFaultString()
260: .equals("A WSE was thrown"));
261: }
262: }
263:
264: /**
265: * Sends an SOAPMessage containing xml data and raw attachments to the web service.
266: * Receives a response containing xml data and the same raw attachments.
267: */
268:
269: public void testProviderSourceRawAttachment() {
270: // Raw Attachments are attachments that are not referenced in the xml with MTOM or SWARef.
271: // Currently there is no support in Axis 2 for these kinds of attachments.
272: // The belief is that most customers will use MTOM. Some legacy customers will use SWARef.
273: // Raw Attachments may be so old that no customers need this behavior.
274: try {
275: // Create the dispatch
276: Dispatch<SOAPMessage> dispatch = createDispatch();
277:
278: // Create the SOAPMessage
279: String msg = reqMsgStart + ATTACHMENT_INVOKE + reqMsgEnd;
280: MessageFactory factory = MessageFactory.newInstance();
281: SOAPMessage request = factory.createMessage(null,
282: new ByteArrayInputStream(msg.getBytes()));
283:
284: // Add the Attachment
285: AttachmentPart ap = request
286: .createAttachmentPart(
287: SoapMessageProvider.TEXT_XML_ATTACHMENT,
288: "text/xml");
289: ap.setContentId(SoapMessageProvider.ID);
290: request.addAttachmentPart(ap);
291:
292: TestLogger.logger.debug("Request Message:");
293: request.writeTo(System.out);
294:
295: // Dispatch
296: TestLogger.logger
297: .debug(">> Invoking SourceMessageProviderDispatch");
298: SOAPMessage response = dispatch.invoke(request);
299:
300: // Check assertions and get the data element
301: SOAPElement dataElement = assertResponseXML(response,
302: SoapMessageProvider.XML_ATTACHMENT_RESPONSE);
303: assertTrue(countAttachments(response) == 1);
304:
305: // Get the Attachment
306: AttachmentPart attachmentPart = (AttachmentPart) response
307: .getAttachments().next();
308:
309: // Check the attachment
310: StreamSource contentSS = (StreamSource) attachmentPart
311: .getContent();
312: String content = SoapMessageProvider.getAsString(contentSS);
313: assertTrue(content != null);
314: assertTrue(content
315: .contains(SoapMessageProvider.TEXT_XML_ATTACHMENT));
316:
317: // Print out the response
318: TestLogger.logger.debug(">> Response ["
319: + response.toString() + "]");
320: response.writeTo(System.out);
321:
322: } catch (Exception e) {
323: e.printStackTrace();
324: fail("Caught exception " + e);
325: }
326:
327: }
328:
329: /**
330: * Sends an SOAPMessage containing xml data and mtom attachment.
331: * Receives a response containing xml data and the mtom attachment.
332: */
333: public void testProviderSourceMTOM() {
334: try {
335: // Create the dispatch
336: Dispatch<SOAPMessage> dispatch = createDispatch();
337:
338: // MTOM should be automatically detected. There is no need to set it
339: //Binding binding = dispatch.getBinding();
340: //SOAPBinding soapBinding = (SOAPBinding) binding;
341: //soapBinding.setMTOMEnabled(true);
342:
343: // Create the SOAPMessage
344: String msg = reqMsgStart + MTOM_INVOKE + reqMsgEnd;
345: MessageFactory factory = MessageFactory.newInstance();
346: SOAPMessage request = factory.createMessage(null,
347: new ByteArrayInputStream(msg.getBytes()));
348:
349: // Add the Attachment
350: AttachmentPart ap = request
351: .createAttachmentPart(
352: SoapMessageProvider.TEXT_XML_ATTACHMENT,
353: "text/xml");
354: ap.setContentId(SoapMessageProvider.ID);
355: request.addAttachmentPart(ap);
356:
357: TestLogger.logger.debug("Request Message:");
358: request.writeTo(System.out);
359:
360: // Dispatch
361: TestLogger.logger
362: .debug(">> Invoking SourceMessageProviderDispatch");
363: SOAPMessage response = dispatch.invoke(request);
364:
365: // Check assertions and get the data element
366: SOAPElement dataElement = assertResponseXML(response,
367: SoapMessageProvider.XML_MTOM_RESPONSE);
368: assertTrue(countAttachments(response) == 1);
369:
370: // Get the Attachment
371: AttachmentPart attachmentPart = (AttachmentPart) response
372: .getAttachments().next();
373:
374: // Check the attachment
375: StreamSource contentSS = (StreamSource) attachmentPart
376: .getContent();
377: String content = SoapMessageProvider.getAsString(contentSS);
378: assertTrue(content != null);
379: assertTrue(content
380: .contains(SoapMessageProvider.TEXT_XML_ATTACHMENT));
381:
382: // Print out the response
383: TestLogger.logger.debug(">> Response ["
384: + response.toString() + "]");
385: response.writeTo(System.out);
386:
387: } catch (Exception e) {
388: e.printStackTrace();
389: fail("Caught exception " + e);
390: }
391:
392: }
393:
394: /**
395: * Sends an SOAPMessage containing xml data and a swaref attachment to the web service.
396: * Receives a response containing xml data and the swaref attachment attachment.
397: */
398: public void testProviderSourceSWARef() {
399: try {
400: // Create the dispatch
401: Dispatch<SOAPMessage> dispatch = createDispatch();
402:
403: // Create the SOAPMessage
404: String msg = reqMsgStart + SWAREF_INVOKE + reqMsgEnd;
405: MessageFactory factory = MessageFactory.newInstance();
406: SOAPMessage request = factory.createMessage(null,
407: new ByteArrayInputStream(msg.getBytes()));
408:
409: // Add the Attachment
410: AttachmentPart ap = request
411: .createAttachmentPart(
412: SoapMessageProvider.TEXT_XML_ATTACHMENT,
413: "text/xml");
414: ap.setContentId(SoapMessageProvider.ID);
415: request.addAttachmentPart(ap);
416:
417: TestLogger.logger.debug("Request Message:");
418: request.writeTo(System.out);
419:
420: // Dispatch
421: TestLogger.logger
422: .debug(">> Invoking SourceMessageProviderDispatch");
423: SOAPMessage response = dispatch.invoke(request);
424:
425: // Check assertions and get the data element
426: SOAPElement dataElement = assertResponseXML(response,
427: SoapMessageProvider.XML_SWAREF_RESPONSE);
428: assertTrue(countAttachments(response) == 1);
429:
430: // Get the Attachment
431: AttachmentPart attachmentPart = (AttachmentPart) response
432: .getAttachments().next();
433:
434: // Check the attachment
435: StreamSource contentSS = (StreamSource) attachmentPart
436: .getContent();
437: String content = SoapMessageProvider.getAsString(contentSS);
438: assertTrue(content != null);
439: assertTrue(content
440: .contains(SoapMessageProvider.TEXT_XML_ATTACHMENT));
441: assertEquals(SoapMessageProvider.ID, attachmentPart
442: .getContentId());
443:
444: // Print out the response
445: TestLogger.logger.debug(">> Response ["
446: + response.toString() + "]");
447: response.writeTo(System.out);
448:
449: } catch (Exception e) {
450: e.printStackTrace();
451: fail("Caught exception " + e);
452: }
453:
454: }
455:
456: /**
457: * @return
458: * @throws Exception
459: */
460: private Dispatch<SOAPMessage> createDispatch() throws Exception {
461: Service svc = Service.create(serviceName);
462: svc.addPort(portName, null, endpointUrl);
463: Dispatch<SOAPMessage> dispatch = svc.createDispatch(portName,
464: SOAPMessage.class, Service.Mode.MESSAGE);
465: return dispatch;
466: }
467:
468: /**
469: * Common assertion checking of the response
470: * @param msg
471: * @param expectedText
472: * @return SOAPElement representing the data element
473: */
474: private SOAPElement assertResponseXML(SOAPMessage msg,
475: String expectedText) throws Exception {
476: assertTrue(msg != null);
477: SOAPBody body = msg.getSOAPBody();
478: assertTrue(body != null);
479:
480: Node invokeElement = (Node) body.getFirstChild();
481: assertTrue(invokeElement instanceof SOAPElement);
482: assertEquals(SoapMessageProvider.RESPONSE_NAME, invokeElement
483: .getLocalName());
484:
485: Node dataElement = (Node) invokeElement.getFirstChild();
486: assertTrue(dataElement instanceof SOAPElement);
487: assertEquals(SoapMessageProvider.RESPONSE_DATA_NAME,
488: dataElement.getLocalName());
489:
490: // TODO AXIS2 SAAJ should (but does not) support the getTextContent();
491: // String text = dataElement.getTextContent();
492: String text = dataElement.getValue();
493: assertEquals("Found (" + text + ") but expected ("
494: + expectedText + ")", expectedText, text);
495:
496: return (SOAPElement) dataElement;
497: }
498:
499: /**
500: * Count Attachments
501: * @param msg
502: * @return
503: */
504: private int countAttachments(SOAPMessage msg) {
505: Iterator it = msg.getAttachments();
506: int count = 0;
507: assertTrue(it != null);
508: while (it.hasNext()) {
509: it.next();
510: count++;
511: }
512: return count;
513: }
514: }
|