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.mapcache;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
13 import com.google.common.collect.Lists;
14 import java.util.AbstractMap.SimpleImmutableEntry;
15 import java.util.Date;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mockito;
21 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
22 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
23 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
24 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
25 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.powermock.modules.junit4.PowerMockRunner;
39 @RunWith(PowerMockRunner.class)
40 @PrepareForTest(MappingMergeUtil.class)
41 public class SimpleMapCacheTest {
43 private static ILispDAO tableMock;
44 private static ILispDAO xtrIdDaoMock;
45 private static MappingRecord mappingRecordMock;
46 private static ILispDAO daoMock;
47 private static SimpleMapCache simpleMapCache;
49 private static final String IPV4_STRING_1 = "1.2.3.0";
50 private static final String IPV4_STRING_DST = "192.168.0.1";
51 private static final String IPV4_PREFIX_STRING = "/24";
52 private static final short MASK = 24;
53 private static final long VNI_0 = 0L;
54 private static final long VNI_100 = 100L;
55 private static final byte[] IPV4_RLOC_BINARY = new byte[] {0, 1, 4, 0};
56 private static final byte[] XTR_ID = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
58 private static final Eid EID_IPV4_PREFIX_1_VNI = LispAddressUtil
59 .asIpv4PrefixEid(IPV4_STRING_1 + IPV4_PREFIX_STRING, new InstanceIdType(VNI_100));
60 private static final Eid EID_IPV4_PREFIX_2 = LispAddressUtil
61 .asIpv4PrefixEid(IPV4_STRING_1 + IPV4_PREFIX_STRING);
62 private static final Eid EID_IPV4_PREFIX_DST = LispAddressUtil
63 .asIpv4PrefixEid(IPV4_STRING_DST + IPV4_PREFIX_STRING);
64 private static final Eid EID_IPV4 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
65 private static final Eid NORMALIZED_EID_1 = MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI);
66 private static final Eid NORMALIZED_EID_2 = MaskUtil.normalize(EID_IPV4_PREFIX_2);
67 private static final Eid NORMALIZED_EID_IPV4 = MaskUtil.normalize(EID_IPV4);
68 private static final Eid NORMALIZED_EID_IPV4_PREFIX_DST = MaskUtil.normalize(EID_IPV4_PREFIX_DST, (short) 24);
70 private static final IpAddressBinary IP_ADDRESS = new IpAddressBinary(new Ipv4AddressBinary(IPV4_RLOC_BINARY));
71 // TODO Get from configuration when ready
72 private static final long REGISTRATION_VALIDITY = 200000L;
73 private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder()
75 .setKeyType(1).build();
76 private static final Date EXPIRED_DATE = new Date(System.currentTimeMillis() - (REGISTRATION_VALIDITY + 1L));
80 daoMock = Mockito.mock(ILispDAO.class, "dao");
81 tableMock = Mockito.mock(ILispDAO.class);
82 xtrIdDaoMock = Mockito.mock(ILispDAO.class);
83 mappingRecordMock = Mockito.mock(MappingRecord.class);
84 simpleMapCache = new SimpleMapCache(daoMock);
88 * Tests {@link SimpleMapCache#getMapping} method with dstEid == null.
91 public void getMappingTest_withNullDstEid() {
92 assertNull(simpleMapCache.getMapping(null, null, XTR_ID));
96 * Tests {@link SimpleMapCache#getMapping} method with VNI_100 table == null.
99 public void getMappingTest_withNullVniTable() {
100 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
101 assertNull(simpleMapCache.getMapping(null, EID_IPV4_PREFIX_1_VNI, XTR_ID));
105 * Tests {@link SimpleMapCache#removeMapping} method with overwrite false.
108 public void removeMappingTest_withOverwriteFalse() {
109 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
110 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
112 simpleMapCache.removeMapping(EID_IPV4, false);
113 Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
114 Mockito.verify(xtrIdDaoMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
118 * Tests {@link SimpleMapCache#removeMapping} method with overwrite true.
121 public void removeMappingTest_withOverwriteTrue() {
122 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
124 simpleMapCache.removeMapping(EID_IPV4, true);
125 Mockito.verify(tableMock).removeSpecific(MaskUtil.normalize(EID_IPV4), SubKeys.RECORD);
126 Mockito.verifyNoMoreInteractions(tableMock);
130 * Tests {@link SimpleMapCache#removeMapping} method with null VNI_100 table.
133 public void removeMappingTest_withNullVniTable() {
134 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
136 simpleMapCache.removeMapping(EID_IPV4, true);
137 Mockito.verifyNoMoreInteractions(tableMock);
141 * Tests {@link SimpleMapCache#addAuthenticationKey} method.
144 public void addAuthenticationKeyTest() {
145 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
147 simpleMapCache.addAuthenticationKey(EID_IPV4, MAPPING_AUTHKEY);
148 Mockito.verify(tableMock)
149 .put(MaskUtil.normalize(EID_IPV4), new MappingEntry<>(SubKeys.AUTH_KEY, MAPPING_AUTHKEY));
153 * Tests {@link SimpleMapCache#getAuthenticationKey} method with maskable address.
156 public void getAuthenticationKeyTest_withMaskableAddress() {
157 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
158 Mockito.when(tableMock.getSpecific(MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI, MASK), SubKeys.AUTH_KEY))
159 .thenReturn(MAPPING_AUTHKEY);
161 assertEquals(MAPPING_AUTHKEY, simpleMapCache.getAuthenticationKey(EID_IPV4_PREFIX_1_VNI));
165 * Tests {@link SimpleMapCache#getAuthenticationKey} method with non maskable address.
168 public void addAuthenticationKeyTest_withNonMaskableAddress() {
169 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
170 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY)).thenReturn(MAPPING_AUTHKEY);
172 assertEquals(MAPPING_AUTHKEY, simpleMapCache.getAuthenticationKey(EID_IPV4));
173 Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
177 * Tests {@link SimpleMapCache#getAuthenticationKey} method with no MappingAuthkey.
180 public void addAuthenticationKeyTest_withNoMappingAuthkey() {
181 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
182 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY)).thenReturn(null);
184 assertNull(simpleMapCache.getAuthenticationKey(EID_IPV4));
188 * Tests {@link SimpleMapCache#getAuthenticationKey} method with no VNI_100 table.
191 public void addAuthenticationKeyTest_withNullVniTable() {
192 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
194 assertNull(simpleMapCache.getAuthenticationKey(EID_IPV4));
198 * Tests {@link SimpleMapCache#removeAuthenticationKey} method.
201 public void removeAuthenticationKeyTest() {
202 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
204 simpleMapCache.removeAuthenticationKey(EID_IPV4);
205 Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
209 * Tests {@link SimpleMapCache#removeAuthenticationKey} method with no VNI_100 table.
212 public void removeAuthenticationKeyTest_withNoVniTable() {
213 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
215 simpleMapCache.removeAuthenticationKey(EID_IPV4);
216 Mockito.verify(tableMock, Mockito.never()).removeSpecific(Mockito.any(Eid.class), Mockito.anyString());
220 * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with maskable address.
223 @SuppressWarnings("unchecked")
224 public void getAllXtrIdMappings_withMaskableAddress() {
225 final Eid normalizedKey = MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI, MASK);
226 final Map<String, Object> entryMock = Mockito.mock(Map.class);
227 final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
229 Mockito.when(daoMock.getBest(normalizedKey)).thenReturn(entryMock);
230 Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
231 Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4_PREFIX_1_VNI, SubKeys.XTRID_RECORDS))
232 .thenReturn(xtrIdDaoMock);
233 simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI);
235 Mockito.verify(daoMock).getBest(Mockito.any(Eid.class));
239 * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with non maskable address.
242 @SuppressWarnings("unchecked")
243 public void getAllXtrIdMappings_withNonMaskableAddress() {
244 final Map<String, Object> entryMock = Mockito.mock(Map.class);
245 final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
247 Mockito.when(daoMock.getBest(NORMALIZED_EID_IPV4)).thenReturn(entryMock);
248 Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
249 Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4, SubKeys.XTRID_RECORDS))
250 .thenReturn(xtrIdDaoMock);
251 simpleMapCache.getAllXtrIdMappings(EID_IPV4);
253 Mockito.verify(daoMock).getBest(Mockito.any(Eid.class));
257 * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with null daoEntry.
260 public void getAllXtrIdMappings_withNullEntry() {
261 Mockito.when(daoMock.getBest(Mockito.any(Eid.class))).thenReturn(null);
263 assertNull(simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI));
264 Mockito.verify(daoMock, Mockito.times(1)).getBest(Mockito.any(Eid.class));
268 * Tests {@link SimpleMapCache#getMappingLpmEid} method.
271 @SuppressWarnings("unchecked")
272 public void getMappingLpmEidTest() throws Exception {
273 final Map<String, Object> mapMock = Mockito.mock(Map.class);
274 final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
275 NORMALIZED_EID_IPV4_PREFIX_DST, mapMock);
276 final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
277 final MappingRecord expiredMappingRecord = getDefaultMappingRecordBuilder().setTimestamp(1L).build(); // expired
278 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build(); // not expired
280 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
281 Mockito.when(tableMock.getBestPair(NORMALIZED_EID_IPV4_PREFIX_DST)).thenReturn(mapPair);
282 Mockito.when(mapMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
283 Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4_PREFIX_DST, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
284 Mockito.when(xtrIdDaoMock.getSpecific(XTR_ID, SubKeys.RECORD))
285 .thenReturn(expiredMappingRecord) // first invocation
286 .thenReturn(mappingRecord); // second invocation
288 // with expired mapping record
289 assertNull(simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, XTR_ID));
290 Mockito.verify(xtrIdDaoMock, Mockito.atMost(1)).removeSpecific(XTR_ID, SubKeys.RECORD);
292 // with non-expired mapping record
293 assertEquals(mappingRecord, simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, XTR_ID));
297 * Tests {@link SimpleMapCache#getMappingLpmEid} method with null XtrId.
300 @SuppressWarnings("unchecked")
301 public void getMappingLpmEidTest_withNullXtrId() throws Exception {
302 final Map<String, Object> mapMock = Mockito.mock(Map.class);
303 final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
304 NORMALIZED_EID_IPV4_PREFIX_DST, mapMock);
305 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
306 Mockito.when(tableMock.getBestPair(MaskUtil.normalize(EID_IPV4_PREFIX_DST, (short) 24))).thenReturn(mapPair);
307 Mockito.when(mapMock.get(SubKeys.REGDATE)).thenReturn(EXPIRED_DATE);
309 simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, null);
310 Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4_PREFIX_DST, SubKeys.REGDATE);
311 Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4_PREFIX_DST, SubKeys.RECORD);
312 Mockito.verify(mapMock).get(SubKeys.RECORD);
316 * Tests {@link SimpleMapCache#getMapping} method with maskable eid.
319 @SuppressWarnings("unchecked")
320 public void getMappingTest_withMaskableEid() {
321 final Eid ipv4PrefixEid = LispAddressUtil.asIpv4PrefixEid("192.168.0.225" + "/32");
322 final Map<String, Object> entryMock = Mockito.mock(Map.class);
323 final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
324 NORMALIZED_EID_IPV4_PREFIX_DST, entryMock);
326 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
327 Mockito.when(tableMock.getBestPair(ipv4PrefixEid)).thenReturn(mapPair);
328 Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
329 Mockito.when(xtrIdDaoMock.getSpecific(NORMALIZED_EID_IPV4_PREFIX_DST, SubKeys.XTRID_RECORDS)).thenReturn(null);
331 simpleMapCache.getMapping(null, ipv4PrefixEid, XTR_ID);
332 Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
336 * Tests {@link SimpleMapCache#getMapping} method with maskable eid and entry not found.
339 public void getMappingTest_withMaskableEid_noEntry() {
340 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
341 Mockito.when(tableMock.get(Mockito.any(Eid.class))).thenReturn(null);
343 assertNull(simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, XTR_ID));
347 * Tests {@link SimpleMapCache#getMapping} method with non-maskable eid.
350 @SuppressWarnings("unchecked")
351 public void getMappingTest_withNonMaskableEid() {
352 final Map<String, Object> entryMock = Mockito.mock(Map.class);
353 final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
354 NORMALIZED_EID_IPV4_PREFIX_DST, entryMock);
356 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
357 Mockito.when(tableMock.getBestPair(NORMALIZED_EID_IPV4)).thenReturn(mapPair);
358 Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
360 simpleMapCache.getMapping(null, EID_IPV4, XTR_ID);
361 Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
365 * Tests {@link SimpleMapCache#getMapping} method with non-maskable eid and entry not found.
368 public void getMappingTest_withNonMaskableEid_noEntry() {
369 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
370 Mockito.when(tableMock.get(NORMALIZED_EID_IPV4)).thenReturn(null);
372 assertNull(simpleMapCache.getMapping(null, EID_IPV4, XTR_ID));
376 * Tests {@link SimpleMapCache#getMapping} method with Eid VNI_100 == null.
379 public void getVniTableTest_withVniNull() {
380 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
382 simpleMapCache.getMapping(null, EID_IPV4_PREFIX_2, XTR_ID);
383 Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
387 * Tests {@link SimpleMapCache#getVniTable} method with Eid VNI_100 == 100L.
390 public void getVniTableTest_withVniNotNull() {
391 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
393 simpleMapCache.getMapping(null, EID_IPV4_PREFIX_1_VNI, XTR_ID);
394 Mockito.verify(daoMock).getSpecific(VNI_100, SubKeys.VNI);
398 * Tests {@link SimpleMapCache#updateMappingRegistration} method.
401 @SuppressWarnings("unchecked")
402 public void updateMappingRegistrationTest() {
403 final Map<String, Object> entryMock = Mockito.mock(Map.class);
404 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
405 Mockito.when(tableMock.getBest(NORMALIZED_EID_IPV4)).thenReturn(entryMock);
407 simpleMapCache.updateMappingRegistration(EID_IPV4, null);
408 Mockito.verify(entryMock).put(Mockito.eq(SubKeys.REGDATE), Mockito.any(Date.class));
412 * Tests {@link SimpleMapCache#updateMappingRegistration} method with no VNI_100 table.
415 public void updateMappingRegistrationTest_withNullVniTable() {
416 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
418 simpleMapCache.updateMappingRegistration(EID_IPV4, null);
419 Mockito.verifyZeroInteractions(tableMock);
423 * Tests {@link SimpleMapCache#addData} method.
426 public void addDataTest() {
427 final Object dummyData = "dummy-data";
428 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
430 simpleMapCache.addData(EID_IPV4, SubKeys.RECORD, dummyData);
431 Mockito.verify(tableMock).put(NORMALIZED_EID_IPV4, new MappingEntry<>(SubKeys.RECORD, dummyData));
435 * Tests {@link SimpleMapCache#getData} method.
438 public void getDataTest() {
439 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
441 simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
442 Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
446 * Tests {@link SimpleMapCache#getData} method with no VNI_100 table.
449 public void getDataTest_withNullVniTable() {
450 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
452 simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
453 Mockito.verifyNoMoreInteractions(tableMock);
457 * Tests {@link SimpleMapCache#removeData} method.
460 public void removeDataTest() {
461 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
463 simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
464 Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
468 * Tests {@link SimpleMapCache#removeData} method with no VNI_100 table.
471 public void removeDataTest_withNullTable() {
472 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
474 simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
475 Mockito.verifyNoMoreInteractions(tableMock);
479 * Tests {@link SimpleMapCache#addMapping} method with mapping merge allowed.
482 @SuppressWarnings("unchecked")
483 public void addMappingTest_mappingMergeTrue() throws Exception {
484 Mockito.when(mappingRecordMock.getTimestamp()).thenReturn(System.currentTimeMillis());
485 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
486 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
487 Mockito.when(mappingRecordMock.getXtrId()).thenReturn(new XtrId(XTR_ID));
489 PowerMockito.mockStatic(MappingMergeUtil.class);
490 PowerMockito.when(MappingMergeUtil.mergeXtrIdMappings(Mockito.anyList(), Mockito.anyList(), Mockito.anySet()))
491 .thenReturn(getDefaultMappingRecordBuilder().build());
493 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, true);
494 Mockito.verify(xtrIdDaoMock).put(new XtrId(XTR_ID), new MappingEntry<>(SubKeys.RECORD, mappingRecordMock));
495 Mockito.verify(tableMock)
496 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.REGDATE, new Date(Long.MAX_VALUE)));
497 Mockito.verify(tableMock)
498 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.RECORD, getDefaultMappingRecordBuilder().build()));
502 * Tests {@link SimpleMapCache#addMapping} method with mapping merge false.
505 public void addMappingTest_mappingMergeFalse() throws Exception {
506 Mockito.when(mappingRecordMock.getTimestamp()).thenReturn(Long.MAX_VALUE);
507 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
508 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
509 Mockito.when(mappingRecordMock.getXtrId()).thenReturn(new XtrId(XTR_ID));
511 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, false);
512 Mockito.verify(xtrIdDaoMock).put(new XtrId(XTR_ID), new MappingEntry<>(SubKeys.RECORD, mappingRecordMock));
514 Mockito.verify(tableMock)
515 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.REGDATE, new Date(Long.MAX_VALUE)));
516 Mockito.verify(tableMock)
517 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.RECORD, mappingRecordMock));
521 * Tests {@link SimpleMapCache#addMapping} method where no interaction is expected with the dao.
524 public void addMappingTest_noDaoInteraction() throws Exception {
525 Mockito.when(mappingRecordMock.getXtrId()).thenReturn(null);
526 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, null, true, true);
527 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, new Object(), true, true);
528 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, true);
530 Mockito.verifyZeroInteractions(daoMock);
534 * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null.
537 public void getOrInstantiateVniTableTest_withNullVni() throws Exception {
538 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
539 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_2, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
541 simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingRecordMock, false, false); // Eid VNI_100 == null
542 Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
543 Mockito.verify(daoMock, Mockito.never()).putNestedTable(VNI_0, SubKeys.VNI);
547 * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null, table == null.
550 public void getOrInstantiateVniTableTest_withNullVniAndTable() throws Exception {
551 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
552 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_2, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
553 Mockito.when(daoMock.putNestedTable(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
555 simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingRecordMock, true, false); // Eid VNI_100 == null
556 Mockito.verify(daoMock).putNestedTable(VNI_0, SubKeys.VNI);
560 * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == 100L, table == null.
563 public void getOrInstantiateVniTableTest_withNullTable() throws Exception {
564 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
565 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
566 Mockito.when(daoMock.putNestedTable(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
568 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, false); // Eid VNI_100 == null
569 Mockito.verify(daoMock).putNestedTable(VNI_100, SubKeys.VNI);
572 private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
573 return new MappingRecordBuilder()
575 .setLocatorRecord(Lists.newArrayList())
576 .setTimestamp(Long.MAX_VALUE)
578 .setAction(MappingRecord.Action.NativelyForward)
579 .setSourceRloc(IP_ADDRESS);