View Javadoc

1   /*
2    * $Id: RestServiceComponentDeleteTestCase.java 22488 2011-07-21 09:23:32Z justin.calleja $
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.http.components;
12  
13  import static org.junit.Assert.assertTrue;
14  
15  import java.io.BufferedReader;
16  import java.util.Arrays;
17  import java.util.Collection;
18  import java.util.StringTokenizer;
19  import java.util.concurrent.CountDownLatch;
20  import java.util.concurrent.TimeUnit;
21  
22  import org.junit.Rule;
23  import org.junit.Test;
24  import org.junit.runners.Parameterized.Parameters;
25  import org.mule.module.client.MuleClient;
26  import org.mule.tck.junit4.rule.DynamicPort;
27  import org.mule.transport.http.HttpConstants;
28  import org.mule.transport.http.functional.AbstractMockHttpServerTestCase;
29  import org.mule.transport.http.functional.MockHttpServer;
30  
31  public class RestServiceComponentDeleteTestCase extends AbstractMockHttpServerTestCase
32  {
33  
34      private CountDownLatch serverRequestCompleteLatch = new CountDownLatch(1);
35      private boolean deleteRequestFound = false;
36  
37      @Rule
38      public DynamicPort dynamicPort = new DynamicPort("port1");
39  
40      public RestServiceComponentDeleteTestCase(ConfigVariant variant, String configResources)
41      {
42          super(variant, configResources);
43      }
44  
45      @Parameters
46      public static Collection<Object[]> parameters()
47      {
48          return Arrays.asList(new Object[][]{
49              {ConfigVariant.SERVICE, "rest-service-component-delete-test-service.xml"},
50              {ConfigVariant.FLOW, "rest-service-component-delete-test-flow.xml"}});
51      }
52  
53      @Override
54      protected MockHttpServer getHttpServer(CountDownLatch serverStartLatch)
55      {
56          return new SimpleHttpServer(dynamicPort.getNumber(), serverStartLatch, serverRequestCompleteLatch);
57      }
58  
59      @Test
60      public void testRestServiceComponentDelete() throws Exception
61      {
62          MuleClient client = new MuleClient(muleContext);
63          client.send("vm://fromTest", TEST_MESSAGE, null);
64  
65          assertTrue(serverRequestCompleteLatch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
66          assertTrue(deleteRequestFound);
67      }
68  
69      private class SimpleHttpServer extends MockHttpServer
70      {
71          public SimpleHttpServer(int listenPort, CountDownLatch startupLatch, CountDownLatch testCompleteLatch)
72          {
73              super(listenPort, startupLatch, testCompleteLatch);
74          }
75  
76          @Override
77          protected void readHttpRequest(BufferedReader reader) throws Exception
78          {
79              String requestLine = reader.readLine();
80              String httpMethod = new StringTokenizer(requestLine).nextToken();
81  
82              deleteRequestFound = httpMethod.equals(HttpConstants.METHOD_DELETE);
83          }
84      }
85  }