676be417816464db88ed55330d1be08eb23da350
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.dblib.test;
23
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;
29 import java.io.File;
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;
40
41 public class TestConfig {
42
43
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"
49             + "\n"
50             + "[mariadb]\n"
51             + "url=${SDNRDBURL}\n"
52             + "username=${SDNRDBUSERNAME}\n"
53             + "password=${SDNRDBPASSWORD}\n"
54             + "controllerId=null\n"
55             + "suffix=-v6";
56
57     @Test
58     public void testEnvControllerId() {
59
60         ConfigurationFileRepresentation cfg = new ConfigurationFileRepresentation(TEST_CONFIG1);
61         Section.setEnvGetter(new EnvGetter() {
62
63             @Override
64             public String getenv(String substring) {
65                 if("SDNRCONTROLLERID".equals(substring)) {
66                     return SDNR_CONTROLLERID_ENV_VALUE;
67                 }
68                 return "";
69             }
70         });
71         SqlDBConfig config = new SqlDBConfig(cfg);
72         assertEquals(SDNR_CONTROLLERID_ENV_VALUE, config.getControllerId());
73     }
74
75     @Test
76     public void testGeneratedControllerId() {
77         ConfigurationFileRepresentation cfg = new ConfigurationFileRepresentation(TEST_CONFIG2);
78         Section.setEnvGetter(new EnvGetter() {
79
80             @Override
81             public String getenv(String substring) {
82                 return "";
83             }
84         });
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);
91     }
92
93     @Test
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() {
98
99             @Override
100             public String getenv(String substring) {
101                 return "";
102             }
103         });
104         SqlDBConfig config = new SqlDBConfig(cfg);
105         final String controllerId = config.getControllerId();
106         assertNull(controllerId);
107         final String controllerId2 = config.getControllerId();
108         assertNull(controllerId2);
109     }
110
111     @Before
112     @After
113     public void after() throws InterruptedException, IOException {
114
115         delete(new File(TEST_CONFIG1));
116         delete(new File(TEST_CONFIG2));
117         delete(new File(TEST_CONFIG3));
118
119     }
120
121     private static void delete(File f) throws IOException {
122         if (f.isDirectory()) {
123             for (File c : f.listFiles()) {
124                 delete(c);
125             }
126         }
127         if (f.exists()) {
128             if (!f.delete()) {
129                 throw new FileNotFoundException("Failed to delete file: " + f);
130             }
131         }
132     }
133 }