2 * Copyright (c) 2015 Cisco Systems, Inc. All rights reserved.
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
9 package org.opendaylight.lispflowmapping.inmemorydb;
11 import java.util.AbstractMap.SimpleImmutableEntry;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.HashMap;
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;
27 * Test for {@link HashMapDb} class.
29 public class HashMapDbTest {
31 private HashMapDb map;
34 public void setUp() throws Exception {
35 map = new HashMapDb();
39 * Test insertion of one entry to the {@link HashMapDb}.
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));
48 Assert.assertEquals("Map entry did not match",
49 Collections.<String, Object>singletonMap(mapKey1, mapValue1),
54 * Test insertion of three entries to the {@link HashMapDb}.
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";
66 new MappingEntry<Object>(mapKey1, mapValue1),
67 new MappingEntry<Object>(mapKey2, mapValue2),
68 new MappingEntry<Object>(mapKey3, mapValue3));
70 Map<String, Object> resultMap = new HashMap<>();
71 resultMap.put(mapKey1, mapValue1);
72 resultMap.put(mapKey2, mapValue2);
73 resultMap.put(mapKey3, mapValue3);
75 Assert.assertEquals("Presence of all 3 entries was expected", resultMap, map.get(dbEntryKey));
79 * Test retrieving a specific value.
82 public void testGetSpecific() throws Exception {
83 Assert.assertNull("Should return the null when map is empty", map.getSpecific("dnEntryKey", "mapEntryKey"));
85 Object dbEntryKey = "dbEntryKey";
86 String mapKey1 = "mapKey1";
87 String mapValue1 = "mapValue1";
88 map.put(dbEntryKey, new MappingEntry<Object>(mapKey1, mapValue1));
90 Assert.assertEquals(mapValue1, map.getSpecific(dbEntryKey, mapKey1));
94 * Test {@link HashMapDb#getBest} with IP prefix.
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";
106 final MappingEntry<Object> mapEntry1 = new MappingEntry<>(mapSubKey1, mapValue1);
107 final MappingEntry<Object> mapEntry2 = new MappingEntry<>(mapSubKey2, mapValue2);
109 map.put(ipv4PrefixEid1, mapEntry1);
110 map.put(ipv4PrefixEid2, mapEntry2);
112 Map<String, ?> res = map.getBest(ipv4PrefixEid3);
113 Assert.assertEquals(Collections.<String, Object>singletonMap(mapSubKey1, mapValue1), res);
117 * Test {@link HashMapDb#getBest} with non-IP prefix.
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);
130 map.put(mac1, mapEntry1);
131 map.put(mac2, mapEntry2);
132 Assert.assertEquals(Collections.<String, Object>singletonMap(mapSubKey1, mapValue1), map.getBest(mac1));
136 * Test {@link HashMapDb#getBestPair} with IP prefix.
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);
150 map.put(ipv4PrefixEid1, mapEntry1);
151 map.put(ipv4PrefixEid2, mapEntry2);
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());
159 * Test {@link HashMapDb#getBestPair} with non-IP prefix.
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);
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());
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";
190 new MappingEntry<Object>(mapKey1, mapValue1),
191 new MappingEntry<Object>(mapKey2, mapValue2),
192 new MappingEntry<Object>(mapKey3, mapValue3));
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);
203 Assert.assertEquals(mapSample, mapResult);
207 public void testRemove() throws Exception {
208 Object dbEntryKey = "dbEntryKey";
209 String mapKey1 = "mapKey1";
210 String mapValue1 = "mapValue1";
212 new MappingEntry<Object>(mapKey1, mapValue1));
213 map.remove(dbEntryKey);
214 Assert.assertNull("DB should be empty after entry removal", map.get(dbEntryKey));
218 * Test {@link HashMapDb#remove} with IP-prefix.
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";
226 final MappingEntry<Object> mapEntry1 = new MappingEntry<>(mapSubKey1, mapValue1);
228 map.put(ipv4PrefixEid1, mapEntry1);
229 map.remove(ipv4PrefixEid1);
231 Assert.assertNull(map.getBest(ipv4PrefixEid1));
232 Assert.assertNull(map.getBestPair(ipv4PrefixEid1));
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";
245 new MappingEntry<Object>(mapKey1, mapValue1),
246 new MappingEntry<Object>(mapKey2, mapValue2),
247 new MappingEntry<Object>(mapKey3, mapValue3));
249 map.removeSpecific(dbEntryKey, mapKey1);
250 Assert.assertNull("Entry should not be present in DB", map.getSpecific(dbEntryKey, mapKey1));
252 map.removeSpecific(dbEntryKey, mapKey2);
253 Assert.assertNull("Entry should not be present in DB", map.getSpecific(dbEntryKey, mapKey2));
255 map.removeSpecific(dbEntryKey, mapKey3);
256 Assert.assertNull("Entry should not be present in DB", map.getSpecific(dbEntryKey, mapKey3));
258 Assert.assertEquals("MapEntry should be empty after removal the last entry", 0, map.get(dbEntryKey).size());
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"));
269 map.getAll((keyId, valueKey, value) -> Assert.fail("DB should be empty"));
273 public void testCleanOld() throws Exception {
274 map.setRecordTimeOut(240);
275 map.setTimeUnit(TimeUnit.SECONDS);
277 Object entry1Key = "entry1Key";
278 String entry1recordKey = SubKeys.RECORD;
279 String entry1recordValue = "record1";
280 String entry1regdateKey = SubKeys.REGDATE;
281 Date entry1regdateValue = new Date();
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()));
291 new MappingEntry<>(entry1recordKey, entry1recordValue),
292 new MappingEntry<>(entry1regdateKey, entry1regdateValue));
294 new MappingEntry<>(entry2recordKey, entry2recordValue),
295 new MappingEntry<>(entry2regdateKey, entry2regdateValue));
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));
306 public void testSetGetTimeUnit() throws Exception {
307 TimeUnit timeUnit = TimeUnit.DAYS;
308 map.setTimeUnit(timeUnit);
309 Assert.assertEquals(timeUnit, map.getTimeUnit());
313 public void testSetGetRecordTimeOut() throws Exception {
314 int recordTimeOut = 12345;
315 map.setRecordTimeOut(recordTimeOut);
316 Assert.assertEquals(recordTimeOut, map.getRecordTimeOut());
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"));
327 map.getAll((keyId, valueKey, value) -> Assert.fail("DB should be empty"));
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);
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));
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);
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));