blob: 0320cf77c6fca08380849edde9f8c3b3e7c82165 [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;
andreadd106a62015-10-02 13:39:48 -070024import org.slf4j.Logger;
andrea33279c82015-09-30 15:30:48 -070025
26import java.io.File;
27import java.io.IOException;
28import java.util.Set;
29
30import static com.google.common.io.ByteStreams.toByteArray;
31import static com.google.common.io.Files.write;
andreadd106a62015-10-02 13:39:48 -070032import static org.junit.Assert.assertEquals;
33import static org.junit.Assert.assertNull;
34import static org.slf4j.LoggerFactory.getLogger;
andrea33279c82015-09-30 15:30:48 -070035
36/**
37 * UnitTest for ComponentLoader.
38 */
39public class ComponentConfigLoaderTest {
40
41 static final File TEST_DIR = Files.createTempDir();
42
43 private static final String FOO_COMPONENT = "fooComponent";
44
45 private ComponentConfigLoader loader;
46
47 private TestConfigService service;
48
andreadd106a62015-10-02 13:39:48 -070049 private final Logger log = getLogger(getClass());
50
andrea33279c82015-09-30 15:30:48 -070051 /*
52 * Method to SetUp the test environment with test file, a config loader a service,
53 * and assign it to the loader.configService for the test.
54 */
55 @Before
56 public void setUp() {
57 ComponentConfigLoader.cfgFile = new File(TEST_DIR, "test.json");
58 loader = new ComponentConfigLoader();
59 service = new TestConfigService();
60 loader.configService = service;
61 }
62
63 /*
64 * Tests that the component in the json receives the correct configuration.
65 */
66 @Test
67 public void basics() throws IOException {
68 stageTestResource("basic.json");
69 loader.activate();
andreadd106a62015-10-02 13:39:48 -070070 assertEquals("incorrect component", FOO_COMPONENT, service.component);
andrea33279c82015-09-30 15:30:48 -070071 }
72
73 /*
74 * Tests that the component is null if the file has a bad configuration format
75 * for which it yielded an exception. Can't test the exception because it happens
andrea73f03782015-10-01 14:01:35 -070076 * in a different thread.
andrea33279c82015-09-30 15:30:48 -070077 */
78 @Test
79 public void badConfig() throws IOException {
80 stageTestResource("badConfig.json");
81 loader.activate();
andreadd106a62015-10-02 13:39:48 -070082 assertNull("incorrect configuration", service.component);
andrea33279c82015-09-30 15:30:48 -070083
84 }
85
86 /*
87 * Writes the necessary file for the tests in the temporary directory
88 */
89 static void stageTestResource(String name) throws IOException {
90 byte[] bytes = toByteArray(ComponentConfigLoaderTest.class.getResourceAsStream(name));
91 write(bytes, ComponentConfigLoader.cfgFile);
92 }
93
94 /*
95 * Mockup class for the config service.
96 */
97 private class TestConfigService extends ComponentConfigAdapter {
98
andreadd106a62015-10-02 13:39:48 -070099 protected String component;
100 protected String name;
101 protected String value;
andrea33279c82015-09-30 15:30:48 -0700102
103 @Override
104 public Set<String> getComponentNames() {
105 return ImmutableSet.of(FOO_COMPONENT);
106 }
107
108 @Override
andreadd106a62015-10-02 13:39:48 -0700109 public void preSetProperty(String componentName, String name, String value) {
110 log.info("preSet");
111 this.component = componentName;
112 this.name = name;
113 this.value = value;
114
115 }
116
117 @Override
andrea33279c82015-09-30 15:30:48 -0700118 public void setProperty(String componentName, String name, String value) {
andreadd106a62015-10-02 13:39:48 -0700119 log.info("Set");
andrea33279c82015-09-30 15:30:48 -0700120 this.component = componentName;
121 this.name = name;
122 this.value = value;
123
124 }
125 }
126}