2 * Copyright (c) 2016 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;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.spy;
17 import static org.mockito.Mockito.when;
19 import javax.ws.rs.core.MultivaluedMap;
20 import java.util.concurrent.ScheduledExecutorService;
22 import com.sun.jersey.api.client.ClientHandlerException;
23 import com.sun.jersey.core.util.MultivaluedMapImpl;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.mockito.Mockito;
29 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics.flowcache.FlowCache;
30 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics.util.SFlowQueryParams;
32 public class SFlowRTConnectionTest {
34 private static final String PATH = "/";
35 private static final String JSON_STRING = "jsonString";
36 private static final String JSON_RESPONSE = "jsonResponse";
38 private ScheduledExecutorService executor;
39 private String collectorUri;
40 private FlowCache flowCache;
41 private JsonRestClient client;
42 private MultivaluedMap<String, String> params;
43 private SFlowRTConnection connection;
44 private JsonRestClientResponse response;
47 public ExpectedException thrown = ExpectedException.none();
51 params = new MultivaluedMapImpl();
52 params.add(SFlowQueryParams.MAX_FLOWS, "20");
53 params.add(SFlowQueryParams.MIN_VALUE, "0.1");
54 params.add(SFlowQueryParams.AGG_MODE, "sum");
56 executor = mock(ScheduledExecutorService.class);
58 flowCache = mock(FlowCache.class);
59 client = mock(JsonRestClient.class);
60 response = mock(JsonRestClientResponse.class);
61 when(response.getJsonResponse()).thenReturn(JSON_RESPONSE);
62 when(response.getStatusCode()).thenReturn(200);
63 when(client.get(any(String.class), Mockito.<MultivaluedMap<String, String>>any())).thenReturn(response);
64 when(client.put(any(String.class), any(String.class))).thenReturn(response);
65 when(client.delete(any(String.class))).thenReturn(response);
67 connection = spy(new SFlowRTConnection(executor, collectorUri, flowCache, client));
71 public void testConstructor() {
72 SFlowRTConnection other = new SFlowRTConnection(executor, collectorUri, flowCache, client);
74 assertNotNull(other.getExecutor());
75 assertNotNull(other.getFlowCache());
79 public void testGetJsonResponse() {
80 String res = connection.getJsonResponse(PATH, params);
82 assertEquals(JSON_RESPONSE, res);
85 @Test(expected = ClientHandlerException.class)
86 public void testGetJsonResponse_ClientHandlerException_noCause() {
87 ClientHandlerException ex = new ClientHandlerException();
88 when(client.get(any(String.class), Mockito.<MultivaluedMap<String, String>>any())).thenThrow(ex);
90 connection.getJsonResponse(PATH, params);
93 @Test(expected = ClientHandlerException.class)
94 public void testGetJsonResponse_ClientHandlerException_caused() {
95 ClientHandlerException ex = new ClientHandlerException();
96 ex.initCause(new java.net.ConnectException());
97 when(client.get(any(String.class), Mockito.<MultivaluedMap<String, String>>any())).thenThrow(ex);
99 connection.getJsonResponse(PATH, params);
103 public void testGet() {
104 JsonRestClientResponse res = connection.get(PATH, params);
106 assertEquals(response, res);
110 public void testGet_notInitialized() {
111 when(connection.isInitialized()).thenReturn(false);
113 thrown.expect(IllegalStateException.class);
114 thrown.expectMessage(SFlowRTConnection.EX_MSG_NOT_INITIALIZED);
115 connection.get(PATH, params);
118 @Test(expected = ClientHandlerException.class)
119 public void testGet_ClientHandlerException_noCause() {
120 ClientHandlerException ex = new ClientHandlerException();
121 when(client.get(any(String.class), Mockito.<MultivaluedMap<String, String>>any())).thenThrow(ex);
123 connection.get(PATH, params);
126 @Test(expected = ClientHandlerException.class)
127 public void testGet_ClientHandlerException_caused() {
128 ClientHandlerException ex = new ClientHandlerException();
129 ex.initCause(new java.net.ConnectException());
130 when(client.get(any(String.class), Mockito.<MultivaluedMap<String, String>>any())).thenThrow(ex);
132 connection.get(PATH, params);
136 public void testPut() {
137 JsonRestClientResponse res = connection.put(PATH, JSON_STRING);
139 assertEquals(response, res);
143 public void testPut_notInitialized() {
144 when(connection.isInitialized()).thenReturn(false);
146 thrown.expect(IllegalStateException.class);
147 thrown.expectMessage(SFlowRTConnection.EX_MSG_NOT_INITIALIZED);
148 connection.put(PATH, JSON_STRING);
151 @Test(expected = ClientHandlerException.class)
152 public void testPut_ClientHandlerException_noCause() {
153 ClientHandlerException ex = new ClientHandlerException();
154 when(client.put(any(String.class), any(String.class))).thenThrow(ex);
156 connection.put(PATH, JSON_STRING);
159 @Test(expected = ClientHandlerException.class)
160 public void testPut_ClientHandlerException_caused() {
161 ClientHandlerException ex = new ClientHandlerException();
162 ex.initCause(new java.net.ConnectException());
163 when(client.put(any(String.class), any(String.class))).thenThrow(ex);
165 connection.put(PATH, JSON_STRING);
169 public void testDelete() {
170 JsonRestClientResponse res = connection.delete(PATH);
172 assertEquals(response, res);
176 public void testDelete_notInitialized() {
177 when(connection.isInitialized()).thenReturn(false);
179 thrown.expect(IllegalStateException.class);
180 thrown.expectMessage(SFlowRTConnection.EX_MSG_NOT_INITIALIZED);
181 connection.delete(PATH);
184 @Test(expected = ClientHandlerException.class)
185 public void testDelete_ClientHandlerException_noCause() {
186 ClientHandlerException ex = new ClientHandlerException();
187 when(client.delete(any(String.class))).thenThrow(ex);
189 connection.delete(PATH);
192 @Test(expected = ClientHandlerException.class)
193 public void testDelete_ClientHandlerException_caused() {
194 ClientHandlerException ex = new ClientHandlerException();
195 ex.initCause(new java.net.ConnectException());
196 when(client.delete(any(String.class))).thenThrow(ex);
198 connection.delete(PATH);
202 public void testInitialize() {
203 when(response.getStatusCode()).thenReturn(300);
204 connection.initialize();
205 assertTrue(connection.isInitialized());
207 when(response.getStatusCode()).thenReturn(400);
208 connection.initialize();
209 assertTrue(connection.isInitialized());
213 public void testLogStatusCode_coverage() {
214 when(response.getStatusCode()).thenReturn(300);
215 connection.getJsonResponse(PATH, params);
216 connection.delete(PATH);
218 when(response.getStatusCode()).thenReturn(400);
219 connection.getJsonResponse(PATH, params);
220 connection.delete(PATH);