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.test.integration.message;
8   
9   import org.mule.api.MuleEventContext;
10  import org.mule.api.MuleMessage;
11  import org.mule.module.client.MuleClient;
12  import org.mule.tck.functional.EventCallback;
13  import org.mule.tck.functional.FunctionalTestComponent;
14  import org.mule.tck.junit4.AbstractMuleContextTestCase;
15  import org.mule.transport.email.transformers.PlainTextDataSource;
16  
17  import javax.activation.DataHandler;
18  
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.fail;
24  
25  public class AttachmentsPropagationTestCase extends AbstractMuleContextTestCase implements EventCallback
26  {
27      @Test
28      public void testSanity()
29      {
30          fail("Convert this test to an XML-based configuration");
31      }    
32  
33      @Override
34      protected void doSetUp() throws Exception
35      {
36          super.doSetUp();
37  
38          // TODO Convert this to an XML config
39  //        EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder("vm://Single", muleContext);
40  //        endpointBuilder.setName("SingleEndpoint");
41  //        ImmutableEndpoint vmSingle = muleContext.getRegistry()
42  //            .lookupEndpointFactory()
43  //            .getOutboundEndpoint(endpointBuilder, muleContext);
44  //        
45  //        EndpointBuilder endpointBuilder2 = new EndpointURIEndpointBuilder("vm://Single", muleContext);
46  //        endpointBuilder2.setName("ChainedEndpoint");
47  //        ImmutableEndpoint vmChained = muleContext.getRegistry()
48  //            .lookupEndpointFactory()
49  //            .getOutboundEndpoint(endpointBuilder2, muleContext);
50  //        
51  //        FunctionalTestComponent single = new FunctionalTestComponent();
52  //        single.setEventCallback(this);
53  //        FunctionalTestComponent chained = new FunctionalTestComponent();
54  //        chained.setEventCallback(this);
55  //        builder.registerComponentInstance(single, "SINGLE", vmSingle.getEndpointURI());
56  //        builder.registerComponentInstance(chained, "CHAINED", vmChained.getEndpointURI(), vmSingle
57  //            .getEndpointURI());
58      }
59  
60      public void eventReceived(MuleEventContext context, Object component) throws Exception
61      {
62          MuleMessage message = context.getMessage();
63          // add an attachment, named after the componentname...
64          message.addAttachment(context.getFlowConstruct().getName(), new DataHandler(
65              new PlainTextDataSource("text/plain", "<content>")));
66  
67          // return the list of attachment names
68          FunctionalTestComponent fc = (FunctionalTestComponent) component;
69          fc.setReturnData(message.getAttachmentNames().toString());
70      }
71  
72      @Test
73      public void testSingleComponentKnowsAttachments() throws Exception
74      {
75  
76          MuleClient client = new MuleClient(muleContext);
77          MuleMessage result = client.send("vm://Single", "", null);
78          assertNotNull(result);
79  
80          // expect SINGLE attachment from SINGLE service
81          assertEquals("[SINGLE]", result.getPayloadAsString());
82      }
83  
84      @Test
85      public void testChainedComponentKnowsAttachments() throws Exception
86      {
87          MuleClient client = new MuleClient(muleContext);
88          MuleMessage result = client.send("vm://Chained", "", null);
89          assertNotNull(result);
90  
91          // expect CHAINED attachment from CHAINED service
92          // and SINGLE attachment from SINGLE service
93          assertEquals("[SINGLE, CHAINED]", result.getPayloadAsString());
94      }
95  
96      @Test
97      public void testClientReceivesAttachments() throws Exception
98      {
99          // a MuleClient should be able to receive attachments
100         MuleClient client = new MuleClient(muleContext);
101 
102         MuleMessage result = client.send("vm://Single", "", null);
103         assertNotNull(result);
104 
105         // expect SINGLE attachment from SINGLE service
106         assertEquals("[SINGLE]", result.getPayloadAsString());
107         assertNotNull(result.getAttachment("SINGLE"));
108         assertEquals("<content>", result.getAttachment("SINGLE").getContent().toString());
109 
110         result = client.send("vm://Chained", "", null);
111         assertNotNull(result);
112 
113         // expect SINGLE and CHAINED attachments
114         assertEquals("[SINGLE, CHAINED]", result.getPayloadAsString());
115         assertNotNull(result.getAttachment("SINGLE"));
116         assertEquals("<content>", result.getAttachment("SINGLE").getContent().toString());
117         assertNotNull(result.getAttachment("CHAINED"));
118         assertEquals("<content>", result.getAttachment("CHAINED").getContent().toString());
119     }
120 }