Unit tests for ofoverlay
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / statistics / SFlowRTConnectionTest.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.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;
18
19 import javax.ws.rs.core.MultivaluedMap;
20 import java.util.concurrent.ScheduledExecutorService;
21
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;
31
32 public class SFlowRTConnectionTest {
33
34     private static final String PATH = "/";
35     private static final String JSON_STRING = "jsonString";
36     private static final String JSON_RESPONSE = "jsonResponse";
37
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;
45
46     @Rule
47     public ExpectedException thrown = ExpectedException.none();
48
49     @Before
50     public void init() {
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");
55
56         executor = mock(ScheduledExecutorService.class);
57         collectorUri = "";
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);
66
67         connection = spy(new SFlowRTConnection(executor, collectorUri, flowCache, client));
68     }
69
70     @Test
71     public void testConstructor() {
72         SFlowRTConnection other = new SFlowRTConnection(executor, collectorUri, flowCache, client);
73
74         assertNotNull(other.getExecutor());
75         assertNotNull(other.getFlowCache());
76     }
77
78     @Test
79     public void testGetJsonResponse() {
80         String res = connection.getJsonResponse(PATH, params);
81
82         assertEquals(JSON_RESPONSE, res);
83     }
84
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);
89
90         connection.getJsonResponse(PATH, params);
91     }
92
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);
98
99         connection.getJsonResponse(PATH, params);
100     }
101
102     @Test
103     public void testGet() {
104         JsonRestClientResponse res = connection.get(PATH, params);
105
106         assertEquals(response, res);
107     }
108
109     @Test
110     public void testGet_notInitialized() {
111         when(connection.isInitialized()).thenReturn(false);
112
113         thrown.expect(IllegalStateException.class);
114         thrown.expectMessage(SFlowRTConnection.EX_MSG_NOT_INITIALIZED);
115         connection.get(PATH, params);
116     }
117
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);
122
123         connection.get(PATH, params);
124     }
125
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);
131
132         connection.get(PATH, params);
133     }
134
135     @Test
136     public void testPut() {
137         JsonRestClientResponse res = connection.put(PATH, JSON_STRING);
138
139         assertEquals(response, res);
140     }
141
142     @Test
143     public void testPut_notInitialized() {
144         when(connection.isInitialized()).thenReturn(false);
145
146         thrown.expect(IllegalStateException.class);
147         thrown.expectMessage(SFlowRTConnection.EX_MSG_NOT_INITIALIZED);
148         connection.put(PATH, JSON_STRING);
149     }
150
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);
155
156         connection.put(PATH, JSON_STRING);
157     }
158
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);
164
165         connection.put(PATH, JSON_STRING);
166     }
167
168     @Test
169     public void testDelete() {
170         JsonRestClientResponse res = connection.delete(PATH);
171
172         assertEquals(response, res);
173     }
174
175     @Test
176     public void testDelete_notInitialized() {
177         when(connection.isInitialized()).thenReturn(false);
178
179         thrown.expect(IllegalStateException.class);
180         thrown.expectMessage(SFlowRTConnection.EX_MSG_NOT_INITIALIZED);
181         connection.delete(PATH);
182     }
183
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);
188
189         connection.delete(PATH);
190     }
191
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);
197
198         connection.delete(PATH);
199     }
200
201     @Test
202     public void testInitialize() {
203         when(response.getStatusCode()).thenReturn(300);
204         connection.initialize();
205         assertTrue(connection.isInitialized());
206
207         when(response.getStatusCode()).thenReturn(400);
208         connection.initialize();
209         assertTrue(connection.isInitialized());
210     }
211
212     @Test
213     public void testLogStatusCode_coverage() {
214         when(response.getStatusCode()).thenReturn(300);
215         connection.getJsonResponse(PATH, params);
216         connection.delete(PATH);
217
218         when(response.getStatusCode()).thenReturn(400);
219         connection.getJsonResponse(PATH, params);
220         connection.delete(PATH);
221     }
222
223 }