View Javadoc

1   /*
2    * $Id: EndpointContentTypeTestCase.java 22735 2011-08-25 16:02:35Z dfeist $
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.vm.functional;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertTrue;
16  
17  import org.mule.api.MessagingException;
18  import org.mule.api.MuleEventContext;
19  import org.mule.api.MuleMessage;
20  import org.mule.api.client.MuleClient;
21  import org.mule.api.lifecycle.Callable;
22  import org.mule.api.transport.PropertyScope;
23  import org.mule.tck.AbstractServiceAndFlowTestCase;
24  
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import org.junit.Test;
31  import org.junit.runners.Parameterized.Parameters;
32  
33  public class EndpointContentTypeTestCase extends AbstractServiceAndFlowTestCase
34  {
35      public EndpointContentTypeTestCase(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-endpoint-configs-service.xml"},
45              {ConfigVariant.FLOW, "org/mule/test/config/content-type-setting-endpoint-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          messageProperties.put("content-type", "text/xml");
55          MuleClient client = muleContext.getClient();
56          MuleMessage result = client.send("vm://in1?connector=vm-in1", "<OK/>", messageProperties);
57          assertNotNull(result.getExceptionPayload());
58          assertTrue(result.getExceptionPayload().getException() instanceof MessagingException);
59  
60          messageProperties.put("content-type", "text/plain");
61          EchoComponent.setExpectedContentType("text/plain");
62          response = client.send("vm://in1?connector=vm-in1", "OK", messageProperties);
63          assertNotNull(response);
64          assertEquals("OK", response.getPayload());
65  
66          messageProperties.remove("content-type");
67          EchoComponent.setExpectedContentType("text/plain");
68          response = client.send("vm://in1?connector=vm-in1", "OK", messageProperties);
69          assertNotNull(response);
70          assertEquals("OK", response.getPayload());
71  
72          messageProperties.put("content-type", "text/plain");
73          EchoComponent.setExpectedContentType("text/xml");
74          response = client.send("vm://in2?connector=vm-in2", "OK", messageProperties);
75          assertNotNull(response);
76          assertEquals("OK", response.getPayload());
77      }
78  
79      public static class EchoComponent implements Callable
80      {
81          static String expectedContentType;
82  
83          @Override
84          public Object onCall(MuleEventContext eventContext) throws Exception
85          {
86              MuleMessage message = eventContext.getMessage();
87              assertEquals(expectedContentType, message.getProperty("content-type", PropertyScope.INBOUND));
88              return message;
89          }
90  
91          public static void setExpectedContentType(String expectedContentType)
92          {
93              EchoComponent.expectedContentType = expectedContentType;
94          }
95      }
96  }
97