Create and switch to a concurrent version of LispSouthboundStats
[lispflowmapping.git] / mappingservice / southbound / src / test / java / org / opendaylight / lispflowmapping / southbound / LispSouthboundRpcTest.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 package org.opendaylight.lispflowmapping.southbound;
9
10 import static org.junit.Assert.assertEquals;
11
12 import com.google.common.collect.Lists;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.Future;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.InjectMocks;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
22 import org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer;
23 import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
24 import org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotify;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapregistermessage.MapRegister;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapregistermessage.MapRegisterBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequest;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.GetStatsOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRegisterInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.get.stats.output.ControlMessageStats;
45 import org.opendaylight.yangtools.yang.common.RpcError;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class LispSouthboundRpcTest {
51
52     @Mock LispSouthboundPlugin lispSouthboundPlugin;
53     @InjectMocks LispSouthboundRPC lispSouthboundRPC;
54
55     private static final RpcResult<Void> RPC_RESULT_FAILURE = RpcResultBuilder.<Void>failed().build();
56     private static final RpcResult<Void> RPC_RESULT_SUCCESS = RpcResultBuilder.<Void>success().build();
57     private static final RpcResult<GetStatsOutput> RPC_RESULT_ERROR = RpcResultBuilder.<GetStatsOutput>failed()
58             .withError(RpcError.ErrorType.APPLICATION, "data-missing", "No stats found").build();
59
60     /**
61      * Tests {@link LispSouthboundRPC#sendMapNotify} method.
62      */
63     @Test
64     public void sendMapNotifyTest_inputNotNull() throws ExecutionException, InterruptedException {
65
66         final MapNotify mapNotify = getDefaultMapNotifyBuilder().build();
67         final TransportAddress transportAddress = new TransportAddressBuilder().build();
68         final SendMapNotifyInput sendMapNotifyInputMock = Mockito.mock(SendMapNotifyInput.class);
69
70         Mockito.when(sendMapNotifyInputMock.getTransportAddress()).thenReturn(transportAddress);
71         Mockito.when(sendMapNotifyInputMock.getMapNotify()).thenReturn(mapNotify);
72         assertEquals(RPC_RESULT_SUCCESS.isSuccessful(),
73                 lispSouthboundRPC.sendMapNotify(sendMapNotifyInputMock).get().isSuccessful());
74
75         Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
76                 MapNotifySerializer.getInstance().serialize(mapNotify), MessageType.MapNotify);
77     }
78
79     /**
80      * Tests {@link LispSouthboundRPC#sendMapNotify} method with null input.
81      */
82     @Test
83     public void sendMapNotifyTest_nullInput() throws ExecutionException, InterruptedException {
84         assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
85                 lispSouthboundRPC.sendMapNotify(null).get().isSuccessful());
86
87         Mockito.verifyZeroInteractions(lispSouthboundPlugin);
88     }
89
90     /**
91      * Tests {@link LispSouthboundRPC#sendMapReply} method.
92      */
93     @Test
94     public void sendMapReplyTest_inputNotNull() throws ExecutionException, InterruptedException {
95
96         final MapReply mapReply = getDefaultMapReplyBuilder().build();
97         final TransportAddress transportAddress = new TransportAddressBuilder().build();
98         final SendMapReplyInput sendMapReplyInputMock = Mockito.mock(SendMapReplyInput.class);
99
100         Mockito.when(sendMapReplyInputMock.getTransportAddress()).thenReturn(transportAddress);
101         Mockito.when(sendMapReplyInputMock.getMapReply()).thenReturn(mapReply);
102         assertEquals(RPC_RESULT_SUCCESS.isSuccessful(),
103                 lispSouthboundRPC.sendMapReply(sendMapReplyInputMock).get().isSuccessful());
104
105         Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
106                 MapReplySerializer.getInstance().serialize(mapReply), MessageType.MapReply);
107     }
108
109     /**
110      * Tests {@link LispSouthboundRPC#sendMapReply} method with null input.
111      */
112     @Test
113     public void sendMapReplyTest_nullInput() throws ExecutionException, InterruptedException {
114         assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
115                 lispSouthboundRPC.sendMapReply(null).get().isSuccessful());
116
117         Mockito.verifyZeroInteractions(lispSouthboundPlugin);
118     }
119
120     /**
121      * Tests {@link LispSouthboundRPC#sendMapRequest} method.
122      */
123     @Test
124     public void sendMapRequestTest_inputNotNull() throws ExecutionException, InterruptedException {
125
126         final MapRequest mapRequest = new MapRequestBuilder().build();
127         final TransportAddress transportAddress = new TransportAddressBuilder().build();
128         final SendMapRequestInput sendMapRequestInputMock = Mockito.mock(SendMapRequestInput.class);
129
130         Mockito.when(sendMapRequestInputMock.getTransportAddress()).thenReturn(transportAddress);
131         Mockito.when(sendMapRequestInputMock.getMapRequest()).thenReturn(mapRequest);
132         assertEquals(RPC_RESULT_SUCCESS.isSuccessful(),
133                 lispSouthboundRPC.sendMapRequest(sendMapRequestInputMock).get().isSuccessful());
134
135         Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
136                 MapRequestSerializer.getInstance().serialize(mapRequest), MessageType.MapRequest);
137     }
138
139     /**
140      * Tests {@link LispSouthboundRPC#sendMapRequest} method with null input.
141      */
142     @Test
143     public void sendMapRequestTest_nullInput() throws ExecutionException, InterruptedException {
144         assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
145                 lispSouthboundRPC.sendMapRequest(null).get().isSuccessful());
146
147         Mockito.verifyZeroInteractions(lispSouthboundPlugin);
148     }
149
150     /**
151      * Tests {@link LispSouthboundRPC#sendMapRegister} method.
152      */
153     @Test
154     public void sendMapRegisterTest_inputNotNull() throws ExecutionException, InterruptedException {
155
156         final MapRegister mapRegister = getDefaultMapRegisterBuilder().build();
157         final TransportAddress transportAddress = new TransportAddressBuilder().build();
158         final SendMapRegisterInput sendMapRegisterInputMock = Mockito.mock(SendMapRegisterInput.class);
159
160         Mockito.when(sendMapRegisterInputMock.getTransportAddress()).thenReturn(transportAddress);
161         Mockito.when(sendMapRegisterInputMock.getMapRegister()).thenReturn(mapRegister);
162         assertEquals(RPC_RESULT_SUCCESS.isSuccessful(),
163                 lispSouthboundRPC.sendMapRegister(sendMapRegisterInputMock).get().isSuccessful());
164
165         Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
166                 MapRegisterSerializer.getInstance().serialize(mapRegister), MessageType.MapRegister);
167     }
168
169     /**
170      * Tests {@link LispSouthboundRPC#sendMapRegister} method with null input.
171      */
172     @Test
173     public void sendMapRegisterTest_nullInput() throws ExecutionException, InterruptedException {
174         assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
175                 lispSouthboundRPC.sendMapRegister(null).get().isSuccessful());
176
177         Mockito.verifyZeroInteractions(lispSouthboundPlugin);
178     }
179
180     /**
181      * Tests {@link LispSouthboundRPC#getStats} method.
182      */
183     @Test
184     public void getStatsTest() throws ExecutionException, InterruptedException {
185         final ConcurrentLispSouthboundStats stats = new ConcurrentLispSouthboundStats();
186         incrementAll(stats);
187         Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(stats);
188
189         // result
190         final ControlMessageStats resultStats = lispSouthboundRPC.getStats().get().getResult().getControlMessageStats();
191
192         assertEquals(stats.getRx()[0], (long) resultStats.getControlMessage().get(0).getRxCount());
193         assertEquals(stats.getRx()[1], (long) resultStats.getControlMessage().get(1).getRxCount());
194         assertEquals(stats.getRx()[2], (long) resultStats.getControlMessage().get(2).getRxCount());
195         assertEquals(stats.getRx()[3], (long) resultStats.getControlMessage().get(3).getRxCount());
196         assertEquals(stats.getRx()[4], (long) resultStats.getControlMessage().get(4).getRxCount());
197         assertEquals(stats.getRx()[6], (long) resultStats.getControlMessage().get(5).getRxCount());
198         assertEquals(stats.getRx()[7], (long) resultStats.getControlMessage().get(6).getRxCount());
199         assertEquals(stats.getRx()[8], (long) resultStats.getControlMessage().get(7).getRxCount());
200     }
201
202     /**
203      * Tests {@link LispSouthboundRPC#getStats} method with null stats.
204      */
205     @Test
206     public void getStatsTest_withNullStats() throws ExecutionException, InterruptedException {
207         final String expectedMsg = ((RpcError) RPC_RESULT_ERROR.getErrors().iterator().next()).getMessage();
208
209         Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(null);
210
211         final Future<RpcResult<GetStatsOutput>> resultFuture = lispSouthboundRPC.getStats();
212         final RpcResult<GetStatsOutput> rpcResult = resultFuture.get();
213
214         assertEquals(RPC_RESULT_ERROR.isSuccessful(), rpcResult.isSuccessful());
215         assertEquals(expectedMsg, rpcResult.getErrors().iterator().next().getMessage());
216     }
217
218     /**
219      * Tests {@link LispSouthboundRPC#resetStats} method.
220      */
221     @Test
222     public void resetStatsTest() throws ExecutionException, InterruptedException {
223         final ConcurrentLispSouthboundStats stats = new ConcurrentLispSouthboundStats();
224         incrementAll(stats);
225         Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(stats);
226
227         assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), lispSouthboundRPC.resetStats().get().isSuccessful());
228
229         for (long rx : stats.getRx()) {
230             assertEquals(0, rx);
231         }
232     }
233
234     /**
235      * Tests {@link LispSouthboundRPC#resetStats} method with null stats.
236      */
237     @Test
238     public void resetStatsTest_withNullStats() throws ExecutionException, InterruptedException {
239         final String expectedMsg = ((RpcError) RPC_RESULT_ERROR.getErrors().iterator().next()).getMessage();
240
241         Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(null);
242
243         final Future<RpcResult<Void>> resultFuture = lispSouthboundRPC.resetStats();
244         final RpcResult<Void> rpcResult = resultFuture.get();
245
246         assertEquals(RPC_RESULT_ERROR.isSuccessful(), rpcResult.isSuccessful());
247         assertEquals(expectedMsg, rpcResult.getErrors().iterator().next().getMessage());
248     }
249
250     private static MappingRecordItem getDefaultMappingRecordItem() {
251         return new MappingRecordItemBuilder()
252                 .setMappingRecord(new MappingRecordBuilder().build()).build();
253     }
254
255     private static MapNotifyBuilder getDefaultMapNotifyBuilder() {
256         return new MapNotifyBuilder()
257                 .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
258     }
259
260     private static MapReplyBuilder getDefaultMapReplyBuilder() {
261         return new MapReplyBuilder()
262                 .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
263     }
264
265     private static MapRegisterBuilder getDefaultMapRegisterBuilder() {
266         return new MapRegisterBuilder()
267                 .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
268     }
269
270     private static void incrementAll(ConcurrentLispSouthboundStats stats) {
271         for (MessageType type : MessageType.values()) {
272             stats.incrementRx(type.getIntValue());
273         }
274     }
275 }