Ray Milkey
Committed by Gerrit Code Review

Unit tests for the Network Config Manager class

Change-Id: Idc96f95acc79ed169abe9fbcbd64f85b8a6fb237
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 + */
16 +package org.onosproject.event;
17 +
18 +import java.util.Set;
19 +
20 +import static org.junit.Assert.*;
21 +
22 +/**
23 + * Testing adapter for the event delivery service.
24 + */
25 +public class EventDeliveryServiceAdapter implements EventDeliveryService {
26 + @Override
27 + public void setDispatchTimeLimit(long millis) {
28 +
29 + }
30 +
31 + @Override
32 + public long getDispatchTimeLimit() {
33 + return 0;
34 + }
35 +
36 + @Override
37 + public void post(Event event) {
38 +
39 + }
40 +
41 + @Override
42 + public <E extends Event> void addSink(Class<E> eventClass, EventSink<E> sink) {
43 +
44 + }
45 +
46 + @Override
47 + public <E extends Event> void removeSink(Class<E> eventClass) {
48 +
49 + }
50 +
51 + @Override
52 + public <E extends Event> EventSink<E> getSink(Class<E> eventClass) {
53 + return null;
54 + }
55 +
56 + @Override
57 + public Set<Class<? extends Event>> getSinks() {
58 + return null;
59 + }
60 +}
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
41 41
42 <dependency> 42 <dependency>
43 <groupId>org.onosproject</groupId> 43 <groupId>org.onosproject</groupId>
44 - <artifactId>onos-incubator-api</artifactId> 44 + <artifactId>onos-core-common</artifactId>
45 <version>${project.version}</version> 45 <version>${project.version}</version>
46 <classifier>tests</classifier> 46 <classifier>tests</classifier>
47 <scope>test</scope> 47 <scope>test</scope>
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
49 49
50 <dependency> 50 <dependency>
51 <groupId>org.onosproject</groupId> 51 <groupId>org.onosproject</groupId>
52 - <artifactId>onos-core-common</artifactId> 52 + <artifactId>onos-core-serializers</artifactId>
53 <version>${project.version}</version> 53 <version>${project.version}</version>
54 <classifier>tests</classifier> 54 <classifier>tests</classifier>
55 <scope>test</scope> 55 <scope>test</scope>
...@@ -80,6 +80,13 @@ ...@@ -80,6 +80,13 @@
80 <groupId>org.apache.karaf.system</groupId> 80 <groupId>org.apache.karaf.system</groupId>
81 <artifactId>org.apache.karaf.system.core</artifactId> 81 <artifactId>org.apache.karaf.system.core</artifactId>
82 </dependency> 82 </dependency>
83 +
84 + <dependency>
85 + <groupId>org.onosproject</groupId>
86 + <artifactId>onos-incubator-store</artifactId>
87 + <version>${project.version}</version>
88 + <scope>test</scope>
89 + </dependency>
83 </dependencies> 90 </dependencies>
84 91
85 <build> 92 <build>
......
...@@ -226,11 +226,11 @@ public class NetworkConfigManager ...@@ -226,11 +226,11 @@ public class NetworkConfigManager
226 } 226 }
227 227
228 // Auxiliary key to track config factories. 228 // Auxiliary key to track config factories.
229 - private static final class ConfigKey { 229 + protected static final class ConfigKey {
230 final Class subjectClass; 230 final Class subjectClass;
231 final Class configClass; 231 final Class configClass;
232 232
233 - private ConfigKey(Class subjectClass, Class configClass) { 233 + protected ConfigKey(Class subjectClass, Class configClass) {
234 this.subjectClass = subjectClass; 234 this.subjectClass = subjectClass;
235 this.configClass = configClass; 235 this.configClass = configClass;
236 } 236 }
...@@ -258,11 +258,11 @@ public class NetworkConfigManager ...@@ -258,11 +258,11 @@ public class NetworkConfigManager
258 return new ConfigIdentifier(factory.subjectFactory().subjectKey(), factory.configKey()); 258 return new ConfigIdentifier(factory.subjectFactory().subjectKey(), factory.configKey());
259 } 259 }
260 260
261 - private static final class ConfigIdentifier { 261 + protected static final class ConfigIdentifier {
262 final String subjectKey; 262 final String subjectKey;
263 final String configKey; 263 final String configKey;
264 264
265 - private ConfigIdentifier(String subjectKey, String configKey) { 265 + protected ConfigIdentifier(String subjectKey, String configKey) {
266 this.subjectKey = subjectKey; 266 this.subjectKey = subjectKey;
267 this.configKey = configKey; 267 this.configKey = configKey;
268 } 268 }
......
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 + */
16 +package org.onosproject.incubator.net.config.impl;
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;
25 +import org.onosproject.incubator.net.config.Config;
26 +import org.onosproject.incubator.net.config.ConfigFactory;
27 +import org.onosproject.incubator.net.config.NetworkConfigRegistry;
28 +import org.onosproject.incubator.net.config.NetworkConfigService;
29 +import org.onosproject.incubator.net.config.SubjectFactory;
30 +import org.onosproject.incubator.store.config.impl.DistributedNetworkConfigStore;
31 +import org.onosproject.net.NetTestTools;
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 +}