View Javadoc

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