andrea
Committed by Ray Milkey

Minor formatting changes in unit test for component config loader

Change-Id: Ifa6e69d09d902d6c894fac4878a18b864c8ec4e3
...@@ -41,8 +41,10 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -41,8 +41,10 @@ import static org.slf4j.LoggerFactory.getLogger;
41 @Component(immediate = true) 41 @Component(immediate = true)
42 public class ComponentConfigLoader { 42 public class ComponentConfigLoader {
43 43
44 - private static final File CFG_FILE = new File("../config/component-cfg.json");
45 private static final int RETRY_DELAY = 5_000; // millis between retries 44 private static final int RETRY_DELAY = 5_000; // millis between retries
45 + private static final String CFG_JSON = "../config/component-cfg.json";
46 +
47 + static File cfgFile = new File(CFG_JSON);
46 48
47 private final Logger log = getLogger(getClass()); 49 private final Logger log = getLogger(getClass());
48 50
...@@ -78,15 +80,15 @@ public class ComponentConfigLoader { ...@@ -78,15 +80,15 @@ public class ComponentConfigLoader {
78 */ 80 */
79 private void loadConfigs() { 81 private void loadConfigs() {
80 try { 82 try {
81 - if (CFG_FILE.exists()) { 83 + if (cfgFile.exists()) {
82 - root = (ObjectNode) new ObjectMapper().readTree(CFG_FILE); 84 + root = (ObjectNode) new ObjectMapper().readTree(cfgFile);
83 root.fieldNames().forEachRemaining(pendingComponents::add); 85 root.fieldNames().forEachRemaining(pendingComponents::add);
84 - SharedExecutors.getTimer().schedule(loader, RETRY_DELAY, RETRY_DELAY); 86 + SharedExecutors.getTimer().schedule(loader, 0, RETRY_DELAY);
85 - log.info("Loaded initial component configuration from {}", CFG_FILE); 87 + log.info("Loaded initial component configuration from {}", cfgFile);
86 } 88 }
87 } catch (Exception e) { 89 } catch (Exception e) {
88 log.warn("Unable to load initial component configuration from {}", 90 log.warn("Unable to load initial component configuration from {}",
89 - CFG_FILE, e); 91 + cfgFile, e);
90 } 92 }
91 } 93 }
92 /* 94 /*
......
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 +
17 +package org.onosproject.cfg.impl;
18 +
19 +import com.google.common.collect.ImmutableSet;
20 +import com.google.common.io.Files;
21 +import org.junit.Before;
22 +import org.junit.Test;
23 +import org.onosproject.cfg.ComponentConfigAdapter;
24 +
25 +import java.io.File;
26 +import java.io.IOException;
27 +import java.util.Set;
28 +
29 +import static com.google.common.io.ByteStreams.toByteArray;
30 +import static com.google.common.io.Files.write;
31 +import static org.junit.Assert.assertEquals;
32 +import static org.junit.Assert.assertNull;
33 +import static org.onlab.junit.TestTools.assertAfter;
34 +
35 +/**
36 + * UnitTest for ComponentLoader.
37 + */
38 +public class ComponentConfigLoaderTest {
39 +
40 + static final File TEST_DIR = Files.createTempDir();
41 +
42 + private static final String FOO_COMPONENT = "fooComponent";
43 +
44 + private ComponentConfigLoader loader;
45 +
46 + private TestConfigService service;
47 +
48 + /*
49 + * Method to SetUp the test environment with test file, a config loader a service,
50 + * and assign it to the loader.configService for the test.
51 + */
52 + @Before
53 + public void setUp() {
54 + ComponentConfigLoader.cfgFile = new File(TEST_DIR, "test.json");
55 + loader = new ComponentConfigLoader();
56 + service = new TestConfigService();
57 + loader.configService = service;
58 + }
59 +
60 + /*
61 + * Tests that the component in the json receives the correct configuration.
62 + */
63 + @Test
64 + public void basics() throws IOException {
65 + stageTestResource("basic.json");
66 + loader.activate();
67 + assertAfter(1_000, () -> assertEquals("incorrect component", FOO_COMPONENT, service.component));
68 + }
69 +
70 + /*
71 + * Tests that the component is null if the file has a bad configuration format
72 + * for which it yielded an exception. Can't test the exception because it happens
73 + * in a different thread,
74 + */
75 + @Test
76 + public void badConfig() throws IOException {
77 + stageTestResource("badConfig.json");
78 + loader.activate();
79 + assertAfter(1_000, () -> assertNull("incorrect component", service.component));
80 +
81 + }
82 +
83 + /*
84 + * Writes the necessary file for the tests in the temporary directory
85 + */
86 + static void stageTestResource(String name) throws IOException {
87 + byte[] bytes = toByteArray(ComponentConfigLoaderTest.class.getResourceAsStream(name));
88 + write(bytes, ComponentConfigLoader.cfgFile);
89 + }
90 +
91 + /*
92 + * Mockup class for the config service.
93 + */
94 + private class TestConfigService extends ComponentConfigAdapter {
95 +
96 + private String component;
97 + private String name;
98 + private String value;
99 +
100 + @Override
101 + public Set<String> getComponentNames() {
102 + return ImmutableSet.of(FOO_COMPONENT);
103 + }
104 +
105 + @Override
106 + public void setProperty(String componentName, String name, String value) {
107 + this.component = componentName;
108 + this.name = name;
109 + this.value = value;
110 +
111 + }
112 + }
113 +}
...\ No newline at end of file ...\ No newline at end of file
1 +{
2 + "fooComponent": {
3 + badconfig
4 + }
5 +}
...\ No newline at end of file ...\ No newline at end of file
1 +{
2 + "fooComponent": {
3 + "testProperty": true
4 + }
5 +}
...\ No newline at end of file ...\ No newline at end of file