Test coverage increasing for iovisor.restclient
[groupbasedpolicy.git] / renderers / iovisor / src / test / java / org / opendaylight / groupbasedpolicy / renderer / restclient / RestClientTest.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.restclient;
10
11 import javax.ws.rs.Consumes;
12 import javax.ws.rs.GET;
13 import javax.ws.rs.POST;
14 import javax.ws.rs.Path;
15 import javax.ws.rs.Produces;
16 import javax.ws.rs.core.MediaType;
17 import javax.ws.rs.core.Response;
18 import java.io.IOException;
19 import java.net.URI;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
24 import com.sun.jersey.api.core.ClassNamesResourceConfig;
25 import com.sun.jersey.api.core.ResourceConfig;
26 import com.sun.jersey.test.framework.AppDescriptor;
27 import com.sun.jersey.test.framework.JerseyTest;
28 import com.sun.jersey.test.framework.WebAppDescriptor;
29 import org.glassfish.grizzly.http.server.HttpServer;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.opendaylight.groupbasedpolicy.renderer.iovisor.restclient.RestClient;
35
36 public class RestClientTest extends JerseyTest {
37
38     private RestClient client;
39     private String uri;
40     private static String resolvedPolicy =
41             " { \"resolved-policy-uri\" : \"/restconf/operational/resolved-policy:resolved-policies/resolved-policy/tenant-red/client/tenant-red/webserver/\" } ";
42
43     private static final String BASE_URI = "http://localhost";
44     private static final int BASE_PORT = 1234;
45     private static HttpServer server;
46
47     private static HttpServer startServer() throws IOException {
48         final ResourceConfig resourceConfig = new ClassNamesResourceConfig(dumbServer.class);
49         HttpServer httpServer = null;
50         httpServer = GrizzlyServerFactory.createHttpServer(URI.create(BASE_URI + ":" + BASE_PORT), resourceConfig);
51         return httpServer;
52     }
53
54     @BeforeClass
55     public static void setUpClass() throws IOException {
56         server = startServer();
57     }
58
59     @AfterClass
60     public static void tearDownClass() {
61         if (server != null && server.isStarted())
62             server.stop();
63     }
64
65     @Before
66     public void init() {
67         client = new RestClient("http://localhost:1234");
68     }
69
70     @Test
71     public void testResolvedPoliciesJSON_coverage() {
72         client.new ResolvedPoliciesJSON("string");
73
74         List<String> uris = new ArrayList<>();
75         uris.add("string1");
76         uris.add("string2");
77         client.new ResolvedPoliciesJSON(uris);
78     }
79
80     @Test
81     public void testGet_coverage() {
82         client.get("/");
83         client.get("/warning");
84         client.get("/error");
85     }
86
87     @Test
88     public void testPost_coverage() {
89         client.post("/", "json");
90     }
91
92     @Override
93     protected AppDescriptor configure() {
94         return new WebAppDescriptor.Builder().build();
95     }
96
97     @Path("/")
98     public static class dumbServer {
99
100         @GET
101         @Produces(MediaType.APPLICATION_JSON)
102         public Response get200() {
103             return Response.status(Response.Status.OK).entity(resolvedPolicy).build();
104         }
105
106         @POST
107         @Consumes(MediaType.APPLICATION_JSON)
108         public Response post200(String json) {
109             return Response.status(Response.Status.OK).build();
110         }
111
112         @Path("/warning")
113         @GET
114         public Response get202() {
115             return Response.status(Response.Status.ACCEPTED).build();
116         }
117
118         @Path("/error")
119         @GET
120         public Response get404() {
121             return Response.status(Response.Status.NOT_FOUND).build();
122         }
123
124     }
125 }