blob: bc9a3cbcc0f6c2af662638589447f1f813ac1c28 [file] [log] [blame]
andrea33279c82015-09-30 15:30:48 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
andrea33279c82015-09-30 15:30:48 -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 */
16
17package org.onosproject.cfg.impl;
18
19import com.google.common.collect.ImmutableSet;
andrea33279c82015-09-30 15:30:48 -070020import org.junit.Before;
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080021import org.junit.ClassRule;
andrea33279c82015-09-30 15:30:48 -070022import org.junit.Test;
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080023import org.junit.rules.TemporaryFolder;
andrea33279c82015-09-30 15:30:48 -070024import org.onosproject.cfg.ComponentConfigAdapter;
andreadd106a62015-10-02 13:39:48 -070025import org.slf4j.Logger;
andrea33279c82015-09-30 15:30:48 -070026
27import java.io.File;
28import java.io.IOException;
29import java.util.Set;
30
31import static com.google.common.io.ByteStreams.toByteArray;
32import static com.google.common.io.Files.write;
andreadd106a62015-10-02 13:39:48 -070033import static org.junit.Assert.assertEquals;
34import static org.junit.Assert.assertNull;
35import static org.slf4j.LoggerFactory.getLogger;
andrea33279c82015-09-30 15:30:48 -070036
37/**
38 * UnitTest for ComponentLoader.
39 */
40public class ComponentConfigLoaderTest {
41
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080042 @ClassRule
43 public static TemporaryFolder testFolder = new TemporaryFolder();
andrea33279c82015-09-30 15:30:48 -070044
45 private static final String FOO_COMPONENT = "fooComponent";
46
47 private ComponentConfigLoader loader;
48
49 private TestConfigService service;
50
andreadd106a62015-10-02 13:39:48 -070051 private final Logger log = getLogger(getClass());
52
andrea33279c82015-09-30 15:30:48 -070053 /*
54 * Method to SetUp the test environment with test file, a config loader a service,
55 * and assign it to the loader.configService for the test.
56 */
57 @Before
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080058 public void setUp() throws IOException {
59 ComponentConfigLoader.cfgFile = new File(testFolder.newFolder(), "test.json");
andrea33279c82015-09-30 15:30:48 -070060 loader = new ComponentConfigLoader();
61 service = new TestConfigService();
62 loader.configService = service;
63 }
64
65 /*
66 * Tests that the component in the json receives the correct configuration.
67 */
68 @Test
69 public void basics() throws IOException {
70 stageTestResource("basic.json");
71 loader.activate();
andreadd106a62015-10-02 13:39:48 -070072 assertEquals("incorrect component", FOO_COMPONENT, service.component);
andrea33279c82015-09-30 15:30:48 -070073 }
74
75 /*
76 * Tests that the component is null if the file has a bad configuration format
77 * for which it yielded an exception. Can't test the exception because it happens
andrea73f03782015-10-01 14:01:35 -070078 * in a different thread.
andrea33279c82015-09-30 15:30:48 -070079 */
80 @Test
81 public void badConfig() throws IOException {
82 stageTestResource("badConfig.json");
83 loader.activate();
andreadd106a62015-10-02 13:39:48 -070084 assertNull("incorrect configuration", service.component);
andrea33279c82015-09-30 15:30:48 -070085
86 }
87
88 /*
89 * Writes the necessary file for the tests in the temporary directory
90 */
91 static void stageTestResource(String name) throws IOException {
92 byte[] bytes = toByteArray(ComponentConfigLoaderTest.class.getResourceAsStream(name));
93 write(bytes, ComponentConfigLoader.cfgFile);
94 }
95
96 /*
97 * Mockup class for the config service.
98 */
99 private class TestConfigService extends ComponentConfigAdapter {
100
andreadd106a62015-10-02 13:39:48 -0700101 protected String component;
102 protected String name;
103 protected String value;
andrea33279c82015-09-30 15:30:48 -0700104
105 @Override
106 public Set<String> getComponentNames() {
107 return ImmutableSet.of(FOO_COMPONENT);
108 }
109
110 @Override
andreadd106a62015-10-02 13:39:48 -0700111 public void preSetProperty(String componentName, String name, String value) {
112 log.info("preSet");
113 this.component = componentName;
114 this.name = name;
115 this.value = value;
116
117 }
118
119 @Override
andrea33279c82015-09-30 15:30:48 -0700120 public void setProperty(String componentName, String name, String value) {
andreadd106a62015-10-02 13:39:48 -0700121 log.info("Set");
andrea33279c82015-09-30 15:30:48 -0700122 this.component = componentName;
123 this.name = name;
124 this.value = value;
125
126 }
127 }
128}