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.store.key.impl; |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 18 | |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
Claudine Chiu | 31ad527 | 2016-02-17 20:56:24 +0000 | [diff] [blame] | 22 | import org.onosproject.net.key.DeviceKey; |
| 23 | import org.onosproject.net.key.DeviceKeyId; |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 24 | import org.onosproject.store.service.TestStorageService; |
| 25 | |
| 26 | import java.util.Collection; |
| 27 | |
| 28 | import static org.junit.Assert.*; |
| 29 | |
| 30 | /** |
| 31 | * Test class for DistributedDeviceKeyStore. |
| 32 | */ |
| 33 | public class DistributedDeviceKeyStoreTest { |
| 34 | private DistributedDeviceKeyStore deviceKeyStore; |
| 35 | |
| 36 | final String deviceKeyIdValue = "DeviceKeyId"; |
| 37 | final String deviceKeyLabel = "DeviceKeyLabel"; |
| 38 | final String deviceKeyLabel2 = "DeviceKeyLabel2"; |
| 39 | final String deviceKeySnmpName = "DeviceKeySnmpName"; |
| 40 | |
| 41 | /** |
| 42 | * Sets up the device key store and the storage service test harness. |
| 43 | */ |
| 44 | @Before |
| 45 | public void setUp() { |
| 46 | deviceKeyStore = new DistributedDeviceKeyStore(); |
| 47 | deviceKeyStore.storageService = new TestStorageService(); |
| 48 | deviceKeyStore.setDelegate(event -> { |
| 49 | }); |
| 50 | deviceKeyStore.activate(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Tears down the device key store. |
| 55 | */ |
| 56 | @After |
| 57 | public void tearDown() { |
| 58 | deviceKeyStore.deactivate(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Tests adding, query and removal of a device key. |
| 63 | */ |
| 64 | @Test(expected = NullPointerException.class) |
| 65 | public void testAddNullKey() { |
| 66 | deviceKeyStore.createOrUpdateDeviceKey(null); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Tests adding a device key to the store. This also tests the device key store |
| 71 | * query methods. |
| 72 | */ |
| 73 | @Test |
| 74 | public void testAddKey() { |
| 75 | DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue); |
| 76 | |
| 77 | DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId, |
| 78 | deviceKeyLabel, deviceKeySnmpName); |
| 79 | |
| 80 | // Test to make sure that the device key store is empty |
| 81 | Collection<DeviceKey> deviceKeys = deviceKeyStore.getDeviceKeys(); |
| 82 | assertTrue("The device key set should be empty.", deviceKeys.isEmpty()); |
| 83 | |
| 84 | // Add the new device key to the store |
| 85 | deviceKeyStore.createOrUpdateDeviceKey(deviceKey); |
| 86 | |
| 87 | // Test the getDeviceKeys method to make sure that the new device key exists |
| 88 | deviceKeys = deviceKeyStore.getDeviceKeys(); |
| 89 | assertEquals("There should be one device key in the set.", deviceKeys.size(), 1); |
| 90 | |
| 91 | // Test the getDeviceKey method using the device key unique identifier |
| 92 | deviceKey = deviceKeyStore.getDeviceKey(deviceKeyId); |
| 93 | assertEquals("There should be one device key in the set.", deviceKeys.size(), 1); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests re-adding the same device key to the store but with a different label. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testAddSameKey() { |
| 101 | DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue); |
| 102 | |
| 103 | DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId, |
| 104 | deviceKeyLabel, deviceKeySnmpName); |
| 105 | |
| 106 | // Add the first device key to the store |
| 107 | deviceKeyStore.createOrUpdateDeviceKey(deviceKey); |
| 108 | |
| 109 | // Test the getDeviceKeys method |
| 110 | Collection<DeviceKey> deviceKeys = deviceKeyStore.getDeviceKeys(); |
| 111 | assertEquals("There should be one device key in the set.", deviceKeys.size(), 1); |
| 112 | |
| 113 | // Now let's create a new device key with the same device key identifier as exists in the store. |
| 114 | DeviceKey deviceKey2 = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId, |
| 115 | deviceKeyLabel2, deviceKeySnmpName); |
| 116 | |
| 117 | // Replace the new device key in the store |
| 118 | deviceKeyStore.createOrUpdateDeviceKey(deviceKey2); |
| 119 | |
| 120 | // Test the getDeviceKeys method to ensure that only 1 device key exists in the store. |
| 121 | deviceKeys = deviceKeyStore.getDeviceKeys(); |
| 122 | assertEquals("There should be one device key in the set.", deviceKeys.size(), 1); |
| 123 | |
| 124 | // Test the getDeviceKey method using the device key unique identifier |
| 125 | deviceKey = deviceKeyStore.getDeviceKey(deviceKeyId); |
| 126 | assertNotNull("The device key should not be null.", deviceKey); |
| 127 | assertEquals("The device key label should match.", deviceKeyLabel2, deviceKey.label()); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Tests removal of a device key from the store using the device key identifier. |
| 132 | */ |
| 133 | @Test |
| 134 | public void testRemoveKey() { |
| 135 | DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue); |
| 136 | DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId, |
| 137 | deviceKeyLabel, deviceKeySnmpName); |
| 138 | // Add the new device key to the store |
| 139 | deviceKeyStore.createOrUpdateDeviceKey(deviceKey); |
| 140 | |
| 141 | // Remove the device key from the store |
| 142 | deviceKeyStore.deleteDeviceKey(deviceKeyId); |
| 143 | |
| 144 | // Validate that the device key was removed from the store by querying it. |
| 145 | deviceKey = deviceKeyStore.getDeviceKey(deviceKeyId); |
| 146 | assertNull("The device key set should be empty.", deviceKey); |
| 147 | } |
| 148 | } |