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.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.mapregistermessage.MapRegister;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapregistermessage.MapRegisterBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequest;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.GetStatsOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRegisterInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.get.stats.output.ControlMessageStats;
45 import org.opendaylight.yangtools.yang.common.RpcError;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
49 @RunWith(MockitoJUnitRunner.class)
50 public class LispSouthboundRpcTest {
52 @Mock LispSouthboundPlugin lispSouthboundPlugin;
53 @InjectMocks LispSouthboundRPC lispSouthboundRPC;
55 private static final RpcResult<Void> RPC_RESULT_FAILURE = RpcResultBuilder.<Void>failed().build();
56 private static final RpcResult<Void> RPC_RESULT_SUCCESS = RpcResultBuilder.<Void>success().build();
57 private static final RpcResult<GetStatsOutput> RPC_RESULT_ERROR = RpcResultBuilder.<GetStatsOutput>failed()
58 .withError(RpcError.ErrorType.APPLICATION, "data-missing", "No stats found").build();
61 * Tests {@link LispSouthboundRPC#sendMapNotify} method.
64 public void sendMapNotifyTest_inputNotNull() throws ExecutionException, InterruptedException {
66 final MapNotify mapNotify = getDefaultMapNotifyBuilder().build();
67 final TransportAddress transportAddress = new TransportAddressBuilder().build();
68 final SendMapNotifyInput sendMapNotifyInputMock = Mockito.mock(SendMapNotifyInput.class);
70 Mockito.when(sendMapNotifyInputMock.getTransportAddress()).thenReturn(transportAddress);
71 Mockito.when(sendMapNotifyInputMock.getMapNotify()).thenReturn(mapNotify);
72 assertEquals(RPC_RESULT_SUCCESS.isSuccessful(),
73 lispSouthboundRPC.sendMapNotify(sendMapNotifyInputMock).get().isSuccessful());
75 Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
76 MapNotifySerializer.getInstance().serialize(mapNotify), MessageType.MapNotify);
80 * Tests {@link LispSouthboundRPC#sendMapNotify} method with null input.
83 public void sendMapNotifyTest_nullInput() throws ExecutionException, InterruptedException {
84 assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
85 lispSouthboundRPC.sendMapNotify(null).get().isSuccessful());
87 Mockito.verifyZeroInteractions(lispSouthboundPlugin);
91 * Tests {@link LispSouthboundRPC#sendMapReply} method.
94 public void sendMapReplyTest_inputNotNull() throws ExecutionException, InterruptedException {
96 final MapReply mapReply = getDefaultMapReplyBuilder().build();
97 final TransportAddress transportAddress = new TransportAddressBuilder().build();
98 final SendMapReplyInput sendMapReplyInputMock = Mockito.mock(SendMapReplyInput.class);
100 Mockito.when(sendMapReplyInputMock.getTransportAddress()).thenReturn(transportAddress);
101 Mockito.when(sendMapReplyInputMock.getMapReply()).thenReturn(mapReply);
102 assertEquals(RPC_RESULT_SUCCESS.isSuccessful(),
103 lispSouthboundRPC.sendMapReply(sendMapReplyInputMock).get().isSuccessful());
105 Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
106 MapReplySerializer.getInstance().serialize(mapReply), MessageType.MapReply);
110 * Tests {@link LispSouthboundRPC#sendMapReply} method with null input.
113 public void sendMapReplyTest_nullInput() throws ExecutionException, InterruptedException {
114 assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
115 lispSouthboundRPC.sendMapReply(null).get().isSuccessful());
117 Mockito.verifyZeroInteractions(lispSouthboundPlugin);
121 * Tests {@link LispSouthboundRPC#sendMapRequest} method.
124 public void sendMapRequestTest_inputNotNull() throws ExecutionException, InterruptedException {
126 final MapRequest mapRequest = new MapRequestBuilder().build();
127 final TransportAddress transportAddress = new TransportAddressBuilder().build();
128 final SendMapRequestInput sendMapRequestInputMock = Mockito.mock(SendMapRequestInput.class);
130 Mockito.when(sendMapRequestInputMock.getTransportAddress()).thenReturn(transportAddress);
131 Mockito.when(sendMapRequestInputMock.getMapRequest()).thenReturn(mapRequest);
132 assertEquals(RPC_RESULT_SUCCESS.isSuccessful(),
133 lispSouthboundRPC.sendMapRequest(sendMapRequestInputMock).get().isSuccessful());
135 Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
136 MapRequestSerializer.getInstance().serialize(mapRequest), MessageType.MapRequest);
140 * Tests {@link LispSouthboundRPC#sendMapRequest} method with null input.
143 public void sendMapRequestTest_nullInput() throws ExecutionException, InterruptedException {
144 assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
145 lispSouthboundRPC.sendMapRequest(null).get().isSuccessful());
147 Mockito.verifyZeroInteractions(lispSouthboundPlugin);
151 * Tests {@link LispSouthboundRPC#sendMapRegister} method.
154 public void sendMapRegisterTest_inputNotNull() throws ExecutionException, InterruptedException {
156 final MapRegister mapRegister = getDefaultMapRegisterBuilder().build();
157 final TransportAddress transportAddress = new TransportAddressBuilder().build();
158 final SendMapRegisterInput sendMapRegisterInputMock = Mockito.mock(SendMapRegisterInput.class);
160 Mockito.when(sendMapRegisterInputMock.getTransportAddress()).thenReturn(transportAddress);
161 Mockito.when(sendMapRegisterInputMock.getMapRegister()).thenReturn(mapRegister);
162 assertEquals(RPC_RESULT_SUCCESS.isSuccessful(),
163 lispSouthboundRPC.sendMapRegister(sendMapRegisterInputMock).get().isSuccessful());
165 Mockito.verify(lispSouthboundPlugin).handleSerializedLispBuffer(transportAddress,
166 MapRegisterSerializer.getInstance().serialize(mapRegister), MessageType.MapRegister);
170 * Tests {@link LispSouthboundRPC#sendMapRegister} method with null input.
173 public void sendMapRegisterTest_nullInput() throws ExecutionException, InterruptedException {
174 assertEquals(RPC_RESULT_FAILURE.isSuccessful(),
175 lispSouthboundRPC.sendMapRegister(null).get().isSuccessful());
177 Mockito.verifyZeroInteractions(lispSouthboundPlugin);
181 * Tests {@link LispSouthboundRPC#getStats} method.
184 public void getStatsTest() throws ExecutionException, InterruptedException {
185 final LispSouthboundStats stats = new LispSouthboundStats();
187 Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(stats);
190 final ControlMessageStats resultStats = lispSouthboundRPC.getStats().get().getResult().getControlMessageStats();
192 assertEquals(stats.getRx()[0], (long) resultStats.getControlMessage().get(0).getRxCount());
193 assertEquals(stats.getRx()[1], (long) resultStats.getControlMessage().get(1).getRxCount());
194 assertEquals(stats.getRx()[2], (long) resultStats.getControlMessage().get(2).getRxCount());
195 assertEquals(stats.getRx()[3], (long) resultStats.getControlMessage().get(3).getRxCount());
196 assertEquals(stats.getRx()[4], (long) resultStats.getControlMessage().get(4).getRxCount());
197 assertEquals(stats.getRx()[6], (long) resultStats.getControlMessage().get(5).getRxCount());
198 assertEquals(stats.getRx()[7], (long) resultStats.getControlMessage().get(6).getRxCount());
199 assertEquals(stats.getRx()[8], (long) resultStats.getControlMessage().get(7).getRxCount());
203 * Tests {@link LispSouthboundRPC#getStats} method with null stats.
206 public void getStatsTest_withNullStats() throws ExecutionException, InterruptedException {
207 final String expectedMsg = ((RpcError) RPC_RESULT_ERROR.getErrors().iterator().next()).getMessage();
209 Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(null);
211 final Future<RpcResult<GetStatsOutput>> resultFuture = lispSouthboundRPC.getStats();
212 final RpcResult<GetStatsOutput> rpcResult = resultFuture.get();
214 assertEquals(RPC_RESULT_ERROR.isSuccessful(), rpcResult.isSuccessful());
215 assertEquals(expectedMsg, rpcResult.getErrors().iterator().next().getMessage());
219 * Tests {@link LispSouthboundRPC#resetStats} method.
222 public void resetStatsTest() throws ExecutionException, InterruptedException {
223 final LispSouthboundStats stats = new LispSouthboundStats();
225 Mockito.when(lispSouthboundPlugin.getStats()).thenReturn(stats);
227 assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), lispSouthboundRPC.resetStats().get().isSuccessful());
229 for (long rx : stats.getRx()) {
235 * Tests {@link LispSouthboundRPC#resetStats} method with null stats.
238 public void resetStatsTest_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<Void>> resultFuture = lispSouthboundRPC.resetStats();
244 final RpcResult<Void> rpcResult = resultFuture.get();
246 assertEquals(RPC_RESULT_ERROR.isSuccessful(), rpcResult.isSuccessful());
247 assertEquals(expectedMsg, rpcResult.getErrors().iterator().next().getMessage());
250 private static MappingRecordItem getDefaultMappingRecordItem() {
251 return new MappingRecordItemBuilder()
252 .setMappingRecord(new MappingRecordBuilder().build()).build();
255 private static MapNotifyBuilder getDefaultMapNotifyBuilder() {
256 return new MapNotifyBuilder()
257 .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
260 private static MapReplyBuilder getDefaultMapReplyBuilder() {
261 return new MapReplyBuilder()
262 .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
265 private static MapRegisterBuilder getDefaultMapRegisterBuilder() {
266 return new MapRegisterBuilder()
267 .setMappingRecordItem(Lists.newArrayList(getDefaultMappingRecordItem()));
270 private static void incrementAll(LispSouthboundStats stats) {
271 for (MessageType type : MessageType.values()) {
272 stats.incrementRx(type.getIntValue());