Bump upstreams
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / LispMappingServiceTest.java
index 3c53ebbea6f73f6c4e736615a221ed03905b7b64..49ef8f72ff677a21b40b7fbfea53bb3ce6c499a9 100644 (file)
@@ -11,11 +11,9 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
 
 import com.google.common.collect.Lists;
-
 import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.List;
-
 import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.apache.commons.lang3.tuple.MutablePair;
 import org.apache.commons.lang3.tuple.Pair;
@@ -25,13 +23,15 @@ import org.mockito.ArgumentMatcher;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.opendaylight.controller.md.sal.binding.api.NotificationService;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
-import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
+import org.opendaylight.mdsal.binding.api.NotificationService;
+import org.opendaylight.mdsal.binding.api.RpcService;
+import org.opendaylight.mdsal.singleton.api.ClusterSingletonServiceProvider;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
@@ -39,6 +39,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.Ad
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MappingKeepAlive;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
@@ -46,20 +47,13 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.Xt
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container
-        .MapRegisterCacheMetadata;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container
-        .MapRegisterCacheMetadataBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container
-        .map.register.cache.metadata.EidLispAddress;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container
-        .map.register.cache.metadata.EidLispAddressBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.MapRegisterCacheMetadata;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.MapRegisterCacheMetadataBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.map.register.cache.metadata.EidLispAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.map.register.cache.metadata.EidLispAddressBuilder;
 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.
-        MappingRecordItemBuilder;
+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.MappingRecordItemBuilder;
 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.maprequest.SourceEidBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequest;
@@ -67,31 +61,40 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.ma
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
 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.OdlLispSbService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotify;
 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.SendMapNotifyInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReply;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequest;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
+import org.opendaylight.yangtools.concepts.Registration;
+import org.opendaylight.yangtools.yang.binding.util.BindingMap;
+import org.opendaylight.yangtools.yang.common.Uint16;
 
 @RunWith(MockitoJUnitRunner.class)
 public class LispMappingServiceTest {
     @Mock private static MapRegister mapRegisterMock;
-    @Mock(name = "mapServer") private static IMapServerAsync mapServerMock;
+    @Mock(name = "mapServer") private static MapServer mapServerMock;
     @Mock(name = "mapResolver") private static IMapResolverAsync mapResolverMock;
     @Mock(name = "tlsMapReply") private static ThreadLocal<MapReply> tlsMapReplyMock;
     @Mock(name = "tlsMapNotify") private static ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotifyMock;
     @Mock(name = "tlsMapRequest") private static ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequestMock;
+    @Mock(name = "listenerRegistration") private static Registration listenerRegistration;
+    @Mock(name = "cssRegistration") private static Registration cssRegistration;
+    @Mock(name = "sendMapRequest") private static SendMapRequest sendMapRequest;
+    @Mock(name = "sendMapReply") private static SendMapReply sendMapReply;
+    @Mock(name = "sendMapNotify") private static SendMapNotify sendMapNotify;
 
     private final NotificationService notificationService = Mockito.mock(NotificationService.class);
+    private final RpcService rpcService = Mockito.mock(RpcService.class);
     private final IMappingService mappingService = Mockito.mock(IMappingService.class);
-    private final OdlLispSbService odlLispSbService = Mockito.mock(OdlLispSbService.class);
+    private final ClusterSingletonServiceProvider clusterSingletonService = Mockito.mock(
+            ClusterSingletonServiceProvider.class);
 
     @InjectMocks
-    private LispMappingService lispMappingService = new LispMappingService(
-            notificationService,
-            mappingService,
-            odlLispSbService);
+    private final LispMappingService lispMappingService = new LispMappingService(
+            mappingService, clusterSingletonService, rpcService, notificationService);
 
     private static final byte[] IPV4_BYTES_1 =       new byte[] {1, 2, 3, 0};
     private static final byte[] IPV4_BYTES_2 =       new byte[] {1, 2, 4, 0};
@@ -113,13 +116,13 @@ public class LispMappingServiceTest {
 
     private static final TransportAddress TRANSPORT_ADDRESS_1 = new TransportAddressBuilder()
             .setIpAddress(new IpAddressBinary(new Ipv4AddressBinary(IPV4_BYTES_1)))
-            .setPort(new PortNumber(9999)).build();
+            .setPort(new PortNumber(Uint16.valueOf(9999))).build();
     private static final TransportAddress TRANSPORT_ADDRESS_2 = new TransportAddressBuilder()
             .setIpAddress(new IpAddressBinary(new Ipv4AddressBinary(IPV4_BYTES_2)))
-            .setPort(new PortNumber(8888)).build();
+            .setPort(new PortNumber(Uint16.valueOf(8888))).build();
     private static final TransportAddress TRANSPORT_ADDRESS = new TransportAddressBuilder()
             .setIpAddress(new IpAddressBinary(new Ipv4AddressBinary(IPV4_BYTES_1)))
-            .setPort(new PortNumber(LispMessage.PORT_NUM)).build();
+            .setPort(new PortNumber(LispMessage.PORT_NUMBER)).build();
 
     /**
      * Tests {@link LispMappingService#handleMapRequest} method.
@@ -128,7 +131,6 @@ public class LispMappingServiceTest {
     public void handleMapRequestTest() {
         final MapRequest mapRequest = Mockito.mock(MapRequest.class);
         final MapReply mapReply = new MapReplyBuilder().build();
-        Mockito.when(mapRequest.getEidItem()).thenReturn(Lists.newArrayList(EID_ITEM_BUILDER.build()));
         Mockito.when(tlsMapRequestMock.get()).thenReturn(null);
         Mockito.when(tlsMapReplyMock.get()).thenReturn(mapReply);
 
@@ -138,7 +140,7 @@ public class LispMappingServiceTest {
         Mockito.verify(tlsMapRequestMock).get();
         Mockito.verifyNoMoreInteractions(tlsMapRequestMock);
         Mockito.verify(mapResolverMock).handleMapRequest(mapRequest);
-        Mockito.verify(tlsMapReplyMock).set(Mockito.any(MapReply.class));
+        Mockito.verify(tlsMapReplyMock).set(Mockito.any());
         Mockito.verify(tlsMapReplyMock).get();
 
         assertEquals(result, mapReply);
@@ -155,11 +157,10 @@ public class LispMappingServiceTest {
                 .setMapRequest(pair.getLeft())
                 .setTransportAddress(pair.getRight());
 
-        Mockito.when(mapRequest.getEidItem()).thenReturn(Lists.newArrayList(EID_ITEM_BUILDER.build()));
         Mockito.when(tlsMapRequestMock.get()).thenReturn(pair);
 
         assertNull(lispMappingService.handleMapRequest(mapRequest));
-        Mockito.verify(odlLispSbService).sendMapRequest(smrib.build());
+        Mockito.verify(sendMapRequest).invoke(smrib.build());
     }
 
     /**
@@ -170,13 +171,11 @@ public class LispMappingServiceTest {
     public void handleMapRegisterTest() {
         final Pair<MapNotify, List<TransportAddress>> pairMock = Mockito.mock(Pair.class);
 
-        Mockito.when(mapRegisterMock.getMappingRecordItem())
-                .thenReturn(Lists.newArrayList(MAPPING_RECORD_ITEM_BUILDER.build()));
         Mockito.when(tlsMapNotifyMock.get()).thenReturn(pairMock);
 
         lispMappingService.handleMapRegister(mapRegisterMock);
         Mockito.verify(mapServerMock).handleMapRegister(mapRegisterMock);
-        Mockito.verify(tlsMapNotifyMock).set(Mockito.any(MutablePair.class));
+        Mockito.verify(tlsMapNotifyMock).set(Mockito.any());
     }
 
     /**
@@ -190,12 +189,10 @@ public class LispMappingServiceTest {
         final AddMapping addMapping = Mockito.mock(AddMapping.class);
 
         Mockito.when(addMapping.getMapRegister()).thenReturn(mapRegister);
-        Mockito.when(mapRegister.getMappingRecordItem())
-                .thenReturn(Lists.newArrayList(MAPPING_RECORD_ITEM_BUILDER.build()));
         Mockito.when(tlsMapNotifyMock.get()).thenReturn(getDefaultMapNotifyPair());
 
         lispMappingService.onAddMapping(addMapping);
-        Mockito.verify(odlLispSbService, Mockito.times(2)).sendMapNotify(Mockito.argThat(new TransportAddressMatch()));
+        Mockito.verify(sendMapNotify, Mockito.times(2)).invoke(Mockito.argThat(new TransportAddressMatch()));
     }
 
     /**
@@ -210,8 +207,6 @@ public class LispMappingServiceTest {
         final MapNotify mapNotify = new MapNotifyBuilder().setKeyId((short) 1).build();
 
         Mockito.when(addMapping.getMapRegister()).thenReturn(mapRegister);
-        Mockito.when(mapRegister.getMappingRecordItem())
-                .thenReturn(Lists.newArrayList(MAPPING_RECORD_ITEM_BUILDER.build()));
         Mockito.when(tlsMapNotifyMock.get()).thenReturn(new MutablePair<>(mapNotify, null));
         Mockito.when(addMapping.getTransportAddress()).thenReturn(TRANSPORT_ADDRESS_1);
 
@@ -222,7 +217,7 @@ public class LispMappingServiceTest {
                 .setTransportAddress(TRANSPORT_ADDRESS);
 
         lispMappingService.onAddMapping(addMapping);
-        Mockito.verify(odlLispSbService).sendMapNotify(smnib.build());
+        Mockito.verify(sendMapNotify).invoke(smnib.build());
     }
 
     /**
@@ -238,7 +233,6 @@ public class LispMappingServiceTest {
 
         Mockito.when(requestMapping.getMapRequest()).thenReturn(mapRequest);
         Mockito.when(requestMapping.getTransportAddress()).thenReturn(TRANSPORT_ADDRESS_1);
-        Mockito.when(mapRequest.getEidItem()).thenReturn(Lists.newArrayList(EID_ITEM_BUILDER.build()));
         Mockito.when(tlsMapReplyMock.get()).thenReturn(mapReply);
 
         // result
@@ -247,7 +241,7 @@ public class LispMappingServiceTest {
                 .setTransportAddress(TRANSPORT_ADDRESS_1);
 
         lispMappingService.onRequestMapping(requestMapping);
-        Mockito.verify(odlLispSbService).sendMapReply(smrib.build());
+        Mockito.verify(sendMapReply).invoke(smrib.build());
     }
 
     /**
@@ -261,11 +255,10 @@ public class LispMappingServiceTest {
                     .maprequestnotification.MapRequest.class);
 
         Mockito.when(requestMapping.getMapRequest()).thenReturn(mapRequest);
-        Mockito.when(mapRequest.getEidItem()).thenReturn(Lists.newArrayList(EID_ITEM_BUILDER.build()));
         Mockito.when(tlsMapReplyMock.get()).thenReturn(null);
 
         lispMappingService.onRequestMapping(requestMapping);
-        Mockito.verifyZeroInteractions(odlLispSbService);
+        Mockito.verifyZeroInteractions(sendMapReply);
     }
 
     /**
@@ -277,8 +270,8 @@ public class LispMappingServiceTest {
         Mockito.when(mappingKeepAlive.getMapRegisterCacheMetadata()).thenReturn(getDefaultMapRegisterCacheMetadata());
 
         lispMappingService.onMappingKeepAlive(mappingKeepAlive);
-        Mockito.verify(mappingService).updateMappingRegistration(MappingOrigin.Southbound, IPV4_EID_1, TIMESTAMP);
-        Mockito.verify(mappingService).updateMappingRegistration(MappingOrigin.Southbound, IPV4_EID_2, TIMESTAMP);
+        Mockito.verify(mappingService).refreshMappingRegistration(IPV4_EID_1, null, TIMESTAMP);
+        Mockito.verify(mappingService).refreshMappingRegistration(IPV4_EID_2, null, TIMESTAMP);
     }
 
     /**
@@ -298,7 +291,7 @@ public class LispMappingServiceTest {
                 .setTransportAddress(TRANSPORT_ADDRESS);
 
         lispMappingService.handleSMR(mapRequest, subscriber);
-        Mockito.verify(odlLispSbService).sendMapRequest(smrib.build());
+        Mockito.verify(sendMapRequest).invoke(smrib.build());
     }
 
     /**
@@ -355,8 +348,11 @@ public class LispMappingServiceTest {
      */
     @Test
     public void closeTest() throws Exception {
+        setMock("listenerRegistration", listenerRegistration);
+        setMock("cssRegistration", cssRegistration);
         lispMappingService.close();
-
+        Mockito.verify(listenerRegistration).close();
+        Mockito.verify(cssRegistration).close();
         assertNull(getField("mapResolver"));
         assertNull(getField("mapServer"));
     }
@@ -378,8 +374,15 @@ public class LispMappingServiceTest {
         lispMappingService.onXtrReplyMapping(Mockito.mock(XtrReplyMapping.class));
     }
 
+    private void setMock(final String fieldName, final Object value)
+            throws IllegalAccessException, NoSuchFieldException {
+        final Field field = LispMappingService.class.getDeclaredField(fieldName);
+        field.setAccessible(true);
+        field.set(lispMappingService, value);
+    }
+
     @SuppressWarnings("unchecked")
-    private <T> T getField(String fieldName) throws NoSuchFieldException, IllegalAccessException {
+    private <T> T getField(final String fieldName) throws NoSuchFieldException, IllegalAccessException {
         final Field field = LispMappingService.class.getDeclaredField(fieldName);
         field.setAccessible(true);
 
@@ -407,14 +410,14 @@ public class LispMappingServiceTest {
                 .setEid(IPV4_EID_2).build();
 
         return new MapRegisterCacheMetadataBuilder()
-                .setEidLispAddress(Lists.newArrayList(eidLispAddress_1, eidLispAddress_2))
+                .setEidLispAddress(BindingMap.ordered(eidLispAddress_1, eidLispAddress_2))
                 .setTimestamp(TIMESTAMP).build();
     }
 
-    class TransportAddressMatch extends ArgumentMatcher<SendMapNotifyInput> {
-        public boolean matches(Object sendMapNotify) {
-            final SendMapNotifyInput sendMapNotifyInput = (SendMapNotifyInput) sendMapNotify;
-            final TransportAddress notifyTransportAddress = sendMapNotifyInput.getTransportAddress();
+    static class TransportAddressMatch implements ArgumentMatcher<SendMapNotifyInput> {
+        @Override
+        public boolean matches(final SendMapNotifyInput argument) {
+            final TransportAddress notifyTransportAddress = argument.getTransportAddress();
             return TRANSPORT_ADDRESS_1.equals(notifyTransportAddress)
                     || TRANSPORT_ADDRESS_2.equals(notifyTransportAddress);
         }