2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics;
11 import static org.junit.Assert.assertEquals;
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;
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;
38 public class JsonRestClientTest extends JerseyTest {
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;
47 private JsonRestClient client;
49 private static String responseJson =
50 " { \"resolved-policy-uri\" : \"/restconf/operational/resolved-policy:resolved-policies/resolved-policy/tenant-red/client/tenant-red/webserver/\" } ";
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);
60 public static void setUpClass() throws IOException {
61 server = startServer();
65 public static void tearDownClass() {
66 if (server != null && server.isStarted())
72 client = new JsonRestClient(SFLOW_URI, CONNECT_TIMEOUT_MILLISEC, READ_TIMEOUT_MILLISEC);
76 public void testGetHost() {
77 String host = client.getHost();
79 assertEquals(SFLOW_HOST, host);
83 public void testGet_coverageOnly() {
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");
94 client.get("/", params);
98 public void testPost_coverageOnly() {
99 client.post("/", "json");
103 public void testPut_coverageOnly() {
104 client.put("/", "json");
108 public void testDelete_coverageOnly() {
113 protected AppDescriptor configure() {
114 return new WebAppDescriptor.Builder().build();
118 public static class dumbServer {
121 @Produces(MediaType.APPLICATION_JSON)
122 public Response get200() {
123 return Response.status(Response.Status.OK).entity(responseJson).build();
127 @Consumes(MediaType.APPLICATION_JSON)
128 public Response post200(String json) {
129 return Response.status(Response.Status.OK).build();