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 | |
| 34 | public class DistributedNetworkConfigStoreTest { |
| 35 | private DistributedNetworkConfigStore configStore; |
| 36 | |
| 37 | /** |
| 38 | * Sets up the config store and the storage service test harness. |
| 39 | */ |
| 40 | @Before |
| 41 | public void setUp() { |
| 42 | configStore = new DistributedNetworkConfigStore(); |
| 43 | configStore.storageService = new TestStorageService(); |
| 44 | configStore.setDelegate(event -> { }); |
| 45 | configStore.activate(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Tears down the config store. |
| 50 | */ |
| 51 | @After |
| 52 | public void tearDown() { |
| 53 | configStore.deactivate(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Config class for testing. |
| 58 | */ |
| 59 | public class BasicConfig extends Config<String> { } |
| 60 | |
| 61 | /** |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 62 | * Config class for testing. |
| 63 | */ |
| 64 | public class BasicIntConfig extends Config<Integer> { } |
| 65 | |
| 66 | /** |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 67 | * Config factory class for testing. |
| 68 | */ |
| 69 | public class MockConfigFactory extends ConfigFactory<String, BasicConfig> { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 70 | protected MockConfigFactory(Class<BasicConfig> configClass, String configKey) { |
| 71 | super(new MockSubjectFactory("strings"), configClass, configKey); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 72 | } |
| 73 | @Override |
| 74 | public BasicConfig createConfig() { |
| 75 | return new BasicConfig(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 80 | * Config factory class for testing. |
| 81 | */ |
| 82 | public class MockIntConfigFactory extends ConfigFactory<Integer, BasicIntConfig> { |
| 83 | protected MockIntConfigFactory(Class<BasicIntConfig> configClass, String configKey) { |
| 84 | super(new MockIntSubjectFactory("strings"), configClass, configKey); |
| 85 | } |
| 86 | @Override |
| 87 | public BasicIntConfig createConfig() { |
| 88 | return new BasicIntConfig(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 93 | * Subject factory class for testing. |
| 94 | */ |
| 95 | public class MockSubjectFactory extends SubjectFactory<String> { |
| 96 | protected MockSubjectFactory(String subjectClassKey) { |
| 97 | super(String.class, subjectClassKey); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public String createSubject(String subjectKey) { |
| 102 | return subjectKey; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 107 | * Subject factory class for testing. |
| 108 | */ |
| 109 | public class MockIntSubjectFactory extends SubjectFactory<Integer> { |
| 110 | protected MockIntSubjectFactory(String subjectClassKey) { |
| 111 | super(Integer.class, subjectClassKey); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public Integer createSubject(String subjectKey) { |
| 116 | return Integer.parseInt(subjectKey); |
| 117 | } |
| 118 | } |
| 119 | /** |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 120 | * Tests creation, query and removal of a config. |
| 121 | */ |
| 122 | @Test |
| 123 | public void testCreateConfig() { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 124 | configStore.addConfigFactory(new MockConfigFactory(BasicConfig.class, "config1")); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 125 | |
| 126 | configStore.createConfig("config1", BasicConfig.class); |
| 127 | assertThat(configStore.getConfigClasses("config1"), hasSize(1)); |
| 128 | assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1)); |
| 129 | assertThat(configStore.getSubjects(String.class), hasSize(1)); |
| 130 | |
| 131 | BasicConfig queried = configStore.getConfig("config1", BasicConfig.class); |
| 132 | assertThat(queried, notNullValue()); |
| 133 | |
| 134 | configStore.clearConfig("config1", BasicConfig.class); |
| 135 | assertThat(configStore.getConfigClasses("config1"), hasSize(0)); |
| 136 | assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(0)); |
| 137 | assertThat(configStore.getSubjects(String.class), hasSize(0)); |
| 138 | |
| 139 | BasicConfig queriedAfterClear = configStore.getConfig("config1", BasicConfig.class); |
| 140 | assertThat(queriedAfterClear, nullValue()); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Tests creation, query and removal of a factory. |
| 145 | */ |
| 146 | @Test |
| 147 | public void testCreateFactory() { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 148 | MockConfigFactory mockFactory = new MockConfigFactory(BasicConfig.class, "config1"); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 149 | |
| 150 | assertThat(configStore.getConfigFactory(BasicConfig.class), nullValue()); |
| 151 | |
| 152 | configStore.addConfigFactory(mockFactory); |
| 153 | assertThat(configStore.getConfigFactory(BasicConfig.class), is(mockFactory)); |
| 154 | |
| 155 | configStore.removeConfigFactory(mockFactory); |
| 156 | assertThat(configStore.getConfigFactory(BasicConfig.class), nullValue()); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Tests applying a config. |
| 161 | */ |
| 162 | @Test |
| 163 | public void testApplyConfig() { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 164 | configStore.addConfigFactory(new MockConfigFactory(BasicConfig.class, "config1")); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 165 | |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 166 | configStore.applyConfig("subject", BasicConfig.class, new ObjectMapper().createObjectNode()); |
| 167 | assertThat(configStore.getConfigClasses("subject"), hasSize(1)); |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 168 | assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1)); |
| 169 | assertThat(configStore.getSubjects(String.class), hasSize(1)); |
| 170 | } |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * Tests inserting a pending configuration. |
| 174 | */ |
| 175 | @Test |
| 176 | public void testPendingConfig() { |
| 177 | configStore.queueConfig("subject", "config1", new ObjectMapper().createObjectNode()); |
| 178 | configStore.addConfigFactory(new MockConfigFactory(BasicConfig.class, "config1")); |
| 179 | |
| 180 | assertThat(configStore.getConfigClasses("subject"), hasSize(1)); |
| 181 | assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1)); |
| 182 | assertThat(configStore.getSubjects(String.class), hasSize(1)); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Tests inserting a pending configuration for the same key, different subject. |
| 187 | */ |
| 188 | @Test |
| 189 | public void testPendingConfigSameKey() { |
| 190 | configStore.queueConfig("subject", "config1", new ObjectMapper().createObjectNode()); |
| 191 | configStore.queueConfig(123, "config1", new ObjectMapper().createObjectNode()); |
| 192 | configStore.addConfigFactory(new MockConfigFactory(BasicConfig.class, "config1")); |
| 193 | |
| 194 | assertThat(configStore.getConfigClasses("subject"), hasSize(1)); |
| 195 | assertThat(configStore.getConfigClasses(123), hasSize(0)); |
| 196 | assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1)); |
| 197 | assertThat(configStore.getSubjects(String.class), hasSize(1)); |
| 198 | |
| 199 | configStore.addConfigFactory(new MockIntConfigFactory(BasicIntConfig.class, "config1")); |
| 200 | |
| 201 | assertThat(configStore.getConfigClasses("subject"), hasSize(1)); |
| 202 | assertThat(configStore.getConfigClasses(123), hasSize(1)); |
| 203 | assertThat(configStore.getSubjects(Integer.class, BasicIntConfig.class), hasSize(1)); |
| 204 | assertThat(configStore.getSubjects(Integer.class), hasSize(1)); |
| 205 | } |
Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -0700 | [diff] [blame] | 206 | } |