Bug 7947: Store MappingOrigin in MappingData
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / lisp / MappingServiceTest.java
index becf95c3762b3b7d6c31d8ba92efa5b6b9a437ca..8eb7497026933c0c4c326bdd3cdadf0df3e528e0 100644 (file)
@@ -19,22 +19,27 @@ import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
+import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
 import org.opendaylight.lispflowmapping.implementation.MappingService;
 import org.opendaylight.lispflowmapping.implementation.MappingSystem;
 import org.opendaylight.lispflowmapping.implementation.mdsal.AuthenticationKeyDataListener;
-import org.opendaylight.lispflowmapping.implementation.mdsal.DataStoreBackEnd;
 import org.opendaylight.lispflowmapping.implementation.mdsal.MappingDataListener;
 import org.opendaylight.lispflowmapping.implementation.util.DSBEInputUtil;
 import org.opendaylight.lispflowmapping.implementation.util.RPCInputConvertorUtil;
+import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
+import org.opendaylight.lispflowmapping.type.MappingData;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
 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.mapping.authkey.container.MappingAuthkey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
-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.container.MappingRecordBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeyInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeyInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeysInput;
@@ -51,7 +56,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev15090
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeyInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeyInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeysInput;
@@ -64,10 +68,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev15090
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingsInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container
-        .MappingAuthkey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container
-        .MappingAuthkeyBuilder;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
@@ -75,14 +75,20 @@ import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 @RunWith(MockitoJUnitRunner.class)
 public class MappingServiceTest {
 
-    @InjectMocks private static MappingService mappingService;
     @Mock(name = "mappingSystem") private static MappingSystem mappingSystem;
     @Mock(name = "dsbe") private static DataStoreBackEnd dsbe;
-    @Mock(name = "mappingServiceRpc") private static BindingAwareBroker
-            .RpcRegistration<OdlMappingserviceService> mappingServiceRpc;
     @Mock(name = "keyListener") private static AuthenticationKeyDataListener keyListener;
     @Mock(name = "mappingListener") private static MappingDataListener mappingListener;
 
+    private final DataBroker dataBroker = Mockito.mock(DataBroker.class);
+    private final NotificationPublishService notificationPublishService = Mockito
+            .mock(NotificationPublishService.class);
+    private final ILispDAO lispDAO = Mockito.mock(ILispDAO.class);
+
+    @InjectMocks
+    MappingService mappingService = new MappingService(dataBroker,
+            notificationPublishService, lispDAO);
+
     private static final String IPV4_STRING = "1.2.3.0";
     private static final Eid IPV4_EID = LispAddressUtil.asIpv4Eid(IPV4_STRING);
 
@@ -92,13 +98,10 @@ public class MappingServiceTest {
             .setKeyType(1).build();
     private static final SiteId SITE_ID = new SiteId(new byte[] {0, 1, 2, 3, 4, 5, 6, 7});
     private static final XtrId XTR_ID = new XtrId(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
-    private static final Object DUMMY_OBJECT = "dummy_object";
+    private static final MappingData DUMMY_MAPPING = new MappingData(MappingOrigin.Southbound, null);
 
     /**
      * Tests {@link MappingService#addKey} method.
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
      */
     @Test
     public void addKeyTest() throws ExecutionException, InterruptedException {
@@ -133,9 +136,6 @@ public class MappingServiceTest {
 
     /**
      * Tests {@link MappingService#addKey} method with null MappingAuthkey.
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
      */
     @Test
     public void addKeyTest_withNullMapAuthkey() throws ExecutionException, InterruptedException {
@@ -152,9 +152,6 @@ public class MappingServiceTest {
 
     /**
      * Tests {@link MappingService#addMapping} method.
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
      */
     @Test
     public void addMappingTest() throws ExecutionException, InterruptedException {
@@ -172,9 +169,6 @@ public class MappingServiceTest {
 
     /**
      * Tests {@link MappingService#getKey} method.
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
      */
     @Test
     public void getKeyTest() throws ExecutionException, InterruptedException {
@@ -192,9 +186,6 @@ public class MappingServiceTest {
 
     /**
      * Tests {@link MappingService#getKey} method with null MappingAuthkey.
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
      */
     @Test
     public void getKeyTest_withNullMappingAuthkey() throws ExecutionException, InterruptedException {
@@ -225,23 +216,22 @@ public class MappingServiceTest {
 
     /**
      * Tests {@link MappingService#getMapping} method.
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
      */
     @Test
     public void getMappingTest() throws ExecutionException, InterruptedException {
         // input
         final GetMappingInput getMappingInput = new GetMappingInputBuilder().setEid(IPV4_EID).build();
-        final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build();
+        final MappingData mappingData = getDefaultMappingData(MappingOrigin.Southbound);
+        final MappingRecord nonBinaryMappingRecord = getDefaultMappingRecordBuilder()
+                .setEid(LispAddressUtil.toEid(new Ipv4Address(IPV4_STRING), null)).build();
 
-        Mockito.when(mappingSystem.getMapping(getMappingInput.getEid())).thenReturn(mappingRecord);
+        Mockito.when(mappingSystem.getMapping(getMappingInput.getEid())).thenReturn(mappingData);
 
         final RpcResult<GetMappingOutput> rpc = RpcResultBuilder
-                .success(new GetMappingOutputBuilder().setMappingRecord(mappingRecord)).build();
+                .success(new GetMappingOutputBuilder().setMappingRecord(nonBinaryMappingRecord)).build();
 
         //result
-        final Future<RpcResult<GetMappingOutput>> result = (mappingService.getMapping(getMappingInput));
+        final Future<RpcResult<GetMappingOutput>> result = mappingService.getMapping(getMappingInput);
         final RpcResult<GetMappingOutput> rpcResult = result.get();
 
         assertEquals(rpc.getResult(), rpcResult.getResult());
@@ -251,9 +241,6 @@ public class MappingServiceTest {
 
     /**
      * Tests {@link MappingService#getMapping} method with null MappingRecord.
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
      */
     @Test
     public void getMappingTest_withNullMapRecord() throws ExecutionException, InterruptedException {
@@ -283,9 +270,6 @@ public class MappingServiceTest {
 
     /**
      * Tests {@link MappingService#removeKey} method.
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
      */
     @Test
     public void removeKeyTest() throws ExecutionException, InterruptedException {
@@ -301,9 +285,6 @@ public class MappingServiceTest {
 
     /**
      * Tests {@link MappingService#removeMapping} method.
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
      */
     @Test
     public void removeMappingTest() throws ExecutionException, InterruptedException {
@@ -319,9 +300,6 @@ public class MappingServiceTest {
 
     /**
      * Tests {@link MappingService#updateKey} method.
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
      */
     @Test
     public void updateKeyTest() throws ExecutionException, InterruptedException {
@@ -338,9 +316,6 @@ public class MappingServiceTest {
 
     /**
      * Tests {@link MappingService#updateKey} method with null MappingAuthkey.
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
      */
     @Test
     public void updateKeyTest_withNullMapAuthkey() throws ExecutionException, InterruptedException {
@@ -370,9 +345,6 @@ public class MappingServiceTest {
 
     /**
      * Tests {@link MappingService#updateMapping} method.
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
      */
     @Test
     public void updateMappingTest() throws ExecutionException, InterruptedException {
@@ -390,37 +362,32 @@ public class MappingServiceTest {
 
     /**
      * Tests {@link MappingService#addMapping} method from southbound.
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
      */
     @Test
     public void addMappingTest_fromSouthbound() throws ExecutionException, InterruptedException {
         // input
-        final MappingRecord data = getDefaultMappingRecordBuilder()
+        final MappingRecord record = getDefaultMappingRecordBuilder()
                 .setXtrId(XTR_ID).build();
+        final MappingData data = getDefaultMappingData(MappingOrigin.Southbound, record);
 
-        mappingService.addMapping(MappingOrigin.Southbound, IPV4_EID, SITE_ID, data, false);
+        mappingService.addMapping(MappingOrigin.Southbound, IPV4_EID, SITE_ID, data);
 
-        Mockito.verify(mappingSystem).addMapping(MappingOrigin.Southbound, IPV4_EID, data, false);
+        Mockito.verify(mappingSystem).addMapping(MappingOrigin.Southbound, IPV4_EID, data);
         Mockito.verify(dsbe).addMapping(DSBEInputUtil.toMapping(MappingOrigin.Southbound, IPV4_EID, SITE_ID, data));
-        Mockito.verify(dsbe).addXtrIdMapping(DSBEInputUtil.toXtrIdMapping(data));
     }
 
     /**
      * Tests {@link MappingService#addMapping} method from northbound.
-     *
-     * @throws ExecutionException
-     * @throws InterruptedException
      */
     @Test
     public void addMappingTest_fromNorthbound() throws ExecutionException, InterruptedException {
         // input
         final MappingOrigin origin = MappingOrigin.Northbound;
-        final MappingRecord data = getDefaultMappingRecordBuilder()
+        final MappingRecord record = getDefaultMappingRecordBuilder()
                 .setXtrId(XTR_ID).build();
+        final MappingData data = getDefaultMappingData(origin, record);
 
-        mappingService.addMapping(origin, IPV4_EID, SITE_ID, data, false);
+        mappingService.addMapping(origin, IPV4_EID, SITE_ID, data);
         Mockito.verify(dsbe).addMapping(DSBEInputUtil.toMapping(origin, IPV4_EID, SITE_ID, data));
         Mockito.verifyZeroInteractions(mappingSystem);
         Mockito.verifyNoMoreInteractions(dsbe);
@@ -432,8 +399,8 @@ public class MappingServiceTest {
     @Test
     public void getMappingTest_withOriginAndEid() {
         Mockito.when(mappingSystem.getMapping(Mockito.any(MappingOrigin.class), Mockito.any(Eid.class)))
-                .thenReturn(DUMMY_OBJECT);
-        assertEquals(DUMMY_OBJECT, mappingService.getMapping(MappingOrigin.Northbound, IPV4_EID));
+                .thenReturn(DUMMY_MAPPING);
+        assertEquals(DUMMY_MAPPING, mappingService.getMapping(MappingOrigin.Northbound, IPV4_EID));
     }
 
     /**
@@ -441,8 +408,8 @@ public class MappingServiceTest {
      */
     @Test
     public void getMappingTest_withEid() {
-        Mockito.when(mappingSystem.getMapping(Mockito.any(Eid.class))).thenReturn(DUMMY_OBJECT);
-        assertEquals(DUMMY_OBJECT, mappingService.getMapping(IPV4_EID));
+        Mockito.when(mappingSystem.getMapping(Mockito.any(Eid.class))).thenReturn(DUMMY_MAPPING);
+        assertEquals(DUMMY_MAPPING, mappingService.getMapping(IPV4_EID));
     }
 
     /**
@@ -450,8 +417,9 @@ public class MappingServiceTest {
      */
     @Test
     public void getMappingTest_withSrcAndDstEid() {
-        Mockito.when(mappingSystem.getMapping(Mockito.any(Eid.class), Mockito.any(Eid.class))).thenReturn(DUMMY_OBJECT);
-        assertEquals(DUMMY_OBJECT, mappingService.getMapping(IPV4_EID, IPV4_EID));
+        Mockito.when(mappingSystem.getMapping(Mockito.any(Eid.class), Mockito.any(Eid.class)))
+            .thenReturn(DUMMY_MAPPING);
+        assertEquals(DUMMY_MAPPING, mappingService.getMapping(IPV4_EID, IPV4_EID));
     }
 
     /**
@@ -495,8 +463,8 @@ public class MappingServiceTest {
      */
     @Test
     public void addDataTest() {
-        mappingService.addData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD, DUMMY_OBJECT);
-        Mockito.verify(mappingSystem).addData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD, DUMMY_OBJECT);
+        mappingService.addData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD, DUMMY_MAPPING);
+        Mockito.verify(mappingSystem).addData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD, DUMMY_MAPPING);
     }
 
     /**
@@ -505,8 +473,8 @@ public class MappingServiceTest {
     @Test
     public void getDataTest() {
         Mockito.when(mappingSystem.getData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD))
-                .thenReturn(DUMMY_OBJECT);
-        assertEquals(DUMMY_OBJECT, mappingService.getData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD));
+                .thenReturn(DUMMY_MAPPING);
+        assertEquals(DUMMY_MAPPING, mappingService.getData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD));
     }
 
     /**
@@ -533,7 +501,6 @@ public class MappingServiceTest {
     @Test
     public void close() throws Exception {
         mappingService.close();
-        Mockito.verify(mappingServiceRpc).close();
         Mockito.verify(keyListener).closeDataChangeListener();
         Mockito.verify(mappingListener).closeDataChangeListener();
     }
@@ -565,6 +532,17 @@ public class MappingServiceTest {
         assertNull(mappingService.getAllMappings());
     }
 
+    private static MappingData getDefaultMappingData(MappingOrigin origin) {
+        return getDefaultMappingData(origin, null);
+    }
+
+    private static MappingData getDefaultMappingData(MappingOrigin origin, MappingRecord mappingRecord) {
+        if (mappingRecord == null) {
+            mappingRecord = getDefaultMappingRecordBuilder().build();
+        }
+        return new MappingData(origin, mappingRecord, System.currentTimeMillis());
+    }
+
     private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
         return new MappingRecordBuilder()
                 .setAction(MappingRecord.Action.NoAction)
@@ -574,4 +552,4 @@ public class MappingServiceTest {
                 .setRecordTtl(60)
                 .setEid(IPV4_EID);
     }
-}
+}
\ No newline at end of file