6fad884ff134329df793924b0aace67841e01d10
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK.apps.sdnr.wt.apigateway
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 package org.onap.ccsdk.features.sdnr.wt.apigateway.test;
22
23 import static org.junit.Assert.*;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.File;
27 import java.util.HashMap;
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.apigateway.MyProperties;
34
35 public class TestProperties {
36
37     private static final boolean DEFAULT_CORSENABLED = false;
38     private static final boolean DEFAULT_AAIOFF = true;
39     private static final boolean DEFAULT_ESOFF = false;
40     private static final boolean DEFAULT_TRUSTINSECURE = false;
41     private static final String DEFAULT_AAIBASEURL = "off";
42     private static Map<String, String> DEFAULT_AAIHEADERS = new HashMap<String, String>();
43     private static final String DEFAULT_ESBASEURL = "http://sdnrdb:9200";
44
45     private static final boolean CUSTOM_CORSENABLED = true;
46     private static final boolean CUSTOM_AAIOFF = false;
47     private static final boolean CUSTOM_ESOFF = false;
48     private static final boolean CUSTOM_TRUSTINSECURE = true;
49     private static final String CUSTOM_AAIBASEURL = "https://aai.tld:2214";
50     private static Map<String, String> CUSTOM_AAIHEADERS = new HashMap<String, String>();
51     private static final String CUSTOM_ESBASEURL = "http://localhost:9200";
52
53     private static final String LR = "\n";
54     final String tmpFilename = "tmp2.cfg";
55
56     @Before
57     @After
58     public void init() {
59         File f = new File(tmpFilename);
60         if (f.exists()) {
61             f.delete();
62         }
63     }
64
65     @Test
66     public void test() {
67         DEFAULT_AAIHEADERS.put("X-FromAppId", "SDNR");
68         DEFAULT_AAIHEADERS.put("Authorization", "Basic QUFJOkFBSQ==");
69         CUSTOM_AAIHEADERS.put("X-FromAppId", "SDNC");
70         CUSTOM_AAIHEADERS.put("Authorization", "Basic 1234");
71         final String TESTPROPERTYFILECONTENT = "aai=" + CUSTOM_AAIBASEURL + LR + "aaiHeaders=[\"X-FromAppId:"
72                 + CUSTOM_AAIHEADERS.get("X-FromAppId") + "\",\"Authorization:" + CUSTOM_AAIHEADERS.get("Authorization")
73                 + "\"]" + LR + "database=" + CUSTOM_ESBASEURL + LR + "insecure=" + (CUSTOM_TRUSTINSECURE ? "1" : "0")
74                 + LR + "cors=" + (CUSTOM_CORSENABLED ? "1" : "0");
75         File ftest = new File(tmpFilename);
76         MyProperties prop = null;
77         ftest = new File(tmpFilename);
78         try {
79             prop = MyProperties.Instantiate(ftest, true);
80         } catch (Exception e) {
81             fail("error instantiating properties");
82         }
83         assertNotNull("problem without exception instantiating properties", prop);
84
85         assertEquals("default config file was not created", true, ftest.exists());
86
87         // test default values
88         assertEquals("default value is not correct", DEFAULT_CORSENABLED, prop.corsEnabled());
89         assertEquals("default value is not correct", DEFAULT_AAIOFF, prop.isAAIOff());
90         assertEquals("default value is not correct", DEFAULT_ESOFF, prop.isEsOff());
91         assertEquals("default value is not correct", DEFAULT_TRUSTINSECURE, prop.trustInsecure());
92         assertEquals("default value is not correct", DEFAULT_AAIBASEURL, prop.getAAIBaseUrl());
93         assertEquals("default value is not correct", DEFAULT_AAIHEADERS, prop.getAAIHeaders());
94         assertEquals("default value is not correct", DEFAULT_ESBASEURL, prop.getEsBaseUrl());
95
96         try {
97             prop.load(new ByteArrayInputStream(TESTPROPERTYFILECONTENT.getBytes()));
98         } catch (Exception e) {
99             fail("error loading custom values into properties");
100         }
101
102         // test custom values
103         assertEquals("custom value is not correct", CUSTOM_CORSENABLED, prop.corsEnabled());
104         assertEquals("custom value is not correct", CUSTOM_AAIOFF, prop.isAAIOff());
105         assertEquals("custom value is not correct", CUSTOM_ESOFF, prop.isEsOff());
106         assertEquals("custom value is not correct", CUSTOM_TRUSTINSECURE, prop.trustInsecure());
107         assertEquals("custom value is not correct", CUSTOM_AAIBASEURL, prop.getAAIBaseUrl());
108         assertEquals("custom value is not correct", CUSTOM_AAIHEADERS, prop.getAAIHeaders());
109         assertEquals("custom value is not correct", CUSTOM_ESBASEURL, prop.getEsBaseUrl());
110
111         // delete autogenerated testfile
112         ftest.delete();
113
114     }
115
116 }