Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snapshot Transformer #223

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@
<version>1.4.11</version>
</dependency>

<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.6.0</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.List;
import java.util.Locale;

import com.askimed.nf.test.util.ObjectUtil;
import groovy.lang.Closure;
import junit.framework.AssertionFailedError;

Expand Down Expand Up @@ -59,4 +63,13 @@ public static void assertContainsInAnyOrder(List<Object> list, List<Object> expe
throw new PowerAssertionError(thrown.getMessage());
}
}

public static String format(String format, Number number) {
DecimalFormat df = new DecimalFormat(format, DecimalFormatSymbols.getInstance(Locale.US));
return df.format(number);
}

public static String md5(Object object) {
return ObjectUtil.getMd5(object);
}
}
67 changes: 64 additions & 3 deletions src/main/java/com/askimed/nf/test/lang/extensions/Snapshot.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.askimed.nf.test.lang.extensions;

import com.askimed.nf.test.core.ITest;
import com.askimed.nf.test.util.ObjectUtil;
import com.askimed.nf.test.util.*;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import groovy.lang.Closure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Snapshot {

Expand All @@ -17,8 +22,11 @@ public class Snapshot {

private static Logger log = LoggerFactory.getLogger(Snapshot.class);

public static String ROOT_OBJECT = "contents";

public Snapshot(Object actual, ITest test) {
this.actual = actual;
//this.actual = actual;
this.actual = ObjectUtil.toMap(actual);
this.file = test.getTestSuite().getSnapshot();
this.test = test;
}
Expand Down Expand Up @@ -72,8 +80,61 @@ public boolean match(String id) throws IOException {

}

public void view() {
private DocumentContext createDocumentContext() {
Map<String, Object> snapshots = new HashMap<String, Object>();
snapshots.put(ROOT_OBJECT, actual);
return JsonPath.parse(snapshots);
}

public Snapshot replace(Object value) {
return replace("$..*", value);
}

public Snapshot replace(String selector, Object value) {
DocumentContext json = createDocumentContext();
json.set(selector, value);
return this;
}

public Snapshot map(Closure closure) {
return map("$..*", closure);
}

public Snapshot map(String selector, Closure closure) {
DocumentContext json = createDocumentContext();
json.map(selector, (currentValue, configuration) -> {
return closure.call(currentValue);
});
return this;
}

public Snapshot traverse(Closure closure) {

MapTraverser.traverse(ROOT_OBJECT, actual, new MapOperation() {
@Override
public Object map(String path, Object value) {
return closure.call(path, value);
}
});

return this;
}

public Snapshot remove(String selector) {
DocumentContext json = createDocumentContext();
json.delete(selector);
return this;
}

public Snapshot view() {
return view(false);
}

public Snapshot view(boolean raw) {
String json = raw ? ObjectUtil.toJsonRaw(actual) : ObjectUtil.toJson(actual);
System.out.println();
System.out.println(AnsiText.padding(AnsiColors.cyan("Snapshot: \n" + json), 6));
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
import java.util.Map;
import java.util.Set;

import com.askimed.nf.test.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.askimed.nf.test.core.ITestSuite;
import com.askimed.nf.test.lang.extensions.util.PathConverter;

import groovy.json.JsonGenerator;
import groovy.json.JsonOutput;
import groovy.json.JsonSlurper;

public class SnapshotFile {
Expand Down Expand Up @@ -127,9 +125,7 @@ private void removeSnapshots(Set<String> obsoleteSnapshots) {
}

public void save() throws IOException {
JsonGenerator jsonGenerator = createJsonGenerator();
String json = jsonGenerator.toJson(snapshots);
String prettyJson = JsonOutput.prettyPrint(json);
String prettyJson = ObjectUtil.toJson(snapshots);
File file = new File(filename);
FileWriter writer;
writer = new FileWriter(file);
Expand All @@ -142,10 +138,4 @@ protected static String createFilename(ITestSuite suite) {
return suite.getFilename() + EXTENSION;
}

public static JsonGenerator createJsonGenerator() {
JsonGenerator jsonGenerator = new JsonGenerator.Options().excludeFieldsByName("mapping")
.addConverter(new PathConverter()).build();
return jsonGenerator;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import com.askimed.nf.test.lang.extensions.util.SnapshotDiffUtil;

import com.askimed.nf.test.nextflow.NextflowCommand;
import groovy.json.JsonGenerator;
import groovy.json.JsonOutput;
import com.askimed.nf.test.util.ObjectUtil;

public class SnapshotFileItem {

Expand Down Expand Up @@ -76,11 +75,7 @@ public static String createTimestamp() {

@Override
public String toString() {
JsonGenerator jsonGenerator = SnapshotFile.createJsonGenerator();
String json = jsonGenerator.toJson(getContent());
String prettyJson = JsonOutput.prettyPrint(json);
return prettyJson;
return ObjectUtil.toJson(getContent());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.askimed.nf.test.lang.extensions.util;

import com.askimed.nf.test.lang.extensions.PathExtension;
import groovy.json.JsonGenerator.Converter;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Vector;

public class PathConverterRaw implements Converter {

@Override
public boolean handles(Class<?> type) {
return true;
}

@Override
public Object convert(Object value, String key) {
Path path = null;
if (value instanceof Path) {
path = (Path) value;
if (!path.toFile().exists()) {
throw new RuntimeException("Path " + path.toString() + " not found.");
}
} else if (value instanceof File) {
path = ((File) value).toPath();
if (!path.toFile().exists()) {
throw new RuntimeException("Path " + path.toString() + " not found.");
}
} else {
path = new File(value.toString()).toPath();

}

if (path.toFile().exists()) {
return path.toFile().getAbsolutePath();
}
return value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import java.io.IOException;
import java.util.*;

import com.askimed.nf.test.lang.extensions.SnapshotFile;
import com.askimed.nf.test.lang.extensions.SnapshotFileItem;
import com.askimed.nf.test.util.BinaryFinder;
import com.askimed.nf.test.util.Command;
import com.askimed.nf.test.util.FileUtil;

import groovy.json.JsonGenerator;
import groovy.json.JsonOutput;
import com.askimed.nf.test.util.ObjectUtil;

public class SnapshotDiffUtil {

Expand Down Expand Up @@ -91,9 +89,7 @@ public static String getSimpleOutput(SnapshotFileItem expected, SnapshotFileItem
}

private static void writeSnapshotToFile(File file, SnapshotFileItem snapshot) throws IOException {
JsonGenerator jsonGenerator = SnapshotFile.createJsonGenerator();
String json = jsonGenerator.toJson(snapshot.getContent());
String prettyJson = JsonOutput.prettyPrint(json);
String prettyJson = ObjectUtil.toJson(snapshot.getContent());
FileWriter fileWriter = new FileWriter(file);
fileWriter.append(prettyJson);
fileWriter.close();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/askimed/nf/test/util/MapOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.askimed.nf.test.util;

public interface MapOperation {
public Object map(String path, Object value);
}
41 changes: 41 additions & 0 deletions src/main/java/com/askimed/nf/test/util/MapTraverser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.askimed.nf.test.util;

import java.util.*;

public class MapTraverser {

public static void traverse(String prefix, Object root, MapOperation operation) {
traverseRecursive(root, prefix, operation);
}

private static void traverseRecursive(Object node, String path, MapOperation operation) {
if (node instanceof Map) {
Map<String, Object> map = (Map<String, Object>) node;
for (Map.Entry<String, Object> entry : new HashSet<>(map.entrySet())) {
String key = entry.getKey();
Object value = entry.getValue();
String currentPath = path.isEmpty() ? key : path + "." + key;
traverseRecursive(value, currentPath, operation);
if (!(value instanceof Map) && !(value instanceof List)) {
Object newValue = operation.map(currentPath, value);
if (newValue != null) {
map.put(key, newValue);
}
}
}
} else if (node instanceof List) {
List<Object> list = (List<Object>) node;
for (int i = 0; i < list.size(); i++) {
Object value = list.get(i);
String currentPath = path + "[" + i + "]";
traverseRecursive(value, currentPath, operation);
if (!(value instanceof Map) && !(value instanceof List)) {
Object newValue = operation.map(currentPath, value);
if (newValue != null) {
list.set(i, newValue);
}
}
}
}
}
}
35 changes: 34 additions & 1 deletion src/main/java/com/askimed/nf/test/util/ObjectUtil.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.askimed.nf.test.util;

import com.askimed.nf.test.lang.extensions.SnapshotFile;
import com.askimed.nf.test.lang.extensions.util.PathConverter;
import com.askimed.nf.test.lang.extensions.util.PathConverterRaw;
import groovy.json.JsonGenerator;
import groovy.json.JsonOutput;
import groovy.json.JsonSlurper;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class ObjectUtil {

public static String getMd5(Object object) {
JsonGenerator jsonGenerator = SnapshotFile.createJsonGenerator();
JsonGenerator jsonGenerator = createJsonGenerator();
String json = jsonGenerator.toJson(object);
try {
return calculateMD5(JsonOutput.prettyPrint(json));
Expand Down Expand Up @@ -38,4 +41,34 @@ public static String calculateMD5(String input) throws NoSuchAlgorithmException

return result.toString();
}


public static String toJson(Object object) {
JsonGenerator jsonGenerator = createJsonGenerator();
String json = jsonGenerator.toJson(object);
return JsonOutput.prettyPrint(json);
}

public static String toJsonRaw(Object object) {
JsonGenerator jsonGenerator = createJsonGeneratorRaw();
String json = jsonGenerator.toJson(object);
return JsonOutput.prettyPrint(json);
}

public static JsonGenerator createJsonGenerator() {
return new JsonGenerator.Options().excludeFieldsByName("mapping")
.addConverter(new PathConverter()).build();
}

public static JsonGenerator createJsonGeneratorRaw() {
return new JsonGenerator.Options().excludeFieldsByName("mapping")
.addConverter(new PathConverterRaw()).build();
}

public static Object toMap(Object object) {
JsonGenerator jsonGenerator = createJsonGeneratorRaw();
String json = jsonGenerator.toJson(object);
return new JsonSlurper().parseText(json);
}

}
Loading
Loading