Skip to content

Commit

Permalink
#274 JSON use relative paths form .json file for screenshots etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Schneck committed Feb 2, 2018
1 parent df2f721 commit 0aa6a61
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.sakuli.services.forwarder.json.serializer.DateTimeSerializer;
import org.sakuli.services.forwarder.json.serializer.PathSerializer;
import org.sakuli.services.forwarder.json.serializer.ThrowableSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.file.Path;
Expand All @@ -39,6 +40,9 @@
@Component
public class GsonOutputBuilder extends AbstractOutputBuilder {

@Autowired
private JsonProperties jsonProperties;

/**
* Converts the current test suite object to a json string.
*
Expand All @@ -50,7 +54,7 @@ public String createOutput() throws SakuliForwarderException {
.setExclusionStrategies(new GsonExclusionStrategy())
.registerTypeAdapter(Date.class, new DateSerializer())
.registerTypeAdapter(DateTime.class, new DateTimeSerializer())
.registerTypeAdapter(Path.class, new PathSerializer())
.registerTypeAdapter(Path.class, new PathSerializer(jsonProperties.getOutputJsonDir()))
.registerTypeHierarchyAdapter(Throwable.class, new ThrowableSerializer())
.serializeNulls()
.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Created by georgi on 27/09/17.
*/
Expand All @@ -32,8 +35,8 @@ public class JsonProperties {
@Value("${" + OUTPUT_DIR + "}")
private String outputJsonDir;

public String getOutputJsonDir() {
return outputJsonDir;
public Path getOutputJsonDir() {
return Paths.get(outputJsonDir);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand Down Expand Up @@ -69,25 +67,22 @@ public void saveAllResults() {
}

protected Path createJsonFilePath() throws SakuliForwarderException {
String outputDirAsString = jsonProperties.getOutputJsonDir();
createDirectoryIfNotExists(outputDirAsString);
String fileName = new StringBuilder()
.append(testSuite.getId())
.append("_")
.append(JSON_FILE_DATE_FORMAT.format(new Date()))
.append(".json")
.toString();
return Paths.get(outputDirAsString + File.separator + fileName);
Path outputDir = jsonProperties.getOutputJsonDir();
createDirectoryIfNotExists(outputDir);
String fileName = testSuite.getId() +
"_" +
JSON_FILE_DATE_FORMAT.format(new Date()) +
".json";
return outputDir.resolve(fileName);
}

protected void createDirectoryIfNotExists(String outputDirAsString) throws SakuliForwarderException {
Path outputDir = Paths.get(outputDirAsString);
protected void createDirectoryIfNotExists(Path outputDir) throws SakuliForwarderException {
if (!Files.exists(outputDir)) {
try {
Files.createDirectories(outputDir);
} catch (IOException e) {
throw new SakuliForwarderException(e,
String.format("Unexpected error during creating the json output directory '%s'", outputDirAsString));
String.format("Unexpected error during creating the json output directory '%s'", outputDir.toString()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Type;
import java.nio.file.Path;
Expand All @@ -31,8 +33,22 @@
* Created by georgi on 27/09/17.
*/
public final class PathSerializer implements JsonSerializer<Path> {
private final static Logger LOGGER = LoggerFactory.getLogger(PathSerializer.class);
private final Path basePath;

public PathSerializer(Path basePath) {
this.basePath = basePath.toAbsolutePath().normalize();
LOGGER.debug("PathSerializer base path: {}", this.basePath);
}

@Override
public JsonElement serialize(final Path src, final Type typeOfSrc, final JsonSerializationContext context) {
return new JsonPrimitive(src == null ? StringUtils.EMPTY : src.toAbsolutePath().normalize().toString());
if (src == null) {
return new JsonPrimitive(StringUtils.EMPTY);
}
final Path normalized = src.toAbsolutePath().normalize();
Path relativePath = basePath.relativize(normalized);
LOGGER.debug("Relativized Path: '{}' => '{}'", normalized, relativePath);
return new JsonPrimitive(relativePath.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
*/
public class PathSerializerTest {

PathSerializer testling = new PathSerializer();
PathSerializer testling = new PathSerializer(Paths.get(".").resolve("baseFolder"));

@DataProvider
public Object[][] serializeDP() {
return new Object[][]{
{null, ""},
{Paths.get("testPath"), Paths.get("testPath").toAbsolutePath().normalize().toString()},
{Paths.get("testPath/sub"), Paths.get("testPath").resolve("sub").toAbsolutePath().normalize().toString()},
{Paths.get("testPath" + File.separator + "sub"), Paths.get("testPath").resolve("sub").toAbsolutePath().normalize().toString()},
{Paths.get("./testPath"), ".." + File.separator + "testPath"},
{Paths.get("./testPath/sub"), ".." + File.separator + "testPath" + File.separator + "sub"},
{Paths.get("." + File.separator + "testPath" + File.separator + "sub"), ".." + File.separator + "testPath" + File.separator + "sub"},
};
}

Expand Down

0 comments on commit 0aa6a61

Please sign in to comment.