Code formatting of configuration framework
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-core / src / test / java / org / onap / config / util / TestUtil.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.config.util;
18
19 import java.io.File;
20 import java.io.FileWriter;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.junit.Assert;
25 import org.onap.config.api.Configuration;
26 import org.onap.config.api.ConfigurationManager;
27
28 /**
29  * Created by sheetalm on 10/13/2016.
30  */
31 public class TestUtil {
32
33     public static final String jsonSchemaLoc = System.getProperty("user.home") + "/TestResources/";
34
35     public static void cleanUp() throws Exception {
36         String data = "{name:\"SCM\"}";
37         TestUtil.writeFile(data);
38     }
39
40     public static void writeFile(String data) throws IOException {
41         File dir = new File(jsonSchemaLoc);
42         dir.mkdirs();
43         File file = new File(jsonSchemaLoc + "/GeneratorsList.json");
44         file.createNewFile();
45         FileWriter fileWriter = new FileWriter(file);
46         fileWriter.write(data);
47         fileWriter.close();
48     }
49
50     public static void validateConfiguration(String nameSpace) {
51         Configuration config = ConfigurationManager.lookup();
52
53         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH), "14");
54
55         // First value from list is picked from Merge properties
56         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_MAXSIZE), "1048576");
57
58         List<String> expectedExtList = new ArrayList<>();
59         expectedExtList.add("pdf");
60         expectedExtList.add("zip");
61         expectedExtList.add("xml");
62         expectedExtList.add("pdf");
63         expectedExtList.add("tgz");
64         expectedExtList.add("xls");
65         List<String> extList = config.getAsStringValues(nameSpace, ConfigTestConstant.ARTIFACT_EXT);
66         Assert.assertEquals(expectedExtList, extList);
67
68         List<String> expectedEncList = new ArrayList<>();
69         expectedEncList.add("Base64");
70         expectedEncList.add("MD5");
71         List<String> encList = config.getAsStringValues(nameSpace, ConfigTestConstant.ARTIFACT_ENC);
72         Assert.assertEquals(expectedEncList, encList);
73
74         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_NAME_UPPER), "a-zA-Z_0-9");
75         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_NAME_LOWER), "a-zA-Z");
76         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_STATUS), "deleted");
77
78         List<String> expectedLocList = new ArrayList<>();
79         expectedLocList.add("/opt/spool");
80         expectedLocList.add(System.getProperty("user.home") + "/asdc");
81         List<String> locList = config.getAsStringValues(nameSpace, ConfigTestConstant.ARTIFACT_LOC);
82         Assert.assertEquals(expectedLocList, locList);
83
84         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_JSON_SCHEMA),
85                 "@GeneratorList.json");
86
87         Assert.assertEquals("@" + getenv(ConfigTestConstant.PATH) + "/myschema.json",
88                 config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_XML_SCHEMA));
89
90         List<String> artifactConsumer = config.getAsStringValues(nameSpace, ConfigTestConstant.ARTIFACT_CONSUMER);
91         Assert.assertEquals(config.getAsStringValues(nameSpace, ConfigTestConstant.ARTIFACT_CONSUMER_APPC),
92                 artifactConsumer);
93
94         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_NAME_MINLENGTH), "6");
95         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_MANDATORY_NAME), "true");
96         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_ENCODED), "true");
97     }
98
99     /**
100      * This to make the behavior of tests consistent with "env:X" in configuration files
101      * when environment variable X is not defined.
102      */
103     public static String getenv(String name) {
104         String value = System.getenv(name);
105         return value == null ? "" : value;
106     }
107 }