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.http.components;
8   
9   import org.mule.api.MuleEventContext;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.client.MuleClient;
12  import org.mule.api.lifecycle.Callable;
13  import org.mule.tck.junit4.FunctionalTestCase;
14  import org.mule.tck.junit4.rule.DynamicPort;
15  import org.mule.transport.NullPayload;
16  import org.mule.transport.http.HttpConstants;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.junit.Rule;
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertTrue;
27  
28  public class RestServiceWrapperFunctionalTestCase extends FunctionalTestCase
29  {
30      protected static String TEST_REQUEST = "Test Http Request";
31  
32      @Rule
33      public DynamicPort port1 = new DynamicPort("port1");
34  
35      @Rule
36      public DynamicPort port2 = new DynamicPort("port2");
37  
38      @Override
39      protected String getConfigResources()
40      {
41          return "http-rest-service-wrapper-functional-test.xml";
42      }
43  
44      @Test
45      public void testErrorExpressionOnRegexFilterFail() throws Exception
46      {
47          MuleClient client = muleContext.getClient();
48          MuleMessage result = client.send("restServiceEndpoint", TEST_REQUEST, null);
49          assertTrue(result.getPayload() instanceof NullPayload);
50      }
51  
52      @Test
53      public void testErrorExpressionOnRegexFilterPass() throws Exception
54      {
55          MuleClient client = muleContext.getClient();
56          MuleMessage result = client.send("restServiceEndpoint2", TEST_REQUEST, null);
57          assertEquals("echo=" + TEST_REQUEST,result.getPayloadAsString());
58      }
59  
60      @Test
61      public void testRequiredParameters() throws Exception
62      {
63          MuleClient client = muleContext.getClient();
64          Map<String, Object> props = new HashMap<String, Object>();
65          props.put("baz-header", "baz");
66          props.put("bar-optional-header", "bar");
67          MuleMessage result = client.send("restServiceEndpoint3", null, props);
68          assertEquals("foo=boo&faz=baz&far=bar",result.getPayloadAsString());
69      }
70  
71      @Test
72      public void testOptionalParametersMissing() throws Exception
73      {
74          MuleClient client = muleContext.getClient();
75          Map<String, Object> props = new HashMap<String, Object>();
76          props.put("baz-header", "baz");
77          MuleMessage result = client.send("restServiceEndpoint3", null, props);
78          assertEquals("foo=boo&faz=baz",result.getPayloadAsString());
79      }
80  
81      @Test
82      public void testRequiredParametersMissing() throws Exception
83      {
84          MuleClient client = muleContext.getClient();
85          Map<String, Object> props = new HashMap<String, Object>();
86  
87          MuleMessage result = client.send("restServiceEndpoint3", null, props);
88          assertEquals(NullPayload.getInstance(),result.getPayload());
89          assertNotNull(result.getExceptionPayload());
90      }
91  
92      @Test
93      public void testRestServiceComponentInFlow() throws Exception
94      {
95          MuleClient client = muleContext.getClient();
96  
97          MuleMessage result = client.send("vm://toFlow", TEST_REQUEST, null);
98          assertNotNull(result);
99          assertEquals("echo=Test Http Request", result.getPayloadAsString());
100     }
101 
102     @Test
103     public void restServiceComponentShouldPreserveContentTypeOnIncomingMessage() throws Exception
104     {
105         MuleClient client = muleContext.getClient();
106         MuleMessage result = client.send("vm://restservice4", TEST_REQUEST, null);
107         assertNotNull(result);
108         assertEquals("foo/bar", result.getPayloadAsString());
109     }
110 
111     public static class CopyContentTypeFromRequest implements Callable
112     {
113         public Object onCall(MuleEventContext eventContext) throws Exception
114         {
115             return eventContext.getMessage().getInboundProperty(HttpConstants.HEADER_CONTENT_TYPE);
116         }
117     }
118 }