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