28611d44c8161ca94bc8ae2153212179a361480f
[sdc.git] /
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.nio.file.Files;
23 import java.nio.file.FileVisitResult;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.nio.file.SimpleFileVisitor;
27 import java.nio.file.attribute.BasicFileAttributes;
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.junit.Assert;
31 import org.onap.config.api.Configuration;
32 import org.onap.config.api.ConfigurationManager;
33
34 /**
35  * Created by sheetalm on 10/13/2016.
36  */
37 public class TestUtil {
38
39     public static final String jsonSchemaLoc = System.getProperty("user.home") + "/TestResources/";
40
41     public static void cleanUp() throws Exception {
42         String data = "{name:\"SCM\"}";
43         TestUtil.writeFile(data);
44     }
45
46     public static void writeFile(String data) throws IOException {
47         File dir = new File(jsonSchemaLoc);
48         dir.mkdirs();
49         File file = new File(jsonSchemaLoc + "/GeneratorsList.json");
50         file.createNewFile();
51         FileWriter fileWriter = new FileWriter(file);
52         fileWriter.write(data);
53         fileWriter.close();
54     }
55
56     public static void validateConfiguration(String nameSpace) {
57         Configuration config = ConfigurationManager.lookup();
58
59         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH), "14");
60
61         // First value from list is picked from Merge properties
62         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_MAXSIZE), "1048576");
63
64         List<String> expectedExtList = new ArrayList<>();
65         expectedExtList.add("pdf");
66         expectedExtList.add("zip");
67         expectedExtList.add("xml");
68         expectedExtList.add("pdf");
69         expectedExtList.add("tgz");
70         expectedExtList.add("xls");
71         List<String> extList = config.getAsStringValues(nameSpace, ConfigTestConstant.ARTIFACT_EXT);
72         Assert.assertEquals(expectedExtList, extList);
73
74         List<String> expectedEncList = new ArrayList<>();
75         expectedEncList.add("Base64");
76         expectedEncList.add("MD5");
77         List<String> encList = config.getAsStringValues(nameSpace, ConfigTestConstant.ARTIFACT_ENC);
78         Assert.assertEquals(expectedEncList, encList);
79
80         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_NAME_UPPER), "a-zA-Z_0-9");
81         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_NAME_LOWER), "a-zA-Z");
82         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_STATUS), "deleted");
83
84         List<String> expectedLocList = new ArrayList<>();
85         expectedLocList.add("/opt/spool");
86         expectedLocList.add(System.getProperty("user.home") + "/asdc");
87         List<String> locList = config.getAsStringValues(nameSpace, ConfigTestConstant.ARTIFACT_LOC);
88         Assert.assertEquals(expectedLocList, locList);
89
90         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_JSON_SCHEMA),
91                 "@GeneratorList.json");
92
93         Assert.assertEquals("@" + getenv(ConfigTestConstant.PATH) + "/myschema.json",
94                 config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_XML_SCHEMA));
95
96         List<String> artifactConsumer = config.getAsStringValues(nameSpace, ConfigTestConstant.ARTIFACT_CONSUMER);
97         Assert.assertEquals(config.getAsStringValues(nameSpace, ConfigTestConstant.ARTIFACT_CONSUMER_APPC),
98                 artifactConsumer);
99
100         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_NAME_MINLENGTH), "6");
101         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_MANDATORY_NAME), "true");
102         Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_ENCODED), "true");
103     }
104
105     /**
106      * This to make the behavior of tests consistent with "env:X" in configuration files
107      * when environment variable X is not defined.
108      */
109     public static String getenv(String name) {
110         String value = System.getenv(name);
111         return value == null ? "" : value;
112     }
113
114     /**
115      * Creates temporary directories structure with files inside every directory
116      *
117      * @param tmpDirPrefix
118      * @return
119      * @throws IOException
120      */
121     public static Path createTestDirsStructure(String tmpDirPrefix) throws IOException {
122         Path tmpPath = Files.createTempDirectory(tmpDirPrefix);
123         Path dir0 = Paths.get(tmpPath.toString(), "dir0", "dir1", "dir2");
124         Files.createDirectories(dir0);
125         Path[] files= {
126                 Paths.get(tmpPath.toString(), "file001"),
127                 Paths.get(tmpPath.toString(), "dir0", "file002"),
128                 Paths.get(tmpPath.toString(), "dir0", "dir1", "file003"),
129                 Paths.get(tmpPath.toString(), "dir0", "dir1", "dir2", "file004"),
130         };
131         for (Path file : files ) {
132             Files.createFile(file);
133         }
134         return tmpPath;
135     }
136
137     public static Path createEmptyTmpDir(String prefix) throws IOException {
138         return Files.createTempDirectory(prefix);
139     }
140
141     /**
142      * Delete all tmp directories and files created for testing
143      *
144      * @param rootPath
145      */
146     public static void deleteTestDirsStrucuture(Path rootPath) {
147         try {
148             Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
149                 @Override
150                 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
151                     Files.delete(dir);
152                     return FileVisitResult.CONTINUE;
153                 }
154
155                 @Override
156                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
157                     Files.delete(file);
158                     return FileVisitResult.CONTINUE;
159                 }
160             });
161         } catch (IOException e) {
162             e.printStackTrace();
163         }
164     }
165 }