2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.dblib.test;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import com.google.common.io.Files;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.nio.charset.StandardCharsets;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
37 import org.onap.ccsdk.features.sdnr.wt.common.configuration.subtypes.Section;
38 import org.onap.ccsdk.features.sdnr.wt.common.configuration.subtypes.Section.EnvGetter;
39 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBConfig;
41 public class TestConfig {
44 private static final String TEST_CONFIG1 = "test.properties";
45 private static final String TEST_CONFIG2 = "test2.properties";
46 private static final String TEST_CONFIG3 = "test3.properties";
47 protected static final String SDNR_CONTROLLERID_ENV_VALUE = "903f3091-24f6-11ec-9cc3-0242ac130003";
48 private static final String CONTENT_CONTROLLER_NULL = "dbType=${SDNRDBTYPE}\n"
51 + "url=${SDNRDBURL}\n"
52 + "username=${SDNRDBUSERNAME}\n"
53 + "password=${SDNRDBPASSWORD}\n"
54 + "controllerId=null\n"
58 public void testEnvControllerId() {
60 ConfigurationFileRepresentation cfg = new ConfigurationFileRepresentation(TEST_CONFIG1);
61 Section.setEnvGetter(new EnvGetter() {
64 public String getenv(String substring) {
65 if("SDNRCONTROLLERID".equals(substring)) {
66 return SDNR_CONTROLLERID_ENV_VALUE;
71 SqlDBConfig config = new SqlDBConfig(cfg);
72 assertEquals(SDNR_CONTROLLERID_ENV_VALUE, config.getControllerId());
76 public void testGeneratedControllerId() {
77 ConfigurationFileRepresentation cfg = new ConfigurationFileRepresentation(TEST_CONFIG2);
78 Section.setEnvGetter(new EnvGetter() {
81 public String getenv(String substring) {
85 SqlDBConfig config = new SqlDBConfig(cfg);
86 final String controllerId = config.getControllerId();
87 assertNotNull(controllerId);
88 assertFalse(controllerId.isBlank());
89 final String controllerId2 = config.getControllerId();
90 assertEquals(controllerId, controllerId2);
94 public void testNullControllerId() throws IOException {
95 Files.asCharSink(new File(TEST_CONFIG3), StandardCharsets.UTF_8).write(CONTENT_CONTROLLER_NULL);
96 ConfigurationFileRepresentation cfg = new ConfigurationFileRepresentation(TEST_CONFIG3);
97 Section.setEnvGetter(new EnvGetter() {
100 public String getenv(String substring) {
104 SqlDBConfig config = new SqlDBConfig(cfg);
105 final String controllerId = config.getControllerId();
106 assertNull(controllerId);
107 final String controllerId2 = config.getControllerId();
108 assertNull(controllerId2);
113 public void after() throws InterruptedException, IOException {
115 delete(new File(TEST_CONFIG1));
116 delete(new File(TEST_CONFIG2));
117 delete(new File(TEST_CONFIG3));
121 private static void delete(File f) throws IOException {
122 if (f.isDirectory()) {
123 for (File c : f.listFiles()) {
129 throw new FileNotFoundException("Failed to delete file: " + f);