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