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.vm.functional;
8   
9   import org.mule.api.MuleEventContext;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.lifecycle.Callable;
12  import org.mule.api.transport.PropertyScope;
13  import org.mule.module.client.MuleClient;
14  import org.mule.tck.junit4.FunctionalTestCase;
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import javax.activation.MimeType;
20  
21  import org.junit.Test;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  
26  public class TransformerContentTypeTestCase extends FunctionalTestCase
27  {
28  
29      @Override
30      protected String getConfigResources()
31      {
32          return "org/mule/test/config/content-type-setting-transform-configs.xml";
33      }
34  
35      @Test
36      public void testContentTypes() throws Exception
37      {
38          MuleMessage response;
39          Map<String, Object> messageProperties = new HashMap<String, Object>();
40  
41          MuleClient client = new MuleClient(muleContext);
42  
43          messageProperties.put("content-type", "text/plain");
44          EchoComponent.setExpectedMimeType("text/xml");
45          response = client.send("vm://in1?connector=vm-in1", "OK", messageProperties);
46          assertNotNull(response);
47          assertEquals("OK", response.getPayload());
48  
49          messageProperties.remove("content-type");
50          response = client.send("vm://in1?connector=vm-in1", "OK", messageProperties);
51          assertNotNull(response);
52          assertEquals("OK", response.getPayload());
53  
54          messageProperties.put("content-type", "text/xml");
55          EchoComponent.setExpectedMimeType("text/plain");
56          response = client.send("vm://in2?connector=vm-in2", "OK", messageProperties);
57          assertNotNull(response);
58          assertEquals("OK", response.getPayload());
59  
60          messageProperties.remove("content-type");
61          response = client.send("vm://in2?connector=vm-in2", "OK", messageProperties);
62          assertNotNull(response);
63          assertEquals("OK", response.getPayload());
64      }
65  
66      public static class EchoComponent implements Callable
67      {
68          static String expectedMimeType;
69  
70          public Object onCall(MuleEventContext eventContext) throws Exception
71          {
72              MuleMessage message = eventContext.getMessage();
73              String contentType = message.getProperty("content-type", PropertyScope.INBOUND);
74              MimeType mt = new MimeType(contentType);
75              String mimeType = mt.getPrimaryType() + "/" + mt.getSubType();
76              assertEquals(expectedMimeType, mimeType);
77              return message;
78          }
79  
80          public static void setExpectedMimeType(String expectedContentType)
81          {
82              EchoComponent.expectedMimeType = expectedContentType;
83          }
84      }
85  }