View Javadoc

1   /*
2    * $Id: HttpAttachmentsFunctionalTestCase.java 22518 2011-07-22 07:00:22Z claude.mamo $
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.transport.http.functional;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  
16  import org.mule.DefaultMuleMessage;
17  import org.mule.api.MuleEventContext;
18  import org.mule.api.MuleMessage;
19  import org.mule.api.client.LocalMuleClient;
20  import org.mule.api.config.MuleProperties;
21  import org.mule.tck.AbstractServiceAndFlowTestCase;
22  import org.mule.tck.functional.EventCallback;
23  import org.mule.tck.functional.FunctionalTestComponent;
24  import org.mule.tck.junit4.rule.DynamicPort;
25  import org.mule.util.IOUtils;
26  import org.mule.util.StringDataSource;
27  
28  import java.util.Arrays;
29  import java.util.Collection;
30  
31  import javax.activation.DataHandler;
32  
33  import org.junit.Rule;
34  import org.junit.Test;
35  import org.junit.runners.Parameterized.Parameters;
36  
37  public class HttpAttachmentsFunctionalTestCase extends AbstractServiceAndFlowTestCase
38  {
39  
40      @Rule
41      public DynamicPort dynamicPort = new DynamicPort("port1");
42      
43      public HttpAttachmentsFunctionalTestCase(ConfigVariant variant, String configResources)
44      {
45          super(variant, configResources);
46      }
47  
48      @Parameters
49      public static Collection<Object[]> parameters()
50      {
51          return Arrays.asList(new Object[][]{
52              {ConfigVariant.SERVICE, "http-attachments-functional-test-service.xml"},
53              {ConfigVariant.FLOW, "http-attachments-functional-test-flow.xml"}
54          });
55      }      
56      
57      @Test
58      public void testSendAttachment() throws Exception
59      {
60          FunctionalTestComponent ftc = getFunctionalTestComponent("testComponent");
61          assertNotNull(ftc);
62          ftc.setEventCallback(new EventCallback(){
63              public void eventReceived(MuleEventContext context, Object component) throws Exception
64              {
65                  assertEquals("application/octet-stream; charset=ISO-8859-1", context.getMessage().getInboundProperty(MuleProperties.CONTENT_TYPE_PROPERTY));
66                  assertEquals("We should have an attachment", 1, context.getMessage().getInboundAttachmentNames().size());
67                  DataHandler dh = context.getMessage().getInboundAttachment("attach1");
68                  assertNotNull("DataHandler with name 'attach1' should not be null", dh);
69                  assertEquals("We should have an attachment with foo", "foo" , IOUtils.toString(dh.getInputStream()));
70                  assertEquals("text/plain; charset=ISO-8859-1", dh.getContentType());
71              }
72          });
73  
74          LocalMuleClient client = muleContext.getClient();
75          MuleMessage msg = new DefaultMuleMessage("test",  muleContext);
76          msg.addOutboundAttachment("attach1", new DataHandler(new StringDataSource("foo", "attach1")));
77  
78          MuleMessage result = client.send("endpoint1", msg);
79          assertEquals("We should have no attachments coming back", 0, result.getInboundAttachmentNames().size());
80      }
81  
82  
83      //TODO MULE-5005 response attachments
84  //    @Test
85  //    public void testReceiveAttachment() throws Exception
86  //    {
87  //        FunctionalTestComponent ftc = getFunctionalTestComponent("testComponent");
88  //        assertNotNull(ftc);
89  //        ftc.setEventCallback(new EventCallback(){
90  //            public void eventReceived(MuleEventContext context, Object component) throws Exception
91  //            {
92  //                context.getMessage().addOutboundAttachment("attach1", new DataHandler(new StringDataSource("foo", "attach1")));
93  //            }
94  //        });
95  //
96  //        LocalMuleClient client = muleContext.getClient();
97  //        MuleMessage msg = new DefaultMuleMessage("test",  muleContext);
98  //
99  //        //msg.addOutboundAttachment("attach1", new DataHandler(new StringDataSource("foo", "attach1")));
100 //
101 //        MuleMessage result = client.send("endpoint1", msg);
102 //        assertEquals("We should have 1 attachments coming back", 1, result.getInboundAttachmentNames().size());
103 //        assertEquals("There should be no outbound attachments", 0, result.getOutboundAttachmentNames().size());
104 //        DataHandler dh = result.getInboundAttachment("attach1");
105 //        assertNotNull("DataHandler with name 'attach1' should not be null", dh);
106 //        assertEquals("We should have an attachment with foo", "foo" , IOUtils.toString(dh.getInputStream()));
107 //        assertEquals("text/plain", dh.getContentType());
108 //    }
109 
110 }