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.servlet.jetty.functional;
8   
9   import static org.junit.Assert.assertEquals;
10  import static org.junit.Assert.assertNotNull;
11  
12  import org.mule.api.MuleEventContext;
13  import org.mule.api.MuleMessage;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.functional.EventCallback;
16  import org.mule.tck.functional.FunctionalTestComponent;
17  import org.mule.tck.junit4.FunctionalTestCase;
18  import org.mule.tck.junit4.rule.DynamicPort;
19  import org.mule.transport.http.HttpConnector;
20  import org.mule.transport.http.HttpConstants;
21  import org.mule.util.IOUtils;
22  
23  import java.io.InputStream;
24  
25  import org.junit.Rule;
26  import org.junit.Test;
27  
28  /**
29   * Functional tests specific to Jetty.
30   */
31  public class JettyFunctionalTestCase extends FunctionalTestCase
32  {
33  
34      @Rule
35      public DynamicPort dynamicPort = new DynamicPort("port1");
36  
37      @Override
38      protected String getConfigResources()
39      {
40          return "jetty-functional-test.xml";
41      }
42  
43      @Test
44      public void testNormalExecutionFlow() throws Exception
45      {
46          FunctionalTestComponent testComponent = getFunctionalTestComponent("normalExecutionFlow");
47          assertNotNull(testComponent);
48          
49          EventCallback callback = new EventCallback()
50          {
51              public void eventReceived(MuleEventContext context, Object component) throws Exception
52              {
53                  MuleMessage msg = context.getMessage();
54                  assertEquals(HttpConstants.METHOD_POST, msg.getInboundProperty(HttpConnector.HTTP_METHOD_PROPERTY));
55                  assertEquals("/normal", msg.getInboundProperty(HttpConnector.HTTP_REQUEST_PROPERTY));
56                  assertEquals("/normal", msg.getInboundProperty(HttpConnector.HTTP_REQUEST_PATH_PROPERTY));
57                  assertEquals("/normal", msg.getInboundProperty(HttpConnector.HTTP_CONTEXT_PATH_PROPERTY));
58              }
59          };
60  
61          testComponent.setEventCallback(callback);
62          
63          MuleClient client = new MuleClient(muleContext);
64          MuleMessage response = client.send("http://localhost:" + dynamicPort.getNumber() + "/normal", TEST_MESSAGE, null);
65          assertEquals("200", response.getInboundProperty("http.status"));
66          assertEquals(TEST_MESSAGE + " received", IOUtils.toString((InputStream) response.getPayload()));
67      }
68      
69      @Test
70      public void testExceptionExecutionFlow() throws Exception
71      {
72          MuleClient client = new MuleClient(muleContext);
73          MuleMessage response = client.send("http://localhost:" + dynamicPort.getNumber() + "/exception", TEST_MESSAGE, null);
74          assertEquals("500", response.getInboundProperty("http.status"));
75          assertNotNull(response.getExceptionPayload());
76      }
77  
78  }