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.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.RpcError;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
53 @RunWith(MockitoJUnitRunner.class)
54 public class LispSouthboundRpcTest {
56 @Mock LispSouthboundPlugin lispSouthboundPlugin;
57 @InjectMocks LispSouthboundRPC lispSouthboundRPC;
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();
65 * Tests {@link LispSouthboundRPC#sendMapNotify} method.
68 public void sendMapNotifyTest_inputNotNull() throws ExecutionException, InterruptedException {
70 final MapNotify mapNotify = getDefaultMapNotifyBuilder().build();
71 final TransportAddress transportAddress = new TransportAddressBuilder().build();
72 final SendMapNotifyInput sendMapNotifyInputMock = Mockito.mock(SendMapNotifyInput.class);
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());
79 Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
80 MapNotifySerializer.getInstance().serialize(mapNotify), MessageType.MapNotify);
84 * Tests {@link LispSouthboundRPC#sendMapNotify} method with null input.
87 public void sendMapNotifyTest_nullInput() throws ExecutionException, InterruptedException {
88 assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
89 lispSouthboundRPC.sendMapNotify(null).get().isSuccessful());
91 Mockito.verifyZeroInteractions(lispSouthboundPlugin);
95 * Tests {@link LispSouthboundRPC#sendMapReply} method.
98 public void sendMapReplyTest_inputNotNull() throws ExecutionException, InterruptedException {
100 final MapReply mapReply = getDefaultMapReplyBuilder().build();
101 final TransportAddress transportAddress = new TransportAddressBuilder().build();
102 final SendMapReplyInput sendMapReplyInputMock = Mockito.mock(SendMapReplyInput.class);
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());
109 Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
110 MapReplySerializer.getInstance().serialize(mapReply), MessageType.MapReply);
114 * Tests {@link LispSouthboundRPC#sendMapReply} method with null input.
117 public void sendMapReplyTest_nullInput() throws ExecutionException, InterruptedException {
118 assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
119 lispSouthboundRPC.sendMapReply(null).get().isSuccessful());
121 Mockito.verifyZeroInteractions(lispSouthboundPlugin);
125 * Tests {@link LispSouthboundRPC#sendMapRequest} method.
128 public void sendMapRequestTest_inputNotNull() throws ExecutionException, InterruptedException {
130 final MapRequest mapRequest = new MapRequestBuilder().build();
131 final TransportAddress transportAddress = new TransportAddressBuilder().build();
132 final SendMapRequestInput sendMapRequestInputMock = Mockito.mock(SendMapRequestInput.class);
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());
139 Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
140 MapRequestSerializer.getInstance().serialize(mapRequest), MessageType.MapRequest);
144 * Tests {@link LispSouthboundRPC#sendMapRequest} method with null input.
147 public void sendMapRequestTest_nullInput() throws ExecutionException, InterruptedException {
148 assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
149 lispSouthboundRPC.sendMapRequest(null).get().isSuccessful());
151 Mockito.verifyZeroInteractions(lispSouthboundPlugin);
155 * Tests {@link LispSouthboundRPC#sendMapRegister} method.
158 public void sendMapRegisterTest_inputNotNull() throws ExecutionException, InterruptedException {
160 final MapRegister mapRegister = getDefaultMapRegisterBuilder().build();
161 final TransportAddress transportAddress = new TransportAddressBuilder().build();
162 final SendMapRegisterInput sendMapRegisterInputMock = Mockito.mock(SendMapRegisterInput.class);
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());
169 Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
170 MapRegisterSerializer.getInstance().serialize(mapRegister), MessageType.MapRegister);
174 * Tests {@link LispSouthboundRPC#sendMapRegister} method with null input.
177 public void sendMapRegisterTest_nullInput() throws ExecutionException, InterruptedException {
178 assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
179 lispSouthboundRPC.sendMapRegister(null).get().isSuccessful());
181 Mockito.verifyZeroInteractions(lispSouthboundPlugin);
185 * Tests {@link LispSouthboundRPC#getStats} method.
188 public void getStatsTest() throws ExecutionException, InterruptedException {
189 final ConcurrentLispSouthboundStats stats = new ConcurrentLispSouthboundStats();
191 Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(stats);
194 final ControlMessageStats resultStats = lispSouthboundRPC.getStats(
195 Mockito.mock(GetStatsInput.class)).get().getResult().getControlMessageStats();
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());
208 * Tests {@link LispSouthboundRPC#getStats} method with null stats.
211 public void getStatsTest_withNullStats() throws ExecutionException, InterruptedException {
212 final String expectedMsg = ((RpcError) RPC_RESULT_ERROR.getErrors().iterator().next()).getMessage();
214 Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(null);
216 final Future<RpcResult<GetStatsOutput>> resultFuture = lispSouthboundRPC.getStats(
217 Mockito.mock(GetStatsInput.class));
218 final RpcResult<GetStatsOutput> rpcResult = resultFuture.get();
220 assertEquals(RPC_RESULT_ERROR.isSuccessful(), rpcResult.isSuccessful());
221 assertEquals(expectedMsg, rpcResult.getErrors().iterator().next().getMessage());
225 * Tests {@link LispSouthboundRPC#resetStats} method.
228 public void resetStatsTest() throws ExecutionException, InterruptedException {
229 final ConcurrentLispSouthboundStats stats = new ConcurrentLispSouthboundStats();
231 Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(stats);
233 assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), lispSouthboundRPC.resetStats(
234 Mockito.mock(ResetStatsInput.class)).get().isSuccessful());
236 for (long rx : stats.getRx()) {
242 * Tests {@link LispSouthboundRPC#resetStats} method with null stats.
245 public void resetStatsTest_withNullStats() throws ExecutionException, InterruptedException {
246 final String expectedMsg = ((RpcError) RPC_RESULT_ERROR.getErrors().iterator().next()).getMessage();
248 Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(null);
250 final Future<RpcResult<ResetStatsOutput>> resultFuture = lispSouthboundRPC.resetStats(
251 Mockito.mock(ResetStatsInput.class));
252 final RpcResult<ResetStatsOutput> rpcResult = resultFuture.get();
254 assertEquals(RPC_RESULT_ERROR.isSuccessful(), rpcResult.isSuccessful());
255 assertEquals(expectedMsg, rpcResult.getErrors().iterator().next().getMessage());
258 private static MappingRecordItem getDefaultMappingRecordItem() {
259 return new MappingRecordItemBuilder()
260 .withKey(new MappingRecordItemKey("foo"))
261 .setMappingRecord(new MappingRecordBuilder().build()).build();
264 private static MapNotifyBuilder getDefaultMapNotifyBuilder() {
265 return new MapNotifyBuilder()
266 .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
269 private static MapReplyBuilder getDefaultMapReplyBuilder() {
270 return new MapReplyBuilder()
271 .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
274 private static MapRegisterBuilder getDefaultMapRegisterBuilder() {
275 return new MapRegisterBuilder()
276 .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
279 private static void incrementAll(ConcurrentLispSouthboundStats stats) {
280 for (MessageType type : MessageType.values()) {
281 stats.incrementRx(type.getIntValue());