blob: 856092eab87ad29e820a667fe690c801947085c6 [file] [log] [blame]
andrea33279c82015-09-30 15:30:48 -07001/*
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
17package org.onosproject.cfg.impl;
18
19import com.google.common.collect.ImmutableSet;
20import com.google.common.io.Files;
21import org.junit.Before;
22import org.junit.Test;
23import org.onosproject.cfg.ComponentConfigAdapter;
24
25import java.io.File;
26import java.io.IOException;
27import java.util.Set;
28
29import static com.google.common.io.ByteStreams.toByteArray;
30import static com.google.common.io.Files.write;
31import static org.junit.Assert.assertEquals;
32import static org.junit.Assert.assertNull;
33import static org.onlab.junit.TestTools.assertAfter;
34
35/**
36 * UnitTest for ComponentLoader.
37 */
38public 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}