Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -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 | */ |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 16 | package org.onosproject.store.config.impl; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 21 | import org.onosproject.net.config.Config; |
| 22 | import org.onosproject.net.config.ConfigFactory; |
| 23 | import org.onosproject.net.config.SubjectFactory; |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 24 | import org.onosproject.store.service.TestStorageService; |
| 25 | |
| 26 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | |
| 28 | import static org.hamcrest.Matchers.hasSize; |
| 29 | import static org.hamcrest.Matchers.is; |
| 30 | import static org.hamcrest.Matchers.notNullValue; |
| 31 | import static org.hamcrest.Matchers.nullValue; |
| 32 | import static org.junit.Assert.assertThat; |
| 33 | |
Deepa Vaddireddy | 0c49b60 | 2016-06-02 12:19:07 +0530 | [diff] [blame] | 34 | import java.util.Set; |
| 35 | |
| 36 | |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 37 | public class DistributedNetworkConfigStoreTest { |
| 38 | private DistributedNetworkConfigStore configStore; |
| 39 | |
| 40 | /** |
| 41 | * Sets up the config store and the storage service test harness. |
| 42 | */ |
| 43 | @Before |
| 44 | public void setUp() { |
| 45 | configStore = new DistributedNetworkConfigStore(); |
| 46 | configStore.storageService = new TestStorageService(); |
| 47 | configStore.setDelegate(event -> { }); |
| 48 | configStore.activate(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Tears down the config store. |
| 53 | */ |
| 54 | @After |
| 55 | public void tearDown() { |
| 56 | configStore.deactivate(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Config class for testing. |
| 61 | */ |
| 62 | public class BasicConfig extends Config<String> { } |
| 63 | |
| 64 | /** |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 65 | * Config class for testing. |
| 66 | */ |
| 67 | public class BasicIntConfig extends Config<Integer> { } |
| 68 | |
| 69 | /** |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 70 | * Config factory class for testing. |
| 71 | */ |
| 72 | public class MockConfigFactory extends ConfigFactory<String, BasicConfig> { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 73 | protected MockConfigFactory(Class<BasicConfig> configClass, String configKey) { |
| 74 | super(new MockSubjectFactory("strings"), configClass, configKey); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 75 | } |
| 76 | @Override |
| 77 | public BasicConfig createConfig() { |
| 78 | return new BasicConfig(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 83 | * Config factory class for testing. |
| 84 | */ |
| 85 | public class MockIntConfigFactory extends ConfigFactory<Integer, BasicIntConfig> { |
| 86 | protected MockIntConfigFactory(Class<BasicIntConfig> configClass, String configKey) { |
| 87 | super(new MockIntSubjectFactory("strings"), configClass, configKey); |
| 88 | } |
| 89 | @Override |
| 90 | public BasicIntConfig createConfig() { |
| 91 | return new BasicIntConfig(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 96 | * Subject factory class for testing. |
| 97 | */ |
| 98 | public class MockSubjectFactory extends SubjectFactory<String> { |
| 99 | protected MockSubjectFactory(String subjectClassKey) { |
| 100 | super(String.class, subjectClassKey); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public String createSubject(String subjectKey) { |
| 105 | return subjectKey; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 110 | * Subject factory class for testing. |
| 111 | */ |
| 112 | public class MockIntSubjectFactory extends SubjectFactory<Integer> { |
| 113 | protected MockIntSubjectFactory(String subjectClassKey) { |
| 114 | super(Integer.class, subjectClassKey); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public Integer createSubject(String subjectKey) { |
| 119 | return Integer.parseInt(subjectKey); |
| 120 | } |
| 121 | } |
| 122 | /** |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 123 | * Tests creation, query and removal of a config. |
| 124 | */ |
| 125 | @Test |
| 126 | public void testCreateConfig() { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 127 | configStore.addConfigFactory(new MockConfigFactory(BasicConfig.class, "config1")); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 128 | |
| 129 | configStore.createConfig("config1", BasicConfig.class); |
| 130 | assertThat(configStore.getConfigClasses("config1"), hasSize(1)); |
| 131 | assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1)); |
| 132 | assertThat(configStore.getSubjects(String.class), hasSize(1)); |
| 133 | |
| 134 | BasicConfig queried = configStore.getConfig("config1", BasicConfig.class); |
| 135 | assertThat(queried, notNullValue()); |
| 136 | |
| 137 | configStore.clearConfig("config1", BasicConfig.class); |
| 138 | assertThat(configStore.getConfigClasses("config1"), hasSize(0)); |
| 139 | assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(0)); |
| 140 | assertThat(configStore.getSubjects(String.class), hasSize(0)); |
| 141 | |
| 142 | BasicConfig queriedAfterClear = configStore.getConfig("config1", BasicConfig.class); |
| 143 | assertThat(queriedAfterClear, nullValue()); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Tests creation, query and removal of a factory. |
| 148 | */ |
| 149 | @Test |
| 150 | public void testCreateFactory() { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 151 | MockConfigFactory mockFactory = new MockConfigFactory(BasicConfig.class, "config1"); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 152 | |
| 153 | assertThat(configStore.getConfigFactory(BasicConfig.class), nullValue()); |
| 154 | |
| 155 | configStore.addConfigFactory(mockFactory); |
| 156 | assertThat(configStore.getConfigFactory(BasicConfig.class), is(mockFactory)); |
| 157 | |
| 158 | configStore.removeConfigFactory(mockFactory); |
| 159 | assertThat(configStore.getConfigFactory(BasicConfig.class), nullValue()); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Tests applying a config. |
| 164 | */ |
| 165 | @Test |
| 166 | public void testApplyConfig() { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 167 | configStore.addConfigFactory(new MockConfigFactory(BasicConfig.class, "config1")); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 168 | |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 169 | configStore.applyConfig("subject", BasicConfig.class, new ObjectMapper().createObjectNode()); |
| 170 | assertThat(configStore.getConfigClasses("subject"), hasSize(1)); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 171 | assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1)); |
| 172 | assertThat(configStore.getSubjects(String.class), hasSize(1)); |
| 173 | } |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Tests inserting a pending configuration. |
| 177 | */ |
| 178 | @Test |
| 179 | public void testPendingConfig() { |
| 180 | configStore.queueConfig("subject", "config1", new ObjectMapper().createObjectNode()); |
| 181 | configStore.addConfigFactory(new MockConfigFactory(BasicConfig.class, "config1")); |
| 182 | |
| 183 | assertThat(configStore.getConfigClasses("subject"), hasSize(1)); |
| 184 | assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1)); |
| 185 | assertThat(configStore.getSubjects(String.class), hasSize(1)); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Tests inserting a pending configuration for the same key, different subject. |
| 190 | */ |
| 191 | @Test |
| 192 | public void testPendingConfigSameKey() { |
| 193 | configStore.queueConfig("subject", "config1", new ObjectMapper().createObjectNode()); |
| 194 | configStore.queueConfig(123, "config1", new ObjectMapper().createObjectNode()); |
| 195 | configStore.addConfigFactory(new MockConfigFactory(BasicConfig.class, "config1")); |
| 196 | |
| 197 | assertThat(configStore.getConfigClasses("subject"), hasSize(1)); |
| 198 | assertThat(configStore.getConfigClasses(123), hasSize(0)); |
| 199 | assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1)); |
| 200 | assertThat(configStore.getSubjects(String.class), hasSize(1)); |
| 201 | |
| 202 | configStore.addConfigFactory(new MockIntConfigFactory(BasicIntConfig.class, "config1")); |
| 203 | |
| 204 | assertThat(configStore.getConfigClasses("subject"), hasSize(1)); |
| 205 | assertThat(configStore.getConfigClasses(123), hasSize(1)); |
| 206 | assertThat(configStore.getSubjects(Integer.class, BasicIntConfig.class), hasSize(1)); |
| 207 | assertThat(configStore.getSubjects(Integer.class), hasSize(1)); |
| 208 | } |
Deepa Vaddireddy | 0c49b60 | 2016-06-02 12:19:07 +0530 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * Tests removal of config including queued. |
| 212 | */ |
| 213 | @Test |
| 214 | public void testRemoveConfig() { |
| 215 | |
| 216 | configStore.addConfigFactory(new MockConfigFactory(BasicConfig.class, "config1")); |
| 217 | configStore.queueConfig("subject", "config2", new ObjectMapper().createObjectNode()); |
| 218 | configStore.queueConfig(123, "config2", new ObjectMapper().createObjectNode()); |
| 219 | configStore.applyConfig("subject1", BasicConfig.class, new ObjectMapper().createObjectNode()); |
| 220 | |
| 221 | configStore.clearConfig(); |
| 222 | |
| 223 | Set<String> subjects = configStore.getSubjects(String.class); |
| 224 | assertThat(subjects.size(), is(0)); |
| 225 | |
| 226 | configStore.addConfigFactory(new MockConfigFactory(BasicConfig.class, "config1")); |
| 227 | configStore.queueConfig("subject", "config3", new ObjectMapper().createObjectNode()); |
| 228 | configStore.queueConfig(123, "config3", new ObjectMapper().createObjectNode()); |
| 229 | configStore.applyConfig("subject1", BasicConfig.class, new ObjectMapper().createObjectNode()); |
| 230 | |
| 231 | Set<String> configs = configStore.getSubjects(String.class); |
| 232 | |
| 233 | configs.forEach(c -> configStore.clearConfig(c)); |
| 234 | Set<String> newConfig1 = configStore.getSubjects(String.class); |
| 235 | |
| 236 | assertThat(newConfig1, notNullValue()); |
| 237 | } |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 238 | } |