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