Ray Milkey | 6f44033 | 2015-07-31 10:40:53 -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.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 | /** |
| 62 | * Config factory class for testing. |
| 63 | */ |
| 64 | public class MockConfigFactory extends ConfigFactory<String, BasicConfig> { |
| 65 | protected MockConfigFactory(SubjectFactory<String> subjectFactory, |
| 66 | Class<BasicConfig> configClass, String configKey) { |
| 67 | super(subjectFactory, configClass, configKey); |
| 68 | } |
| 69 | @Override |
| 70 | public BasicConfig createConfig() { |
| 71 | return new BasicConfig(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Tests creation, query and removal of a config. |
| 77 | */ |
| 78 | @Test |
| 79 | public void testCreateConfig() { |
| 80 | configStore.addConfigFactory(new MockConfigFactory(null, BasicConfig.class, "config1")); |
| 81 | |
| 82 | configStore.createConfig("config1", BasicConfig.class); |
| 83 | assertThat(configStore.getConfigClasses("config1"), hasSize(1)); |
| 84 | assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1)); |
| 85 | assertThat(configStore.getSubjects(String.class), hasSize(1)); |
| 86 | |
| 87 | BasicConfig queried = configStore.getConfig("config1", BasicConfig.class); |
| 88 | assertThat(queried, notNullValue()); |
| 89 | |
| 90 | configStore.clearConfig("config1", BasicConfig.class); |
| 91 | assertThat(configStore.getConfigClasses("config1"), hasSize(0)); |
| 92 | assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(0)); |
| 93 | assertThat(configStore.getSubjects(String.class), hasSize(0)); |
| 94 | |
| 95 | BasicConfig queriedAfterClear = configStore.getConfig("config1", BasicConfig.class); |
| 96 | assertThat(queriedAfterClear, nullValue()); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests creation, query and removal of a factory. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testCreateFactory() { |
| 104 | MockConfigFactory mockFactory = new MockConfigFactory(null, BasicConfig.class, "config1"); |
| 105 | |
| 106 | assertThat(configStore.getConfigFactory(BasicConfig.class), nullValue()); |
| 107 | |
| 108 | configStore.addConfigFactory(mockFactory); |
| 109 | assertThat(configStore.getConfigFactory(BasicConfig.class), is(mockFactory)); |
| 110 | |
| 111 | configStore.removeConfigFactory(mockFactory); |
| 112 | assertThat(configStore.getConfigFactory(BasicConfig.class), nullValue()); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Tests applying a config. |
| 117 | */ |
| 118 | @Test |
| 119 | public void testApplyConfig() { |
| 120 | configStore.addConfigFactory(new MockConfigFactory(null, BasicConfig.class, "config1")); |
| 121 | |
| 122 | configStore.applyConfig("config1", BasicConfig.class, new ObjectMapper().createObjectNode()); |
| 123 | assertThat(configStore.getConfigClasses("config1"), hasSize(1)); |
| 124 | assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1)); |
| 125 | assertThat(configStore.getSubjects(String.class), hasSize(1)); |
| 126 | } |
| 127 | } |