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