View Javadoc

1   /*
2    * $Id: CxfInboundMessageProcessorTestCase.java 20372 2010-11-28 18:49:05Z dandiep $
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.lifecycle.InitialisationException;
17  import org.mule.api.processor.MessageProcessor;
18  import org.mule.api.transport.OutputHandler;
19  import org.mule.module.cxf.builder.WebServiceMessageProcessorBuilder;
20  import org.mule.module.cxf.testmodels.Echo;
21  import org.mule.tck.AbstractMuleTestCase;
22  
23  public class CxfInboundMessageProcessorTestCase extends AbstractMuleTestCase
24  {
25      String msg = 
26          "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>" +
27              "<ns1:echo xmlns:ns1=\"http://testmodels.cxf.module.mule.org/\">" +
28                  "<text>echo</text>" +
29              "</ns1:echo>" +
30          "</soap:Body></soap:Envelope>";
31  
32      boolean gotEvent = false;
33      Object payload;
34      
35      public void testInbound() throws Exception
36      {
37          CxfInboundMessageProcessor processor = createCxfMessageProcessor();
38          
39          MessageProcessor messageProcessor = new MessageProcessor()
40          {
41              public MuleEvent process(MuleEvent event) throws MuleException
42              {
43                  payload = event.getMessage().getPayload();
44                  assertEquals("echo", payload);
45                  event.getMessage().setPayload("echo");
46                  gotEvent = true;
47                  return event;
48              }
49          };
50          processor.setListener(messageProcessor);
51          
52          MuleEvent event = getTestEvent(msg, getTestInboundEndpoint(MessageExchangePattern.REQUEST_RESPONSE));
53          
54          MuleEvent response = processor.process(event);
55          
56          Object payload = response.getMessage().getPayload();
57          assertTrue(payload instanceof OutputHandler);
58          
59          ((OutputHandler) payload).write(response, System.out);
60          assertTrue(gotEvent);
61      }
62      
63  
64      public void testOneWay() throws Exception
65      {
66          CxfInboundMessageProcessor processor = createCxfMessageProcessor();
67          
68          MessageProcessor messageProcessor = new MessageProcessor()
69          {
70              public MuleEvent process(MuleEvent event) throws MuleException
71              {
72                  payload = event.getMessage().getPayload();
73                  assertEquals("echo", payload);
74                  event.getMessage().setPayload("echo");
75                  gotEvent = true;
76                  return null;
77              }
78          };
79          processor.setListener(messageProcessor);
80          
81          MuleEvent event = getTestEvent(msg, getTestInboundEndpoint(MessageExchangePattern.ONE_WAY));
82          
83          MuleEvent response = processor.process(event);
84          
85          assertTrue(gotEvent);
86          assertNull(response);
87      }
88  
89      private CxfInboundMessageProcessor createCxfMessageProcessor()
90          throws InitialisationException, MuleException
91      {
92          CxfConfiguration config = new CxfConfiguration();
93          config.setMuleContext(muleContext);
94          config.initialise();
95          
96          // Build a CXF MessageProcessor
97          WebServiceMessageProcessorBuilder builder = new WebServiceMessageProcessorBuilder();
98          builder.setConfiguration(config);
99          builder.setServiceClass(Echo.class);
100         builder.setMuleContext(muleContext);
101         
102         CxfInboundMessageProcessor processor = builder.build();
103         processor.start();
104         return processor;
105     }
106 
107 }