andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 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; |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 20 | import org.junit.Before; |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 21 | import org.junit.ClassRule; |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 22 | import org.junit.Test; |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 23 | import org.junit.rules.TemporaryFolder; |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 24 | import org.onosproject.cfg.ComponentConfigAdapter; |
andrea | dd106a6 | 2015-10-02 13:39:48 -0700 | [diff] [blame] | 25 | import org.slf4j.Logger; |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 26 | |
| 27 | import java.io.File; |
| 28 | import java.io.IOException; |
| 29 | import java.util.Set; |
| 30 | |
| 31 | import static com.google.common.io.ByteStreams.toByteArray; |
| 32 | import static com.google.common.io.Files.write; |
andrea | dd106a6 | 2015-10-02 13:39:48 -0700 | [diff] [blame] | 33 | import static org.junit.Assert.assertEquals; |
| 34 | import static org.junit.Assert.assertNull; |
| 35 | import static org.slf4j.LoggerFactory.getLogger; |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * UnitTest for ComponentLoader. |
| 39 | */ |
| 40 | public class ComponentConfigLoaderTest { |
| 41 | |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 42 | @ClassRule |
| 43 | public static TemporaryFolder testFolder = new TemporaryFolder(); |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 44 | |
| 45 | private static final String FOO_COMPONENT = "fooComponent"; |
| 46 | |
| 47 | private ComponentConfigLoader loader; |
| 48 | |
| 49 | private TestConfigService service; |
| 50 | |
andrea | dd106a6 | 2015-10-02 13:39:48 -0700 | [diff] [blame] | 51 | private final Logger log = getLogger(getClass()); |
| 52 | |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 53 | /* |
| 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 Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 58 | public void setUp() throws IOException { |
| 59 | ComponentConfigLoader.cfgFile = new File(testFolder.newFolder(), "test.json"); |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 60 | 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(); |
andrea | dd106a6 | 2015-10-02 13:39:48 -0700 | [diff] [blame] | 72 | assertEquals("incorrect component", FOO_COMPONENT, service.component); |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 73 | } |
| 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 |
andrea | 73f0378 | 2015-10-01 14:01:35 -0700 | [diff] [blame] | 78 | * in a different thread. |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 79 | */ |
| 80 | @Test |
| 81 | public void badConfig() throws IOException { |
| 82 | stageTestResource("badConfig.json"); |
| 83 | loader.activate(); |
andrea | dd106a6 | 2015-10-02 13:39:48 -0700 | [diff] [blame] | 84 | assertNull("incorrect configuration", service.component); |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 85 | |
| 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 | |
andrea | dd106a6 | 2015-10-02 13:39:48 -0700 | [diff] [blame] | 101 | protected String component; |
| 102 | protected String name; |
| 103 | protected String value; |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 104 | |
| 105 | @Override |
| 106 | public Set<String> getComponentNames() { |
| 107 | return ImmutableSet.of(FOO_COMPONENT); |
| 108 | } |
| 109 | |
| 110 | @Override |
andrea | dd106a6 | 2015-10-02 13:39:48 -0700 | [diff] [blame] | 111 | 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 |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 120 | public void setProperty(String componentName, String name, String value) { |
andrea | dd106a6 | 2015-10-02 13:39:48 -0700 | [diff] [blame] | 121 | log.info("Set"); |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 122 | this.component = componentName; |
| 123 | this.name = name; |
| 124 | this.value = value; |
| 125 | |
| 126 | } |
| 127 | } |
| 128 | } |