Merge "Restoring authentication key from MDSAL."
[lispflowmapping.git] / mappingservice / mapcache / src / test / java / org / opendaylight / lispflowmapping / mapcache / SimpleMapCacheTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.lispflowmapping.mapcache;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12
13 import com.google.common.collect.Lists;
14
15 import java.util.Date;
16 import java.util.Map;
17
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mockito;
22 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
23 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
24 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
25 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
26 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container
34         .MappingRecordBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container
36         .MappingAuthkey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container
38         .MappingAuthkeyBuilder;
39 import org.powermock.api.mockito.PowerMockito;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
42
43 @RunWith(PowerMockRunner.class)
44 @PrepareForTest(MappingMergeUtil.class)
45 public class SimpleMapCacheTest {
46
47     private static ILispDAO tableMock;
48     private static ILispDAO xtrIdDaoMock;
49     private static MappingRecord mappingRecordMock;
50     private static ILispDAO daoMock;
51     private static SimpleMapCache simpleMapCache;
52
53     private static final String IPV4_STRING_1 =      "1.2.3.0";
54     private static final String IPV4_STRING_DST =    "192.168.0.1";
55     private static final String IPV4_PREFIX_STRING = "/24";
56     private static final short MASK = 24;
57     private static final long VNI_0 = 0L;
58     private static final long VNI_100 = 100L;
59     private static final byte[] IPV4_RLOC_BINARY = new byte[] {0, 1, 4, 0};
60     private static final byte[] XTR_ID = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
61
62     private static final Eid EID_IPV4_PREFIX_1_VNI = LispAddressUtil
63             .asIpv4PrefixEid(IPV4_STRING_1 + IPV4_PREFIX_STRING, new InstanceIdType(VNI_100));
64     private static final Eid EID_IPV4_PREFIX_2 = LispAddressUtil
65             .asIpv4PrefixEid(IPV4_STRING_1 + IPV4_PREFIX_STRING);
66     private static final Eid EID_IPV4_PREFIX_DST = LispAddressUtil
67             .asIpv4PrefixEid(IPV4_STRING_DST + IPV4_PREFIX_STRING);
68     private static final Eid EID_IPV4 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
69     private static final Eid NORMALIZED_EID_1 = MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI);
70     private static final Eid NORMALIZED_EID_2 = MaskUtil.normalize(EID_IPV4_PREFIX_2);
71     private static final Eid NORMALIZED_EID_IPV4 = MaskUtil.normalize(EID_IPV4);
72     private static final Eid NORMALIZED_EID_IPV4_PREFIX_DST = MaskUtil.normalize(EID_IPV4_PREFIX_DST, (short) 24);
73
74     private static final IpAddressBinary IP_ADDRESS = new IpAddressBinary(new Ipv4AddressBinary(IPV4_RLOC_BINARY));
75     // TODO Get from configuration when ready
76     private static final long REGISTRATION_VALIDITY = 200000L;
77     private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder()
78             .setKeyString("pass")
79             .setKeyType(1).build();
80     private static final Date EXPIRED_DATE = new Date(System.currentTimeMillis() - (REGISTRATION_VALIDITY + 1L));
81
82     @Before
83     public void init() {
84         daoMock = Mockito.mock(ILispDAO.class, "dao");
85         tableMock = Mockito.mock(ILispDAO.class);
86         xtrIdDaoMock = Mockito.mock(ILispDAO.class);
87         mappingRecordMock = Mockito.mock(MappingRecord.class);
88         simpleMapCache = new SimpleMapCache(daoMock);
89     }
90
91     /**
92      * Tests {@link SimpleMapCache#getMapping} method with dstEid == null.
93      */
94     @Test
95     public void getMappingTest_withNullDstEid() {
96         assertNull(simpleMapCache.getMapping(null, null, XTR_ID));
97     }
98
99     /**
100      * Tests {@link SimpleMapCache#getMapping} method with VNI_100 table == null.
101      */
102     @Test
103     public void getMappingTest_withNullVniTable() {
104         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
105         assertNull(simpleMapCache.getMapping(null, EID_IPV4_PREFIX_1_VNI, XTR_ID));
106     }
107
108     /**
109      * Tests {@link SimpleMapCache#removeMapping} method with overwrite false.
110      */
111     @Test
112     public void removeMappingTest_withOverwriteFalse() {
113         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
114         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
115
116         simpleMapCache.removeMapping(EID_IPV4, false);
117         Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
118         Mockito.verify(xtrIdDaoMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
119     }
120
121     /**
122      * Tests {@link SimpleMapCache#removeMapping} method with overwrite true.
123      */
124     @Test
125     public void removeMappingTest_withOverwriteTrue() {
126         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
127
128         simpleMapCache.removeMapping(EID_IPV4, true);
129         Mockito.verify(tableMock).removeSpecific(MaskUtil.normalize(EID_IPV4), SubKeys.RECORD);
130         Mockito.verifyNoMoreInteractions(tableMock);
131     }
132
133     /**
134      * Tests {@link SimpleMapCache#removeMapping} method with null VNI_100 table.
135      */
136     @Test
137     public void removeMappingTest_withNullVniTable() {
138         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
139
140         simpleMapCache.removeMapping(EID_IPV4, true);
141         Mockito.verifyNoMoreInteractions(tableMock);
142     }
143
144     /**
145      * Tests {@link SimpleMapCache#addAuthenticationKey} method.
146      */
147     @Test
148     public void addAuthenticationKeyTest() {
149         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
150
151         simpleMapCache.addAuthenticationKey(EID_IPV4, MAPPING_AUTHKEY);
152         Mockito.verify(tableMock)
153                 .put(MaskUtil.normalize(EID_IPV4), new MappingEntry<>(SubKeys.AUTH_KEY, MAPPING_AUTHKEY));
154     }
155
156     /**
157      * Tests {@link SimpleMapCache#getAuthenticationKey} method with maskable address.
158      */
159     @Test
160     public void getAuthenticationKeyTest_withMaskableAddress() {
161         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
162         Mockito.when(tableMock.getSpecific(MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI, MASK), SubKeys.AUTH_KEY))
163                 .thenReturn(MAPPING_AUTHKEY);
164
165         assertEquals(MAPPING_AUTHKEY, simpleMapCache.getAuthenticationKey(EID_IPV4_PREFIX_1_VNI));
166     }
167
168     /**
169      * Tests {@link SimpleMapCache#getAuthenticationKey} method with non maskable address.
170      */
171     @Test
172     public void addAuthenticationKeyTest_withNonMaskableAddress() {
173         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
174         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY)).thenReturn(MAPPING_AUTHKEY);
175
176         assertEquals(MAPPING_AUTHKEY, simpleMapCache.getAuthenticationKey(EID_IPV4));
177         Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
178     }
179
180     /**
181      * Tests {@link SimpleMapCache#getAuthenticationKey} method with no MappingAuthkey.
182      */
183     @Test
184     public void addAuthenticationKeyTest_withNoMappingAuthkey() {
185         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
186         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY)).thenReturn(null);
187
188         assertNull(simpleMapCache.getAuthenticationKey(EID_IPV4));
189     }
190
191     /**
192      * Tests {@link SimpleMapCache#getAuthenticationKey} method with no VNI_100 table.
193      */
194     @Test
195     public void addAuthenticationKeyTest_withNullVniTable() {
196         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
197
198         assertNull(simpleMapCache.getAuthenticationKey(EID_IPV4));
199     }
200
201     /**
202      * Tests {@link SimpleMapCache#removeAuthenticationKey} method.
203      */
204     @Test
205     public void removeAuthenticationKeyTest() {
206         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
207
208         simpleMapCache.removeAuthenticationKey(EID_IPV4);
209         Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
210     }
211
212     /**
213      * Tests {@link SimpleMapCache#removeAuthenticationKey} method with no VNI_100 table.
214      */
215     @Test
216     public void removeAuthenticationKeyTest_withNoVniTable() {
217         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
218
219         simpleMapCache.removeAuthenticationKey(EID_IPV4);
220         Mockito.verify(tableMock, Mockito.never()).removeSpecific(Mockito.any(Eid.class), Mockito.anyString());
221     }
222
223     /**
224      * Tests {@link SimpleMapCache#getDaoEntryBest} method with maskable address.
225      */
226     @Test
227     @SuppressWarnings("unchecked")
228     public void getDaoEntryBestTest_withMaskableAddress() {
229         final Eid normalizedKey = MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI, MASK);
230         final Map<String, Object> entryMock = Mockito.mock(Map.class);
231         final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
232
233         Mockito.when(daoMock.get(normalizedKey)).thenReturn(entryMock);
234         Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
235         Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4_PREFIX_1_VNI, SubKeys.XTRID_RECORDS))
236                 .thenReturn(xtrIdDaoMock);
237         simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI);
238
239         Mockito.verify(daoMock).get(Mockito.any(Eid.class));
240     }
241
242     /**
243      * Tests {@link SimpleMapCache#getDaoEntryBest} method with non maskable address.
244      */
245     @Test
246     @SuppressWarnings("unchecked")
247     public void getDaoEntryBestTest_withNonMaskableAddress() {
248         final Map<String, Object> entryMock = Mockito.mock(Map.class);
249         final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
250
251         Mockito.when(daoMock.get(NORMALIZED_EID_IPV4)).thenReturn(entryMock);
252         Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
253         Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4, SubKeys.XTRID_RECORDS))
254                 .thenReturn(xtrIdDaoMock);
255         simpleMapCache.getAllXtrIdMappings(EID_IPV4);
256
257         Mockito.verify(daoMock).get(Mockito.any(Eid.class));
258     }
259
260     /**
261      * Tests {@link SimpleMapCache#getDaoEntryBest} method with null daoEntry.
262      */
263     @Test
264     public void getDaoEntryBestTest_withNullEntry() {
265         Mockito.when(daoMock.get(Mockito.any(Eid.class))).thenReturn(null);
266
267         assertNull(simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI));
268         Mockito.verify(daoMock, Mockito.times(24)).get(Mockito.any(Eid.class));
269     }
270
271     /**
272      * Tests {@link SimpleMapCache#getMappingLpmEid} method.
273      */
274     @Test
275     @SuppressWarnings("unchecked")
276     public void getMappingLpmEidTest() throws Exception {
277         final Map<String, Object> mapMock = Mockito.mock(Map.class);
278         final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
279         final MappingRecord expiredMappingRecord = getDefaultMappingRecordBuilder().setTimestamp(1L).build(); // expired
280         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build(); // not expired
281
282         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
283         Mockito.when(tableMock.get(NORMALIZED_EID_IPV4_PREFIX_DST)).thenReturn(mapMock);
284         Mockito.when(mapMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
285         Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4_PREFIX_DST, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
286         Mockito.when(xtrIdDaoMock.getSpecific(XTR_ID, SubKeys.RECORD))
287                 .thenReturn(expiredMappingRecord) // first invocation
288                 .thenReturn(mappingRecord);       // second invocation
289
290         // with expired mapping record
291         assertNull(simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, XTR_ID));
292         Mockito.verify(xtrIdDaoMock, Mockito.atMost(1)).removeSpecific(XTR_ID, SubKeys.RECORD);
293
294         // with non-expired mapping record
295         assertEquals(mappingRecord, simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, XTR_ID));
296     }
297
298     /**
299      * Tests {@link SimpleMapCache#getMappingLpmEid} method with null XtrId.
300      */
301     @Test
302     @SuppressWarnings("unchecked")
303     public void getMappingLpmEidTest_withNullXtrId() throws Exception {
304         final Map<String, Object> mapMock = Mockito.mock(Map.class);
305         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
306         Mockito.when(tableMock.get(MaskUtil.normalize(EID_IPV4_PREFIX_DST, (short) 24))).thenReturn(mapMock);
307         Mockito.when(mapMock.get(SubKeys.REGDATE)).thenReturn(EXPIRED_DATE);
308
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);
313
314     }
315
316     /**
317      * Tests {@link SimpleMapCache#getDaoPairEntryBest} method with maskable eid.
318      */
319     @Test
320     @SuppressWarnings("unchecked")
321     public void getDaoPairEntryBestTest_withMaskableEid() {
322         final Eid ipv4PrefixEid = LispAddressUtil.asIpv4PrefixEid("192.168.0.225" + "/32");
323         final Eid normalizedKey = MaskUtil.normalize(EID_IPV4_PREFIX_DST, MASK);
324         final Map<String, Object> entryMock = Mockito.mock(Map.class);
325
326         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
327         Mockito.when(tableMock.get(Mockito.any(Eid.class))).thenReturn(null);
328         Mockito.when(tableMock.get(normalizedKey)).thenReturn(entryMock);
329         Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
330
331         simpleMapCache.getMapping(null, ipv4PrefixEid, XTR_ID);
332         Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
333     }
334
335     /**
336      * Tests {@link SimpleMapCache#getDaoPairEntryBest} method with maskable eid and entry not found.
337      */
338     @Test
339     public void getDaoPairEntryBestTest_withMaskableEid_noEntry() {
340         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
341         Mockito.when(tableMock.get(Mockito.any(Eid.class))).thenReturn(null);
342
343         assertNull(simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, XTR_ID));
344     }
345
346     /**
347      * Tests {@link SimpleMapCache#getDaoPairEntryBest} method with non-maskable eid.
348      */
349     @Test
350     @SuppressWarnings("unchecked")
351     public void getDaoPairEntryBestTest_withNonMaskableEid() {
352         final Map<String, Object> entryMock = Mockito.mock(Map.class);
353         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
354         Mockito.when(tableMock.get(NORMALIZED_EID_IPV4)).thenReturn(entryMock);
355         Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
356
357         simpleMapCache.getMapping(null, EID_IPV4, XTR_ID);
358         Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
359     }
360
361     /**
362      * Tests {@link SimpleMapCache#getDaoPairEntryBest} method with non-maskable eid and entry not found.
363      */
364     @Test
365     public void getDaoPairEntryBestTest_withNonMaskableEid_noEntry() {
366         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
367         Mockito.when(tableMock.get(NORMALIZED_EID_IPV4)).thenReturn(null);
368
369         assertNull(simpleMapCache.getMapping(null, EID_IPV4, XTR_ID));
370     }
371
372     /**
373      * Tests {@link SimpleMapCache#getMapping} method with Eid VNI_100 == null.
374      */
375     @Test
376     public void getVniTableTest_withVniNull() {
377         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
378
379         simpleMapCache.getMapping(null, EID_IPV4_PREFIX_2, XTR_ID);
380         Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
381     }
382
383     /**
384      * Tests {@link SimpleMapCache#getVniTable} method with Eid VNI_100 == 100L.
385      */
386     @Test
387     public void getVniTableTest_withVniNotNull() {
388         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
389
390         simpleMapCache.getMapping(null, EID_IPV4_PREFIX_1_VNI, XTR_ID);
391         Mockito.verify(daoMock).getSpecific(VNI_100, SubKeys.VNI);
392     }
393
394     /**
395      * Tests {@link SimpleMapCache#updateMappingRegistration} method.
396      */
397     @Test
398     @SuppressWarnings("unchecked")
399     public void updateMappingRegistrationTest() {
400         final Map<String, Object> entryMock = Mockito.mock(Map.class);
401         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
402         Mockito.when(tableMock.get(NORMALIZED_EID_IPV4)).thenReturn(entryMock);
403
404         simpleMapCache.updateMappingRegistration(EID_IPV4, null);
405         Mockito.verify(entryMock).put(Mockito.eq(SubKeys.REGDATE), Mockito.any(Date.class));
406     }
407
408     /**
409      * Tests {@link SimpleMapCache#updateMappingRegistration} method with no VNI_100 table.
410      */
411     @Test
412     public void updateMappingRegistrationTest_withNullVniTable() {
413         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
414
415         simpleMapCache.updateMappingRegistration(EID_IPV4, null);
416         Mockito.verifyZeroInteractions(tableMock);
417     }
418
419     /**
420      * Tests {@link SimpleMapCache#addData} method.
421      */
422     @Test
423     public void addDataTest() {
424         final Object dummyData = "dummy-data";
425         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
426
427         simpleMapCache.addData(EID_IPV4, SubKeys.RECORD, dummyData);
428         Mockito.verify(tableMock).put(NORMALIZED_EID_IPV4, new MappingEntry<>(SubKeys.RECORD, dummyData));
429     }
430
431     /**
432      * Tests {@link SimpleMapCache#getData} method.
433      */
434     @Test
435     public void getDataTest() {
436         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
437
438         simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
439         Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
440     }
441
442     /**
443      * Tests {@link SimpleMapCache#getData} method with no VNI_100 table.
444      */
445     @Test
446     public void getDataTest_withNullVniTable() {
447         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
448
449         simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
450         Mockito.verifyNoMoreInteractions(tableMock);
451     }
452
453     /**
454      * Tests {@link SimpleMapCache#removeData} method.
455      */
456     @Test
457     public void removeDataTest() {
458         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
459
460         simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
461         Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
462     }
463
464     /**
465      * Tests {@link SimpleMapCache#removeData} method with no VNI_100 table.
466      */
467     @Test
468     public void removeDataTest_withNullTable() {
469         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
470
471         simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
472         Mockito.verifyNoMoreInteractions(tableMock);
473     }
474
475     /**
476      * Tests {@link SimpleMapCache#addMapping} method with mapping merge allowed.
477      */
478     @Test
479     @SuppressWarnings("unchecked")
480     public void addMappingTest_mappingMergeTrue() throws Exception {
481         Mockito.when(mappingRecordMock.getTimestamp()).thenReturn(System.currentTimeMillis());
482         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
483         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
484         Mockito.when(mappingRecordMock.getXtrId()).thenReturn(new XtrId(XTR_ID));
485
486         PowerMockito.mockStatic(MappingMergeUtil.class);
487         PowerMockito.when(MappingMergeUtil.mergeXtrIdMappings(Mockito.anyList(), Mockito.anyList(), Mockito.anySet()))
488                 .thenReturn(getDefaultMappingRecordBuilder().build());
489
490         simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, true);
491         Mockito.verify(xtrIdDaoMock).put(new XtrId(XTR_ID), new MappingEntry<>(SubKeys.RECORD, mappingRecordMock));
492         Mockito.verify(tableMock)
493                 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.REGDATE, new Date(Long.MAX_VALUE)));
494         Mockito.verify(tableMock)
495                 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.RECORD, getDefaultMappingRecordBuilder().build()));
496     }
497
498     /**
499      * Tests {@link SimpleMapCache#addMapping} method with mapping merge false.
500      */
501     @Test
502     public void addMappingTest_mappingMergeFalse() throws Exception {
503         Mockito.when(mappingRecordMock.getTimestamp()).thenReturn(Long.MAX_VALUE);
504         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
505         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
506         Mockito.when(mappingRecordMock.getXtrId()).thenReturn(new XtrId(XTR_ID));
507
508         simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, false);
509         Mockito.verify(xtrIdDaoMock).put(new XtrId(XTR_ID), new MappingEntry<>(SubKeys.RECORD, mappingRecordMock));
510
511         Mockito.verify(tableMock)
512                 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.REGDATE, new Date(Long.MAX_VALUE)));
513         Mockito.verify(tableMock)
514                 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.RECORD, mappingRecordMock));
515     }
516
517     /**
518      * Tests {@link SimpleMapCache#addMapping} method where no interaction is expected with the dao.
519      */
520     @Test
521     public void addMappingTest_noDaoInteraction() throws Exception {
522         Mockito.when(mappingRecordMock.getXtrId()).thenReturn(null);
523         simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, null, true, true);
524         simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, new Object(), true, true);
525         simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, true);
526
527         Mockito.verifyZeroInteractions(daoMock);
528     }
529
530     /**
531      * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null.
532      */
533     @Test
534     public void getOrInstantiateVniTableTest_withNullVni() throws Exception {
535         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
536         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_2, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
537
538         simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingRecordMock, false, false); // Eid VNI_100 == null
539         Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
540         Mockito.verify(daoMock, Mockito.never()).putNestedTable(VNI_0, SubKeys.VNI);
541     }
542
543     /**
544      * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null, table == null.
545      */
546     @Test
547     public void getOrInstantiateVniTableTest_withNullVniAndTable() throws Exception {
548         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
549         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_2, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
550         Mockito.when(daoMock.putNestedTable(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
551
552         simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingRecordMock, true, false); // Eid VNI_100 == null
553         Mockito.verify(daoMock).putNestedTable(VNI_0, SubKeys.VNI);
554     }
555
556     /**
557      * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == 100L, table == null.
558      */
559     @Test
560     public void getOrInstantiateVniTableTest_withNullTable() throws Exception {
561         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
562         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
563         Mockito.when(daoMock.putNestedTable(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
564
565         simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, false); // Eid VNI_100 == null
566         Mockito.verify(daoMock).putNestedTable(VNI_100, SubKeys.VNI);
567     }
568
569     private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
570         return new MappingRecordBuilder()
571                 .setEid(EID_IPV4)
572                 .setLocatorRecord(Lists.newArrayList())
573                 .setTimestamp(Long.MAX_VALUE)
574                 .setRecordTtl(10)
575                 .setAction(MappingRecord.Action.NativelyForward)
576                 .setSourceRloc(IP_ADDRESS);
577     }
578 }