Fix NPE in SimpleMapCache
[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 import java.util.AbstractMap.SimpleImmutableEntry;
15 import java.util.Date;
16 import java.util.Map;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mockito;
20 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
21 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
22 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
23 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
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
36 public class SimpleMapCacheTest {
37
38     private static ILispDAO tableMock;
39     private static ILispDAO xtrIdDaoMock;
40     private static MappingData mappingDataMock;
41     private static ILispDAO daoMock;
42     private static SimpleMapCache simpleMapCache;
43
44     private static final String IPV4_STRING_1 =      "1.2.3.0";
45     private static final String IPV4_STRING_DST =    "192.168.0.1";
46     private static final String IPV4_PREFIX_STRING = "/24";
47     private static final short MASK = 24;
48     private static final long VNI_0 = 0L;
49     private static final long VNI_100 = 100L;
50     private static final byte[] IPV4_RLOC_BINARY = new byte[] {0, 1, 4, 0};
51     private static final XtrId XTR_ID = new XtrId(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
52
53     private static final Eid EID_IPV4_PREFIX_1_VNI = LispAddressUtil
54             .asIpv4PrefixEid(IPV4_STRING_1 + IPV4_PREFIX_STRING, new InstanceIdType(VNI_100));
55     private static final Eid EID_IPV4_PREFIX_2 = LispAddressUtil
56             .asIpv4PrefixEid(IPV4_STRING_1 + IPV4_PREFIX_STRING);
57     private static final Eid EID_IPV4_PREFIX_DST = LispAddressUtil
58             .asIpv4PrefixEid(IPV4_STRING_DST + IPV4_PREFIX_STRING);
59     private static final Eid EID_IPV4 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
60     private static final Eid NORMALIZED_EID_1 = MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI);
61     private static final Eid NORMALIZED_EID_2 = MaskUtil.normalize(EID_IPV4_PREFIX_2);
62     private static final Eid NORMALIZED_EID_IPV4 = MaskUtil.normalize(EID_IPV4);
63     private static final Eid NORMALIZED_EID_IPV4_PREFIX_DST = MaskUtil.normalize(EID_IPV4_PREFIX_DST, (short) 24);
64
65     private static final IpAddressBinary IP_ADDRESS = new IpAddressBinary(new Ipv4AddressBinary(IPV4_RLOC_BINARY));
66     private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder()
67             .setKeyString("pass")
68             .setKeyType(1).build();
69
70     @Before
71     public void init() {
72         daoMock = Mockito.mock(ILispDAO.class, "dao");
73         tableMock = Mockito.mock(ILispDAO.class);
74         xtrIdDaoMock = Mockito.mock(ILispDAO.class);
75         mappingDataMock = Mockito.mock(MappingData.class);
76         simpleMapCache = new SimpleMapCache(daoMock);
77     }
78
79     /**
80      * Tests {@link SimpleMapCache#getMapping} method with dstEid == null.
81      */
82     @Test
83     public void getMappingTest_withNullDstEid() {
84         assertNull(simpleMapCache.getMapping(null, XTR_ID));
85     }
86
87     /**
88      * Tests {@link SimpleMapCache#getMapping} method with VNI_100 table == null.
89      */
90     @Test
91     public void getMappingTest_withNullVniTable() {
92         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
93         assertNull(simpleMapCache.getMapping(EID_IPV4_PREFIX_1_VNI, XTR_ID));
94     }
95
96     /**
97      * Tests {@link SimpleMapCache#removeMapping} method with xTR-ID.
98      */
99     @Test
100     public void removeMappingTest_withXtrId() {
101         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
102         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
103
104         simpleMapCache.removeMapping(EID_IPV4, XTR_ID);
105         Mockito.verify(xtrIdDaoMock).removeSpecific(XTR_ID, SubKeys.RECORD);
106     }
107
108     /**
109      * Tests {@link SimpleMapCache#removeMapping} method.
110      */
111     @Test
112     public void removeMappingTest() {
113         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
114
115         simpleMapCache.removeMapping(EID_IPV4);
116         Mockito.verify(tableMock).removeSpecific(MaskUtil.normalize(EID_IPV4), SubKeys.RECORD);
117         Mockito.verify(tableMock).removeSpecific(MaskUtil.normalize(EID_IPV4), SubKeys.SRC_RLOCS);
118         Mockito.verify(tableMock).removeSpecific(MaskUtil.normalize(EID_IPV4), SubKeys.XTRID_RECORDS);
119         Mockito.verifyNoMoreInteractions(tableMock);
120     }
121
122     /**
123      * Tests {@link SimpleMapCache#removeMapping} method with null VNI_100 table.
124      */
125     @Test
126     public void removeMappingTest_withNullVniTable() {
127         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
128
129         simpleMapCache.removeMapping(EID_IPV4);
130         Mockito.verifyNoMoreInteractions(tableMock);
131     }
132
133     /**
134      * Tests {@link SimpleMapCache#addAuthenticationKey} method.
135      */
136     @Test
137     public void addAuthenticationKeyTest() {
138         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
139
140         simpleMapCache.addAuthenticationKey(EID_IPV4, MAPPING_AUTHKEY);
141         Mockito.verify(tableMock)
142                 .put(MaskUtil.normalize(EID_IPV4), new MappingEntry<>(SubKeys.AUTH_KEY, MAPPING_AUTHKEY));
143     }
144
145     /**
146      * Tests {@link SimpleMapCache#getAuthenticationKey} method with maskable address.
147      */
148     @Test
149     public void getAuthenticationKeyTest_withMaskableAddress() {
150         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
151         Mockito.when(tableMock.getSpecific(MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI, MASK), SubKeys.AUTH_KEY))
152                 .thenReturn(MAPPING_AUTHKEY);
153
154         assertEquals(MAPPING_AUTHKEY, simpleMapCache.getAuthenticationKey(EID_IPV4_PREFIX_1_VNI));
155     }
156
157     /**
158      * Tests {@link SimpleMapCache#getAuthenticationKey} method with non maskable address.
159      */
160     @Test
161     public void addAuthenticationKeyTest_withNonMaskableAddress() {
162         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
163         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY)).thenReturn(MAPPING_AUTHKEY);
164
165         assertEquals(MAPPING_AUTHKEY, simpleMapCache.getAuthenticationKey(EID_IPV4));
166         Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
167     }
168
169     /**
170      * Tests {@link SimpleMapCache#getAuthenticationKey} method with no MappingAuthkey.
171      */
172     @Test
173     public void addAuthenticationKeyTest_withNoMappingAuthkey() {
174         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
175         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY)).thenReturn(null);
176
177         assertNull(simpleMapCache.getAuthenticationKey(EID_IPV4));
178     }
179
180     /**
181      * Tests {@link SimpleMapCache#getAuthenticationKey} method with no VNI_100 table.
182      */
183     @Test
184     public void addAuthenticationKeyTest_withNullVniTable() {
185         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
186
187         assertNull(simpleMapCache.getAuthenticationKey(EID_IPV4));
188     }
189
190     /**
191      * Tests {@link SimpleMapCache#removeAuthenticationKey} method.
192      */
193     @Test
194     public void removeAuthenticationKeyTest() {
195         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
196
197         simpleMapCache.removeAuthenticationKey(EID_IPV4);
198         Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
199     }
200
201     /**
202      * Tests {@link SimpleMapCache#removeAuthenticationKey} method with no VNI_100 table.
203      */
204     @Test
205     public void removeAuthenticationKeyTest_withNoVniTable() {
206         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
207
208         simpleMapCache.removeAuthenticationKey(EID_IPV4);
209         Mockito.verify(tableMock, Mockito.never()).removeSpecific(Mockito.any(Eid.class), Mockito.anyString());
210     }
211
212     /**
213      * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with maskable address.
214      */
215     @Test
216     @SuppressWarnings("unchecked")
217     public void getAllXtrIdMappings_withMaskableAddress() {
218         final Eid normalizedKey = MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI, MASK);
219         final Map<String, Object> entryMock = Mockito.mock(Map.class);
220         final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
221
222         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
223         Mockito.when(tableMock.getBest(normalizedKey)).thenReturn(entryMock);
224         Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
225         Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4_PREFIX_1_VNI, SubKeys.RECORD))
226                 .thenReturn(xtrIdDaoMock);
227         simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI);
228
229         Mockito.verify(tableMock).getBest(Mockito.any(Eid.class));
230     }
231
232     /**
233      * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with non maskable address.
234      */
235     @Test
236     @SuppressWarnings("unchecked")
237     public void getAllXtrIdMappings_withNonMaskableAddress() {
238         final Map<String, Object> entryMock = Mockito.mock(Map.class);
239         final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
240
241         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
242         Mockito.when(tableMock.getBest(NORMALIZED_EID_IPV4)).thenReturn(entryMock);
243         Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
244         Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4, SubKeys.RECORD))
245                 .thenReturn(xtrIdDaoMock);
246         simpleMapCache.getAllXtrIdMappings(EID_IPV4);
247
248         Mockito.verify(tableMock).getBest(Mockito.any(Eid.class));
249     }
250
251     /**
252      * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with null daoEntry.
253      */
254     @Test
255     public void getAllXtrIdMappings_withNullEntry() {
256         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
257         Mockito.when(tableMock.getBest(Mockito.any(Eid.class))).thenReturn(null);
258
259         assertNull(simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI));
260         Mockito.verify(tableMock, Mockito.times(1)).getBest(Mockito.any(Eid.class));
261     }
262
263     /**
264      * Tests {@link SimpleMapCache#getMappingLpmEid} method.
265      */
266     @Test
267     @SuppressWarnings("unchecked")
268     public void getMappingLpmEidTest() throws Exception {
269         final Map<String, Object> mapMock = Mockito.mock(Map.class);
270         final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
271                 NORMALIZED_EID_IPV4_PREFIX_DST, mapMock);
272         final MappingData mappingData = new MappingData(getDefaultMappingRecordBuilder().build());
273
274         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
275         Mockito.when(tableMock.getBestPair(NORMALIZED_EID_IPV4_PREFIX_DST)).thenReturn(mapPair);
276         Mockito.when(mapMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
277         Mockito.when(xtrIdDaoMock.getSpecific(XTR_ID, SubKeys.RECORD))
278                 .thenReturn(mappingData);       // second invocation
279
280         // with non-expired mapping record
281         assertEquals(mappingData, simpleMapCache.getMapping(EID_IPV4_PREFIX_DST, XTR_ID));
282     }
283
284     /**
285      * Tests {@link SimpleMapCache#getMappingLpmEid} method with null XtrId.
286      */
287     @Test
288     @SuppressWarnings("unchecked")
289     public void getMappingLpmEidTest_withNullXtrId() throws Exception {
290         final Map<String, Object> mapMock = Mockito.mock(Map.class);
291         final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
292                 NORMALIZED_EID_IPV4_PREFIX_DST, mapMock);
293         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
294         Mockito.when(tableMock.getBestPair(MaskUtil.normalize(EID_IPV4_PREFIX_DST, (short) 24))).thenReturn(mapPair);
295
296         simpleMapCache.getMapping(EID_IPV4_PREFIX_DST, (XtrId) null);
297         Mockito.verify(tableMock).getBestPair(NORMALIZED_EID_IPV4_PREFIX_DST);
298         Mockito.verify(mapMock).get(SubKeys.RECORD);
299     }
300
301     /**
302      * Tests {@link SimpleMapCache#getMapping} method with maskable eid.
303      */
304     @Test
305     @SuppressWarnings("unchecked")
306     public void getMappingTest_withMaskableEid() {
307         final Eid ipv4PrefixEid = LispAddressUtil.asIpv4PrefixEid("192.168.0.225" + "/32");
308         final Map<String, Object> entryMock = Mockito.mock(Map.class);
309         final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
310                 NORMALIZED_EID_IPV4_PREFIX_DST, entryMock);
311
312         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
313         Mockito.when(tableMock.getBestPair(ipv4PrefixEid)).thenReturn(mapPair);
314         Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
315         Mockito.when(xtrIdDaoMock.getSpecific(NORMALIZED_EID_IPV4_PREFIX_DST, SubKeys.XTRID_RECORDS)).thenReturn(null);
316
317         simpleMapCache.getMapping(ipv4PrefixEid, XTR_ID);
318         Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
319     }
320
321     /**
322      * Tests {@link SimpleMapCache#getMapping} method with maskable eid and entry not found.
323      */
324     @Test
325     public void getMappingTest_withMaskableEid_noEntry() {
326         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
327         Mockito.when(tableMock.get(Mockito.any(Eid.class))).thenReturn(null);
328
329         assertNull(simpleMapCache.getMapping(EID_IPV4_PREFIX_DST, XTR_ID));
330     }
331
332     /**
333      * Tests {@link SimpleMapCache#getMapping} method with non-maskable eid.
334      */
335     @Test
336     @SuppressWarnings("unchecked")
337     public void getMappingTest_withNonMaskableEid() {
338         final Map<String, Object> entryMock = Mockito.mock(Map.class);
339         final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
340                 NORMALIZED_EID_IPV4_PREFIX_DST, entryMock);
341
342         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
343         Mockito.when(tableMock.getBestPair(NORMALIZED_EID_IPV4)).thenReturn(mapPair);
344         Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
345
346         simpleMapCache.getMapping(EID_IPV4, XTR_ID);
347         Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
348     }
349
350     /**
351      * Tests {@link SimpleMapCache#getMapping} method with non-maskable eid and entry not found.
352      */
353     @Test
354     public void getMappingTest_withNonMaskableEid_noEntry() {
355         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
356         Mockito.when(tableMock.get(NORMALIZED_EID_IPV4)).thenReturn(null);
357
358         assertNull(simpleMapCache.getMapping(EID_IPV4, XTR_ID));
359     }
360
361     /**
362      * Tests {@link SimpleMapCache#getMapping} method with Eid VNI_100 == null.
363      */
364     @Test
365     public void getVniTableTest_withVniNull() {
366         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
367
368         simpleMapCache.getMapping(EID_IPV4_PREFIX_2, XTR_ID);
369         Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
370     }
371
372     /**
373      * Tests {@link SimpleMapCache#getVniTable} method with Eid VNI_100 == 100L.
374      */
375     @Test
376     public void getVniTableTest_withVniNotNull() {
377         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
378
379         simpleMapCache.getMapping(EID_IPV4_PREFIX_1_VNI, XTR_ID);
380         Mockito.verify(daoMock).getSpecific(VNI_100, SubKeys.VNI);
381     }
382
383     /**
384      * Tests {@link SimpleMapCache#addData} method.
385      */
386     @Test
387     public void addDataTest() {
388         final Object dummyData = "dummy-data";
389         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
390
391         simpleMapCache.addData(EID_IPV4, SubKeys.RECORD, dummyData);
392         Mockito.verify(tableMock).put(NORMALIZED_EID_IPV4, new MappingEntry<>(SubKeys.RECORD, dummyData));
393     }
394
395     /**
396      * Tests {@link SimpleMapCache#getData} method.
397      */
398     @Test
399     public void getDataTest() {
400         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
401
402         simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
403         Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
404     }
405
406     /**
407      * Tests {@link SimpleMapCache#getData} method with no VNI_100 table.
408      */
409     @Test
410     public void getDataTest_withNullVniTable() {
411         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
412
413         simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
414         Mockito.verifyNoMoreInteractions(tableMock);
415     }
416
417     /**
418      * Tests {@link SimpleMapCache#removeData} method.
419      */
420     @Test
421     public void removeDataTest() {
422         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
423
424         simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
425         Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
426     }
427
428     /**
429      * Tests {@link SimpleMapCache#removeData} method with no VNI_100 table.
430      */
431     @Test
432     public void removeDataTest_withNullTable() {
433         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
434
435         simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
436         Mockito.verifyNoMoreInteractions(tableMock);
437     }
438
439     /**
440      * Tests {@link SimpleMapCache#addMapping} method.
441      */
442     @Test
443     public void addMappingTest() throws Exception {
444         final Date timestamp = new Date(System.currentTimeMillis());
445         Mockito.when(mappingDataMock.getTimestamp()).thenReturn(timestamp);
446         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
447         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
448         Mockito.when(mappingDataMock.getXtrId()).thenReturn(XTR_ID);
449
450         simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingDataMock);
451         Mockito.verify(tableMock)
452                 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.RECORD, mappingDataMock));
453     }
454
455     /**
456      * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null.
457      */
458     @Test
459     public void getOrInstantiateVniTableTest_withNullVni() throws Exception {
460         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
461         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_2, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
462
463         simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingDataMock); // Eid VNI_100 == null
464         Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
465         Mockito.verify(daoMock, Mockito.never()).putNestedTable(VNI_0, SubKeys.VNI);
466     }
467
468     /**
469      * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null, table == null.
470      */
471     @Test
472     public void getOrInstantiateVniTableTest_withNullVniAndTable() throws Exception {
473         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
474         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_2, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
475         Mockito.when(daoMock.putNestedTable(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
476
477         simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingDataMock); // Eid VNI_100 == null
478         Mockito.verify(daoMock).putNestedTable(VNI_0, SubKeys.VNI);
479     }
480
481     /**
482      * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == 100L, table == null.
483      */
484     @Test
485     public void getOrInstantiateVniTableTest_withNullTable() throws Exception {
486         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
487         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
488         Mockito.when(daoMock.putNestedTable(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
489
490         simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingDataMock); // Eid VNI_100 == null
491         Mockito.verify(daoMock).putNestedTable(VNI_100, SubKeys.VNI);
492     }
493
494     private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
495         return new MappingRecordBuilder()
496                 .setEid(EID_IPV4)
497                 .setLocatorRecord(Lists.newArrayList())
498                 .setTimestamp(Long.MAX_VALUE)
499                 .setRecordTtl(10)
500                 .setAction(MappingRecord.Action.NativelyForward)
501                 .setSourceRloc(IP_ADDRESS);
502     }
503 }