View Javadoc

1   /*
2    * $Id: CxfInboundMessageProcessorTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.cxf;
12  
13  import org.mule.MessageExchangePattern;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.processor.MessageProcessor;
17  import org.mule.api.transport.OutputHandler;
18  import org.mule.module.cxf.builder.WebServiceMessageProcessorBuilder;
19  import org.mule.module.cxf.testmodels.Echo;
20  import org.mule.tck.junit4.AbstractMuleContextTestCase;
21  
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNull;
26  import static org.junit.Assert.assertTrue;
27  
28  public class CxfInboundMessageProcessorTestCase extends AbstractMuleContextTestCase
29  {
30      String msg = 
31          "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>" +
32              "<ns1:echo xmlns:ns1=\"http://testmodels.cxf.module.mule.org/\">" +
33                  "<text>echo</text>" +
34              "</ns1:echo>" +
35          "</soap:Body></soap:Envelope>";
36  
37      boolean gotEvent = false;
38      Object payload;
39      
40      @Test
41      public void testInbound() throws Exception
42      {
43          CxfInboundMessageProcessor processor = createCxfMessageProcessor();
44          
45          MessageProcessor messageProcessor = new MessageProcessor()
46          {
47              public MuleEvent process(MuleEvent event) throws MuleException
48              {
49                  payload = event.getMessage().getPayload();
50                  assertEquals("echo", payload);
51                  event.getMessage().setPayload("echo");
52                  gotEvent = true;
53                  return event;
54              }
55          };
56          processor.setListener(messageProcessor);
57          
58          MuleEvent event = getTestEvent(msg, getTestInboundEndpoint(MessageExchangePattern.REQUEST_RESPONSE));
59          
60          MuleEvent response = processor.process(event);
61          
62          Object payload = response.getMessage().getPayload();
63          assertTrue(payload instanceof OutputHandler);
64          
65          ((OutputHandler) payload).write(response, System.out);
66          assertTrue(gotEvent);
67      }
68      
69      @Test
70      public void testOneWay() throws Exception
71      {
72          CxfInboundMessageProcessor processor = createCxfMessageProcessor();
73          
74          MessageProcessor messageProcessor = new MessageProcessor()
75          {
76              public MuleEvent process(MuleEvent event) throws MuleException
77              {
78                  payload = event.getMessage().getPayload();
79                  assertEquals("echo", payload);
80                  event.getMessage().setPayload("echo");
81                  gotEvent = true;
82                  return null;
83              }
84          };
85          processor.setListener(messageProcessor);
86          
87          MuleEvent event = getTestEvent(msg, getTestInboundEndpoint(MessageExchangePattern.ONE_WAY));
88          
89          MuleEvent response = processor.process(event);
90          
91          assertTrue(gotEvent);
92          assertNull(response);
93      }
94  
95      private CxfInboundMessageProcessor createCxfMessageProcessor() throws MuleException
96      {
97          CxfConfiguration config = new CxfConfiguration();
98          config.setMuleContext(muleContext);
99          config.initialise();
100         
101         // Build a CXF MessageProcessor
102         WebServiceMessageProcessorBuilder builder = new WebServiceMessageProcessorBuilder();
103         builder.setConfiguration(config);
104         builder.setServiceClass(Echo.class);
105         builder.setMuleContext(muleContext);
106         
107         CxfInboundMessageProcessor processor = builder.build();
108         processor.start();
109         return processor;
110     }
111 
112 }