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.HashMap;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
19 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
20 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
24 * Test for {@link HashMapDb} class.
26 public class HashMapDbTest {
28 private HashMapDb map;
31 public void setUp() throws Exception {
32 map = new HashMapDb();
36 * Test insertion of one entry to the {@link HashMapDb}.
39 public void testPutGet_oneEntry() throws Exception {
40 Object dbEntryKey = "dbEntryKey";
41 String mapKey1 = "mapKey1";
42 String mapValue1 = "mapValue1";
43 map.put(dbEntryKey, new MappingEntry<>(mapKey1, mapValue1));
45 Assert.assertEquals("Map entry did not match",
46 Collections.<String, Object>singletonMap(mapKey1, mapValue1),
51 * Test insertion of three entries to the {@link HashMapDb}.
54 public void testPutGet_threeEntries() throws Exception {
55 Object dbEntryKey = "dbEntryKey";
56 String mapKey1 = "mapKey1";
57 String mapValue1 = "mapValue1";
58 String mapKey2 = "mapKey2";
59 String mapValue2 = "mapValue2";
60 String mapKey3 = "mapKey3";
61 String mapValue3 = "mapValue3";
63 new MappingEntry<Object>(mapKey1, mapValue1),
64 new MappingEntry<Object>(mapKey2, mapValue2),
65 new MappingEntry<Object>(mapKey3, mapValue3));
67 Map<String, Object> resultMap = new HashMap<>();
68 resultMap.put(mapKey1, mapValue1);
69 resultMap.put(mapKey2, mapValue2);
70 resultMap.put(mapKey3, mapValue3);
72 Assert.assertEquals("Presence of all 3 entries was expected", resultMap, map.get(dbEntryKey));
76 * Test retrieving a specific value.
79 public void testGetSpecific() throws Exception {
80 Assert.assertNull("Should return the null when map is empty", map.getSpecific("dnEntryKey", "mapEntryKey"));
82 Object dbEntryKey = "dbEntryKey";
83 String mapKey1 = "mapKey1";
84 String mapValue1 = "mapValue1";
85 map.put(dbEntryKey, new MappingEntry<Object>(mapKey1, mapValue1));
87 Assert.assertEquals(mapValue1, map.getSpecific(dbEntryKey, mapKey1));
91 * Test {@link HashMapDb#getBest} with IP prefix.
94 public void testGetBest_withIpPrefix() throws Exception {
95 final Eid ipv4PrefixEid1 = LispAddressUtil.asIpv4PrefixBinaryEid("192.168.0.0" + "/16");
96 final Eid ipv4PrefixEid2 = LispAddressUtil.asIpv4PrefixBinaryEid("192.169.0.0" + "/16");
97 final Eid ipv4PrefixEid3 = LispAddressUtil.asIpv4PrefixBinaryEid("192.168.1.1" + "/32");
98 final String mapSubKey1 = "mapSubKey1";
99 final String mapSubKey2 = "mapSubKey2";
100 final String mapValue1 = "mapValue1";
101 final String mapValue2 = "mapValue2";
103 final MappingEntry<Object> mapEntry1 = new MappingEntry<>(mapSubKey1, mapValue1);
104 final MappingEntry<Object> mapEntry2 = new MappingEntry<>(mapSubKey2, mapValue2);
106 map.put(ipv4PrefixEid1, mapEntry1);
107 map.put(ipv4PrefixEid2, mapEntry2);
109 Map<String, ?> res = map.getBest(ipv4PrefixEid3);
110 Assert.assertEquals(Collections.<String, Object>singletonMap(mapSubKey1, mapValue1), res);
114 * Test {@link HashMapDb#getBest} with non-IP prefix.
117 public void testGetBest_withNonIpPrefix() throws Exception {
118 final Eid mac1 = LispAddressUtil.asMacEid("01:02:03:04:05:06");
119 final Eid mac2 = LispAddressUtil.asMacEid("01:02:03:04:05:07");
120 final String mapSubKey1 = "mapSubKey1";
121 final String mapSubKey2 = "mapSubKey2";
122 final String mapValue1 = "mapValue1";
123 final String mapValue2 = "mapValue2";
124 final MappingEntry<Object> mapEntry1 = new MappingEntry<>(mapSubKey1, mapValue1);
125 final MappingEntry<Object> mapEntry2 = new MappingEntry<>(mapSubKey2, mapValue2);
127 map.put(mac1, mapEntry1);
128 map.put(mac2, mapEntry2);
129 Assert.assertEquals(Collections.<String, Object>singletonMap(mapSubKey1, mapValue1), map.getBest(mac1));
133 * Test {@link HashMapDb#getBestPair} with IP prefix.
136 public void testGetBestPair_withIpPrefix() throws Exception {
137 final Eid ipv4PrefixEid1 = LispAddressUtil.asIpv4PrefixBinaryEid("192.168.0.0" + "/16");
138 final Eid ipv4PrefixEid2 = LispAddressUtil.asIpv4PrefixBinaryEid("192.169.0.0" + "/16");
139 final Eid ipv4PrefixEid3 = LispAddressUtil.asIpv4PrefixBinaryEid("192.168.1.1" + "/32");
140 final String mapSubKey1 = "mapSubKey1";
141 final String mapSubKey2 = "mapSubKey2";
142 final String mapValue1 = "mapValue1";
143 final String mapValue2 = "mapValue2";
144 final MappingEntry<Object> mapEntry1 = new MappingEntry<>(mapSubKey1, mapValue1);
145 final MappingEntry<Object> mapEntry2 = new MappingEntry<>(mapSubKey2, mapValue2);
147 map.put(ipv4PrefixEid1, mapEntry1);
148 map.put(ipv4PrefixEid2, mapEntry2);
150 SimpleImmutableEntry<Eid, Map<String, ?>> res = map.getBestPair(ipv4PrefixEid3);
151 Assert.assertEquals(ipv4PrefixEid1, res.getKey());
152 Assert.assertEquals(Collections.<String, Object>singletonMap(mapSubKey1, mapValue1), res.getValue());
156 * Test {@link HashMapDb#getBestPair} with non-IP prefix.
159 public void testGetBestPair_withNonIpPrefix() throws Exception {
160 final Eid mac1 = LispAddressUtil.asMacEid("01:02:03:04:05:06");
161 final Eid mac2 = LispAddressUtil.asMacEid("01:02:03:04:05:07");
162 final String mapSubKey1 = "mapSubKey1";
163 final String mapSubKey2 = "mapSubKey2";
164 final String mapValue1 = "mapValue1";
165 final String mapValue2 = "mapValue2";
166 final MappingEntry<Object> mapEntry1 = new MappingEntry<>(mapSubKey1, mapValue1);
167 final MappingEntry<Object> mapEntry2 = new MappingEntry<>(mapSubKey2, mapValue2);
170 map.put(mac1, mapEntry1);
171 map.put(mac2, mapEntry2);
172 SimpleImmutableEntry<Eid, Map<String, ?>> res = map.getBestPair(mac1);
173 Assert.assertEquals(mac1, res.getKey());
174 Assert.assertEquals(Collections.<String, Object>singletonMap(mapSubKey1, mapValue1), res.getValue());
178 public void testGetAll() throws Exception {
179 Object dbEntryKey = "dbEntryKey";
180 String mapKey1 = "mapKey1";
181 String mapValue1 = "mapValue1";
182 String mapKey2 = "mapKey2";
183 String mapValue2 = "mapValue2";
184 String mapKey3 = "mapKey3";
185 String mapValue3 = "mapValue3";
187 new MappingEntry<Object>(mapKey1, mapValue1),
188 new MappingEntry<Object>(mapKey2, mapValue2),
189 new MappingEntry<Object>(mapKey3, mapValue3));
191 Map mapSample = new HashMap<>();
192 mapSample.put(mapKey1, mapValue1);
193 mapSample.put(mapKey2, mapValue2);
194 mapSample.put(mapKey3, mapValue3);
195 Map mapResult = new HashMap<>();
196 map.getAll((keyId, valueKey, value) -> {
197 Assert.assertEquals(dbEntryKey, keyId);
198 mapResult.put(valueKey, value);
200 Assert.assertEquals(mapSample, mapResult);
204 public void testRemove() throws Exception {
205 Object dbEntryKey = "dbEntryKey";
206 String mapKey1 = "mapKey1";
207 String mapValue1 = "mapValue1";
209 new MappingEntry<Object>(mapKey1, mapValue1));
210 map.remove(dbEntryKey);
211 Assert.assertNull("DB should be empty after entry removal", map.get(dbEntryKey));
215 * Test {@link HashMapDb#remove} with IP-prefix.
218 public void testRemove_withIpPrefix() throws Exception {
219 final Eid ipv4PrefixEid1 = LispAddressUtil.asIpv4PrefixBinaryEid("192.168.0.0" + "/16");
220 final String mapSubKey1 = "mapSubKey1";
221 final String mapValue1 = "mapValue1";
223 final MappingEntry<Object> mapEntry1 = new MappingEntry<>(mapSubKey1, mapValue1);
225 map.put(ipv4PrefixEid1, mapEntry1);
226 map.remove(ipv4PrefixEid1);
228 Assert.assertNull(map.getBest(ipv4PrefixEid1));
229 Assert.assertNull(map.getBestPair(ipv4PrefixEid1));
233 public void testRemoveSpecific() throws Exception {
234 Object dbEntryKey = "dbEntryKey";
235 String mapKey1 = "mapKey1";
236 String mapValue1 = "mapValue1";
237 String mapKey2 = "mapKey2";
238 String mapValue2 = "mapValue2";
239 String mapKey3 = "mapKey3";
240 String mapValue3 = "mapValue3";
242 new MappingEntry<Object>(mapKey1, mapValue1),
243 new MappingEntry<Object>(mapKey2, mapValue2),
244 new MappingEntry<Object>(mapKey3, mapValue3));
246 map.removeSpecific(dbEntryKey, mapKey1);
247 Assert.assertNull("Entry should not be present in DB", map.getSpecific(dbEntryKey, mapKey1));
249 map.removeSpecific(dbEntryKey, mapKey2);
250 Assert.assertNull("Entry should not be present in DB", map.getSpecific(dbEntryKey, mapKey2));
252 map.removeSpecific(dbEntryKey, mapKey3);
253 Assert.assertNull("Entry should not be present in DB", map.getSpecific(dbEntryKey, mapKey3));
255 Assert.assertNull("MapEntry should not be present after removal the last entry", map.get(dbEntryKey));
259 public void testRemoveAll() throws Exception {
260 Object dbEntryKey1 = "dbEntryKey";
261 map.put(dbEntryKey1, new MappingEntry<>("mapKey1", "mapValue1"));
262 Object dbEntryKey2 = "dbEntryKey";
263 map.put(dbEntryKey2, new MappingEntry<>("mapKey2", "mapValue2"));
266 map.getAll((keyId, valueKey, value) -> Assert.fail("DB should be empty"));
270 public void testClose() throws Exception {
271 Object dbEntryKey1 = "dbEntryKey";
272 map.put(dbEntryKey1, new MappingEntry<>("mapKey1", "mapValue1"));
273 Object dbEntryKey2 = "dbEntryKey";
274 map.put(dbEntryKey2, new MappingEntry<>("mapKey2", "mapValue2"));
277 map.getAll((keyId, valueKey, value) -> Assert.fail("DB should be empty"));
281 public void testPutNestedTable_newEntry() throws Exception {
282 Object dbEntryKey = "dbEntryKey";
283 String mapKey1 = "mapKey1";
284 map.putNestedTable(dbEntryKey, mapKey1);
285 Assert.assertTrue(map.getSpecific(dbEntryKey, mapKey1) instanceof ILispDAO);
289 public void testPutNestedTable_entryExists() throws Exception {
290 Object dbEntryKey = "dbEntryKey";
291 String mapKey1 = "mapKey1";
292 ILispDAO mapValue1 = new HashMapDb();
293 map.put(dbEntryKey, new MappingEntry<>(mapKey1, mapValue1));
294 Assert.assertEquals(mapValue1, map.putNestedTable(dbEntryKey, mapKey1));
295 Assert.assertEquals(mapValue1, map.getSpecific(dbEntryKey, mapKey1));
299 public void testPutTable_newEntry() throws Exception {
300 Object dbEntryKey = "tables";
301 String mapKey1 = "mapKey1";
302 map.putTable(mapKey1);
303 Assert.assertTrue(map.getSpecific(dbEntryKey, mapKey1) instanceof ILispDAO);
307 public void testPutTable_entryExists() throws Exception {
308 Object dbEntryKey = "tables";
309 String mapKey1 = "mapKey1";
310 ILispDAO mapValue1 = new HashMapDb();
311 map.put(dbEntryKey, new MappingEntry<>(mapKey1, mapValue1));
312 Assert.assertEquals(mapValue1, map.putTable(mapKey1));
313 Assert.assertEquals(mapValue1, map.getSpecific(dbEntryKey, mapKey1));