1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.vm.functional;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15
16 import org.mule.api.MuleEventContext;
17 import org.mule.api.MuleMessage;
18 import org.mule.api.lifecycle.Callable;
19 import org.mule.api.transport.PropertyScope;
20 import org.mule.module.client.MuleClient;
21 import org.mule.tck.AbstractServiceAndFlowTestCase;
22
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.activation.MimeType;
29
30 import org.junit.Test;
31 import org.junit.runners.Parameterized.Parameters;
32
33 public class TransformerContentTypeTestCase extends AbstractServiceAndFlowTestCase
34 {
35 public TransformerContentTypeTestCase(ConfigVariant variant, String configResources)
36 {
37 super(variant, configResources);
38 }
39
40 @Parameters
41 public static Collection<Object[]> parameters()
42 {
43 return Arrays.asList(new Object[][]{
44 {ConfigVariant.SERVICE, "org/mule/test/config/content-type-setting-transform-configs-service.xml"},
45 {ConfigVariant.FLOW, "org/mule/test/config/content-type-setting-transform-configs-flow.xml"}
46 });
47 }
48
49 @Test
50 public void testContentTypes() throws Exception
51 {
52 MuleMessage response;
53 Map<String, Object> messageProperties = new HashMap<String, Object>();
54
55 MuleClient client = new MuleClient(muleContext);
56
57 messageProperties.put("content-type", "text/plain");
58 EchoComponent.setExpectedMimeType("text/xml");
59 response = client.send("vm://in1?connector=vm-in1", "OK", messageProperties);
60 assertNotNull(response);
61 assertEquals("OK", response.getPayload());
62
63 messageProperties.remove("content-type");
64 response = client.send("vm://in1?connector=vm-in1", "OK", messageProperties);
65 assertNotNull(response);
66 assertEquals("OK", response.getPayload());
67
68 messageProperties.put("content-type", "text/xml");
69 EchoComponent.setExpectedMimeType("text/plain");
70 response = client.send("vm://in2?connector=vm-in2", "OK", messageProperties);
71 assertNotNull(response);
72 assertEquals("OK", response.getPayload());
73
74 messageProperties.remove("content-type");
75 response = client.send("vm://in2?connector=vm-in2", "OK", messageProperties);
76 assertNotNull(response);
77 assertEquals("OK", response.getPayload());
78 }
79
80 public static class EchoComponent implements Callable
81 {
82 static String expectedMimeType;
83
84 public Object onCall(MuleEventContext eventContext) throws Exception
85 {
86 MuleMessage message = eventContext.getMessage();
87 String contentType = message.getProperty("content-type", PropertyScope.INBOUND);
88 MimeType mt = new MimeType(contentType);
89 String mimeType = mt.getPrimaryType() + "/" + mt.getSubType();
90 assertEquals(expectedMimeType, mimeType);
91 return message;
92 }
93
94 public static void setExpectedMimeType(String expectedContentType)
95 {
96 EchoComponent.expectedMimeType = expectedContentType;
97 }
98 }
99 }