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;
15 import java.util.Date;
17 import java.util.AbstractMap.SimpleImmutableEntry;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mockito;
23 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
24 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
25 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
26 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
27 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
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
35 .MappingRecordBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container
39 .MappingAuthkeyBuilder;
40 import org.powermock.api.mockito.PowerMockito;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.modules.junit4.PowerMockRunner;
44 @RunWith(PowerMockRunner.class)
45 @PrepareForTest(MappingMergeUtil.class)
46 public class SimpleMapCacheTest {
48 private static ILispDAO tableMock;
49 private static ILispDAO xtrIdDaoMock;
50 private static MappingRecord mappingRecordMock;
51 private static ILispDAO daoMock;
52 private static SimpleMapCache simpleMapCache;
54 private static final String IPV4_STRING_1 = "1.2.3.0";
55 private static final String IPV4_STRING_DST = "192.168.0.1";
56 private static final String IPV4_PREFIX_STRING = "/24";
57 private static final short MASK = 24;
58 private static final long VNI_0 = 0L;
59 private static final long VNI_100 = 100L;
60 private static final byte[] IPV4_RLOC_BINARY = new byte[] {0, 1, 4, 0};
61 private static final byte[] XTR_ID = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
63 private static final Eid EID_IPV4_PREFIX_1_VNI = LispAddressUtil
64 .asIpv4PrefixEid(IPV4_STRING_1 + IPV4_PREFIX_STRING, new InstanceIdType(VNI_100));
65 private static final Eid EID_IPV4_PREFIX_2 = LispAddressUtil
66 .asIpv4PrefixEid(IPV4_STRING_1 + IPV4_PREFIX_STRING);
67 private static final Eid EID_IPV4_PREFIX_DST = LispAddressUtil
68 .asIpv4PrefixEid(IPV4_STRING_DST + IPV4_PREFIX_STRING);
69 private static final Eid EID_IPV4 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
70 private static final Eid NORMALIZED_EID_1 = MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI);
71 private static final Eid NORMALIZED_EID_2 = MaskUtil.normalize(EID_IPV4_PREFIX_2);
72 private static final Eid NORMALIZED_EID_IPV4 = MaskUtil.normalize(EID_IPV4);
73 private static final Eid NORMALIZED_EID_IPV4_PREFIX_DST = MaskUtil.normalize(EID_IPV4_PREFIX_DST, (short) 24);
75 private static final IpAddressBinary IP_ADDRESS = new IpAddressBinary(new Ipv4AddressBinary(IPV4_RLOC_BINARY));
76 // TODO Get from configuration when ready
77 private static final long REGISTRATION_VALIDITY = 200000L;
78 private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder()
80 .setKeyType(1).build();
81 private static final Date EXPIRED_DATE = new Date(System.currentTimeMillis() - (REGISTRATION_VALIDITY + 1L));
85 daoMock = Mockito.mock(ILispDAO.class, "dao");
86 tableMock = Mockito.mock(ILispDAO.class);
87 xtrIdDaoMock = Mockito.mock(ILispDAO.class);
88 mappingRecordMock = Mockito.mock(MappingRecord.class);
89 simpleMapCache = new SimpleMapCache(daoMock);
93 * Tests {@link SimpleMapCache#getMapping} method with dstEid == null.
96 public void getMappingTest_withNullDstEid() {
97 assertNull(simpleMapCache.getMapping(null, null, XTR_ID));
101 * Tests {@link SimpleMapCache#getMapping} method with VNI_100 table == null.
104 public void getMappingTest_withNullVniTable() {
105 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
106 assertNull(simpleMapCache.getMapping(null, EID_IPV4_PREFIX_1_VNI, XTR_ID));
110 * Tests {@link SimpleMapCache#removeMapping} method with overwrite false.
113 public void removeMappingTest_withOverwriteFalse() {
114 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
115 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
117 simpleMapCache.removeMapping(EID_IPV4, false);
118 Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
119 Mockito.verify(xtrIdDaoMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
123 * Tests {@link SimpleMapCache#removeMapping} method with overwrite true.
126 public void removeMappingTest_withOverwriteTrue() {
127 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
129 simpleMapCache.removeMapping(EID_IPV4, true);
130 Mockito.verify(tableMock).removeSpecific(MaskUtil.normalize(EID_IPV4), SubKeys.RECORD);
131 Mockito.verifyNoMoreInteractions(tableMock);
135 * Tests {@link SimpleMapCache#removeMapping} method with null VNI_100 table.
138 public void removeMappingTest_withNullVniTable() {
139 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
141 simpleMapCache.removeMapping(EID_IPV4, true);
142 Mockito.verifyNoMoreInteractions(tableMock);
146 * Tests {@link SimpleMapCache#addAuthenticationKey} method.
149 public void addAuthenticationKeyTest() {
150 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
152 simpleMapCache.addAuthenticationKey(EID_IPV4, MAPPING_AUTHKEY);
153 Mockito.verify(tableMock)
154 .put(MaskUtil.normalize(EID_IPV4), new MappingEntry<>(SubKeys.AUTH_KEY, MAPPING_AUTHKEY));
158 * Tests {@link SimpleMapCache#getAuthenticationKey} method with maskable address.
161 public void getAuthenticationKeyTest_withMaskableAddress() {
162 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
163 Mockito.when(tableMock.getSpecific(MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI, MASK), SubKeys.AUTH_KEY))
164 .thenReturn(MAPPING_AUTHKEY);
166 assertEquals(MAPPING_AUTHKEY, simpleMapCache.getAuthenticationKey(EID_IPV4_PREFIX_1_VNI));
170 * Tests {@link SimpleMapCache#getAuthenticationKey} method with non maskable address.
173 public void addAuthenticationKeyTest_withNonMaskableAddress() {
174 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
175 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY)).thenReturn(MAPPING_AUTHKEY);
177 assertEquals(MAPPING_AUTHKEY, simpleMapCache.getAuthenticationKey(EID_IPV4));
178 Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
182 * Tests {@link SimpleMapCache#getAuthenticationKey} method with no MappingAuthkey.
185 public void addAuthenticationKeyTest_withNoMappingAuthkey() {
186 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
187 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY)).thenReturn(null);
189 assertNull(simpleMapCache.getAuthenticationKey(EID_IPV4));
193 * Tests {@link SimpleMapCache#getAuthenticationKey} method with no VNI_100 table.
196 public void addAuthenticationKeyTest_withNullVniTable() {
197 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
199 assertNull(simpleMapCache.getAuthenticationKey(EID_IPV4));
203 * Tests {@link SimpleMapCache#removeAuthenticationKey} method.
206 public void removeAuthenticationKeyTest() {
207 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
209 simpleMapCache.removeAuthenticationKey(EID_IPV4);
210 Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
214 * Tests {@link SimpleMapCache#removeAuthenticationKey} method with no VNI_100 table.
217 public void removeAuthenticationKeyTest_withNoVniTable() {
218 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
220 simpleMapCache.removeAuthenticationKey(EID_IPV4);
221 Mockito.verify(tableMock, Mockito.never()).removeSpecific(Mockito.any(Eid.class), Mockito.anyString());
225 * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with maskable address.
228 @SuppressWarnings("unchecked")
229 public void getAllXtrIdMappings_withMaskableAddress() {
230 final Eid normalizedKey = MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI, MASK);
231 final Map<String, Object> entryMock = Mockito.mock(Map.class);
232 final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
234 Mockito.when(daoMock.getBest(normalizedKey)).thenReturn(entryMock);
235 Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
236 Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4_PREFIX_1_VNI, SubKeys.XTRID_RECORDS))
237 .thenReturn(xtrIdDaoMock);
238 simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI);
240 Mockito.verify(daoMock).getBest(Mockito.any(Eid.class));
244 * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with non maskable address.
247 @SuppressWarnings("unchecked")
248 public void getAllXtrIdMappings_withNonMaskableAddress() {
249 final Map<String, Object> entryMock = Mockito.mock(Map.class);
250 final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
252 Mockito.when(daoMock.getBest(NORMALIZED_EID_IPV4)).thenReturn(entryMock);
253 Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
254 Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4, SubKeys.XTRID_RECORDS))
255 .thenReturn(xtrIdDaoMock);
256 simpleMapCache.getAllXtrIdMappings(EID_IPV4);
258 Mockito.verify(daoMock).getBest(Mockito.any(Eid.class));
262 * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with null daoEntry.
265 public void getAllXtrIdMappings_withNullEntry() {
266 Mockito.when(daoMock.getBest(Mockito.any(Eid.class))).thenReturn(null);
268 assertNull(simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI));
269 Mockito.verify(daoMock, Mockito.times(1)).getBest(Mockito.any(Eid.class));
273 * Tests {@link SimpleMapCache#getMappingLpmEid} method.
276 @SuppressWarnings("unchecked")
277 public void getMappingLpmEidTest() throws Exception {
278 final Map<String, Object> mapMock = Mockito.mock(Map.class);
279 final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
280 NORMALIZED_EID_IPV4_PREFIX_DST, mapMock);
281 final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
282 final MappingRecord expiredMappingRecord = getDefaultMappingRecordBuilder().setTimestamp(1L).build(); // expired
283 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build(); // not expired
285 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
286 Mockito.when(tableMock.getBestPair(NORMALIZED_EID_IPV4_PREFIX_DST)).thenReturn(mapPair);
287 Mockito.when(mapMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
288 Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4_PREFIX_DST, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
289 Mockito.when(xtrIdDaoMock.getSpecific(XTR_ID, SubKeys.RECORD))
290 .thenReturn(expiredMappingRecord) // first invocation
291 .thenReturn(mappingRecord); // second invocation
293 // with expired mapping record
294 assertNull(simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, XTR_ID));
295 Mockito.verify(xtrIdDaoMock, Mockito.atMost(1)).removeSpecific(XTR_ID, SubKeys.RECORD);
297 // with non-expired mapping record
298 assertEquals(mappingRecord, simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, XTR_ID));
302 * Tests {@link SimpleMapCache#getMappingLpmEid} method with null XtrId.
305 @SuppressWarnings("unchecked")
306 public void getMappingLpmEidTest_withNullXtrId() throws Exception {
307 final Map<String, Object> mapMock = Mockito.mock(Map.class);
308 final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
309 NORMALIZED_EID_IPV4_PREFIX_DST, mapMock);
310 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
311 Mockito.when(tableMock.getBestPair(MaskUtil.normalize(EID_IPV4_PREFIX_DST, (short) 24))).thenReturn(mapPair);
312 Mockito.when(mapMock.get(SubKeys.REGDATE)).thenReturn(EXPIRED_DATE);
314 simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, null);
315 Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4_PREFIX_DST, SubKeys.REGDATE);
316 Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4_PREFIX_DST, SubKeys.RECORD);
317 Mockito.verify(mapMock).get(SubKeys.RECORD);
321 * Tests {@link SimpleMapCache#getMapping} method with maskable eid.
324 @SuppressWarnings("unchecked")
325 public void getMappingTest_withMaskableEid() {
326 final Eid ipv4PrefixEid = LispAddressUtil.asIpv4PrefixEid("192.168.0.225" + "/32");
327 final Map<String, Object> entryMock = Mockito.mock(Map.class);
328 final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
329 NORMALIZED_EID_IPV4_PREFIX_DST, entryMock);
331 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
332 Mockito.when(tableMock.getBestPair(ipv4PrefixEid)).thenReturn(mapPair);
333 Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
334 Mockito.when(xtrIdDaoMock.getSpecific(NORMALIZED_EID_IPV4_PREFIX_DST, SubKeys.XTRID_RECORDS)).thenReturn(null);
336 simpleMapCache.getMapping(null, ipv4PrefixEid, XTR_ID);
337 Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
341 * Tests {@link SimpleMapCache#getMapping} method with maskable eid and entry not found.
344 public void getMappingTest_withMaskableEid_noEntry() {
345 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
346 Mockito.when(tableMock.get(Mockito.any(Eid.class))).thenReturn(null);
348 assertNull(simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, XTR_ID));
352 * Tests {@link SimpleMapCache#getMapping} method with non-maskable eid.
355 @SuppressWarnings("unchecked")
356 public void getMappingTest_withNonMaskableEid() {
357 final Map<String, Object> entryMock = Mockito.mock(Map.class);
358 final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
359 NORMALIZED_EID_IPV4_PREFIX_DST, entryMock);
361 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
362 Mockito.when(tableMock.getBestPair(NORMALIZED_EID_IPV4)).thenReturn(mapPair);
363 Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
365 simpleMapCache.getMapping(null, EID_IPV4, XTR_ID);
366 Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
370 * Tests {@link SimpleMapCache#getMapping} method with non-maskable eid and entry not found.
373 public void getMappingTest_withNonMaskableEid_noEntry() {
374 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
375 Mockito.when(tableMock.get(NORMALIZED_EID_IPV4)).thenReturn(null);
377 assertNull(simpleMapCache.getMapping(null, EID_IPV4, XTR_ID));
381 * Tests {@link SimpleMapCache#getMapping} method with Eid VNI_100 == null.
384 public void getVniTableTest_withVniNull() {
385 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
387 simpleMapCache.getMapping(null, EID_IPV4_PREFIX_2, XTR_ID);
388 Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
392 * Tests {@link SimpleMapCache#getVniTable} method with Eid VNI_100 == 100L.
395 public void getVniTableTest_withVniNotNull() {
396 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
398 simpleMapCache.getMapping(null, EID_IPV4_PREFIX_1_VNI, XTR_ID);
399 Mockito.verify(daoMock).getSpecific(VNI_100, SubKeys.VNI);
403 * Tests {@link SimpleMapCache#updateMappingRegistration} method.
406 @SuppressWarnings("unchecked")
407 public void updateMappingRegistrationTest() {
408 final Map<String, Object> entryMock = Mockito.mock(Map.class);
409 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
410 Mockito.when(tableMock.getBest(NORMALIZED_EID_IPV4)).thenReturn(entryMock);
412 simpleMapCache.updateMappingRegistration(EID_IPV4, null);
413 Mockito.verify(entryMock).put(Mockito.eq(SubKeys.REGDATE), Mockito.any(Date.class));
417 * Tests {@link SimpleMapCache#updateMappingRegistration} method with no VNI_100 table.
420 public void updateMappingRegistrationTest_withNullVniTable() {
421 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
423 simpleMapCache.updateMappingRegistration(EID_IPV4, null);
424 Mockito.verifyZeroInteractions(tableMock);
428 * Tests {@link SimpleMapCache#addData} method.
431 public void addDataTest() {
432 final Object dummyData = "dummy-data";
433 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
435 simpleMapCache.addData(EID_IPV4, SubKeys.RECORD, dummyData);
436 Mockito.verify(tableMock).put(NORMALIZED_EID_IPV4, new MappingEntry<>(SubKeys.RECORD, dummyData));
440 * Tests {@link SimpleMapCache#getData} method.
443 public void getDataTest() {
444 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
446 simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
447 Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
451 * Tests {@link SimpleMapCache#getData} method with no VNI_100 table.
454 public void getDataTest_withNullVniTable() {
455 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
457 simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
458 Mockito.verifyNoMoreInteractions(tableMock);
462 * Tests {@link SimpleMapCache#removeData} method.
465 public void removeDataTest() {
466 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
468 simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
469 Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
473 * Tests {@link SimpleMapCache#removeData} method with no VNI_100 table.
476 public void removeDataTest_withNullTable() {
477 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
479 simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
480 Mockito.verifyNoMoreInteractions(tableMock);
484 * Tests {@link SimpleMapCache#addMapping} method with mapping merge allowed.
487 @SuppressWarnings("unchecked")
488 public void addMappingTest_mappingMergeTrue() throws Exception {
489 Mockito.when(mappingRecordMock.getTimestamp()).thenReturn(System.currentTimeMillis());
490 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
491 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
492 Mockito.when(mappingRecordMock.getXtrId()).thenReturn(new XtrId(XTR_ID));
494 PowerMockito.mockStatic(MappingMergeUtil.class);
495 PowerMockito.when(MappingMergeUtil.mergeXtrIdMappings(Mockito.anyList(), Mockito.anyList(), Mockito.anySet()))
496 .thenReturn(getDefaultMappingRecordBuilder().build());
498 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, true);
499 Mockito.verify(xtrIdDaoMock).put(new XtrId(XTR_ID), new MappingEntry<>(SubKeys.RECORD, mappingRecordMock));
500 Mockito.verify(tableMock)
501 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.REGDATE, new Date(Long.MAX_VALUE)));
502 Mockito.verify(tableMock)
503 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.RECORD, getDefaultMappingRecordBuilder().build()));
507 * Tests {@link SimpleMapCache#addMapping} method with mapping merge false.
510 public void addMappingTest_mappingMergeFalse() throws Exception {
511 Mockito.when(mappingRecordMock.getTimestamp()).thenReturn(Long.MAX_VALUE);
512 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
513 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
514 Mockito.when(mappingRecordMock.getXtrId()).thenReturn(new XtrId(XTR_ID));
516 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, false);
517 Mockito.verify(xtrIdDaoMock).put(new XtrId(XTR_ID), new MappingEntry<>(SubKeys.RECORD, mappingRecordMock));
519 Mockito.verify(tableMock)
520 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.REGDATE, new Date(Long.MAX_VALUE)));
521 Mockito.verify(tableMock)
522 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.RECORD, mappingRecordMock));
526 * Tests {@link SimpleMapCache#addMapping} method where no interaction is expected with the dao.
529 public void addMappingTest_noDaoInteraction() throws Exception {
530 Mockito.when(mappingRecordMock.getXtrId()).thenReturn(null);
531 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, null, true, true);
532 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, new Object(), true, true);
533 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, true);
535 Mockito.verifyZeroInteractions(daoMock);
539 * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null.
542 public void getOrInstantiateVniTableTest_withNullVni() throws Exception {
543 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
544 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_2, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
546 simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingRecordMock, false, false); // Eid VNI_100 == null
547 Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
548 Mockito.verify(daoMock, Mockito.never()).putNestedTable(VNI_0, SubKeys.VNI);
552 * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null, table == null.
555 public void getOrInstantiateVniTableTest_withNullVniAndTable() throws Exception {
556 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
557 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_2, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
558 Mockito.when(daoMock.putNestedTable(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
560 simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingRecordMock, true, false); // Eid VNI_100 == null
561 Mockito.verify(daoMock).putNestedTable(VNI_0, SubKeys.VNI);
565 * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == 100L, table == null.
568 public void getOrInstantiateVniTableTest_withNullTable() throws Exception {
569 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
570 Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
571 Mockito.when(daoMock.putNestedTable(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
573 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, false); // Eid VNI_100 == null
574 Mockito.verify(daoMock).putNestedTable(VNI_100, SubKeys.VNI);
577 private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
578 return new MappingRecordBuilder()
580 .setLocatorRecord(Lists.newArrayList())
581 .setTimestamp(Long.MAX_VALUE)
583 .setAction(MappingRecord.Action.NativelyForward)
584 .setSourceRloc(IP_ADDRESS);