Checkstyle: fix issues and enforce on inmemorydb
[lispflowmapping.git] / mappingservice / inmemorydb / src / test / java / org / opendaylight / lispflowmapping / inmemorydb / HashMapDbTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc.  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
9 package org.opendaylight.lispflowmapping.inmemorydb;
10
11 import java.util.AbstractMap.SimpleImmutableEntry;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.concurrent.TimeUnit;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
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.util.LispAddressUtil;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
25
26 /**
27  * Test for {@link HashMapDb} class.
28  */
29 public class HashMapDbTest {
30
31     private HashMapDb map;
32
33     @Before
34     public void setUp() throws Exception {
35         map = new HashMapDb();
36     }
37
38     /**
39      * Test insertion of one entry to the {@link HashMapDb}.
40      */
41     @Test
42     public void testPutGet_oneEntry() throws Exception {
43         Object dbEntryKey = "dbEntryKey";
44         String mapKey1 = "mapKey1";
45         String mapValue1 = "mapValue1";
46         map.put(dbEntryKey,  new MappingEntry<>(mapKey1, mapValue1));
47
48         Assert.assertEquals("Map entry did not match",
49                 Collections.<String, Object>singletonMap(mapKey1, mapValue1),
50                 map.get(dbEntryKey));
51     }
52
53     /**
54      * Test insertion of three entries to the {@link HashMapDb}.
55      */
56     @Test
57     public void testPutGet_threeEntries() throws Exception {
58         Object dbEntryKey = "dbEntryKey";
59         String mapKey1 = "mapKey1";
60         String mapValue1 = "mapValue1";
61         String mapKey2 = "mapKey2";
62         String mapValue2 = "mapValue2";
63         String mapKey3 = "mapKey3";
64         String mapValue3 = "mapValue3";
65         map.put(dbEntryKey,
66                 new MappingEntry<Object>(mapKey1, mapValue1),
67                 new MappingEntry<Object>(mapKey2, mapValue2),
68                 new MappingEntry<Object>(mapKey3, mapValue3));
69
70         Map<String, Object> resultMap = new HashMap<>();
71         resultMap.put(mapKey1, mapValue1);
72         resultMap.put(mapKey2, mapValue2);
73         resultMap.put(mapKey3, mapValue3);
74
75         Assert.assertEquals("Presence of all 3 entries was expected", resultMap, map.get(dbEntryKey));
76     }
77
78     /**
79      * Test retrieving a specific value.
80      */
81     @Test
82     public void testGetSpecific() throws Exception {
83         Assert.assertNull("Should return the null when map is empty", map.getSpecific("dnEntryKey", "mapEntryKey"));
84
85         Object dbEntryKey = "dbEntryKey";
86         String mapKey1 = "mapKey1";
87         String mapValue1 = "mapValue1";
88         map.put(dbEntryKey, new MappingEntry<Object>(mapKey1, mapValue1));
89
90         Assert.assertEquals(mapValue1, map.getSpecific(dbEntryKey, mapKey1));
91     }
92
93     /**
94      * Test {@link HashMapDb#getBest} with IP prefix.
95      */
96     @Test
97     public void testGetBest_withIpPrefix() throws Exception {
98         final Eid ipv4PrefixEid1 = LispAddressUtil.asIpv4PrefixBinaryEid("192.168.0.0" + "/16");
99         final Eid ipv4PrefixEid2 = LispAddressUtil.asIpv4PrefixBinaryEid("192.169.0.0" + "/16");
100         final Eid ipv4PrefixEid3 = LispAddressUtil.asIpv4PrefixBinaryEid("192.168.1.1" + "/32");
101         final String mapSubKey1 = "mapSubKey1";
102         final String mapSubKey2 = "mapSubKey2";
103         final String mapValue1 = "mapValue1";
104         final String mapValue2 = "mapValue2";
105
106         final MappingEntry<Object> mapEntry1 = new MappingEntry<>(mapSubKey1, mapValue1);
107         final MappingEntry<Object> mapEntry2 = new MappingEntry<>(mapSubKey2, mapValue2);
108
109         map.put(ipv4PrefixEid1, mapEntry1);
110         map.put(ipv4PrefixEid2, mapEntry2);
111
112         Map<String, ?> res = map.getBest(ipv4PrefixEid3);
113         Assert.assertEquals(Collections.<String, Object>singletonMap(mapSubKey1, mapValue1), res);
114     }
115
116     /**
117      * Test {@link HashMapDb#getBest} with non-IP prefix.
118      */
119     @Test
120     public void testGetBest_withNonIpPrefix() throws Exception {
121         final Eid mac1 = LispAddressUtil.asMacEid("01:02:03:04:05:06");
122         final Eid mac2 = LispAddressUtil.asMacEid("01:02:03:04:05:07");
123         final String mapSubKey1 = "mapSubKey1";
124         final String mapSubKey2 = "mapSubKey2";
125         final String mapValue1 = "mapValue1";
126         final String mapValue2 = "mapValue2";
127         final MappingEntry<Object> mapEntry1 = new MappingEntry<>(mapSubKey1, mapValue1);
128         final MappingEntry<Object> mapEntry2 = new MappingEntry<>(mapSubKey2, mapValue2);
129
130         map.put(mac1, mapEntry1);
131         map.put(mac2, mapEntry2);
132         Assert.assertEquals(Collections.<String, Object>singletonMap(mapSubKey1, mapValue1), map.getBest(mac1));
133     }
134
135     /**
136      * Test {@link HashMapDb#getBestPair} with IP prefix.
137      */
138     @Test
139     public void testGetBestPair_withIpPrefix() throws Exception {
140         final Eid ipv4PrefixEid1 = LispAddressUtil.asIpv4PrefixBinaryEid("192.168.0.0" + "/16");
141         final Eid ipv4PrefixEid2 = LispAddressUtil.asIpv4PrefixBinaryEid("192.169.0.0" + "/16");
142         final Eid ipv4PrefixEid3 = LispAddressUtil.asIpv4PrefixBinaryEid("192.168.1.1" + "/32");
143         final String mapSubKey1 = "mapSubKey1";
144         final String mapSubKey2 = "mapSubKey2";
145         final String mapValue1 = "mapValue1";
146         final String mapValue2 = "mapValue2";
147         final MappingEntry<Object> mapEntry1 = new MappingEntry<>(mapSubKey1, mapValue1);
148         final MappingEntry<Object> mapEntry2 = new MappingEntry<>(mapSubKey2, mapValue2);
149
150         map.put(ipv4PrefixEid1, mapEntry1);
151         map.put(ipv4PrefixEid2, mapEntry2);
152
153         SimpleImmutableEntry<Eid, Map<String, ?>> res =  map.getBestPair(ipv4PrefixEid3);
154         Assert.assertEquals(ipv4PrefixEid1, res.getKey());
155         Assert.assertEquals(Collections.<String, Object>singletonMap(mapSubKey1, mapValue1), res.getValue());
156     }
157
158     /**
159      * Test {@link HashMapDb#getBestPair} with non-IP prefix.
160      */
161     @Test
162     public void testGetBestPair_withNonIpPrefix() throws Exception {
163         final Eid mac1 = LispAddressUtil.asMacEid("01:02:03:04:05:06");
164         final Eid mac2 = LispAddressUtil.asMacEid("01:02:03:04:05:07");
165         final String mapSubKey1 = "mapSubKey1";
166         final String mapSubKey2 = "mapSubKey2";
167         final String mapValue1 = "mapValue1";
168         final String mapValue2 = "mapValue2";
169         final MappingEntry<Object> mapEntry1 = new MappingEntry<>(mapSubKey1, mapValue1);
170         final MappingEntry<Object> mapEntry2 = new MappingEntry<>(mapSubKey2, mapValue2);
171
172
173         map.put(mac1, mapEntry1);
174         map.put(mac2, mapEntry2);
175         SimpleImmutableEntry<Eid, Map<String, ?>> res =  map.getBestPair(mac1);
176         Assert.assertEquals(mac1, res.getKey());
177         Assert.assertEquals(Collections.<String, Object>singletonMap(mapSubKey1, mapValue1), res.getValue());
178     }
179
180     @Test
181     public void testGetAll() throws Exception {
182         Object dbEntryKey = "dbEntryKey";
183         String mapKey1 = "mapKey1";
184         String mapValue1 = "mapValue1";
185         String mapKey2 = "mapKey2";
186         String mapValue2 = "mapValue2";
187         String mapKey3 = "mapKey3";
188         String mapValue3 = "mapValue3";
189         map.put(dbEntryKey,
190                 new MappingEntry<Object>(mapKey1, mapValue1),
191                 new MappingEntry<Object>(mapKey2, mapValue2),
192                 new MappingEntry<Object>(mapKey3, mapValue3));
193
194         Map mapResult = new HashMap<>();
195         Map mapSample = new HashMap<>();
196         mapSample.put(mapKey1, mapValue1);
197         mapSample.put(mapKey2, mapValue2);
198         mapSample.put(mapKey3, mapValue3);
199         map.getAll((keyId, valueKey, value) -> {
200             Assert.assertEquals(dbEntryKey, keyId);
201             mapResult.put(valueKey, value);
202         });
203         Assert.assertEquals(mapSample, mapResult);
204     }
205
206     @Test
207     public void testRemove() throws Exception {
208         Object dbEntryKey = "dbEntryKey";
209         String mapKey1 = "mapKey1";
210         String mapValue1 = "mapValue1";
211         map.put(dbEntryKey,
212                 new MappingEntry<Object>(mapKey1, mapValue1));
213         map.remove(dbEntryKey);
214         Assert.assertNull("DB should be empty after entry removal", map.get(dbEntryKey));
215     }
216
217     /**
218      * Test {@link HashMapDb#remove} with IP-prefix.
219      */
220     @Test
221     public void testRemove_withIpPrefix() throws Exception {
222         final Eid ipv4PrefixEid1 = LispAddressUtil.asIpv4PrefixBinaryEid("192.168.0.0" + "/16");
223         final String mapSubKey1 = "mapSubKey1";
224         final String mapValue1 = "mapValue1";
225
226         final MappingEntry<Object> mapEntry1 = new MappingEntry<>(mapSubKey1, mapValue1);
227
228         map.put(ipv4PrefixEid1, mapEntry1);
229         map.remove(ipv4PrefixEid1);
230
231         Assert.assertNull(map.getBest(ipv4PrefixEid1));
232         Assert.assertNull(map.getBestPair(ipv4PrefixEid1));
233     }
234
235     @Test
236     public void testRemoveSpecific() throws Exception {
237         Object dbEntryKey = "dbEntryKey";
238         String mapKey1 = "mapKey1";
239         String mapValue1 = "mapValue1";
240         String mapKey2 = "mapKey2";
241         String mapValue2 = "mapValue2";
242         String mapKey3 = "mapKey3";
243         String mapValue3 = "mapValue3";
244         map.put(dbEntryKey,
245                 new MappingEntry<Object>(mapKey1, mapValue1),
246                 new MappingEntry<Object>(mapKey2, mapValue2),
247                 new MappingEntry<Object>(mapKey3, mapValue3));
248
249         map.removeSpecific(dbEntryKey, mapKey1);
250         Assert.assertNull("Entry should not be present in DB", map.getSpecific(dbEntryKey, mapKey1));
251
252         map.removeSpecific(dbEntryKey, mapKey2);
253         Assert.assertNull("Entry should not be present in DB", map.getSpecific(dbEntryKey, mapKey2));
254
255         map.removeSpecific(dbEntryKey, mapKey3);
256         Assert.assertNull("Entry should not be present in DB", map.getSpecific(dbEntryKey, mapKey3));
257
258         Assert.assertEquals("MapEntry should be empty after removal the last entry", 0, map.get(dbEntryKey).size());
259     }
260
261     @Test
262     public void testRemoveAll() throws Exception {
263         Object dbEntryKey1 = "dbEntryKey";
264         map.put(dbEntryKey1, new MappingEntry<>("mapKey1", "mapValue1"));
265         Object dbEntryKey2 = "dbEntryKey";
266         map.put(dbEntryKey2, new MappingEntry<>("mapKey2", "mapValue2"));
267
268         map.removeAll();
269         map.getAll((keyId, valueKey, value) -> Assert.fail("DB should be empty"));
270     }
271
272     @Test
273     public void testCleanOld() throws Exception {
274         map.setRecordTimeOut(240);
275         map.setTimeUnit(TimeUnit.SECONDS);
276
277         Object entry1Key = "entry1Key";
278         String entry1recordKey = SubKeys.RECORD;
279         String entry1recordValue = "record1";
280         String entry1regdateKey = SubKeys.REGDATE;
281         Date entry1regdateValue = new Date();
282
283         Object entry2Key = "entry2Key";
284         String entry2recordKey = SubKeys.RECORD;
285         String entry2recordValue = "record2";
286         String entry2regdateKey = SubKeys.REGDATE;
287         Date entry2regdateValue = new Date(
288                 System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(map.getRecordTimeOut(), map.getTimeUnit()));
289
290         map.put(entry1Key,
291                 new MappingEntry<>(entry1recordKey, entry1recordValue),
292                 new MappingEntry<>(entry1regdateKey, entry1regdateValue));
293         map.put(entry2Key,
294                 new MappingEntry<>(entry2recordKey, entry2recordValue),
295                 new MappingEntry<>(entry2regdateKey, entry2regdateValue));
296         map.cleanOld();
297
298         Assert.assertEquals("Entry should be present in the DB due to its time didn't expired",
299                 entry1recordValue, map.getSpecific(entry1Key, entry1recordKey));
300         Assert.assertNull("Entry should not be present in the DB due to its time expired",
301                 map.getSpecific(entry2Key, entry2recordKey));
302
303     }
304
305     @Test
306     public void testSetGetTimeUnit() throws Exception {
307         TimeUnit timeUnit = TimeUnit.DAYS;
308         map.setTimeUnit(timeUnit);
309         Assert.assertEquals(timeUnit, map.getTimeUnit());
310     }
311
312     @Test
313     public void testSetGetRecordTimeOut() throws Exception {
314         int recordTimeOut = 12345;
315         map.setRecordTimeOut(recordTimeOut);
316         Assert.assertEquals(recordTimeOut, map.getRecordTimeOut());
317     }
318
319     @Test
320     public void testClose() throws Exception {
321         Object dbEntryKey1 = "dbEntryKey";
322         map.put(dbEntryKey1, new MappingEntry<>("mapKey1", "mapValue1"));
323         Object dbEntryKey2 = "dbEntryKey";
324         map.put(dbEntryKey2, new MappingEntry<>("mapKey2", "mapValue2"));
325
326         map.close();
327         map.getAll((keyId, valueKey, value) -> Assert.fail("DB should be empty"));
328     }
329
330     @Test
331     public void testPutNestedTable_newEntry() throws Exception {
332         Object dbEntryKey = "dbEntryKey";
333         String mapKey1 = "mapKey1";
334         map.putNestedTable(dbEntryKey, mapKey1);
335         Assert.assertTrue(map.getSpecific(dbEntryKey, mapKey1) instanceof ILispDAO);
336     }
337
338     @Test
339     public void testPutNestedTable_entryExists() throws Exception {
340         Object dbEntryKey = "dbEntryKey";
341         String mapKey1 = "mapKey1";
342         ILispDAO mapValue1 = new HashMapDb();
343         map.put(dbEntryKey, new MappingEntry<>(mapKey1, mapValue1));
344         Assert.assertEquals(mapValue1, map.putNestedTable(dbEntryKey, mapKey1));
345         Assert.assertEquals(mapValue1, map.getSpecific(dbEntryKey, mapKey1));
346     }
347
348     @Test
349     public void testPutTable_newEntry() throws Exception {
350         Object dbEntryKey = "tables";
351         String mapKey1 = "mapKey1";
352         map.putTable(mapKey1);
353         Assert.assertTrue(map.getSpecific(dbEntryKey, mapKey1) instanceof ILispDAO);
354     }
355
356     @Test
357     public void testPutTable_entryExists() throws Exception {
358         Object dbEntryKey = "tables";
359         String mapKey1 = "mapKey1";
360         ILispDAO mapValue1 = new HashMapDb();
361         map.put(dbEntryKey, new MappingEntry<>(mapKey1, mapValue1));
362         Assert.assertEquals(mapValue1, map.putTable(mapKey1));
363         Assert.assertEquals(mapValue1, map.getSpecific(dbEntryKey, mapKey1));
364     }
365 }