Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 3 | * |
| 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 Yuta | 805d96c | 2016-03-17 13:29:09 -0700 | [diff] [blame] | 17 | package org.onosproject.persistence.impl; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.Maps; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 22 | import org.onosproject.store.service.Serializer; |
| 23 | |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import static org.junit.Assert.assertEquals; |
| 28 | import static org.junit.Assert.assertFalse; |
| 29 | import static org.junit.Assert.assertNull; |
| 30 | import static org.junit.Assert.assertTrue; |
| 31 | |
| 32 | /** |
| 33 | * Test suite for Persistent Map. |
| 34 | */ |
HIGUCHI Yuta | 8eb6ba1 | 2016-05-21 15:01:41 -0700 | [diff] [blame] | 35 | public class PersistentMapTest extends MapDBTest { |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 36 | |
HIGUCHI Yuta | 8eb6ba1 | 2016-05-21 15:01:41 -0700 | [diff] [blame] | 37 | private PersistentMap<Integer, Integer> map = null; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 38 | |
| 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 Yuta | 8eb6ba1 | 2016-05-21 15:01:41 -0700 | [diff] [blame] | 46 | //Creates, a map within it and a basic integer serializer |
| 47 | map = new PersistentMap<>(new Serializer() { |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 48 | @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 Yuta | 8eb6ba1 | 2016-05-21 15:01:41 -0700 | [diff] [blame] | 75 | return (T) Integer.valueOf(num); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 76 | } |
Jordan Halterman | 2c83a10 | 2017-08-20 17:11:41 -0700 | [diff] [blame] | 77 | |
| 78 | @Override |
| 79 | public <T> T copy(T object) { |
| 80 | return decode(encode(object)); |
| 81 | } |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 82 | }, fakeDB, "map"); |
| 83 | } |
| 84 | |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 85 | @Test |
| 86 | public void testRemove() throws Exception { |
| 87 | //Checks removal and return values |
| 88 | fillMap(10); |
| 89 | assertEquals(10, map.size()); |
| 90 | for (int i = 0; i < 10; i++) { |
HIGUCHI Yuta | 8eb6ba1 | 2016-05-21 15:01:41 -0700 | [diff] [blame] | 91 | assertEquals("The previous value was wrong.", Integer.valueOf(i), map.remove(i)); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 92 | assertNull("The previous value was wrong.", map.remove(i)); |
| 93 | //(i+1) compensates for base zero. |
| 94 | assertEquals("The size was wrong.", 10 - (i + 1), map.size()); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testSize() throws Exception { |
| 100 | //Checks size values throughout addition and removal |
| 101 | for (int i = 0; i < 10; i++) { |
| 102 | map.put(i, i); |
| 103 | assertEquals("The map size is wrong.", i + 1, map.size()); |
| 104 | } |
| 105 | for (int i = 0; i < 10; i++) { |
| 106 | map.remove(i); |
| 107 | assertEquals("The map size is wrong.", 9 - i, map.size()); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void testIsEmpty() throws Exception { |
| 113 | //Checks empty condition |
| 114 | //asserts that the map starts out empty |
| 115 | assertTrue("Map should be empty", map.isEmpty()); |
| 116 | map.put(1, 1); |
| 117 | assertFalse("Map shouldn't be empty.", map.isEmpty()); |
| 118 | map.remove(1); |
| 119 | assertTrue("Map should be empty", map.isEmpty()); |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void testContains() throws Exception { |
| 124 | //Checks both containsKey and containsValue be aware the implementations vary widely (value does not use mapDB |
| 125 | //due to object '=='being an insufficient check) |
| 126 | for (int i = 0; i < 10; i++) { |
| 127 | assertFalse("Map should not contain the key", map.containsKey(i)); |
| 128 | assertFalse("Map should not contain the value", map.containsValue(i)); |
| 129 | map.put(i, i); |
| 130 | assertTrue("Map should contain the key", map.containsKey(i)); |
| 131 | assertTrue("Map should contain the value", map.containsValue(i)); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void testGet() throws Exception { |
| 137 | //Tests value retrieval and nonexistent key return values |
| 138 | for (int i = 0; i < 10; i++) { |
| 139 | map.put(i, i); |
| 140 | for (int j = 0; j <= i; j++) { |
HIGUCHI Yuta | 8eb6ba1 | 2016-05-21 15:01:41 -0700 | [diff] [blame] | 141 | assertEquals("The value was wrong.", Integer.valueOf(j), map.get(j)); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | assertNull("Null return value for nonexistent keys.", map.get(10)); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testPutAll() throws Exception { |
| 149 | //Tests adding of an outside map |
| 150 | Map<Integer, Integer> testMap = Maps.newHashMap(); |
| 151 | fillMap(10); |
| 152 | map.putAll(testMap); |
| 153 | for (int i = 0; i < 10; i++) { |
| 154 | assertTrue("The map should contain the current 'i' value.", map.containsKey(i)); |
| 155 | assertTrue("The map should contain the current 'i' value.", map.containsValue(i)); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void testClear() throws Exception { |
| 161 | //Tests clearing the map |
| 162 | assertTrue("Map was initialized incorrectly, should be empty.", map.isEmpty()); |
| 163 | fillMap(10); |
| 164 | assertFalse("Map should contain entries now.", map.isEmpty()); |
| 165 | map.clear(); |
| 166 | assertTrue("Map should have been cleared of entries.", map.isEmpty()); |
| 167 | |
| 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void testKeySet() throws Exception { |
| 172 | //Tests key set generation |
| 173 | fillMap(10); |
| 174 | Set<Integer> keys = map.keySet(); |
| 175 | for (int i = 0; i < 10; i++) { |
| 176 | assertTrue("The key set doesn't contain all keys 0-9", keys.contains(i)); |
| 177 | } |
| 178 | assertEquals("The key set has an incorrect number of entries", 10, keys.size()); |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void testValues() throws Exception { |
| 183 | //Tests value set generation |
| 184 | fillMap(10); |
| 185 | Set<Integer> values = (Set<Integer>) map.values(); |
| 186 | for (int i = 0; i < 10; i++) { |
| 187 | assertTrue("The key set doesn't contain all keys 0-9", values.contains(i)); |
| 188 | } |
| 189 | assertEquals("The key set has an incorrect number of entries", 10, values.size()); |
| 190 | } |
| 191 | |
| 192 | @Test |
| 193 | public void testEntrySet() throws Exception { |
| 194 | //Test entry set generation (violates abstraction by knowing the type of the returned entries) |
| 195 | fillMap(10); |
| 196 | Set<Map.Entry<Integer, Integer>> entries = map.entrySet(); |
| 197 | for (int i = 0; i < 10; i++) { |
| 198 | assertTrue("The key set doesn't contain all keys 0-9", entries.contains(Maps.immutableEntry(i, i))); |
| 199 | } |
| 200 | assertEquals("The key set has an incorrect number of entries", 10, entries.size()); |
| 201 | } |
| 202 | |
| 203 | @Test public void testPut() throws Exception { |
| 204 | //Tests insertion behavior (particularly the returning of previous value) |
| 205 | fillMap(10); |
| 206 | for (int i = 0; i < 10; i++) { |
HIGUCHI Yuta | 8eb6ba1 | 2016-05-21 15:01:41 -0700 | [diff] [blame] | 207 | assertEquals("Put should return the previous value", Integer.valueOf(i), map.put(i, i + 1)); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 208 | } |
| 209 | assertNull(map.put(11, 11)); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Populated the map with pairs of integers from (0, 0) up to (numEntries - 1, numEntries -1). |
| 214 | * @param numEntries number of entries to add |
| 215 | */ |
| 216 | private void fillMap(int numEntries) { |
| 217 | for (int i = 0; i < numEntries; i++) { |
| 218 | map.put(i, i); |
| 219 | } |
| 220 | } |
Jonathan Hart | 4bb4b05 | 2015-10-26 18:10:56 -0700 | [diff] [blame] | 221 | } |