Unit tests for ofoverlay
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / statistics / JsonRestClientResponseTest.java
1 /*
2  * Copyright (c) 2016 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 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertSame;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import com.sun.jersey.api.client.ClientHandlerException;
18 import com.sun.jersey.api.client.ClientResponse;
19 import com.sun.jersey.api.client.UniformInterfaceException;
20 import org.junit.Before;
21 import org.junit.Test;
22
23 public class JsonRestClientResponseTest {
24
25     private static final String STRING_ENTITY = "string entity";
26     private ClientResponse clientResponse;
27
28     @Before
29     public void init() {
30         clientResponse = mock(ClientResponse.class);
31
32     }
33
34     @Test
35     public void testResponse_Ok() {
36         when(clientResponse.getEntity(String.class)).thenReturn(STRING_ENTITY);
37         when(clientResponse.getStatus()).thenReturn(200);
38         JsonRestClientResponse response = new JsonRestClientResponse(clientResponse);
39
40         assertSame(clientResponse, response.getClientResponse());
41         assertEquals(STRING_ENTITY, response.getJsonResponse());
42         assertEquals(200, response.getStatusCode());
43         assertNull(response.getClientHandlerException());
44     }
45
46     @Test
47     public void testResponse_UniformInterfaceException() {
48         UniformInterfaceException ex = new UniformInterfaceException(clientResponse);
49         when(clientResponse.getEntity(String.class)).thenThrow(ex);
50         when(clientResponse.getStatus()).thenReturn(204);
51
52         JsonRestClientResponse response = new JsonRestClientResponse(clientResponse);
53
54         assertNull(response.getJsonResponse());
55         assertEquals(204, response.getStatusCode());
56         assertNull(response.getClientHandlerException());
57     }
58
59     @Test
60     public void testResponse_ClientHandlerException() {
61         ClientHandlerException ex = new ClientHandlerException();
62         when(clientResponse.getEntity(String.class)).thenThrow(ex);
63         when(clientResponse.getStatus()).thenReturn(404);
64
65         JsonRestClientResponse response = new JsonRestClientResponse(clientResponse);
66
67         assertNull(response.getJsonResponse());
68         assertEquals(404, response.getStatusCode());
69         assertSame(ex, response.getClientHandlerException());
70     }
71 }