View Javadoc

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