blob: 8b32b670d0e4aae85cad69f2722a41e62faa309b [file] [log] [blame]
Ray Milkey6f440332015-07-31 10:40:53 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkey6f440332015-07-31 10:40:53 -07003 *
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 Vachuska4998caa2015-08-26 13:28:38 -070016package org.onosproject.store.config.impl;
Ray Milkey6f440332015-07-31 10:40:53 -070017
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
Ray Milkeya4122362015-08-18 15:19:08 -070021import org.onosproject.net.config.Config;
22import org.onosproject.net.config.ConfigFactory;
23import org.onosproject.net.config.SubjectFactory;
Ray Milkey6f440332015-07-31 10:40:53 -070024import org.onosproject.store.service.TestStorageService;
25
26import com.fasterxml.jackson.databind.ObjectMapper;
27
28import static org.hamcrest.Matchers.hasSize;
29import static org.hamcrest.Matchers.is;
30import static org.hamcrest.Matchers.notNullValue;
31import static org.hamcrest.Matchers.nullValue;
32import static org.junit.Assert.assertThat;
33
34public 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 Vachuska096bcc82016-03-07 21:30:29 -080062 * Config class for testing.
63 */
64 public class BasicIntConfig extends Config<Integer> { }
65
66 /**
Ray Milkey6f440332015-07-31 10:40:53 -070067 * Config factory class for testing.
68 */
69 public class MockConfigFactory extends ConfigFactory<String, BasicConfig> {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -080070 protected MockConfigFactory(Class<BasicConfig> configClass, String configKey) {
71 super(new MockSubjectFactory("strings"), configClass, configKey);
Ray Milkey6f440332015-07-31 10:40:53 -070072 }
73 @Override
74 public BasicConfig createConfig() {
75 return new BasicConfig();
76 }
77 }
78
79 /**
Thomas Vachuska096bcc82016-03-07 21:30:29 -080080 * 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 Vachuska6f350ed2016-01-08 09:53:03 -080093 * 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 Vachuska096bcc82016-03-07 21:30:29 -0800107 * 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 Milkey6f440332015-07-31 10:40:53 -0700120 * Tests creation, query and removal of a config.
121 */
122 @Test
123 public void testCreateConfig() {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800124 configStore.addConfigFactory(new MockConfigFactory(BasicConfig.class, "config1"));
Ray Milkey6f440332015-07-31 10:40:53 -0700125
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 Vachuska6f350ed2016-01-08 09:53:03 -0800148 MockConfigFactory mockFactory = new MockConfigFactory(BasicConfig.class, "config1");
Ray Milkey6f440332015-07-31 10:40:53 -0700149
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 Vachuska6f350ed2016-01-08 09:53:03 -0800164 configStore.addConfigFactory(new MockConfigFactory(BasicConfig.class, "config1"));
Ray Milkey6f440332015-07-31 10:40:53 -0700165
Thomas Vachuska096bcc82016-03-07 21:30:29 -0800166 configStore.applyConfig("subject", BasicConfig.class, new ObjectMapper().createObjectNode());
167 assertThat(configStore.getConfigClasses("subject"), hasSize(1));
Ray Milkey6f440332015-07-31 10:40:53 -0700168 assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1));
169 assertThat(configStore.getSubjects(String.class), hasSize(1));
170 }
Thomas Vachuska096bcc82016-03-07 21:30:29 -0800171
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 Milkey6f440332015-07-31 10:40:53 -0700206}