Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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.net.config.impl; |
Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.Set; |
| 19 | |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.junit.TestUtils; |
| 24 | import org.onosproject.event.EventDeliveryServiceAdapter; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 25 | import org.onosproject.net.config.Config; |
| 26 | import org.onosproject.net.config.ConfigFactory; |
| 27 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 28 | import org.onosproject.net.config.NetworkConfigService; |
| 29 | import org.onosproject.net.config.SubjectFactory; |
Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -0700 | [diff] [blame] | 30 | import org.onosproject.net.NetTestTools; |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 31 | import org.onosproject.store.config.impl.DistributedNetworkConfigStore; |
Ray Milkey | ae9faf1 | 2015-08-03 15:52:26 -0700 | [diff] [blame] | 32 | import org.onosproject.store.service.TestStorageService; |
| 33 | |
| 34 | import static org.hamcrest.Matchers.hasSize; |
| 35 | import static org.hamcrest.Matchers.instanceOf; |
| 36 | import static org.hamcrest.Matchers.is; |
| 37 | import static org.hamcrest.Matchers.notNullValue; |
| 38 | import static org.hamcrest.Matchers.nullValue; |
| 39 | import static org.junit.Assert.assertThat; |
| 40 | |
| 41 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 42 | import com.google.common.testing.EqualsTester; |
| 43 | |
| 44 | /** |
| 45 | * Unit tests for network config registry. |
| 46 | */ |
| 47 | public class NetworkConfigManagerTest { |
| 48 | private NetworkConfigManager manager; |
| 49 | private NetworkConfigRegistry registry; |
| 50 | private NetworkConfigService configService; |
| 51 | private DistributedNetworkConfigStore configStore; |
| 52 | |
| 53 | /** |
| 54 | * Config classes for testing. |
| 55 | */ |
| 56 | public class BasicConfig1 extends Config<String> { } |
| 57 | public class BasicConfig2 extends Config<String> { } |
| 58 | |
| 59 | public class MockSubjectFactory extends SubjectFactory<String> { |
| 60 | protected MockSubjectFactory(Class<String> subjectClass, String subjectKey) { |
| 61 | super(subjectClass, subjectKey); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public String createSubject(String subjectKey) { |
| 66 | return subjectKey + "-subject"; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Config factory classes for testing. |
| 72 | */ |
| 73 | public class MockConfigFactory1 extends ConfigFactory<String, BasicConfig1> { |
| 74 | protected MockConfigFactory1(SubjectFactory<String> subjectFactory, |
| 75 | Class<BasicConfig1> configClass, String configKey) { |
| 76 | super(subjectFactory, configClass, configKey); |
| 77 | } |
| 78 | @Override |
| 79 | public BasicConfig1 createConfig() { |
| 80 | return new BasicConfig1(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public class MockConfigFactory2 extends ConfigFactory<String, BasicConfig2> { |
| 85 | protected MockConfigFactory2(SubjectFactory<String> subjectFactory, |
| 86 | Class<BasicConfig2> configClass, String configKey) { |
| 87 | super(subjectFactory, configClass, configKey); |
| 88 | } |
| 89 | @Override |
| 90 | public BasicConfig2 createConfig() { |
| 91 | return new BasicConfig2(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | MockSubjectFactory factory1 = new MockSubjectFactory(String.class, |
| 96 | "key1"); |
| 97 | MockSubjectFactory factory2 = new MockSubjectFactory(String.class, |
| 98 | "key2"); |
| 99 | |
| 100 | MockConfigFactory1 config1Factory = new MockConfigFactory1(factory1, |
| 101 | BasicConfig1.class, "config1"); |
| 102 | MockConfigFactory2 config2Factory = new MockConfigFactory2(factory2, |
| 103 | BasicConfig2.class, "config2"); |
| 104 | |
| 105 | |
| 106 | @Before |
| 107 | public void setUp() throws Exception { |
| 108 | configStore = new DistributedNetworkConfigStore(); |
| 109 | TestUtils.setField(configStore, "storageService", new TestStorageService()); |
| 110 | configStore.activate(); |
| 111 | manager = new NetworkConfigManager(); |
| 112 | manager.store = configStore; |
| 113 | NetTestTools.injectEventDispatcher(manager, new EventDeliveryServiceAdapter()); |
| 114 | manager.activate(); |
| 115 | registry = manager; |
| 116 | configService = manager; |
| 117 | } |
| 118 | |
| 119 | @After |
| 120 | public void tearDown() { |
| 121 | configStore.deactivate(); |
| 122 | manager.deactivate(); |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void testRegistry() { |
| 127 | assertThat(registry.getConfigFactories(), hasSize(0)); |
| 128 | assertThat(registry.getConfigFactories(String.class), hasSize(0)); |
| 129 | assertThat(registry.getConfigFactory(BasicConfig1.class), nullValue()); |
| 130 | |
| 131 | registry.registerConfigFactory(config1Factory); |
| 132 | registry.registerConfigFactory(config2Factory); |
| 133 | |
| 134 | assertThat(registry.getConfigFactories(), hasSize(2)); |
| 135 | assertThat(registry.getConfigFactories(String.class), hasSize(2)); |
| 136 | |
| 137 | ConfigFactory queried = registry.getConfigFactory(BasicConfig1.class); |
| 138 | assertThat(queried, is(config1Factory)); |
| 139 | |
| 140 | registry.unregisterConfigFactory(queried); |
| 141 | // Factory associations are not removed according to code documentation |
| 142 | assertThat(registry.getConfigFactories(), hasSize(1)); |
| 143 | assertThat(registry.getConfigFactories(String.class), hasSize(1)); |
| 144 | assertThat(registry.getConfigFactory(BasicConfig1.class), nullValue()); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void configIdEquals() { |
| 149 | NetworkConfigManager.ConfigIdentifier id1 = |
| 150 | new NetworkConfigManager.ConfigIdentifier("s1", "c1"); |
| 151 | NetworkConfigManager.ConfigIdentifier likeId1 = |
| 152 | new NetworkConfigManager.ConfigIdentifier("s1", "c1"); |
| 153 | NetworkConfigManager.ConfigIdentifier id2 = |
| 154 | new NetworkConfigManager.ConfigIdentifier("s1", "c2"); |
| 155 | NetworkConfigManager.ConfigIdentifier id3 = |
| 156 | new NetworkConfigManager.ConfigIdentifier("s2", "c1"); |
| 157 | |
| 158 | new EqualsTester().addEqualityGroup(id1, likeId1) |
| 159 | .addEqualityGroup(id2) |
| 160 | .addEqualityGroup(id3) |
| 161 | .testEquals(); |
| 162 | } |
| 163 | |
| 164 | @Test |
| 165 | public void configKeyEquals() { |
| 166 | NetworkConfigManager.ConfigKey key1 = |
| 167 | new NetworkConfigManager.ConfigKey(String.class, String.class); |
| 168 | NetworkConfigManager.ConfigKey likeKey1 = |
| 169 | new NetworkConfigManager.ConfigKey(String.class, String.class); |
| 170 | NetworkConfigManager.ConfigKey key2 = |
| 171 | new NetworkConfigManager.ConfigKey(String.class, Integer.class); |
| 172 | NetworkConfigManager.ConfigKey key3 = |
| 173 | new NetworkConfigManager.ConfigKey(Integer.class, String.class); |
| 174 | |
| 175 | new EqualsTester().addEqualityGroup(key1, likeKey1) |
| 176 | .addEqualityGroup(key2) |
| 177 | .addEqualityGroup(key3) |
| 178 | .testEquals(); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Tests creation, query and removal of a factory. |
| 183 | */ |
| 184 | @Test |
| 185 | public void testAddConfig() { |
| 186 | |
| 187 | assertThat(configService.getSubjectFactory(String.class), nullValue()); |
| 188 | assertThat(configService.getSubjectFactory("key"), nullValue()); |
| 189 | |
| 190 | registry.registerConfigFactory(config1Factory); |
| 191 | registry.registerConfigFactory(config2Factory); |
| 192 | configService.addConfig("configKey", BasicConfig1.class); |
| 193 | |
| 194 | Config newConfig = configService.getConfig("configKey", BasicConfig1.class); |
| 195 | assertThat(newConfig, notNullValue()); |
| 196 | |
| 197 | assertThat(configService.getSubjectFactory(String.class), notNullValue()); |
| 198 | assertThat(configService.getSubjectFactory("key1"), notNullValue()); |
| 199 | |
| 200 | Set<Class> classes = configService.getSubjectClasses(); |
| 201 | assertThat(classes, hasSize(1)); |
| 202 | |
| 203 | Set<String> subjectsForClass = |
| 204 | configService.getSubjects(String.class); |
| 205 | assertThat(subjectsForClass, hasSize(1)); |
| 206 | |
| 207 | Set<String> subjectsForConfig = |
| 208 | configService.getSubjects(String.class, BasicConfig1.class); |
| 209 | assertThat(subjectsForConfig, hasSize(1)); |
| 210 | |
| 211 | Class queriedConfigClass = configService.getConfigClass("key1", "config1"); |
| 212 | assertThat(queriedConfigClass == BasicConfig1.class, is(true)); |
| 213 | |
| 214 | Set<? extends Config> configs = configService.getConfigs("configKey"); |
| 215 | assertThat(configs.size(), is(1)); |
| 216 | configs.forEach(c -> assertThat(c, instanceOf(BasicConfig1.class))); |
| 217 | |
| 218 | configService.removeConfig("configKey", BasicConfig1.class); |
| 219 | Config newConfigAfterRemove = configService.getConfig("configKey", BasicConfig1.class); |
| 220 | assertThat(newConfigAfterRemove, nullValue()); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Tests creation, query and removal of a factory. |
| 225 | */ |
| 226 | @Test |
| 227 | public void testApplyConfig() { |
| 228 | |
| 229 | assertThat(configService.getSubjectFactory(String.class), nullValue()); |
| 230 | assertThat(configService.getSubjectFactory("key"), nullValue()); |
| 231 | |
| 232 | registry.registerConfigFactory(config1Factory); |
| 233 | registry.registerConfigFactory(config2Factory); |
| 234 | configService.applyConfig("configKey", BasicConfig1.class, new ObjectMapper().createObjectNode()); |
| 235 | |
| 236 | Config newConfig = configService.getConfig("configKey", BasicConfig1.class); |
| 237 | assertThat(newConfig, notNullValue()); |
| 238 | |
| 239 | assertThat(configService.getSubjectFactory(String.class), notNullValue()); |
| 240 | assertThat(configService.getSubjectFactory("key1"), notNullValue()); |
| 241 | } |
| 242 | } |