1275b03722f41e986eefe959e5ef2d1e26320b30
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 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.test;
23
24 import static org.junit.Assert.assertEquals;
25 import java.io.File;
26 import java.lang.reflect.Field;
27 import java.util.Map;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
32 import org.onap.ccsdk.features.sdnr.wt.dataprovider.impl.EsConfig;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class TestConfig {
37
38     private static final Logger LOG = LoggerFactory.getLogger(TestConfig.class);
39
40     private static final String TESTFILENAME = "testconfig.properties";
41     private static final String HOSTNAME = "localhost";
42
43     @After
44     @Before
45     public void afterAndBefore() {
46         File f = new File(TESTFILENAME);
47         if (f.exists()) {
48             LOG.info("Remove {}", f.getAbsolutePath());
49             f.delete();
50         }
51     }
52
53     @Test
54     public void test() {
55         int databasePort = setSDNRDBURLEnv();
56
57         ConfigurationFileRepresentation configuration = new ConfigurationFileRepresentation(TESTFILENAME);
58         EsConfig esConfig = new EsConfig(configuration);
59         LOG.info("Defaultconfiguration: {}", esConfig.toString());
60         assertEquals("http", esConfig.getHosts()[0].protocol.getValue());
61         assertEquals(databasePort, esConfig.getHosts()[0].port);
62         assertEquals(HOSTNAME, esConfig.getHosts()[0].hostname);
63
64     }
65
66     public static int setSDNRDBURLEnv() {
67         int databasePort = Integer
68                 .valueOf(System.getProperty("databaseport") != null ? System.getProperty("databaseport") : "49200");
69         System.out.println("DB Port: " + databasePort);
70         setEnv("SDNRDBURL", "http://"+HOSTNAME+":"+databasePort);
71         return databasePort;
72     }
73
74     public static void setEnv(String key, String value) {
75         try {
76             Map<String, String> env = System.getenv();
77             Class<?> cl = env.getClass();
78             Field field = cl.getDeclaredField("m");
79             field.setAccessible(true);
80             Map<String, String> writableEnv = (Map<String, String>) field.get(env);
81             writableEnv.put(key, value);
82         } catch (Exception e) {
83             throw new IllegalStateException("Failed to set environment variable", e);
84         }
85     }
86 }