2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Samsung. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END============================================
19 * ===================================================================
22 package org.onap.config.impl;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.OutputStream;
33 import java.nio.file.Paths;
34 import java.util.Properties;
37 import org.apache.commons.configuration2.BaseConfiguration;
38 import org.apache.commons.configuration2.Configuration;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.onap.config.Constants;
44 import org.onap.config.util.ConfigTestConstant;
45 import org.onap.config.util.TestUtil;
47 public class ConfigurationRepositoryTest {
49 private static final String[] EMPTY_ARRAY_OF_STRING = new String[0];
50 private static final String TEST_NAME_SPACE = "testNameSpace";
51 private static final String TEST_CONFIG_FILE = TestUtil.jsonSchemaLoc + "config.properties";
53 private ConfigurationRepository repository;
56 public static void setUp() throws Exception {
57 Properties props = new Properties();
58 props.setProperty(ConfigTestConstant.ARTIFACT_MAXSIZE, "10240");
59 File dir = new File(TestUtil.jsonSchemaLoc);
61 File f = new File(TEST_CONFIG_FILE);
62 try (OutputStream out = new FileOutputStream(f)) {
63 props.store(out, "Config Property at Conventional Resource");
68 public static void tearDown() throws IOException {
69 TestUtil.deleteTestDirsStrucuture(Paths.get(TestUtil.jsonSchemaLoc));
74 repository = ConfigurationRepository.lookup();
78 public void testFreshCreatedOne() {
80 assertTrue(repository.isValidTenant(Constants.DEFAULT_TENANT));
81 assertTrue(repository.isValidNamespace(Constants.DEFAULT_NAMESPACE));
85 public void testFreshCreatedTwo() {
87 final Set<String> tenants = repository.getTenants();
88 final Set<String> namespaces = repository.getNamespaces();
91 assertTrue(tenants.size() > 0);
92 assertTrue(namespaces.size() > 0);
94 assertNotNull(tenants.toArray(EMPTY_ARRAY_OF_STRING)[0]);
95 assertNotNull(namespaces.toArray(EMPTY_ARRAY_OF_STRING)[0]);
99 public void testConfigurationPopulated() throws Exception {
101 BaseConfiguration inputConfig = new BaseConfiguration();
104 repository.populateConfiguration(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER
105 + TEST_NAME_SPACE, inputConfig);
106 final Configuration outputConfig =
107 repository.getConfigurationFor(Constants.DEFAULT_TENANT, TEST_NAME_SPACE);
110 assertEquals(inputConfig, outputConfig);
114 public void testPopulateOverrideConfiguration() throws Exception {
116 BaseConfiguration inputConfig = new BaseConfiguration();
117 repository.populateConfiguration(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER
118 + TEST_NAME_SPACE, inputConfig);
121 repository.populateOverrideConfiguration(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER
122 + TEST_NAME_SPACE, new File(TEST_CONFIG_FILE));
123 final Configuration outputConfig = repository.getConfigurationFor(Constants.DEFAULT_TENANT, TEST_NAME_SPACE);
126 assertNotEquals(inputConfig, outputConfig);
127 assertEquals(0, inputConfig.size());
128 assertEquals(1, outputConfig.size());
129 assertEquals("10240", outputConfig.getString(ConfigTestConstant.ARTIFACT_MAXSIZE));