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