2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.lispflowmapping.southbound;
10 import static org.junit.Assert.assertEquals;
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;
51 @RunWith(MockitoJUnitRunner.class)
52 public class LispSouthboundRpcTest {
54 @Mock LispSouthboundPlugin lispSouthboundPlugin;
55 @InjectMocks LispSouthboundRPC lispSouthboundRPC;
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();
63 * Tests {@link LispSouthboundRPC#sendMapNotify} method.
65 * @throws ExecutionException
66 * @throws InterruptedException
69 public void sendMapNotifyTest_inputNotNull() throws ExecutionException, InterruptedException {
71 final MapNotify mapNotify = getDefaultMapNotifyBuilder().build();
72 final TransportAddress transportAddress = new TransportAddressBuilder().build();
73 final SendMapNotifyInput sendMapNotifyInputMock = Mockito.mock(SendMapNotifyInput.class);
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());
80 Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
81 MapNotifySerializer.getInstance().serialize(mapNotify), MessageType.MapNotify);
85 * Tests {@link LispSouthboundRPC#sendMapNotify} method with null input.
87 * @throws ExecutionException
88 * @throws InterruptedException
91 public void sendMapNotifyTest_nullInput() throws ExecutionException, InterruptedException {
92 assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
93 lispSouthboundRPC.sendMapNotify(null).get().isSuccessful());
95 Mockito.verifyZeroInteractions(lispSouthboundPlugin);
99 * Tests {@link LispSouthboundRPC#sendMapReply} method.
101 * @throws ExecutionException
102 * @throws InterruptedException
105 public void sendMapReplyTest_inputNotNull() throws ExecutionException, InterruptedException {
107 final MapReply mapReply = getDefaultMapReplyBuilder().build();
108 final TransportAddress transportAddress = new TransportAddressBuilder().build();
109 final SendMapReplyInput sendMapReplyInputMock = Mockito.mock(SendMapReplyInput.class);
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());
116 Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
117 MapReplySerializer.getInstance().serialize(mapReply), MessageType.MapReply);
121 * Tests {@link LispSouthboundRPC#sendMapReply} method with null input.
123 * @throws ExecutionException
124 * @throws InterruptedException
127 public void sendMapReplyTest_nullInput() throws ExecutionException, InterruptedException {
128 assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
129 lispSouthboundRPC.sendMapReply(null).get().isSuccessful());
131 Mockito.verifyZeroInteractions(lispSouthboundPlugin);
135 * Tests {@link LispSouthboundRPC#sendMapRequest} method.
137 * @throws ExecutionException
138 * @throws InterruptedException
141 public void sendMapRequestTest_inputNotNull() throws ExecutionException, InterruptedException {
143 final MapRequest mapRequest = new MapRequestBuilder().build();
144 final TransportAddress transportAddress = new TransportAddressBuilder().build();
145 final SendMapRequestInput sendMapRequestInputMock = Mockito.mock(SendMapRequestInput.class);
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());
152 Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
153 MapRequestSerializer.getInstance().serialize(mapRequest), MessageType.MapRequest);
157 * Tests {@link LispSouthboundRPC#sendMapRequest} method with null input.
159 * @throws ExecutionException
160 * @throws InterruptedException
163 public void sendMapRequestTest_nullInput() throws ExecutionException, InterruptedException {
164 assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
165 lispSouthboundRPC.sendMapRequest(null).get().isSuccessful());
167 Mockito.verifyZeroInteractions(lispSouthboundPlugin);
171 * Tests {@link LispSouthboundRPC#sendMapRegister} method.
173 * @throws ExecutionException
174 * @throws InterruptedException
177 public void sendMapRegisterTest_inputNotNull() throws ExecutionException, InterruptedException {
179 final MapRegister mapRegister = getDefaultMapRegisterBuilder().build();
180 final TransportAddress transportAddress = new TransportAddressBuilder().build();
181 final SendMapRegisterInput sendMapRegisterInputMock = Mockito.mock(SendMapRegisterInput.class);
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());
188 Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
189 MapRegisterSerializer.getInstance().serialize(mapRegister), MessageType.MapRegister);
193 * Tests {@link LispSouthboundRPC#sendMapRegister} method with null input.
195 * @throws ExecutionException
196 * @throws InterruptedException
199 public void sendMapRegisterTest_nullInput() throws ExecutionException, InterruptedException {
200 assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
201 lispSouthboundRPC.sendMapRegister(null).get().isSuccessful());
203 Mockito.verifyZeroInteractions(lispSouthboundPlugin);
207 * Tests {@link LispSouthboundRPC#getStats} method.
209 * @throws ExecutionException
210 * @throws InterruptedException
213 public void getStatsTest() throws ExecutionException, InterruptedException {
214 final LispSouthboundStats stats = new LispSouthboundStats();
216 Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(stats);
219 final ControlMessageStats resultStats = lispSouthboundRPC.getStats().get().getResult().getControlMessageStats();
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());
232 * Tests {@link LispSouthboundRPC#getStats} method with null stats.
234 * @throws ExecutionException
235 * @throws InterruptedException
238 public void getStatsTest_withNullStats() throws ExecutionException, InterruptedException {
239 final String expectedMsg = ((RpcError) RPC_RESULT_ERROR.getErrors().iterator().next()).getMessage();
241 Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(null);
243 final Future<RpcResult<GetStatsOutput>> resultFuture = lispSouthboundRPC.getStats();
244 final RpcResult<GetStatsOutput> rpcResult = resultFuture.get();
246 assertEquals(RPC_RESULT_ERROR.isSuccessful(), rpcResult.isSuccessful());
247 assertEquals(expectedMsg, rpcResult.getErrors().iterator().next().getMessage());
251 * Tests {@link LispSouthboundRPC#resetStats} method.
253 * @throws ExecutionException
254 * @throws InterruptedException
257 public void resetStatsTest() throws ExecutionException, InterruptedException {
258 final LispSouthboundStats stats = new LispSouthboundStats();
260 Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(stats);
262 assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), lispSouthboundRPC.resetStats().get().isSuccessful());
264 for (long rx : stats.getRx()) {
270 * Tests {@link LispSouthboundRPC#resetStats} method with null stats.
272 * @throws ExecutionException
273 * @throws InterruptedException
276 public void resetStatsTest_withNullStats() throws ExecutionException, InterruptedException {
277 final String expectedMsg = ((RpcError) RPC_RESULT_ERROR.getErrors().iterator().next()).getMessage();
279 Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(null);
281 final Future<RpcResult<Void>> resultFuture = lispSouthboundRPC.resetStats();
282 final RpcResult<Void> rpcResult = resultFuture.get();
284 assertEquals(RPC_RESULT_ERROR.isSuccessful(), rpcResult.isSuccessful());
285 assertEquals(expectedMsg, rpcResult.getErrors().iterator().next().getMessage());
288 private static MappingRecordItem getDefaultMappingRecordItem() {
289 return new MappingRecordItemBuilder()
290 .setMappingRecord(new MappingRecordBuilder().build()).build();
293 private static MapNotifyBuilder getDefaultMapNotifyBuilder() {
294 return new MapNotifyBuilder()
295 .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
298 private static MapReplyBuilder getDefaultMapReplyBuilder() {
299 return new MapReplyBuilder()
300 .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
303 private static MapRegisterBuilder getDefaultMapRegisterBuilder() {
304 return new MapRegisterBuilder()
305 .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
308 private static void incrementAll(LispSouthboundStats stats) {
309 for (MessageType type : MessageType.values()) {
310 stats.incrementRx(type.getIntValue());