Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [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 | |
Claudine Chiu | e014b3a | 2016-02-18 19:56:42 +0000 | [diff] [blame] | 17 | package org.onosproject.net.key.impl; |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.junit.TestTools; |
| 24 | import org.onlab.junit.TestUtils; |
| 25 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 26 | import org.onosproject.event.Event; |
Claudine Chiu | 31ad527 | 2016-02-17 20:56:24 +0000 | [diff] [blame] | 27 | import org.onosproject.net.key.DeviceKey; |
| 28 | import org.onosproject.net.key.DeviceKeyEvent; |
| 29 | import org.onosproject.net.key.DeviceKeyId; |
| 30 | import org.onosproject.net.key.DeviceKeyListener; |
| 31 | import org.onosproject.net.key.DeviceKeyService; |
Claudine Chiu | e014b3a | 2016-02-18 19:56:42 +0000 | [diff] [blame] | 32 | import org.onosproject.store.key.impl.DistributedDeviceKeyStore; |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 33 | import org.onosproject.net.NetTestTools; |
| 34 | import org.onosproject.store.service.TestStorageService; |
| 35 | |
| 36 | import java.util.Collection; |
| 37 | import java.util.List; |
| 38 | |
| 39 | import static org.junit.Assert.*; |
| 40 | |
| 41 | /** |
| 42 | * Tests for DeviceKeyManager. |
| 43 | */ |
| 44 | public class DeviceKeyManagerTest { |
| 45 | |
| 46 | final String deviceKeyIdValue = "DeviceKeyId"; |
| 47 | final String deviceKeyLabel = "DeviceKeyLabel"; |
| 48 | final String deviceKeyLabel2 = "DeviceKeyLabel2"; |
| 49 | final String deviceKeySnmpName = "DeviceKeySnmpName"; |
| 50 | |
| 51 | private DeviceKeyManager manager; |
| 52 | private DeviceKeyService deviceKeyService; |
| 53 | private DistributedDeviceKeyStore deviceKeyStore; |
| 54 | protected TestListener listener = new TestListener(); |
| 55 | |
| 56 | @Before |
| 57 | public void setUp() throws Exception { |
| 58 | deviceKeyStore = new DistributedDeviceKeyStore(); |
| 59 | TestUtils.setField(deviceKeyStore, "storageService", new TestStorageService()); |
| 60 | deviceKeyStore.activate(); |
| 61 | |
| 62 | manager = new DeviceKeyManager(); |
| 63 | manager.store = deviceKeyStore; |
| 64 | manager.addListener(listener); |
| 65 | NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher()); |
| 66 | manager.activate(); |
| 67 | deviceKeyService = manager; |
| 68 | } |
| 69 | |
| 70 | @After |
| 71 | public void tearDown() { |
| 72 | deviceKeyStore.deactivate(); |
| 73 | manager.removeListener(listener); |
| 74 | manager.deactivate(); |
| 75 | NetTestTools.injectEventDispatcher(manager, null); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Tests adding, query and removal of a device key. |
| 80 | */ |
| 81 | @Test(expected = NullPointerException.class) |
| 82 | public void testAddNullKey() { |
| 83 | manager.addKey(null); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Tests adding a device key using the device key manager. |
| 88 | * This also tests the device key manager query methods. |
| 89 | */ |
| 90 | @Test |
| 91 | public void testAddKey() { |
| 92 | DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue); |
| 93 | |
| 94 | DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId, |
| 95 | deviceKeyLabel, deviceKeySnmpName); |
| 96 | |
| 97 | // Test to make sure that the device key store is empty |
| 98 | Collection<DeviceKey> deviceKeys = manager.getDeviceKeys(); |
| 99 | assertTrue("The device key set should be empty.", deviceKeys.isEmpty()); |
| 100 | |
| 101 | // Add the new device key using the device key manager. |
| 102 | manager.addKey(deviceKey); |
| 103 | |
| 104 | // Test the getDeviceKeys method to make sure that the new device key exists |
| 105 | deviceKeys = manager.getDeviceKeys(); |
| 106 | assertEquals("There should be one device key in the set.", deviceKeys.size(), 1); |
| 107 | |
| 108 | // Test the getDeviceKey method using the device key unique identifier |
| 109 | deviceKey = manager.getDeviceKey(deviceKeyId); |
| 110 | assertEquals("There should be one device key in the set.", deviceKeys.size(), 1); |
| 111 | |
| 112 | // Validate that only the DEVICE_KEY_ADDED event was received. |
| 113 | validateEvents(DeviceKeyEvent.Type.DEVICE_KEY_ADDED); |
| 114 | |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Tests re-adding the same device key to the store but with a different label. |
| 119 | */ |
| 120 | @Test |
| 121 | public void testAddSameKey() { |
| 122 | DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue); |
| 123 | |
| 124 | DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId, |
| 125 | deviceKeyLabel, deviceKeySnmpName); |
| 126 | |
| 127 | // Add the first device key via the device key manager |
| 128 | manager.addKey(deviceKey); |
| 129 | |
| 130 | // Test the getDeviceKeys method |
| 131 | Collection<DeviceKey> deviceKeys = manager.getDeviceKeys(); |
| 132 | assertEquals("There should be one device key in the set.", deviceKeys.size(), 1); |
| 133 | |
| 134 | // Now let's create a new device key with the same device key identifier as exists in the store. |
| 135 | DeviceKey deviceKey2 = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId, |
| 136 | deviceKeyLabel2, deviceKeySnmpName); |
| 137 | |
| 138 | // Replace the new device key in the store |
| 139 | manager.addKey(deviceKey2); |
| 140 | |
| 141 | // Test the getDeviceKeys method to ensure that only 1 device key exists in the store. |
| 142 | deviceKeys = manager.getDeviceKeys(); |
| 143 | assertEquals("There should be one device key in the set.", deviceKeys.size(), 1); |
| 144 | |
| 145 | // Test the getDeviceKey method using the device key unique identifier |
| 146 | deviceKey = manager.getDeviceKey(deviceKeyId); |
| 147 | assertNotNull("The device key should not be null.", deviceKey); |
| 148 | assertEquals("The device key label should match.", deviceKeyLabel2, deviceKey.label()); |
| 149 | |
| 150 | // Validate that the following events were received in order, |
| 151 | // DEVICE_KEY_ADDED, DEVICE_KEY_REMOVED, DEVICE_KEY_ADDED. |
| 152 | validateEvents(DeviceKeyEvent.Type.DEVICE_KEY_ADDED, DeviceKeyEvent.Type.DEVICE_KEY_UPDATED); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Tests removal of a device key from the store using the device key identifier. |
| 158 | */ |
| 159 | @Test |
| 160 | public void testRemoveKey() { |
| 161 | DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue); |
| 162 | DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId, |
| 163 | deviceKeyLabel, deviceKeySnmpName); |
| 164 | // Add the new device key using the device key manager |
| 165 | manager.addKey(deviceKey); |
| 166 | |
| 167 | // Remove the device key from the store |
| 168 | manager.removeKey(deviceKeyId); |
| 169 | |
| 170 | // Validate that the device key was removed from the store by querying it. |
| 171 | deviceKey = manager.getDeviceKey(deviceKeyId); |
| 172 | assertNull("The device key set should be empty.", deviceKey); |
| 173 | |
| 174 | // Validate that the following events were received in order, |
| 175 | // DEVICE_KEY_ADDED, DEVICE_KEY_REMOVED. |
| 176 | validateEvents(DeviceKeyEvent.Type.DEVICE_KEY_ADDED, DeviceKeyEvent.Type.DEVICE_KEY_REMOVED); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Method to validate that actual versus expected device key events were |
| 181 | * received correctly. |
| 182 | * |
| 183 | * @param types expected device key events. |
| 184 | */ |
| 185 | private void validateEvents(Enum... types) { |
| 186 | TestTools.assertAfter(100, () -> { |
| 187 | int i = 0; |
| 188 | assertEquals("wrong events received", types.length, listener.events.size()); |
| 189 | for (Event event : listener.events) { |
| 190 | assertEquals("incorrect event type", types[i], event.type()); |
| 191 | i++; |
| 192 | } |
| 193 | listener.events.clear(); |
| 194 | }); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Test listener class to receive device key events. |
| 199 | */ |
| 200 | private static class TestListener implements DeviceKeyListener { |
| 201 | |
| 202 | protected List<DeviceKeyEvent> events = Lists.newArrayList(); |
| 203 | |
| 204 | @Override |
| 205 | public void event(DeviceKeyEvent event) { |
| 206 | events.add(event); |
| 207 | } |
| 208 | |
| 209 | } |
| 210 | } |