andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 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; |
andrea | 73f0378 | 2015-10-01 14:01:35 -0700 | [diff] [blame^] | 27 | import java.lang.reflect.Field; |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 28 | import java.util.Set; |
andrea | 73f0378 | 2015-10-01 14:01:35 -0700 | [diff] [blame^] | 29 | import java.util.TimerTask; |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 30 | |
| 31 | import static com.google.common.io.ByteStreams.toByteArray; |
| 32 | import static com.google.common.io.Files.write; |
andrea | 73f0378 | 2015-10-01 14:01:35 -0700 | [diff] [blame^] | 33 | import static org.junit.Assert.*; |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 34 | import static org.onlab.junit.TestTools.assertAfter; |
| 35 | |
| 36 | /** |
| 37 | * UnitTest for ComponentLoader. |
| 38 | */ |
| 39 | public 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 | |
| 49 | /* |
| 50 | * Method to SetUp the test environment with test file, a config loader a service, |
| 51 | * and assign it to the loader.configService for the test. |
| 52 | */ |
| 53 | @Before |
| 54 | public void setUp() { |
| 55 | ComponentConfigLoader.cfgFile = new File(TEST_DIR, "test.json"); |
| 56 | loader = new ComponentConfigLoader(); |
| 57 | service = new TestConfigService(); |
| 58 | loader.configService = service; |
andrea | 73f0378 | 2015-10-01 14:01:35 -0700 | [diff] [blame^] | 59 | loader.retryDelay = 50; |
| 60 | loader.stopRetryTime = 200; |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 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(); |
| 70 | assertAfter(1_000, () -> assertEquals("incorrect component", FOO_COMPONENT, service.component)); |
| 71 | } |
| 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 |
andrea | 73f0378 | 2015-10-01 14:01:35 -0700 | [diff] [blame^] | 76 | * in a different thread. |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 77 | */ |
| 78 | @Test |
| 79 | public void badConfig() throws IOException { |
| 80 | stageTestResource("badConfig.json"); |
| 81 | loader.activate(); |
andrea | 73f0378 | 2015-10-01 14:01:35 -0700 | [diff] [blame^] | 82 | assertAfter(1_000, () -> assertNull("incorrect configuration", service.component)); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Tests that tasks stops itself after the stopRetryTime if the component was |
| 88 | * not loaded. |
| 89 | */ |
| 90 | @Test |
| 91 | public void noComponentForConfig() throws IOException { |
| 92 | stageTestResource("badComponent.json"); |
| 93 | loader.activate(); |
| 94 | assertAfter(loader.stopRetryTime + loader.retryDelay, () -> { |
| 95 | try { |
| 96 | Field state = TimerTask.class.getDeclaredField("state"); |
| 97 | state.setAccessible(true); |
| 98 | assertEquals("incorrect component", state.getInt(loader.loader), 3); |
| 99 | } catch (Exception e) { |
| 100 | e.printStackTrace(); |
| 101 | fail(); |
| 102 | } |
| 103 | }); |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 104 | |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Writes the necessary file for the tests in the temporary directory |
| 109 | */ |
| 110 | static void stageTestResource(String name) throws IOException { |
| 111 | byte[] bytes = toByteArray(ComponentConfigLoaderTest.class.getResourceAsStream(name)); |
| 112 | write(bytes, ComponentConfigLoader.cfgFile); |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Mockup class for the config service. |
| 117 | */ |
| 118 | private class TestConfigService extends ComponentConfigAdapter { |
| 119 | |
| 120 | private String component; |
| 121 | private String name; |
| 122 | private String value; |
| 123 | |
| 124 | @Override |
| 125 | public Set<String> getComponentNames() { |
| 126 | return ImmutableSet.of(FOO_COMPONENT); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void setProperty(String componentName, String name, String value) { |
| 131 | this.component = componentName; |
| 132 | this.name = name; |
| 133 | this.value = value; |
| 134 | |
| 135 | } |
| 136 | } |
| 137 | } |