--- /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.implementation.mapcache;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+
+import java.util.AbstractMap;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
+import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
+import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
+import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
+import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
+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.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.mappingservice.rev150906.mapping.authkey.container
+ .MappingAuthkey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container
+ .MappingAuthkeyBuilder;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(SimpleMapCache.class)
+public class SimpleMapCacheTest {
+
+ private static ILispDAO tableMock;
+ private static ILispDAO xtrIdDaoMock;
+ private static MappingRecord mappingRecordMock;
+ private static ILispDAO daoMock;
+ private static SimpleMapCache simpleMapCache;
+
+ private static final String IPV4_STRING_1 = "1.2.3.0";
+ private static final String IPV4_STRING_2 = "1.2.4.0";
+ private static final String IPV4_STRING_DST = "192.168.0.1";
+ private static final String IPV4_PREFIX_STRING = "/24";
+ private static final short MASK = 24;
+ private static final long VNI_0 = 0L;
+ private static final long VNI_100 = 100L;
+ private static final byte[] XTR_ID = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
+
+ private static final Eid EID_IPV4_PREFIX_1_VNI = LispAddressUtil
+ .asIpv4PrefixEid(IPV4_STRING_1 + IPV4_PREFIX_STRING, new InstanceIdType(VNI_100));
+ private static final Eid EID_IPV4_PREFIX_2 = LispAddressUtil
+ .asIpv4PrefixEid(IPV4_STRING_1 + IPV4_PREFIX_STRING);
+ private static final Eid EID_IPV4_PREFIX_DST = LispAddressUtil
+ .asIpv4PrefixEid(IPV4_STRING_DST + IPV4_PREFIX_STRING);
+ private static final Eid EID_IPV4 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
+ private static final Eid NORMALIZED_EID_1 = MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI);
+ private static final Eid NORMALIZED_EID_2 = MaskUtil.normalize(EID_IPV4_PREFIX_2);
+ private static final Eid NORMALIZED_EID_IPV4 = MaskUtil.normalize(EID_IPV4);
+
+ private static final IpAddress IP_ADDRESS = new IpAddress(new Ipv4Address(IPV4_STRING_2));
+ private static final ConfigIni CONFIG_INI = ConfigIni.getInstance();
+ private static final long REGISTRATION_VALIDITY = CONFIG_INI.getRegistrationValiditySb();
+ private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder()
+ .setKeyString("pass")
+ .setKeyType(1).build();
+ private static final Date EXPIRED_DATE = new Date(System.currentTimeMillis() - (REGISTRATION_VALIDITY + 1L));
+
+ @Before
+ public void init() {
+ daoMock = Mockito.mock(ILispDAO.class, "dao");
+ tableMock = Mockito.mock(ILispDAO.class);
+ xtrIdDaoMock = Mockito.mock(ILispDAO.class);
+ mappingRecordMock = Mockito.mock(MappingRecord.class);
+ simpleMapCache = PowerMockito.spy(new SimpleMapCache(daoMock));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getMapping} method with dstEid == null.
+ */
+ @Test
+ public void getMappingTest_withNullDstEid() {
+ assertNull(simpleMapCache.getMapping(null, null, XTR_ID));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getMapping} method with VNI_100 table == null.
+ */
+ @Test
+ public void getMappingTest_withNullVniTable() {
+ Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
+ assertNull(simpleMapCache.getMapping(null, EID_IPV4_PREFIX_1_VNI, XTR_ID));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#removeMapping} method with overwrite false.
+ */
+ @Test
+ public void removeMappingTest_withOverwriteFalse() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+ Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
+
+ simpleMapCache.removeMapping(EID_IPV4, false);
+ Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
+ Mockito.verify(xtrIdDaoMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#removeMapping} method with overwrite true.
+ */
+ @Test
+ public void removeMappingTest_withOverwriteTrue() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+
+ simpleMapCache.removeMapping(EID_IPV4, true);
+ Mockito.verify(tableMock).removeSpecific(MaskUtil.normalize(EID_IPV4), SubKeys.RECORD);
+ Mockito.verifyNoMoreInteractions(tableMock);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#removeMapping} method with null VNI_100 table.
+ */
+ @Test
+ public void removeMappingTest_withNullVniTable() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
+
+ simpleMapCache.removeMapping(EID_IPV4, true);
+ Mockito.verifyNoMoreInteractions(tableMock);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#addAuthenticationKey} method.
+ */
+ @Test
+ public void addAuthenticationKeyTest() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+
+ simpleMapCache.addAuthenticationKey(EID_IPV4, MAPPING_AUTHKEY);
+ Mockito.verify(tableMock)
+ .put(MaskUtil.normalize(EID_IPV4), new MappingEntry<>(SubKeys.AUTH_KEY, MAPPING_AUTHKEY));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getAuthenticationKey} method with maskable address.
+ */
+ @Test
+ public void getAuthenticationKeyTest_withMaskableAddress() {
+ Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
+ Mockito.when(tableMock.getSpecific(MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI, MASK), SubKeys.AUTH_KEY))
+ .thenReturn(MAPPING_AUTHKEY);
+
+ assertEquals(MAPPING_AUTHKEY, simpleMapCache.getAuthenticationKey(EID_IPV4_PREFIX_1_VNI));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getAuthenticationKey} method with non maskable address.
+ */
+ @Test
+ public void addAuthenticationKeyTest_withNonMaskableAddress() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+ Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY)).thenReturn(MAPPING_AUTHKEY);
+
+ assertEquals(MAPPING_AUTHKEY, simpleMapCache.getAuthenticationKey(EID_IPV4));
+ Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getAuthenticationKey} method with no MappingAuthkey.
+ */
+ @Test
+ public void addAuthenticationKeyTest_withNoMappingAuthkey() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+ Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY)).thenReturn(null);
+
+ assertNull(simpleMapCache.getAuthenticationKey(EID_IPV4));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getAuthenticationKey} method with no VNI_100 table.
+ */
+ @Test
+ public void addAuthenticationKeyTest_withNullVniTable() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
+
+ assertNull(simpleMapCache.getAuthenticationKey(EID_IPV4));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#removeAuthenticationKey} method.
+ */
+ @Test
+ public void removeAuthenticationKeyTest() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+
+ simpleMapCache.removeAuthenticationKey(EID_IPV4);
+ Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#removeAuthenticationKey} method with no VNI_100 table.
+ */
+ @Test
+ public void removeAuthenticationKeyTest_withNoVniTable() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
+
+ simpleMapCache.removeAuthenticationKey(EID_IPV4);
+ Mockito.verify(tableMock, Mockito.never()).removeSpecific(Mockito.any(Eid.class), Mockito.anyString());
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getDaoEntryBest} method with maskable address.
+ */
+ @Test
+ @SuppressWarnings("unchecked")
+ public void getDaoEntryBestTest_withMaskableAddress() {
+ final Eid normalizedKey = MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI, MASK);
+ final Map<String, Object> entryMock = Mockito.mock(Map.class);
+ final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
+
+ Mockito.when(daoMock.get(normalizedKey)).thenReturn(entryMock);
+ Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
+ Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4_PREFIX_1_VNI, SubKeys.XTRID_RECORDS))
+ .thenReturn(xtrIdDaoMock);
+ simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI);
+
+ Mockito.verify(daoMock).get(Mockito.any(Eid.class));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getDaoEntryBest} method with non maskable address.
+ */
+ @Test
+ @SuppressWarnings("unchecked")
+ public void getDaoEntryBestTest_withNonMaskableAddress() {
+ final Map<String, Object> entryMock = Mockito.mock(Map.class);
+ final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
+
+ Mockito.when(daoMock.get(NORMALIZED_EID_IPV4)).thenReturn(entryMock);
+ Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
+ Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4, SubKeys.XTRID_RECORDS))
+ .thenReturn(xtrIdDaoMock);
+ simpleMapCache.getAllXtrIdMappings(EID_IPV4);
+
+ Mockito.verify(daoMock).get(Mockito.any(Eid.class));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getDaoEntryBest} method with null daoEntry.
+ */
+ @Test
+ public void getDaoEntryBestTest_withNullEntry() {
+ Mockito.when(daoMock.get(Mockito.any(Eid.class))).thenReturn(null);
+
+ assertNull(simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI));
+ Mockito.verify(daoMock, Mockito.times(24)).get(Mockito.any(Eid.class));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getMappingLpmEid} method.
+ */
+ @Test
+ @SuppressWarnings("unchecked")
+ public void getMappingLpmEidTest() throws Exception {
+ final AbstractMap.SimpleImmutableEntry<Eid, Map<String, Object>> daoEntryMock =
+ Mockito.mock(AbstractMap.SimpleImmutableEntry.class);
+ final Map<String, Object> mapMock = Mockito.mock(Map.class);
+ final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
+ final MappingRecord expiredMappingRecord = getDefaultMappingRecordBuilder().setTimestamp(1L).build(); // expired
+ final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build(); // not expired
+
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+ PowerMockito.doReturn(daoEntryMock) // stubbing private method getDaoPairEntryBest(Eid, ILispDAO)
+ .when(simpleMapCache,
+ PowerMockito.method(SimpleMapCache.class, "getDaoPairEntryBest", Eid.class, ILispDAO.class))
+ .withArguments(EID_IPV4_PREFIX_DST, tableMock);
+ Mockito.when(daoEntryMock.getValue()).thenReturn(mapMock);
+ Mockito.when(mapMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
+ Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4_PREFIX_DST, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
+ Mockito.when(xtrIdDaoMock.getSpecific(XTR_ID, SubKeys.RECORD))
+ .thenReturn(expiredMappingRecord) // first invocation
+ .thenReturn(mappingRecord); // second invocation
+
+ // with expired mapping record
+ assertNull(simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, XTR_ID));
+ Mockito.verify(xtrIdDaoMock, Mockito.atMost(1)).removeSpecific(XTR_ID, SubKeys.RECORD);
+
+ // with non-expired mapping record
+ assertEquals(mappingRecord, simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, XTR_ID));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getMappingLpmEid} method with null XtrId.
+ */
+ @Test
+ @SuppressWarnings("unchecked")
+ public void getMappingLpmEidTest_withNullXtrId() throws Exception {
+ final AbstractMap.SimpleImmutableEntry<Eid, Map<String, Object>> daoEntryMock =
+ Mockito.mock(AbstractMap.SimpleImmutableEntry.class);
+ final Map<String, Object> mapMock = Mockito.mock(Map.class);
+
+
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+ PowerMockito.doReturn(daoEntryMock) // stubbing private method getDaoPairEntryBest(Eid, ILispDAO)
+ .when(simpleMapCache,
+ PowerMockito.method(SimpleMapCache.class, "getDaoPairEntryBest", Eid.class, ILispDAO.class))
+ .withArguments(EID_IPV4_PREFIX_DST, tableMock);
+ Mockito.when(daoEntryMock.getValue()).thenReturn(mapMock);
+ Mockito.when(mapMock.get(SubKeys.REGDATE)).thenReturn(EXPIRED_DATE);
+ Mockito.when(daoEntryMock.getKey()).thenReturn(NORMALIZED_EID_1);
+
+ simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, null);
+ Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_1, SubKeys.REGDATE);
+ Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_1, SubKeys.RECORD);
+ Mockito.verify(mapMock).get(SubKeys.RECORD);
+
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getDaoPairEntryBest} method with maskable eid.
+ */
+ @Test
+ @SuppressWarnings("unchecked")
+ public void getDaoPairEntryBestTest_withMaskableEid() {
+ final Eid ipv4PrefixEid = LispAddressUtil.asIpv4PrefixEid("192.168.0.225" + "/32");
+ final Eid normalizedKey = MaskUtil.normalize(EID_IPV4_PREFIX_DST, MASK);
+ final Map<String, Object> entryMock = Mockito.mock(Map.class);
+
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+ Mockito.when(tableMock.get(Mockito.any(Eid.class))).thenReturn(null);
+ Mockito.when(tableMock.get(normalizedKey)).thenReturn(entryMock);
+ Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
+
+ simpleMapCache.getMapping(null, ipv4PrefixEid, XTR_ID);
+ Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getDaoPairEntryBest} method with maskable eid and entry not found.
+ */
+ @Test
+ public void getDaoPairEntryBestTest_withMaskableEid_noEntry() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+ Mockito.when(tableMock.get(Mockito.any(Eid.class))).thenReturn(null);
+
+ assertNull(simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, XTR_ID));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getDaoPairEntryBest} method with non-maskable eid.
+ */
+ @Test
+ @SuppressWarnings("unchecked")
+ public void getDaoPairEntryBestTest_withNonMaskableEid() {
+ final Map<String, Object> entryMock = Mockito.mock(Map.class);
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+ Mockito.when(tableMock.get(NORMALIZED_EID_IPV4)).thenReturn(entryMock);
+ Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
+
+ simpleMapCache.getMapping(null, EID_IPV4, XTR_ID);
+ Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getDaoPairEntryBest} method with non-maskable eid and entry not found.
+ */
+ @Test
+ public void getDaoPairEntryBestTest_withNonMaskableEid_noEntry() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+ Mockito.when(tableMock.get(NORMALIZED_EID_IPV4)).thenReturn(null);
+
+ assertNull(simpleMapCache.getMapping(null, EID_IPV4, XTR_ID));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getMapping} method with Eid VNI_100 == null.
+ */
+ @Test
+ public void getVniTableTest_withVniNull() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
+
+ simpleMapCache.getMapping(null, EID_IPV4_PREFIX_2, XTR_ID);
+ Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getVniTable} method with Eid VNI_100 == 100L.
+ */
+ @Test
+ public void getVniTableTest_withVniNotNull() {
+ Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
+
+ simpleMapCache.getMapping(null, EID_IPV4_PREFIX_1_VNI, XTR_ID);
+ Mockito.verify(daoMock).getSpecific(VNI_100, SubKeys.VNI);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#updateMappingRegistration} method.
+ */
+ @Test
+ @SuppressWarnings("unchecked")
+ public void updateMappingRegistrationTest() {
+ final Map<String, Object> entryMock = Mockito.mock(Map.class);
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+ Mockito.when(tableMock.get(NORMALIZED_EID_IPV4)).thenReturn(entryMock);
+
+ simpleMapCache.updateMappingRegistration(EID_IPV4);
+ Mockito.verify(entryMock).put(Mockito.eq(SubKeys.REGDATE), Mockito.any(Date.class));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#updateMappingRegistration} method with no VNI_100 table.
+ */
+ @Test
+ public void updateMappingRegistrationTest_withNullVniTable() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
+
+ simpleMapCache.updateMappingRegistration(EID_IPV4);
+ Mockito.verifyZeroInteractions(tableMock);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#addData} method.
+ */
+ @Test
+ public void addDataTest() {
+ final Object dummyData = "dummy-data";
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+
+ simpleMapCache.addData(EID_IPV4, SubKeys.RECORD, dummyData);
+ Mockito.verify(tableMock).put(NORMALIZED_EID_IPV4, new MappingEntry<>(SubKeys.RECORD, dummyData));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getData} method.
+ */
+ @Test
+ public void getDataTest() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+
+ simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
+ Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getData} method with no VNI_100 table.
+ */
+ @Test
+ public void getDataTest_withNullVniTable() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
+
+ simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
+ Mockito.verifyNoMoreInteractions(tableMock);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#removeData} method.
+ */
+ @Test
+ public void removeDataTest() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+
+ simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
+ Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#removeData} method with no VNI_100 table.
+ */
+ @Test
+ public void removeDataTest_withNullTable() {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
+
+ simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
+ Mockito.verifyNoMoreInteractions(tableMock);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#addMapping} method with mapping merge allowed.
+ */
+ @Test
+ public void addMappingTest_mappingMergeTrue() throws Exception {
+ Mockito.when(mappingRecordMock.getTimestamp()).thenReturn(System.currentTimeMillis());
+ Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
+ Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
+ Mockito.when(mappingRecordMock.getXtrId()).thenReturn(new XtrId(XTR_ID));
+
+ Set<IpAddress> ipAddresses = Sets.newHashSet(IP_ADDRESS);
+ List<Object> records = Lists.newArrayList(getDefaultMappingRecordBuilder().build());
+ PowerMockito.doReturn(records).when(simpleMapCache, // stubs private getXtrIdMappingList method
+ PowerMockito.method(SimpleMapCache.class, "getXtrIdMappingList", ILispDAO.class))
+ .withArguments(xtrIdDaoMock);
+
+ simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, true);
+ Mockito.verify(xtrIdDaoMock).put(new XtrId(XTR_ID), new MappingEntry<>(SubKeys.RECORD, mappingRecordMock));
+ Mockito.verify(tableMock)
+ .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.REGDATE, new Date(Long.MAX_VALUE)));
+ Mockito.verify(tableMock)
+ .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.RECORD, getDefaultMappingRecordBuilder().build()));
+ Mockito.verify(tableMock).put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.SRC_RLOCS, ipAddresses));
+ Mockito.verifyNoMoreInteractions(xtrIdDaoMock);
+ //Mockito.verifyZeroInteractions(xtrIdDaoMock).removeSpecific(new XtrId(XTR_ID), SubKeys.RECORD);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#addMapping} method with mapping merge allowed and expired mapping.
+ */
+ @Test
+ public void addMappingTest_mappingMergeTrue_expiredXtrMapping() throws Exception {
+ Mockito.when(mappingRecordMock.getTimestamp()).thenReturn(System.currentTimeMillis());
+ Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
+ Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
+ Mockito.when(mappingRecordMock.getXtrId()).thenReturn(new XtrId(XTR_ID));
+
+ List<Object> records = Lists.newArrayList(getDefaultMappingRecordBuilder()
+ .setXtrId(new XtrId(XTR_ID))
+ .setTimestamp(EXPIRED_DATE.getTime()).build());
+ PowerMockito.doReturn(records).when(simpleMapCache, // stubs private getXtrIdMappingList method
+ PowerMockito.method(SimpleMapCache.class, "getXtrIdMappingList", ILispDAO.class))
+ .withArguments(xtrIdDaoMock);
+
+ simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, true);
+ Mockito.verify(xtrIdDaoMock).put(new XtrId(XTR_ID), new MappingEntry<>(SubKeys.RECORD, mappingRecordMock));
+ Mockito.verify(xtrIdDaoMock).removeSpecific(new XtrId(XTR_ID), SubKeys.RECORD);
+ Mockito.verify(tableMock, Mockito.never()).put(Mockito.any(Eid.class), Mockito.any(MappingEntry.class));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#addMapping} method with mapping merge false.
+ */
+ @Test
+ public void addMappingTest_mappingMergeFalse() throws Exception {
+ Mockito.when(mappingRecordMock.getTimestamp()).thenReturn(Long.MAX_VALUE);
+ Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
+ Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
+ Mockito.when(mappingRecordMock.getXtrId()).thenReturn(new XtrId(XTR_ID));
+
+ simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, false);
+ Mockito.verify(xtrIdDaoMock).put(new XtrId(XTR_ID), new MappingEntry<>(SubKeys.RECORD, mappingRecordMock));
+
+ Mockito.verify(tableMock)
+ .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.REGDATE, new Date(Long.MAX_VALUE)));
+ Mockito.verify(tableMock)
+ .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.RECORD, mappingRecordMock));
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#addMapping} method where no interaction is expected with the dao.
+ */
+ @Test
+ public void addMappingTest_noDaoInteraction() throws Exception {
+ Mockito.when(mappingRecordMock.getXtrId()).thenReturn(null);
+ simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, null, true, true);
+ simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, new Object(), true, true);
+ simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, true);
+
+ Mockito.verifyZeroInteractions(daoMock);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null.
+ */
+ @Test
+ public void getOrInstantiateVniTableTest_withNullVni() throws Exception {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+ Mockito.when(tableMock.getSpecific(NORMALIZED_EID_2, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
+
+ simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingRecordMock, false, false); // Eid VNI_100 == null
+ Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
+ Mockito.verify(daoMock, Mockito.never()).putNestedTable(VNI_0, SubKeys.VNI);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null, table == null.
+ */
+ @Test
+ public void getOrInstantiateVniTableTest_withNullVniAndTable() throws Exception {
+ Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
+ Mockito.when(tableMock.getSpecific(NORMALIZED_EID_2, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
+ Mockito.when(daoMock.putNestedTable(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
+
+ simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingRecordMock, true, false); // Eid VNI_100 == null
+ Mockito.verify(daoMock).putNestedTable(VNI_0, SubKeys.VNI);
+ }
+
+ /**
+ * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == 100L, table == null.
+ */
+ @Test
+ public void getOrInstantiateVniTableTest_withNullTable() throws Exception {
+ Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
+ Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
+ Mockito.when(daoMock.putNestedTable(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
+
+ simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, false); // Eid VNI_100 == null
+ Mockito.verify(daoMock).putNestedTable(VNI_100, SubKeys.VNI);
+ }
+
+ private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
+ return new MappingRecordBuilder()
+ .setEid(EID_IPV4)
+ .setLocatorRecord(Lists.newArrayList())
+ .setTimestamp(Long.MAX_VALUE)
+ .setRecordTtl(10)
+ .setAction(MappingRecord.Action.NativelyForward)
+ .setSourceRloc(IP_ADDRESS);
+ }
+}