blob: 69b21df33fd6ecac560bc5230c88265a7a02bf91 [file] [log] [blame]
Aaron Kruglikov92511f22015-10-12 14:39:04 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Aaron Kruglikov92511f22015-10-12 14:39:04 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
HIGUCHI Yuta805d96c2016-03-17 13:29:09 -070017package org.onosproject.persistence.impl;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070018
19import com.google.common.collect.Maps;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070020import org.junit.Before;
21import org.junit.Test;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070022import org.onosproject.store.service.Serializer;
23
Aaron Kruglikov92511f22015-10-12 14:39:04 -070024import java.util.Map;
25import java.util.Set;
26
27import static org.junit.Assert.assertEquals;
28import static org.junit.Assert.assertFalse;
29import static org.junit.Assert.assertNull;
30import static org.junit.Assert.assertTrue;
31
32/**
33 * Test suite for Persistent Map.
34 */
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -070035public class PersistentMapTest extends MapDBTest {
Aaron Kruglikov92511f22015-10-12 14:39:04 -070036
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -070037 private PersistentMap<Integer, Integer> map = null;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070038
39 /**
40 * Set up the database, create a map and a direct executor to handle it.
41 *
42 * @throws Exception if instantiation fails
43 */
44 @Before
45 public void setUp() throws Exception {
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -070046 //Creates, a map within it and a basic integer serializer
47 map = new PersistentMap<>(new Serializer() {
Aaron Kruglikov92511f22015-10-12 14:39:04 -070048 @Override
49 public <T> byte[] encode(T object) {
50 if (object == null) {
51 return null;
52 }
53 int num = (Integer) object;
54 byte[] result = new byte[4];
55
56 result[0] = (byte) (num >> 24);
57 result[1] = (byte) (num >> 16);
58 result[2] = (byte) (num >> 8);
59 result[3] = (byte) num;
60 return result;
61 }
62
63 @Override
64 public <T> T decode(byte[] bytes) {
65 if (bytes == null) {
66 return null;
67 }
68 int num = 0x00000000;
69
70 num = num | bytes[0] << 24;
71 num = num | bytes[1] << 16;
72 num = num | bytes[2] << 8;
73 num = num | bytes[3];
74
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -070075 return (T) Integer.valueOf(num);
Aaron Kruglikov92511f22015-10-12 14:39:04 -070076 }
77 }, fakeDB, "map");
78 }
79
Aaron Kruglikov92511f22015-10-12 14:39:04 -070080 @Test
81 public void testRemove() throws Exception {
82 //Checks removal and return values
83 fillMap(10);
84 assertEquals(10, map.size());
85 for (int i = 0; i < 10; i++) {
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -070086 assertEquals("The previous value was wrong.", Integer.valueOf(i), map.remove(i));
Aaron Kruglikov92511f22015-10-12 14:39:04 -070087 assertNull("The previous value was wrong.", map.remove(i));
88 //(i+1) compensates for base zero.
89 assertEquals("The size was wrong.", 10 - (i + 1), map.size());
90 }
91 }
92
93 @Test
94 public void testSize() throws Exception {
95 //Checks size values throughout addition and removal
96 for (int i = 0; i < 10; i++) {
97 map.put(i, i);
98 assertEquals("The map size is wrong.", i + 1, map.size());
99 }
100 for (int i = 0; i < 10; i++) {
101 map.remove(i);
102 assertEquals("The map size is wrong.", 9 - i, map.size());
103 }
104 }
105
106 @Test
107 public void testIsEmpty() throws Exception {
108 //Checks empty condition
109 //asserts that the map starts out empty
110 assertTrue("Map should be empty", map.isEmpty());
111 map.put(1, 1);
112 assertFalse("Map shouldn't be empty.", map.isEmpty());
113 map.remove(1);
114 assertTrue("Map should be empty", map.isEmpty());
115 }
116
117 @Test
118 public void testContains() throws Exception {
119 //Checks both containsKey and containsValue be aware the implementations vary widely (value does not use mapDB
120 //due to object '=='being an insufficient check)
121 for (int i = 0; i < 10; i++) {
122 assertFalse("Map should not contain the key", map.containsKey(i));
123 assertFalse("Map should not contain the value", map.containsValue(i));
124 map.put(i, i);
125 assertTrue("Map should contain the key", map.containsKey(i));
126 assertTrue("Map should contain the value", map.containsValue(i));
127 }
128 }
129
130 @Test
131 public void testGet() throws Exception {
132 //Tests value retrieval and nonexistent key return values
133 for (int i = 0; i < 10; i++) {
134 map.put(i, i);
135 for (int j = 0; j <= i; j++) {
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -0700136 assertEquals("The value was wrong.", Integer.valueOf(j), map.get(j));
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700137 }
138 }
139 assertNull("Null return value for nonexistent keys.", map.get(10));
140 }
141
142 @Test
143 public void testPutAll() throws Exception {
144 //Tests adding of an outside map
145 Map<Integer, Integer> testMap = Maps.newHashMap();
146 fillMap(10);
147 map.putAll(testMap);
148 for (int i = 0; i < 10; i++) {
149 assertTrue("The map should contain the current 'i' value.", map.containsKey(i));
150 assertTrue("The map should contain the current 'i' value.", map.containsValue(i));
151 }
152 }
153
154 @Test
155 public void testClear() throws Exception {
156 //Tests clearing the map
157 assertTrue("Map was initialized incorrectly, should be empty.", map.isEmpty());
158 fillMap(10);
159 assertFalse("Map should contain entries now.", map.isEmpty());
160 map.clear();
161 assertTrue("Map should have been cleared of entries.", map.isEmpty());
162
163 }
164
165 @Test
166 public void testKeySet() throws Exception {
167 //Tests key set generation
168 fillMap(10);
169 Set<Integer> keys = map.keySet();
170 for (int i = 0; i < 10; i++) {
171 assertTrue("The key set doesn't contain all keys 0-9", keys.contains(i));
172 }
173 assertEquals("The key set has an incorrect number of entries", 10, keys.size());
174 }
175
176 @Test
177 public void testValues() throws Exception {
178 //Tests value set generation
179 fillMap(10);
180 Set<Integer> values = (Set<Integer>) map.values();
181 for (int i = 0; i < 10; i++) {
182 assertTrue("The key set doesn't contain all keys 0-9", values.contains(i));
183 }
184 assertEquals("The key set has an incorrect number of entries", 10, values.size());
185 }
186
187 @Test
188 public void testEntrySet() throws Exception {
189 //Test entry set generation (violates abstraction by knowing the type of the returned entries)
190 fillMap(10);
191 Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
192 for (int i = 0; i < 10; i++) {
193 assertTrue("The key set doesn't contain all keys 0-9", entries.contains(Maps.immutableEntry(i, i)));
194 }
195 assertEquals("The key set has an incorrect number of entries", 10, entries.size());
196 }
197
198 @Test public void testPut() throws Exception {
199 //Tests insertion behavior (particularly the returning of previous value)
200 fillMap(10);
201 for (int i = 0; i < 10; i++) {
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -0700202 assertEquals("Put should return the previous value", Integer.valueOf(i), map.put(i, i + 1));
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700203 }
204 assertNull(map.put(11, 11));
205 }
206
207 /**
208 * Populated the map with pairs of integers from (0, 0) up to (numEntries - 1, numEntries -1).
209 * @param numEntries number of entries to add
210 */
211 private void fillMap(int numEntries) {
212 for (int i = 0; i < numEntries; i++) {
213 map.put(i, i);
214 }
215 }
Jonathan Hart4bb4b052015-10-26 18:10:56 -0700216}