Unit tests for ofoverlay
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / statistics / JsonRestClientTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics;
10
11 import static org.junit.Assert.assertEquals;
12
13 import javax.ws.rs.Consumes;
14 import javax.ws.rs.GET;
15 import javax.ws.rs.POST;
16 import javax.ws.rs.Path;
17 import javax.ws.rs.Produces;
18 import javax.ws.rs.core.MediaType;
19 import javax.ws.rs.core.MultivaluedMap;
20 import javax.ws.rs.core.Response;
21 import java.io.IOException;
22
23 import com.google.common.collect.ImmutableList;
24 import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
25 import com.sun.jersey.api.core.ClassNamesResourceConfig;
26 import com.sun.jersey.api.core.ResourceConfig;
27 import com.sun.jersey.core.util.MultivaluedMapImpl;
28 import com.sun.jersey.test.framework.AppDescriptor;
29 import com.sun.jersey.test.framework.JerseyTest;
30 import com.sun.jersey.test.framework.WebAppDescriptor;
31 import org.glassfish.grizzly.http.server.HttpServer;
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics.util.SFlowQueryParams;
37
38 public class JsonRestClientTest extends JerseyTest {
39
40     private static final int CONNECT_TIMEOUT_MILLISEC = 20000;
41     private static final int READ_TIMEOUT_MILLISEC = 30000;
42     private static final String SFLOW_HOST = "localhost";
43     private static final int SFLOW_PORT = 1234;
44     private static HttpServer server;
45     private static final String SFLOW_URI = "http://" + SFLOW_HOST + ":" + SFLOW_PORT;
46
47     private JsonRestClient client;
48     private String uri;
49     private static String responseJson =
50             " { \"resolved-policy-uri\" : \"/restconf/operational/resolved-policy:resolved-policies/resolved-policy/tenant-red/client/tenant-red/webserver/\" } ";
51
52     private static HttpServer startServer() throws IOException {
53         final ResourceConfig resourceConfig = new ClassNamesResourceConfig(dumbServer.class);
54         HttpServer httpServer;
55         httpServer = GrizzlyServerFactory.createHttpServer(java.net.URI.create(SFLOW_URI), resourceConfig);
56         return httpServer;
57     }
58
59     @BeforeClass
60     public static void setUpClass() throws IOException {
61         server = startServer();
62     }
63
64     @AfterClass
65     public static void tearDownClass() {
66         if (server != null && server.isStarted())
67             server.stop();
68     }
69
70     @Before
71     public void init() {
72         client = new JsonRestClient(SFLOW_URI, CONNECT_TIMEOUT_MILLISEC, READ_TIMEOUT_MILLISEC);
73     }
74
75     @Test
76     public void testGetHost() {
77         String host = client.getHost();
78
79         assertEquals(SFLOW_HOST, host);
80     }
81
82     @Test
83     public void testGet_coverageOnly() {
84         client.get("/");
85     }
86
87     @Test
88     public void testGet_params_coverageOnly() {
89         MultivaluedMap<String, String> params = new MultivaluedMapImpl();
90         params.add(SFlowQueryParams.MAX_FLOWS, "20");
91         params.add(SFlowQueryParams.MIN_VALUE, "0.1");
92         params.add(SFlowQueryParams.AGG_MODE, "sum");
93
94         client.get("/", params);
95     }
96
97     @Test
98     public void testPost_coverageOnly() {
99         client.post("/", "json");
100     }
101
102     @Test
103     public void testPut_coverageOnly() {
104         client.put("/", "json");
105     }
106
107     @Test
108     public void testDelete_coverageOnly() {
109         client.delete("/");
110     }
111
112     @Override
113     protected AppDescriptor configure() {
114         return new WebAppDescriptor.Builder().build();
115     }
116
117     @Path("/")
118     public static class dumbServer {
119
120         @GET
121         @Produces(MediaType.APPLICATION_JSON)
122         public Response get200() {
123             return Response.status(Response.Status.OK).entity(responseJson).build();
124         }
125
126         @POST
127         @Consumes(MediaType.APPLICATION_JSON)
128         public Response post200(String json) {
129             return Response.status(Response.Status.OK).build();
130         }
131
132     }
133 }