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.transport.http.functional;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleEventContext;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.client.LocalMuleClient;
13  import org.mule.api.config.MuleProperties;
14  import org.mule.tck.functional.EventCallback;
15  import org.mule.tck.functional.FunctionalTestComponent;
16  import org.mule.tck.junit4.FunctionalTestCase;
17  import org.mule.tck.junit4.rule.DynamicPort;
18  import org.mule.util.IOUtils;
19  import org.mule.util.StringDataSource;
20  
21  import javax.activation.DataHandler;
22  
23  import org.junit.Rule;
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  
29  public class HttpAttachmentsFunctionalTestCase extends FunctionalTestCase
30  {
31  
32      @Rule
33      public DynamicPort dynamicPort = new DynamicPort("port1");
34      
35      @Override
36      protected String getConfigResources()
37      {
38          return "http-attachments-functional-test.xml";
39      }
40  
41      @Test
42      public void testSendAttachment() throws Exception
43      {
44          FunctionalTestComponent ftc = getFunctionalTestComponent("testComponent");
45          assertNotNull(ftc);
46          ftc.setEventCallback(new EventCallback(){
47              public void eventReceived(MuleEventContext context, Object component) throws Exception
48              {
49                  assertEquals("application/octet-stream; charset=ISO-8859-1", context.getMessage().getInboundProperty(MuleProperties.CONTENT_TYPE_PROPERTY));
50                  assertEquals("We should have an attachment", 1, context.getMessage().getInboundAttachmentNames().size());
51                  DataHandler dh = context.getMessage().getInboundAttachment("attach1");
52                  assertNotNull("DataHandler with name 'attach1' should not be null", dh);
53                  assertEquals("We should have an attachment with foo", "foo" , IOUtils.toString(dh.getInputStream()));
54                  assertEquals("text/plain; charset=ISO-8859-1", dh.getContentType());
55              }
56          });
57  
58          LocalMuleClient client = muleContext.getClient();
59          MuleMessage msg = new DefaultMuleMessage("test",  muleContext);
60          msg.addOutboundAttachment("attach1", new DataHandler(new StringDataSource("foo", "attach1")));
61  
62          MuleMessage result = client.send("endpoint1", msg);
63          assertEquals("We should have no attachments coming back", 0, result.getInboundAttachmentNames().size());
64      }
65  
66  
67      //TODO MULE-5005 response attachments
68  //    @Test
69  //    public void testReceiveAttachment() throws Exception
70  //    {
71  //        FunctionalTestComponent ftc = getFunctionalTestComponent("testComponent");
72  //        assertNotNull(ftc);
73  //        ftc.setEventCallback(new EventCallback(){
74  //            public void eventReceived(MuleEventContext context, Object component) throws Exception
75  //            {
76  //                context.getMessage().addOutboundAttachment("attach1", new DataHandler(new StringDataSource("foo", "attach1")));
77  //            }
78  //        });
79  //
80  //        LocalMuleClient client = muleContext.getClient();
81  //        MuleMessage msg = new DefaultMuleMessage("test",  muleContext);
82  //
83  //        //msg.addOutboundAttachment("attach1", new DataHandler(new StringDataSource("foo", "attach1")));
84  //
85  //        MuleMessage result = client.send("endpoint1", msg);
86  //        assertEquals("We should have 1 attachments coming back", 1, result.getInboundAttachmentNames().size());
87  //        assertEquals("There should be no outbound attachments", 0, result.getOutboundAttachmentNames().size());
88  //        DataHandler dh = result.getInboundAttachment("attach1");
89  //        assertNotNull("DataHandler with name 'attach1' should not be null", dh);
90  //        assertEquals("We should have an attachment with foo", "foo" , IOUtils.toString(dh.getInputStream()));
91  //        assertEquals("text/plain", dh.getContentType());
92  //    }
93  
94  }