Fix the lack of test coverage reported by sonar
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-core / src / test / java / org / onap / config / impl / ConfigurationRepositoryTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP SDC
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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  * ===================================================================
20  */
21
22 package org.onap.config.impl;
23
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;
28
29 import java.io.File;
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;
35 import java.util.Set;
36
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;
46
47 public class ConfigurationRepositoryTest {
48
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";
52
53     private ConfigurationRepository repository;
54
55     @BeforeClass
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);
60         dir.mkdirs();
61         File f = new File(TEST_CONFIG_FILE);
62         try (OutputStream out = new FileOutputStream(f)) {
63             props.store(out, "Config Property at Conventional Resource");
64         }
65     }
66
67     @AfterClass
68     public static void tearDown() throws IOException {
69         TestUtil.deleteTestDirsStrucuture(Paths.get(TestUtil.jsonSchemaLoc));
70     }
71
72     @Before
73     public void init() {
74         repository = ConfigurationRepository.lookup();
75     }
76
77     @Test
78     public void testFreshCreatedOne() {
79         // when - then
80         assertTrue(repository.isValidTenant(Constants.DEFAULT_TENANT));
81         assertTrue(repository.isValidNamespace(Constants.DEFAULT_NAMESPACE));
82     }
83
84     @Test
85     public void testFreshCreatedTwo() {
86         // when
87         final Set<String> tenants = repository.getTenants();
88         final Set<String> namespaces = repository.getNamespaces();
89
90         // then
91         assertTrue(tenants.size() > 0);
92         assertTrue(namespaces.size() > 0);
93
94         assertNotNull(tenants.toArray(EMPTY_ARRAY_OF_STRING)[0]);
95         assertNotNull(namespaces.toArray(EMPTY_ARRAY_OF_STRING)[0]);
96     }
97
98     @Test
99     public void testConfigurationPopulated() throws Exception {
100         // given
101         BaseConfiguration inputConfig = new BaseConfiguration();
102
103         // when
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);
108
109         // then
110         assertEquals(inputConfig, outputConfig);
111     }
112
113     @Test
114     public void testPopulateOverrideConfiguration() throws Exception {
115         // given
116         BaseConfiguration inputConfig = new BaseConfiguration();
117         repository.populateConfiguration(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER
118                 + TEST_NAME_SPACE, inputConfig);
119
120         // when
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);
124
125         // then
126         assertNotEquals(inputConfig, outputConfig);
127         assertEquals(0, inputConfig.size());
128         assertEquals(1, outputConfig.size());
129         assertEquals("10240", outputConfig.getString(ConfigTestConstant.ARTIFACT_MAXSIZE));
130     }
131 }