blob: 03e2184da427fbc817c6f1f044a909a5b1d63b96 [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;
andrea73f03782015-10-01 14:01:35 -070027import java.lang.reflect.Field;
andrea33279c82015-09-30 15:30:48 -070028import java.util.Set;
andrea73f03782015-10-01 14:01:35 -070029import java.util.TimerTask;
andrea33279c82015-09-30 15:30:48 -070030
31import static com.google.common.io.ByteStreams.toByteArray;
32import static com.google.common.io.Files.write;
andrea73f03782015-10-01 14:01:35 -070033import static org.junit.Assert.*;
andrea33279c82015-09-30 15:30:48 -070034import static org.onlab.junit.TestTools.assertAfter;
35
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
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;
andrea73f03782015-10-01 14:01:35 -070059 loader.retryDelay = 50;
60 loader.stopRetryTime = 200;
andrea33279c82015-09-30 15:30:48 -070061 }
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
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();
andrea73f03782015-10-01 14:01:35 -070082 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 });
andrea33279c82015-09-30 15:30:48 -0700104
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}