2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.config.util;
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;
35 * Created by sheetalm on 10/13/2016.
37 public class TestUtil {
39 public static final String jsonSchemaLoc = System.getProperty("user.home") + "/TestResources/";
41 public static void cleanUp() throws Exception {
42 String data = "{name:\"SCM\"}";
43 TestUtil.writeFile(data);
46 public static void writeFile(String data) throws IOException {
47 File dir = new File(jsonSchemaLoc);
49 File file = new File(jsonSchemaLoc + "/GeneratorsList.json");
51 FileWriter fileWriter = new FileWriter(file);
52 fileWriter.write(data);
56 public static void validateConfiguration(String nameSpace) {
57 Configuration config = ConfigurationManager.lookup();
59 Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH), "14");
61 // First value from list is picked from Merge properties
62 Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_MAXSIZE), "1048576");
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);
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);
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");
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);
90 Assert.assertEquals(config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_JSON_SCHEMA),
91 "@GeneratorList.json");
93 Assert.assertEquals("@" + getenv(ConfigTestConstant.PATH) + "/myschema.json",
94 config.getAsString(nameSpace, ConfigTestConstant.ARTIFACT_XML_SCHEMA));
96 List<String> artifactConsumer = config.getAsStringValues(nameSpace, ConfigTestConstant.ARTIFACT_CONSUMER);
97 Assert.assertEquals(config.getAsStringValues(nameSpace, ConfigTestConstant.ARTIFACT_CONSUMER_APPC),
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");
106 * This to make the behavior of tests consistent with "env:X" in configuration files
107 * when environment variable X is not defined.
109 public static String getenv(String name) {
110 String value = System.getenv(name);
111 return value == null ? "" : value;
115 * Creates temporary directories structure with files inside every directory
117 * @param tmpDirPrefix
119 * @throws IOException
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);
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"),
131 for (Path file : files ) {
132 Files.createFile(file);
137 public static Path createEmptyTmpDir(String prefix) throws IOException {
138 return Files.createTempDirectory(prefix);
142 * Delete all tmp directories and files created for testing
146 public static void deleteTestDirsStrucuture(Path rootPath) {
148 Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
150 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
152 return FileVisitResult.CONTINUE;
156 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
158 return FileVisitResult.CONTINUE;
161 } catch (IOException e) {