19 package org.sleuthkit.autopsy.integrationtesting.config;
21 import com.fasterxml.jackson.databind.JsonNode;
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import com.fasterxml.jackson.databind.type.CollectionType;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.JsonElement;
28 import java.io.IOException;
29 import java.lang.reflect.Type;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.List;
36 import java.util.logging.Level;
38 import java.util.stream.Collectors;
39 import java.util.stream.IntStream;
40 import org.apache.commons.io.FileUtils;
41 import org.apache.commons.lang.StringUtils;
42 import org.apache.commons.collections.CollectionUtils;
52 private static final ObjectMapper
mapper =
new ObjectMapper();
73 String configFileKey = String.join(
".", INTEGRATION_TEST_NAMESPACE, CONFIG_FILE_KEY);
74 if (System.getProperty(configFileKey) != null) {
76 String fileLoc = System.getProperty(configFileKey);
77 File envConfigFile =
new File(fileLoc);
78 if (envConfigFile.exists()) {
81 throw new IllegalStateException(String.format(
"No file exists at %s", fileLoc));
88 }
catch (IllegalStateException ex) {
89 throw new IllegalStateException(
"EnvConfig could not be determined from system property values", ex);
103 Map<String, Object> mapToRet =
new HashMap<>();
105 for (String key : System.getProperties().stringPropertyNames()) {
106 String value = System.getProperty(key);
108 String[] keyPieces = key.split(
"\\.");
109 Map<String, Object> mapToAddTo = mapToRet;
110 for (
int i = 0; i < keyPieces.length - 1; i++) {
111 mapToAddTo =
getOrCreate(mapToAddTo, keyPieces[i]);
114 mapToAddTo.put(keyPieces[keyPieces.length - 1], value);
145 private Map<String, Object>
getOrCreate(Map<String, Object> parent, String key) {
146 Object child = parent.get(key);
148 return (StringObjMap) child;
150 Map<String, Object> toRet =
new StringObjMap();
151 parent.put(key, toRet);
164 public <T> T convertToObj(Map<String, Object> toConvert, Type clazz) {
165 GsonBuilder builder =
new GsonBuilder();
166 Gson gson = builder.create();
167 JsonElement jsonElement = gson.toJsonTree(toConvert);
168 return gson.fromJson(jsonElement, clazz);
200 return validate(envConfigFile, config);
214 if (config.getWorkingDirectory() == null && envConfigFile != null && envConfigFile.getParentFile() != null) {
220 || StringUtils.isBlank(config.getRootCaseOutputPath())
221 || StringUtils.isBlank(config.getRootTestOutputPath())) {
222 throw new IllegalStateException(
"EnvConfig must have the root case output path and the root test output path set.");
240 JsonNode root = mapper.readTree(configFile);
241 if (root.isArray()) {
243 CollectionType listClass = mapper.getTypeFactory().constructCollectionType(List.class,
248 @SuppressWarnings(
"unchecked")
249 List<TestSuiteConfig> testSuites = (List<TestSuiteConfig>) mapper.readValue(mapper.treeAsTokens(root), listClass);
251 return validate(rootDirectory, configFile, testSuites);
256 }
catch (IOException ex) {
257 logger.log(Level.WARNING,
"Unable to read test suite config at " + configFile.getPath(), ex);
258 return Collections.emptyList();
271 if (fileOrDirectory.isDirectory()) {
272 Collection<File> jsonFiles = FileUtils.listFiles(fileOrDirectory,
new String[]{JSON_EXT},
true);
273 return jsonFiles.stream()
275 .collect(Collectors.toList());
276 }
else if (fileOrDirectory.isFile()) {
280 logger.log(Level.WARNING,
"Unable to read file at: {0}", fileOrDirectory);
281 return Collections.emptyList();
295 private List<TestSuiteConfig>
validate(File rootDirectory, File file, List<TestSuiteConfig> testSuites) {
296 return IntStream.range(0, testSuites.size())
297 .mapToObj(idx ->
validate(rootDirectory, file, idx, testSuites.get(idx)))
298 .filter(c -> c != null)
299 .collect(Collectors.toList());
318 || StringUtils.isBlank(config.
getName())
323 logger.log(Level.WARNING, String.format(
"Item in %s at index %d must contain a valid 'name', 'caseTypes', 'dataSources', and 'integrationTests'", file.toString(), index));
329 String relative = rootDirectory.toURI().relativize(file.toURI()).getPath();
330 if (relative.endsWith(
"." + JSON_EXT)) {
331 relative = relative.substring(0, relative.length() - (
"." +
JSON_EXT).length());
String getRelativeOutputPath()
List< TestSuiteConfig > validate(File rootDirectory, File file, List< TestSuiteConfig > testSuites)
static final String CONFIG_FILE_KEY
EnvConfig validate(File envConfigFile, EnvConfig config)
String getWorkingDirectory()
static final String JSON_EXT
void setWorkingDirectory(String workingDirectory)
static final long serialVersionUID
static final String INTEGRATION_TEST_NAMESPACE
Map< String, Object > getSysPropsMap()
EnvConfig getEnvConfig(File envConfigFile)
static String getAbsolutePath(String workingDirectory, String relPath)
String getRootTestSuitesPath()
IntegrationTestConfig getIntegrationTestConfig()
List< TestSuiteConfig > getTestSuiteConfigs(File fileOrDirectory)
static final ObjectMapper mapper
IntegrationCaseType getCaseTypes()
List< TestSuiteConfig > getTestSuiteConfig(File rootDirectory, File configFile)
EnvConfig getEnvConfigFromSysProps()
Map< String, Object > getOrCreate(Map< String, Object > parent, String key)
void setRelativeOutputPath(String relativeOutputPath)
TestingConfig getIntegrationTests()
List< String > getDataSources()
static final Logger logger
synchronized static Logger getLogger(String name)
TestSuiteConfig validate(File rootDirectory, File file, int index, TestSuiteConfig config)