Bug 6071: Move authentication package to lisp-proto
[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 import java.util.AbstractMap.SimpleImmutableEntry;
18
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
37         .MappingAuthkey;
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;
43
44 @RunWith(PowerMockRunner.class)
45 @PrepareForTest(MappingMergeUtil.class)
46 public class SimpleMapCacheTest {
47
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;
53
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};
62
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);
74
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()
79             .setKeyString("pass")
80             .setKeyType(1).build();
81     private static final Date EXPIRED_DATE = new Date(System.currentTimeMillis() - (REGISTRATION_VALIDITY + 1L));
82
83     @Before
84     public void init() {
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);
90     }
91
92     /**
93      * Tests {@link SimpleMapCache#getMapping} method with dstEid == null.
94      */
95     @Test
96     public void getMappingTest_withNullDstEid() {
97         assertNull(simpleMapCache.getMapping(null, null, XTR_ID));
98     }
99
100     /**
101      * Tests {@link SimpleMapCache#getMapping} method with VNI_100 table == null.
102      */
103     @Test
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));
107     }
108
109     /**
110      * Tests {@link SimpleMapCache#removeMapping} method with overwrite false.
111      */
112     @Test
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);
116
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);
120     }
121
122     /**
123      * Tests {@link SimpleMapCache#removeMapping} method with overwrite true.
124      */
125     @Test
126     public void removeMappingTest_withOverwriteTrue() {
127         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
128
129         simpleMapCache.removeMapping(EID_IPV4, true);
130         Mockito.verify(tableMock).removeSpecific(MaskUtil.normalize(EID_IPV4), SubKeys.RECORD);
131         Mockito.verifyNoMoreInteractions(tableMock);
132     }
133
134     /**
135      * Tests {@link SimpleMapCache#removeMapping} method with null VNI_100 table.
136      */
137     @Test
138     public void removeMappingTest_withNullVniTable() {
139         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
140
141         simpleMapCache.removeMapping(EID_IPV4, true);
142         Mockito.verifyNoMoreInteractions(tableMock);
143     }
144
145     /**
146      * Tests {@link SimpleMapCache#addAuthenticationKey} method.
147      */
148     @Test
149     public void addAuthenticationKeyTest() {
150         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
151
152         simpleMapCache.addAuthenticationKey(EID_IPV4, MAPPING_AUTHKEY);
153         Mockito.verify(tableMock)
154                 .put(MaskUtil.normalize(EID_IPV4), new MappingEntry<>(SubKeys.AUTH_KEY, MAPPING_AUTHKEY));
155     }
156
157     /**
158      * Tests {@link SimpleMapCache#getAuthenticationKey} method with maskable address.
159      */
160     @Test
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);
165
166         assertEquals(MAPPING_AUTHKEY, simpleMapCache.getAuthenticationKey(EID_IPV4_PREFIX_1_VNI));
167     }
168
169     /**
170      * Tests {@link SimpleMapCache#getAuthenticationKey} method with non maskable address.
171      */
172     @Test
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);
176
177         assertEquals(MAPPING_AUTHKEY, simpleMapCache.getAuthenticationKey(EID_IPV4));
178         Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
179     }
180
181     /**
182      * Tests {@link SimpleMapCache#getAuthenticationKey} method with no MappingAuthkey.
183      */
184     @Test
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);
188
189         assertNull(simpleMapCache.getAuthenticationKey(EID_IPV4));
190     }
191
192     /**
193      * Tests {@link SimpleMapCache#getAuthenticationKey} method with no VNI_100 table.
194      */
195     @Test
196     public void addAuthenticationKeyTest_withNullVniTable() {
197         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
198
199         assertNull(simpleMapCache.getAuthenticationKey(EID_IPV4));
200     }
201
202     /**
203      * Tests {@link SimpleMapCache#removeAuthenticationKey} method.
204      */
205     @Test
206     public void removeAuthenticationKeyTest() {
207         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
208
209         simpleMapCache.removeAuthenticationKey(EID_IPV4);
210         Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.AUTH_KEY);
211     }
212
213     /**
214      * Tests {@link SimpleMapCache#removeAuthenticationKey} method with no VNI_100 table.
215      */
216     @Test
217     public void removeAuthenticationKeyTest_withNoVniTable() {
218         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
219
220         simpleMapCache.removeAuthenticationKey(EID_IPV4);
221         Mockito.verify(tableMock, Mockito.never()).removeSpecific(Mockito.any(Eid.class), Mockito.anyString());
222     }
223
224     /**
225      * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with maskable address.
226      */
227     @Test
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);
233
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);
239
240         Mockito.verify(daoMock).getBest(Mockito.any(Eid.class));
241     }
242
243     /**
244      * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with non maskable address.
245      */
246     @Test
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);
251
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);
257
258         Mockito.verify(daoMock).getBest(Mockito.any(Eid.class));
259     }
260
261     /**
262      * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with null daoEntry.
263      */
264     @Test
265     public void getAllXtrIdMappings_withNullEntry() {
266         Mockito.when(daoMock.getBest(Mockito.any(Eid.class))).thenReturn(null);
267
268         assertNull(simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI));
269         Mockito.verify(daoMock, Mockito.times(1)).getBest(Mockito.any(Eid.class));
270     }
271
272     /**
273      * Tests {@link SimpleMapCache#getMappingLpmEid} method.
274      */
275     @Test
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
284
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
292
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);
296
297         // with non-expired mapping record
298         assertEquals(mappingRecord, simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, XTR_ID));
299     }
300
301     /**
302      * Tests {@link SimpleMapCache#getMappingLpmEid} method with null XtrId.
303      */
304     @Test
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);
313
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);
318     }
319
320     /**
321      * Tests {@link SimpleMapCache#getMapping} method with maskable eid.
322      */
323     @Test
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);
330
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);
335
336         simpleMapCache.getMapping(null, ipv4PrefixEid, XTR_ID);
337         Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
338     }
339
340     /**
341      * Tests {@link SimpleMapCache#getMapping} method with maskable eid and entry not found.
342      */
343     @Test
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);
347
348         assertNull(simpleMapCache.getMapping(null, EID_IPV4_PREFIX_DST, XTR_ID));
349     }
350
351     /**
352      * Tests {@link SimpleMapCache#getMapping} method with non-maskable eid.
353      */
354     @Test
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);
360
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);
364
365         simpleMapCache.getMapping(null, EID_IPV4, XTR_ID);
366         Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
367     }
368
369     /**
370      * Tests {@link SimpleMapCache#getMapping} method with non-maskable eid and entry not found.
371      */
372     @Test
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);
376
377         assertNull(simpleMapCache.getMapping(null, EID_IPV4, XTR_ID));
378     }
379
380     /**
381      * Tests {@link SimpleMapCache#getMapping} method with Eid VNI_100 == null.
382      */
383     @Test
384     public void getVniTableTest_withVniNull() {
385         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
386
387         simpleMapCache.getMapping(null, EID_IPV4_PREFIX_2, XTR_ID);
388         Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
389     }
390
391     /**
392      * Tests {@link SimpleMapCache#getVniTable} method with Eid VNI_100 == 100L.
393      */
394     @Test
395     public void getVniTableTest_withVniNotNull() {
396         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
397
398         simpleMapCache.getMapping(null, EID_IPV4_PREFIX_1_VNI, XTR_ID);
399         Mockito.verify(daoMock).getSpecific(VNI_100, SubKeys.VNI);
400     }
401
402     /**
403      * Tests {@link SimpleMapCache#updateMappingRegistration} method.
404      */
405     @Test
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);
411
412         simpleMapCache.updateMappingRegistration(EID_IPV4, null);
413         Mockito.verify(entryMock).put(Mockito.eq(SubKeys.REGDATE), Mockito.any(Date.class));
414     }
415
416     /**
417      * Tests {@link SimpleMapCache#updateMappingRegistration} method with no VNI_100 table.
418      */
419     @Test
420     public void updateMappingRegistrationTest_withNullVniTable() {
421         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
422
423         simpleMapCache.updateMappingRegistration(EID_IPV4, null);
424         Mockito.verifyZeroInteractions(tableMock);
425     }
426
427     /**
428      * Tests {@link SimpleMapCache#addData} method.
429      */
430     @Test
431     public void addDataTest() {
432         final Object dummyData = "dummy-data";
433         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
434
435         simpleMapCache.addData(EID_IPV4, SubKeys.RECORD, dummyData);
436         Mockito.verify(tableMock).put(NORMALIZED_EID_IPV4, new MappingEntry<>(SubKeys.RECORD, dummyData));
437     }
438
439     /**
440      * Tests {@link SimpleMapCache#getData} method.
441      */
442     @Test
443     public void getDataTest() {
444         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
445
446         simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
447         Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
448     }
449
450     /**
451      * Tests {@link SimpleMapCache#getData} method with no VNI_100 table.
452      */
453     @Test
454     public void getDataTest_withNullVniTable() {
455         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
456
457         simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
458         Mockito.verifyNoMoreInteractions(tableMock);
459     }
460
461     /**
462      * Tests {@link SimpleMapCache#removeData} method.
463      */
464     @Test
465     public void removeDataTest() {
466         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
467
468         simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
469         Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
470     }
471
472     /**
473      * Tests {@link SimpleMapCache#removeData} method with no VNI_100 table.
474      */
475     @Test
476     public void removeDataTest_withNullTable() {
477         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
478
479         simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
480         Mockito.verifyNoMoreInteractions(tableMock);
481     }
482
483     /**
484      * Tests {@link SimpleMapCache#addMapping} method with mapping merge allowed.
485      */
486     @Test
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));
493
494         PowerMockito.mockStatic(MappingMergeUtil.class);
495         PowerMockito.when(MappingMergeUtil.mergeXtrIdMappings(Mockito.anyList(), Mockito.anyList(), Mockito.anySet()))
496                 .thenReturn(getDefaultMappingRecordBuilder().build());
497
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()));
504     }
505
506     /**
507      * Tests {@link SimpleMapCache#addMapping} method with mapping merge false.
508      */
509     @Test
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));
515
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));
518
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));
523     }
524
525     /**
526      * Tests {@link SimpleMapCache#addMapping} method where no interaction is expected with the dao.
527      */
528     @Test
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);
534
535         Mockito.verifyZeroInteractions(daoMock);
536     }
537
538     /**
539      * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null.
540      */
541     @Test
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);
545
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);
549     }
550
551     /**
552      * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null, table == null.
553      */
554     @Test
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);
559
560         simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingRecordMock, true, false); // Eid VNI_100 == null
561         Mockito.verify(daoMock).putNestedTable(VNI_0, SubKeys.VNI);
562     }
563
564     /**
565      * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == 100L, table == null.
566      */
567     @Test
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);
572
573         simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingRecordMock, false, false); // Eid VNI_100 == null
574         Mockito.verify(daoMock).putNestedTable(VNI_100, SubKeys.VNI);
575     }
576
577     private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
578         return new MappingRecordBuilder()
579                 .setEid(EID_IPV4)
580                 .setLocatorRecord(Lists.newArrayList())
581                 .setTimestamp(Long.MAX_VALUE)
582                 .setRecordTtl(10)
583                 .setAction(MappingRecord.Action.NativelyForward)
584                 .setSourceRloc(IP_ADDRESS);
585     }
586 }