--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.lispflowmapping.southbound;
+
+import static org.junit.Assert.assertEquals;
+
+import com.google.common.collect.Lists;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
+import org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer;
+import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
+import org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotify;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container
+ .MappingRecordBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list
+ .MappingRecordItemBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapregistermessage.MapRegister;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapregistermessage.MapRegisterBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReply;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequest;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.GetStatsOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRegisterInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.get.stats.output.ControlMessageStats;
+import org.opendaylight.yangtools.yang.common.RpcError;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+
+@RunWith(MockitoJUnitRunner.class)
+public class LispSouthboundRpcTest {
+
+ @Mock LispSouthboundPlugin lispSouthboundPlugin;
+ @InjectMocks LispSouthboundRPC lispSouthboundRPC;
+
+ private static final RpcResult<Void> RPC_RESULT_FAILURE = RpcResultBuilder.<Void>failed().build();
+ private static final RpcResult<Void> RPC_RESULT_SUCCESS = RpcResultBuilder.<Void>success().build();
+ private static final RpcResult<GetStatsOutput> RPC_RESULT_ERROR = RpcResultBuilder.<GetStatsOutput>failed()
+ .withError(RpcError.ErrorType.APPLICATION, "data-missing", "No stats found").build();
+
+ /**
+ * Tests {@link LispSouthboundRPC#sendMapNotify} method.
+ *
+ * @throws ExecutionException
+ * @throws InterruptedException
+ */
+ @Test
+ public void sendMapNotifyTest_inputNotNull() throws ExecutionException, InterruptedException {
+
+ final MapNotify mapNotify = getDefaultMapNotifyBuilder().build();
+ final TransportAddress transportAddress = new TransportAddressBuilder().build();
+ final SendMapNotifyInput sendMapNotifyInputMock = Mockito.mock(SendMapNotifyInput.class);
+
+ Mockito.when(sendMapNotifyInputMock.getTransportAddress()).thenReturn(transportAddress);
+ Mockito.when(sendMapNotifyInputMock.getMapNotify()).thenReturn(mapNotify);
+ assertEquals(RPC_RESULT_SUCCESS.isSuccessful(),
+ lispSouthboundRPC.sendMapNotify(sendMapNotifyInputMock).get().isSuccessful());
+
+ Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
+ MapNotifySerializer.getInstance().serialize(mapNotify), MessageType.MapNotify);
+ }
+
+ /**
+ * Tests {@link LispSouthboundRPC#sendMapNotify} method with null input.
+ *
+ * @throws ExecutionException
+ * @throws InterruptedException
+ */
+ @Test
+ public void sendMapNotifyTest_nullInput() throws ExecutionException, InterruptedException {
+ assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
+ lispSouthboundRPC.sendMapNotify(null).get().isSuccessful());
+
+ Mockito.verifyZeroInteractions(lispSouthboundPlugin);
+ }
+
+ /**
+ * Tests {@link LispSouthboundRPC#sendMapReply} method.
+ *
+ * @throws ExecutionException
+ * @throws InterruptedException
+ */
+ @Test
+ public void sendMapReplyTest_inputNotNull() throws ExecutionException, InterruptedException {
+
+ final MapReply mapReply = getDefaultMapReplyBuilder().build();
+ final TransportAddress transportAddress = new TransportAddressBuilder().build();
+ final SendMapReplyInput sendMapReplyInputMock = Mockito.mock(SendMapReplyInput.class);
+
+ Mockito.when(sendMapReplyInputMock.getTransportAddress()).thenReturn(transportAddress);
+ Mockito.when(sendMapReplyInputMock.getMapReply()).thenReturn(mapReply);
+ assertEquals(RPC_RESULT_SUCCESS.isSuccessful(),
+ lispSouthboundRPC.sendMapReply(sendMapReplyInputMock).get().isSuccessful());
+
+ Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
+ MapReplySerializer.getInstance().serialize(mapReply), MessageType.MapReply);
+ }
+
+ /**
+ * Tests {@link LispSouthboundRPC#sendMapReply} method with null input.
+ *
+ * @throws ExecutionException
+ * @throws InterruptedException
+ */
+ @Test
+ public void sendMapReplyTest_nullInput() throws ExecutionException, InterruptedException {
+ assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
+ lispSouthboundRPC.sendMapReply(null).get().isSuccessful());
+
+ Mockito.verifyZeroInteractions(lispSouthboundPlugin);
+ }
+
+ /**
+ * Tests {@link LispSouthboundRPC#sendMapRequest} method.
+ *
+ * @throws ExecutionException
+ * @throws InterruptedException
+ */
+ @Test
+ public void sendMapRequestTest_inputNotNull() throws ExecutionException, InterruptedException {
+
+ final MapRequest mapRequest = new MapRequestBuilder().build();
+ final TransportAddress transportAddress = new TransportAddressBuilder().build();
+ final SendMapRequestInput sendMapRequestInputMock = Mockito.mock(SendMapRequestInput.class);
+
+ Mockito.when(sendMapRequestInputMock.getTransportAddress()).thenReturn(transportAddress);
+ Mockito.when(sendMapRequestInputMock.getMapRequest()).thenReturn(mapRequest);
+ assertEquals(RPC_RESULT_SUCCESS.isSuccessful(),
+ lispSouthboundRPC.sendMapRequest(sendMapRequestInputMock).get().isSuccessful());
+
+ Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
+ MapRequestSerializer.getInstance().serialize(mapRequest), MessageType.MapRequest);
+ }
+
+ /**
+ * Tests {@link LispSouthboundRPC#sendMapRequest} method with null input.
+ *
+ * @throws ExecutionException
+ * @throws InterruptedException
+ */
+ @Test
+ public void sendMapRequestTest_nullInput() throws ExecutionException, InterruptedException {
+ assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
+ lispSouthboundRPC.sendMapRequest(null).get().isSuccessful());
+
+ Mockito.verifyZeroInteractions(lispSouthboundPlugin);
+ }
+
+ /**
+ * Tests {@link LispSouthboundRPC#sendMapRegister} method.
+ *
+ * @throws ExecutionException
+ * @throws InterruptedException
+ */
+ @Test
+ public void sendMapRegisterTest_inputNotNull() throws ExecutionException, InterruptedException {
+
+ final MapRegister mapRegister = getDefaultMapRegisterBuilder().build();
+ final TransportAddress transportAddress = new TransportAddressBuilder().build();
+ final SendMapRegisterInput sendMapRegisterInputMock = Mockito.mock(SendMapRegisterInput.class);
+
+ Mockito.when(sendMapRegisterInputMock.getTransportAddress()).thenReturn(transportAddress);
+ Mockito.when(sendMapRegisterInputMock.getMapRegister()).thenReturn(mapRegister);
+ assertEquals(RPC_RESULT_SUCCESS.isSuccessful(),
+ lispSouthboundRPC.sendMapRegister(sendMapRegisterInputMock).get().isSuccessful());
+
+ Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
+ MapRegisterSerializer.getInstance().serialize(mapRegister), MessageType.MapRegister);
+ }
+
+ /**
+ * Tests {@link LispSouthboundRPC#sendMapRegister} method with null input.
+ *
+ * @throws ExecutionException
+ * @throws InterruptedException
+ */
+ @Test
+ public void sendMapRegisterTest_nullInput() throws ExecutionException, InterruptedException {
+ assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
+ lispSouthboundRPC.sendMapRegister(null).get().isSuccessful());
+
+ Mockito.verifyZeroInteractions(lispSouthboundPlugin);
+ }
+
+ /**
+ * Tests {@link LispSouthboundRPC#getStats} method.
+ *
+ * @throws ExecutionException
+ * @throws InterruptedException
+ */
+ @Test
+ public void getStatsTest() throws ExecutionException, InterruptedException {
+ final LispSouthboundStats stats = new LispSouthboundStats();
+ incrementAll(stats);
+ Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(stats);
+
+ // result
+ final ControlMessageStats resultStats = lispSouthboundRPC.getStats().get().getResult().getControlMessageStats();
+
+ assertEquals(stats.getRx()[0], (long) resultStats.getControlMessage().get(0).getRxCount());
+ assertEquals(stats.getRx()[1], (long) resultStats.getControlMessage().get(1).getRxCount());
+ assertEquals(stats.getRx()[2], (long) resultStats.getControlMessage().get(2).getRxCount());
+ assertEquals(stats.getRx()[3], (long) resultStats.getControlMessage().get(3).getRxCount());
+ assertEquals(stats.getRx()[4], (long) resultStats.getControlMessage().get(4).getRxCount());
+ assertEquals(stats.getRx()[6], (long) resultStats.getControlMessage().get(5).getRxCount());
+ assertEquals(stats.getRx()[7], (long) resultStats.getControlMessage().get(6).getRxCount());
+ assertEquals(stats.getRx()[8], (long) resultStats.getControlMessage().get(7).getRxCount());
+ }
+
+ /**
+ * Tests {@link LispSouthboundRPC#getStats} method with null stats.
+ *
+ * @throws ExecutionException
+ * @throws InterruptedException
+ */
+ @Test
+ public void getStatsTest_withNullStats() throws ExecutionException, InterruptedException {
+ final String expectedMsg = ((RpcError) RPC_RESULT_ERROR.getErrors().iterator().next()).getMessage();
+
+ Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(null);
+
+ final Future<RpcResult<GetStatsOutput>> resultFuture = lispSouthboundRPC.getStats();
+ final RpcResult<GetStatsOutput> rpcResult = resultFuture.get();
+
+ assertEquals(RPC_RESULT_ERROR.isSuccessful(), rpcResult.isSuccessful());
+ assertEquals(expectedMsg, rpcResult.getErrors().iterator().next().getMessage());
+ }
+
+ /**
+ * Tests {@link LispSouthboundRPC#resetStats} method.
+ *
+ * @throws ExecutionException
+ * @throws InterruptedException
+ */
+ @Test
+ public void resetStatsTest() throws ExecutionException, InterruptedException {
+ final LispSouthboundStats stats = new LispSouthboundStats();
+ incrementAll(stats);
+ Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(stats);
+
+ assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), lispSouthboundRPC.resetStats().get().isSuccessful());
+
+ for (long rx : stats.getRx()) {
+ assertEquals(0, rx);
+ }
+ }
+
+ /**
+ * Tests {@link LispSouthboundRPC#resetStats} method with null stats.
+ *
+ * @throws ExecutionException
+ * @throws InterruptedException
+ */
+ @Test
+ public void resetStatsTest_withNullStats() throws ExecutionException, InterruptedException {
+ final String expectedMsg = ((RpcError) RPC_RESULT_ERROR.getErrors().iterator().next()).getMessage();
+
+ Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(null);
+
+ final Future<RpcResult<Void>> resultFuture = lispSouthboundRPC.resetStats();
+ final RpcResult<Void> rpcResult = resultFuture.get();
+
+ assertEquals(RPC_RESULT_ERROR.isSuccessful(), rpcResult.isSuccessful());
+ assertEquals(expectedMsg, rpcResult.getErrors().iterator().next().getMessage());
+ }
+
+ private static MappingRecordItem getDefaultMappingRecordItem() {
+ return new MappingRecordItemBuilder()
+ .setMappingRecord(new MappingRecordBuilder().build()).build();
+ }
+
+ private static MapNotifyBuilder getDefaultMapNotifyBuilder() {
+ return new MapNotifyBuilder()
+ .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
+ }
+
+ private static MapReplyBuilder getDefaultMapReplyBuilder() {
+ return new MapReplyBuilder()
+ .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
+ }
+
+ private static MapRegisterBuilder getDefaultMapRegisterBuilder() {
+ return new MapRegisterBuilder()
+ .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
+ }
+
+ private static void incrementAll(LispSouthboundStats stats) {
+ for (MessageType type : MessageType.values()) {
+ stats.incrementRx(type.getIntValue());
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.lispflowmapping.southbound;
+
+import static org.junit.Assert.assertEquals;
+
+import java.lang.reflect.Field;
+import org.junit.Before;
+import org.junit.Test;
+
+public class LispSouthboundStatsTest {
+
+ private static LispSouthboundStats lispSouthboundStats;
+
+ @Before
+ public void init() {
+ lispSouthboundStats = new LispSouthboundStats();
+ }
+
+ /**
+ * Tests {@link LispSouthboundStats#resetStats} method.
+ *
+ * @throws NoSuchFieldException
+ * @throws IllegalAccessException
+ */
+
+ @Test
+ public void resetStatsTest() throws NoSuchFieldException, IllegalAccessException {
+ setRxField(new long[]{0, 1, 2, 3, 4, 5, 6, 7, 8});
+
+ lispSouthboundStats.resetStats();
+ for (long value : lispSouthboundStats.getRx()) {
+ assertEquals(0, value);
+ }
+ }
+
+ /**
+ * Tests {@link LispSouthboundStats#incrementRx} method.
+ */
+ @Test
+ public void incrementRxTest() {
+ lispSouthboundStats.incrementRx(4);
+ long[] rx = lispSouthboundStats.getRx();
+
+ for (int i = 0; i < rx.length; i++) {
+ if (i == 4) {
+ assertEquals(1, rx[i]);
+ } else {
+ assertEquals(0, rx[i]);
+ }
+ }
+ }
+
+ /**
+ * Tests {@link LispSouthboundStats#incrementRx} method.
+ *
+ * @throws NoSuchFieldException
+ * @throws IllegalAccessException
+ */
+ @Test
+ public void incrementRxTest_withMaxValue() throws NoSuchFieldException, IllegalAccessException {
+ setRxField(new long []{Long.MAX_VALUE, 1, 1, 1, 1, 1, 1, 1, 1});
+ lispSouthboundStats.incrementRx(0);
+ long[] rx = lispSouthboundStats.getRx();
+
+ for (int i = 0; i < rx.length; i++) {
+ if (i == 0) {
+ assertEquals(0, rx[i]);
+ } else {
+ assertEquals(1, rx[i]);
+ }
+ }
+ }
+
+ /**
+ * Tests {@link LispSouthboundStats#incrementTx} method.
+ */
+ @Test
+ public void incrementTxTest() {
+ lispSouthboundStats.incrementTx(4);
+ long[] tx = lispSouthboundStats.getTx();
+
+ for (int i = 0; i < tx.length; i++) {
+ if (i == 4) {
+ assertEquals(1, tx[i]);
+ } else {
+ assertEquals(0, tx[i]);
+ }
+ }
+ }
+
+ /**
+ * Tests {@link LispSouthboundStats#incrementRxUnknown} method.
+ */
+ @Test
+ public void incrementRxUnknownTest() throws NoSuchFieldException, IllegalAccessException {
+ setRxUnkownField(100L);
+ lispSouthboundStats.incrementRxUnknown();
+
+ assertEquals(101L, lispSouthboundStats.getRxUnknown());
+ }
+
+ /**
+ * Tests {@link LispSouthboundStats#incrementRxUnknown} method with Long.MAX_VALUE.
+ */
+ @Test
+ public void incrementRxUnknownTest_withMaxValue() throws NoSuchFieldException, IllegalAccessException {
+ setRxUnkownField(Long.MAX_VALUE);
+ lispSouthboundStats.incrementRxUnknown();
+
+ assertEquals(0, lispSouthboundStats.getRxUnknown());
+ }
+
+ /**
+ * Tests {@link LispSouthboundStats#incrementTxErrors} method.
+ */
+ @Test
+ public void incrementTxErrorsTest() throws NoSuchFieldException, IllegalAccessException {
+ setTxErrorsField(100L);
+ lispSouthboundStats.incrementTxErrors();
+
+ assertEquals(101L, lispSouthboundStats.getTxErrors());
+ }
+
+ /**
+ * Tests {@link LispSouthboundStats#incrementTxErrors} method with Long.MAX_VALUE.
+ */
+ @Test
+ public void incrementTxErrorsTest_withMaxValue() throws NoSuchFieldException, IllegalAccessException {
+ setTxErrorsField(Long.MAX_VALUE);
+ lispSouthboundStats.incrementTxErrors();
+
+ assertEquals(0, lispSouthboundStats.getTxErrors());
+ }
+
+ private static void setRxField(long[] array) throws NoSuchFieldException, IllegalAccessException {
+ Field rx = LispSouthboundStats.class.getDeclaredField("rx");
+ rx.setAccessible(true);
+ rx.set(lispSouthboundStats, array);
+ }
+
+ private static void setRxUnkownField(long value) throws NoSuchFieldException, IllegalAccessException {
+ Field rxUnknown = LispSouthboundStats.class.getDeclaredField("rxUnknown");
+ rxUnknown.setAccessible(true);
+ rxUnknown.set(lispSouthboundStats, value);
+ }
+
+ private static void setTxErrorsField(long value) throws NoSuchFieldException, IllegalAccessException {
+ Field txErrors = LispSouthboundStats.class.getDeclaredField("txErrors");
+ txErrors.setAccessible(true);
+ txErrors.set(lispSouthboundStats, value);
+ }
+}