View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.chukwa.datacollection.adaptor;
19  
20  import java.io.IOException;
21  
22  import javax.servlet.ServletException;
23  import javax.servlet.http.HttpServlet;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import junit.framework.TestCase;
28  
29  import org.apache.hadoop.chukwa.Chunk;
30  import org.apache.hadoop.chukwa.datacollection.ChunkReceiver;
31  import org.json.simple.JSONObject;
32  import org.junit.After;
33  import org.junit.Before;
34  import org.junit.Test;
35  import org.mortbay.jetty.Server;
36  import org.mortbay.jetty.servlet.Context;
37  import org.mortbay.jetty.servlet.ServletHolder;
38  
39  public class TestRestAdaptor extends TestCase implements ChunkReceiver {
40  
41    private Server jettyServer = null;
42    private JSONObject metricsMap = new JSONObject();
43    private static String args = "http://localhost:9090/metrics/instrumentation/data 2";
44    private static long offset = 0;
45  
46    @Before
47    public void setUp() throws Exception {
48      metricsMap.put("FreeSpace", "10GB");
49      metricsMap.put("UsedSpace", "90GB");
50      metricsMap.put("maps_killed", "20");
51  
52      jettyServer = new Server(9090);
53      Context root = new Context(jettyServer, "/metrics/instrumentation/data",
54          Context.SESSIONS);
55      root.addServlet(new ServletHolder(new RestServlet()), "/*");
56      System.out.println(" Rest Server starting..");
57  
58      jettyServer.start();
59      jettyServer.setStopAtShutdown(true);
60    }
61  
62    @Test
63    public void testMessageReceivedOk() throws Exception {
64      RestAdaptor restAdaptor = new RestAdaptor();
65      restAdaptor.parseArgs(args);
66      restAdaptor.start("id", "TestRestAdaptor", 0, this);
67      Thread.sleep(2000); // wait for processing
68    }
69  
70    @Override
71    public void add(Chunk event) throws InterruptedException {
72      offset += event.getData().length;
73      assertTrue(event.getDataType().equals("TestRestAdaptor"));
74      assertEquals(event.getSeqID(), offset);
75      assertTrue(metricsMap.toString().equals(new String(event.getData())));
76    }
77  
78    @After
79    public void tearDown() throws Exception {
80      if (jettyServer != null) {
81        jettyServer.stop();
82      }
83    }
84  
85    private class RestServlet extends HttpServlet {
86      private static final long serialVersionUID = -8007387020169769539L;
87  
88      protected void doGet(HttpServletRequest request,
89          HttpServletResponse response) throws ServletException, IOException {
90        response.getWriter().write(metricsMap.toString());
91      }
92    }
93  }