Autopsy  4.18.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
OutputResults.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.integrationtesting;
20 
21 import java.io.File;
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;
28 import java.util.Map;
29 import java.util.Set;
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;
40 
44 class OutputResults {
45  private static final Logger logger = Logger.getLogger(IntegrationTestService.class.getName());
46 
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);
62  }
63  return curValue;
64  }
65 
69  private final Map<String, Map<String, Map<String, Object>>> data = new HashMap<>();
70 
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);
84  }
85 
89  Object getSerializableData() {
90  return Collections.unmodifiableMap(data);
91  }
92 
99  private String getCaseTypeId(Case.CaseType caseType) {
100  if (caseType == null) {
101  return "";
102  }
103 
104  switch (caseType) {
105  case SINGLE_USER_CASE:
106  return "singleUser";
107  case MULTI_USER_CASE:
108  return "multiUser";
109  default:
110  throw new IllegalArgumentException("Unknown case type: " + caseType);
111  }
112  }
113 
117  private static final Representer MAP_REPRESENTER = new Representer() {
118  @Override
119  protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
120  // don't show class name in yaml
121  if (!classTags.containsKey(javaBean.getClass())) {
122  addClassTag(javaBean.getClass(), Tag.MAP);
123  }
124 
125  return super.representJavaBean(properties, javaBean);
126  }
127  };
128 
132  private static final DumperOptions DUMPER_OPTS = new DumperOptions() {
133  {
134  // Show each property on a new line.
135  setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
136  // allow for serializing properties that only have getters.
137  setAllowReadOnlyProperties(true);
138  }
139  };
140 
144  private static final Yaml YAML_SERIALIZER = new Yaml(MAP_REPRESENTER, DUMPER_OPTS);
145 
153  public void serializeToFile(String outputFolder, String caseName, Case.CaseType caseType) {
154  serializeToFile(getSerializationPath(outputFolder, caseName, caseType));
155  }
156 
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();
161  }
162 
168  void serializeToFile(String outputPath) {
169  File outputFile = new File(outputPath);
170 
171  if (!outputFile.getParentFile().exists()) {
172  outputFile.getParentFile().mkdirs();
173  }
174 
175  try {
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);
180  }
181  }
182 }

Copyright © 2012-2021 Basis Technology. Generated on: Thu Jul 8 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.