19 package org.sleuthkit.autopsy.integrationtesting;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.Collections;
27 import java.util.HashMap;
30 import java.util.function.Supplier;
31 import java.util.logging.Level;
34 import org.yaml.snakeyaml.DumperOptions;
35 import org.yaml.snakeyaml.Yaml;
36 import org.yaml.snakeyaml.introspector.Property;
37 import org.yaml.snakeyaml.nodes.MappingNode;
38 import org.yaml.snakeyaml.nodes.Tag;
39 import org.yaml.snakeyaml.representer.Representer;
45 private static final Logger logger = Logger.getLogger(IntegrationTestService.class.getName());
57 private static <K, V> V getOrCreate(Map<K, V> map, K key, Supplier<V> onNotPresent) {
58 V curValue = map.get(key);
59 if (curValue == null) {
60 curValue = onNotPresent.get();
61 map.put(key, curValue);
69 private final Map<String, Map<String, Map<String, Object>>> data =
new HashMap<>();
79 void addResult(String pkgName, String className, String methodName, Object result) {
80 Map<String, Map<String, Object>> packageClasses = getOrCreate(data, pkgName, () ->
new HashMap<>());
81 Map<String, Object> classMethods = getOrCreate(packageClasses, className, () ->
new HashMap<>());
82 Object toWrite = result instanceof Throwable ?
new ExceptionObject((Throwable) result) : result;
83 classMethods.put(methodName, toWrite);
89 Object getSerializableData() {
90 return Collections.unmodifiableMap(data);
99 private String getCaseTypeId(Case.CaseType caseType) {
100 if (caseType == null) {
105 case SINGLE_USER_CASE:
107 case MULTI_USER_CASE:
110 throw new IllegalArgumentException(
"Unknown case type: " + caseType);
117 private static final Representer MAP_REPRESENTER =
new Representer() {
119 protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
121 if (!classTags.containsKey(javaBean.getClass())) {
122 addClassTag(javaBean.getClass(), Tag.MAP);
125 return super.representJavaBean(properties, javaBean);
132 private static final DumperOptions DUMPER_OPTS =
new DumperOptions() {
135 setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
137 setAllowReadOnlyProperties(
true);
144 private static final Yaml YAML_SERIALIZER =
new Yaml(MAP_REPRESENTER, DUMPER_OPTS);
153 public void serializeToFile(String outputFolder, String caseName, Case.CaseType caseType) {
154 serializeToFile(getSerializationPath(outputFolder, caseName, caseType));
157 private String getSerializationPath(String outputFolder, String caseName, Case.CaseType caseType) {
158 String outputExtension =
".yml";
159 Path outputPath = Paths.get(outputFolder, String.format(
"%s-%s%s", caseName, getCaseTypeId(caseType), outputExtension));
160 return outputPath.toString();
168 void serializeToFile(String outputPath) {
169 File outputFile =
new File(outputPath);
171 if (!outputFile.getParentFile().exists()) {
172 outputFile.getParentFile().mkdirs();
176 FileWriter writer =
new FileWriter(outputFile);
177 YAML_SERIALIZER.dump(getSerializableData(), writer);
178 }
catch (IOException ex) {
179 logger.log(Level.WARNING,
"There was an error writing results to outputPath: " + outputPath, ex);