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.hamcrest.core.Is;
10  import org.hamcrest.core.IsInstanceOf;
11  import org.mule.MessageExchangePattern;
12  import org.mule.api.MuleEvent;
13  import org.mule.api.MuleException;
14  import org.mule.api.component.simple.EchoService;
15  import org.mule.api.processor.MessageProcessor;
16  import org.mule.module.cxf.builder.SimpleClientMessageProcessorBuilder;
17  import org.mule.tck.junit4.AbstractMuleContextTestCase;
18  
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertThat;
23  import static org.junit.Assert.assertTrue;
24  
25  public class CxfOutboundMessageProcessorTestCase extends AbstractMuleContextTestCase
26  {
27      String msg = 
28          "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>" +
29              "<ns1:echo xmlns:ns1=\"http://simple.component.api.mule.org/\">" +
30                  "<ns1:return>hello</ns1:return>" +
31              "</ns1:echo>" +
32          "</soap:Body></soap:Envelope>";
33  
34      boolean gotEvent = false;
35      Object payload;
36      
37      @Test
38      public void testOutbound() throws Exception
39      {
40          CxfConfiguration config = new CxfConfiguration();
41          config.setMuleContext(muleContext);
42          config.initialise();
43          
44          // Build a CXF MessageProcessor
45          SimpleClientMessageProcessorBuilder builder = new SimpleClientMessageProcessorBuilder();
46          builder.setConfiguration(config);
47          builder.setServiceClass(EchoService.class);
48          builder.setOperation("echo");
49          builder.setMuleContext(muleContext);
50          
51          CxfOutboundMessageProcessor processor = builder.build();
52          
53          MessageProcessor messageProcessor = new MessageProcessor()
54          {
55              public MuleEvent process(MuleEvent event) throws MuleException
56              {
57                  payload = event.getMessage().getPayload();
58                  try
59                  {
60                      System.out.println(event.getMessage().getPayloadAsString());
61                  }
62                  catch (Exception e)
63                  {
64                      e.printStackTrace();
65                  }
66                  
67                  event.getMessage().setPayload(msg);
68                  gotEvent = true;
69                  return event;
70              }
71          };
72          processor.setListener(messageProcessor);
73          
74          MuleEvent event = getTestEvent("hello", getTestInboundEndpoint(MessageExchangePattern.REQUEST_RESPONSE));
75          MuleEvent response = processor.process(event);
76          assertThat(processor.getClient().getRequestContext().isEmpty(), Is.is(true));
77          assertThat(processor.getClient().getResponseContext().isEmpty(), Is.is(true));
78          Object payload = response.getMessage().getPayload();
79          assertThat(payload, IsInstanceOf.instanceOf(String.class));
80          assertThat((String) payload,Is.is("hello"));
81          assertThat(gotEvent,Is.is(true));
82      }
83  
84  }