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