Neon MRI changes
[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.HashMap;
14 import java.util.Map;
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;
22
23 /**
24  * Test for {@link HashMapDb} class.
25  */
26 public class HashMapDbTest {
27
28     private HashMapDb map;
29
30     @Before
31     public void setUp() throws Exception {
32         map = new HashMapDb();
33     }
34
35     /**
36      * Test insertion of one entry to the {@link HashMapDb}.
37      */
38     @Test
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));
44
45         Assert.assertEquals("Map entry did not match",
46                 Collections.<String, Object>singletonMap(mapKey1, mapValue1),
47                 map.get(dbEntryKey));
48     }
49
50     /**
51      * Test insertion of three entries to the {@link HashMapDb}.
52      */
53     @Test
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";
62         map.put(dbEntryKey,
63                 new MappingEntry<Object>(mapKey1, mapValue1),
64                 new MappingEntry<Object>(mapKey2, mapValue2),
65                 new MappingEntry<Object>(mapKey3, mapValue3));
66
67         Map<String, Object> resultMap = new HashMap<>();
68         resultMap.put(mapKey1, mapValue1);
69         resultMap.put(mapKey2, mapValue2);
70         resultMap.put(mapKey3, mapValue3);
71
72         Assert.assertEquals("Presence of all 3 entries was expected", resultMap, map.get(dbEntryKey));
73     }
74
75     /**
76      * Test retrieving a specific value.
77      */
78     @Test
79     public void testGetSpecific() throws Exception {
80         Assert.assertNull("Should return the null when map is empty", map.getSpecific("dnEntryKey", "mapEntryKey"));
81
82         Object dbEntryKey = "dbEntryKey";
83         String mapKey1 = "mapKey1";
84         String mapValue1 = "mapValue1";
85         map.put(dbEntryKey, new MappingEntry<Object>(mapKey1, mapValue1));
86
87         Assert.assertEquals(mapValue1, map.getSpecific(dbEntryKey, mapKey1));
88     }
89
90     /**
91      * Test {@link HashMapDb#getBest} with IP prefix.
92      */
93     @Test
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";
102
103         final MappingEntry<Object> mapEntry1 = new MappingEntry<>(mapSubKey1, mapValue1);
104         final MappingEntry<Object> mapEntry2 = new MappingEntry<>(mapSubKey2, mapValue2);
105
106         map.put(ipv4PrefixEid1, mapEntry1);
107         map.put(ipv4PrefixEid2, mapEntry2);
108
109         Map<String, ?> res = map.getBest(ipv4PrefixEid3);
110         Assert.assertEquals(Collections.<String, Object>singletonMap(mapSubKey1, mapValue1), res);
111     }
112
113     /**
114      * Test {@link HashMapDb#getBest} with non-IP prefix.
115      */
116     @Test
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);
126
127         map.put(mac1, mapEntry1);
128         map.put(mac2, mapEntry2);
129         Assert.assertEquals(Collections.<String, Object>singletonMap(mapSubKey1, mapValue1), map.getBest(mac1));
130     }
131
132     /**
133      * Test {@link HashMapDb#getBestPair} with IP prefix.
134      */
135     @Test
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);
146
147         map.put(ipv4PrefixEid1, mapEntry1);
148         map.put(ipv4PrefixEid2, mapEntry2);
149
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());
153     }
154
155     /**
156      * Test {@link HashMapDb#getBestPair} with non-IP prefix.
157      */
158     @Test
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);
168
169
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());
175     }
176
177     @Test
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";
186         map.put(dbEntryKey,
187                 new MappingEntry<Object>(mapKey1, mapValue1),
188                 new MappingEntry<Object>(mapKey2, mapValue2),
189                 new MappingEntry<Object>(mapKey3, mapValue3));
190
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);
199         });
200         Assert.assertEquals(mapSample, mapResult);
201     }
202
203     @Test
204     public void testRemove() throws Exception {
205         Object dbEntryKey = "dbEntryKey";
206         String mapKey1 = "mapKey1";
207         String mapValue1 = "mapValue1";
208         map.put(dbEntryKey,
209                 new MappingEntry<Object>(mapKey1, mapValue1));
210         map.remove(dbEntryKey);
211         Assert.assertNull("DB should be empty after entry removal", map.get(dbEntryKey));
212     }
213
214     /**
215      * Test {@link HashMapDb#remove} with IP-prefix.
216      */
217     @Test
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";
222
223         final MappingEntry<Object> mapEntry1 = new MappingEntry<>(mapSubKey1, mapValue1);
224
225         map.put(ipv4PrefixEid1, mapEntry1);
226         map.remove(ipv4PrefixEid1);
227
228         Assert.assertNull(map.getBest(ipv4PrefixEid1));
229         Assert.assertNull(map.getBestPair(ipv4PrefixEid1));
230     }
231
232     @Test
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";
241         map.put(dbEntryKey,
242                 new MappingEntry<Object>(mapKey1, mapValue1),
243                 new MappingEntry<Object>(mapKey2, mapValue2),
244                 new MappingEntry<Object>(mapKey3, mapValue3));
245
246         map.removeSpecific(dbEntryKey, mapKey1);
247         Assert.assertNull("Entry should not be present in DB", map.getSpecific(dbEntryKey, mapKey1));
248
249         map.removeSpecific(dbEntryKey, mapKey2);
250         Assert.assertNull("Entry should not be present in DB", map.getSpecific(dbEntryKey, mapKey2));
251
252         map.removeSpecific(dbEntryKey, mapKey3);
253         Assert.assertNull("Entry should not be present in DB", map.getSpecific(dbEntryKey, mapKey3));
254
255         Assert.assertNull("MapEntry should not be present after removal the last entry", map.get(dbEntryKey));
256     }
257
258     @Test
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"));
264
265         map.removeAll();
266         map.getAll((keyId, valueKey, value) -> Assert.fail("DB should be empty"));
267     }
268
269     @Test
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"));
275
276         map.close();
277         map.getAll((keyId, valueKey, value) -> Assert.fail("DB should be empty"));
278     }
279
280     @Test
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);
286     }
287
288     @Test
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));
296     }
297
298     @Test
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);
304     }
305
306     @Test
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));
314     }
315 }