+ /**
+ * Tests {@link MappingService#addMapping} method from southbound.
+ *
+ * @throws ExecutionException
+ * @throws InterruptedException
+ */
+ @Test
+ public void addMappingTest_fromSouthbound() throws ExecutionException, InterruptedException {
+ // input
+ final MappingRecord data = getDefaultMappingRecordBuilder()
+ .setXtrId(XTR_ID).build();
+
+ mappingService.addMapping(MappingOrigin.Southbound, IPV4_EID, SITE_ID, data, false);
+
+ Mockito.verify(mappingSystem).addMapping(MappingOrigin.Southbound, IPV4_EID, data, false);
+ 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()
+ .setXtrId(XTR_ID).build();
+
+ mappingService.addMapping(origin, IPV4_EID, SITE_ID, data, false);
+ Mockito.verify(dsbe).addMapping(DSBEInputUtil.toMapping(origin, IPV4_EID, SITE_ID, data));
+ Mockito.verifyZeroInteractions(mappingSystem);
+ Mockito.verifyNoMoreInteractions(dsbe);
+ }
+
+ /**
+ * Tests {@link MappingService#getMapping(MappingOrigin, Eid)} method.
+ */
+ @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));
+ }
+
+ /**
+ * Tests {@link MappingService#getMapping(Eid)} method.
+ */
+ @Test
+ public void getMappingTest_withEid() {
+ Mockito.when(mappingSystem.getMapping(Mockito.any(Eid.class))).thenReturn(DUMMY_OBJECT);
+ assertEquals(DUMMY_OBJECT, mappingService.getMapping(IPV4_EID));
+ }
+
+ /**
+ * Tests {@link MappingService#getMapping(Eid)} method.
+ */
+ @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));
+ }
+
+ /**
+ * Tests {@link MappingService#removeMapping} method.
+ */
+ @Test
+ public void removeMappingTest_withMapOrginAndEid() {
+ mappingService.removeMapping(MappingOrigin.Northbound, IPV4_EID);
+ Mockito.verify(dsbe).removeMapping(DSBEInputUtil.toMapping(MappingOrigin.Northbound, IPV4_EID));
+ }
+
+ /**
+ * Tests {@link MappingService#addAuthenticationKey} method.
+ */
+ @Test
+ public void addAuthenticationKeyTest() {
+ mappingService.addAuthenticationKey(IPV4_EID, MAPPING_AUTHKEY);
+ Mockito.verify(dsbe).addAuthenticationKey(DSBEInputUtil.toAuthenticationKey(IPV4_EID, MAPPING_AUTHKEY));
+ }
+
+ /**
+ * Tests {@link MappingService#getAuthenticationKey(Eid)} method.
+ */
+ @Test
+ public void getAuthenticationKeyTest() {
+ Mockito.when(mappingSystem.getAuthenticationKey(IPV4_EID)).thenReturn(MAPPING_AUTHKEY);
+ assertEquals(MAPPING_AUTHKEY, mappingService.getAuthenticationKey(IPV4_EID));
+ }
+
+ /**
+ * Tests {@link MappingService#removeAuthenticationKey} method.
+ */
+ @Test
+ public void removeAuthenticationKeyTest() {
+ mappingService.removeAuthenticationKey(IPV4_EID);
+ Mockito.verify(dsbe).removeAuthenticationKey(DSBEInputUtil.toAuthenticationKey(IPV4_EID, null));
+ }
+
+ /**
+ * Tests {@link MappingService#addData} method.
+ */
+ @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);
+ }
+
+ /**
+ * Tests {@link MappingService#getData} method.
+ */
+ @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));
+ }
+
+ /**
+ * Tests {@link MappingService#removeData} method.
+ */
+ @Test
+ public void removeDataTest() {
+ mappingService.removeData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD);
+ Mockito.verify(mappingSystem).removeData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD);
+ }
+
+ /**
+ * Tests {@link MappingService#printMappings} method.
+ */
+ @Test
+ public void printMappingsTest() {
+ mappingService.printMappings();
+ Mockito.verify(mappingSystem).printMappings();
+ }
+
+ /**
+ * Tests {@link MappingService#close} method.
+ */
+ @Test
+ public void close() throws Exception {
+ mappingService.close();
+ Mockito.verify(mappingServiceRpc).close();
+ Mockito.verify(keyListener).closeDataChangeListener();
+ Mockito.verify(mappingListener).closeDataChangeListener();
+ }
+
+ /**
+ * Tests {@link MappingService#cleanCachedMappings} method.
+ */
+ @Test
+ public void cleanCachedMappingsTest() throws Exception {
+ mappingService.cleanCachedMappings();
+ Mockito.verify(mappingSystem).cleanCaches();
+ Mockito.verify(dsbe).removeAllDatastoreContent();
+ }
+
+ /**
+ * Tests {@link MappingService} not implemented methods.
+ */
+ @Test
+ public void nullReturnMethodTest() throws ExecutionException, InterruptedException {
+ assertNull(mappingService.removeKeys(Mockito.mock(RemoveKeysInput.class)));
+ assertNull(mappingService.removeMappings(Mockito.mock(RemoveMappingsInput.class)));
+ assertNull(mappingService.getKeys(Mockito.mock(GetKeysInput.class)));
+ assertNull(mappingService.addMappings(Mockito.mock(AddMappingsInput.class)));
+ assertNull(mappingService.updateKeys(Mockito.mock(UpdateKeysInput.class)));
+ assertNull(mappingService.removeAllMappings());
+ assertNull(mappingService.getAllKeys());
+ assertNull(mappingService.updateMappings(Mockito.mock(UpdateMappingsInput.class)));
+ assertNull(mappingService.addKeys(Mockito.mock(AddKeysInput.class)));
+ assertNull(mappingService.getAllMappings());
+ }
+
+ private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
+ return new MappingRecordBuilder()
+ .setAction(MappingRecord.Action.NoAction)
+ .setAuthoritative(false)
+ .setLocatorRecord(new ArrayList<>())
+ .setMapVersion((short) 0)
+ .setRecordTtl(60)
+ .setEid(IPV4_EID);
+ }