Skip to content

✨ Add Snapshot.ignore(String... paths) method #7

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

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 20 additions & 0 deletions src/main/java/io/github/jsonSnapshot/Snapshot.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.github.jsonSnapshot;

import lombok.Getter;
import org.assertj.core.util.diff.DiffUtils;
import org.assertj.core.util.diff.Patch;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

Expand All @@ -19,6 +21,7 @@ public class Snapshot {

private Function<Object, String> jsonFunction;

@Getter
private Object[] current;

Snapshot(SnapshotFile snapshotFile, Class clazz, Method method, Function<Object, String> jsonFunction, Object... current) {
Expand Down Expand Up @@ -76,4 +79,21 @@ private String takeSnapshot() {
public String getSnapshotName() {
return clazz.getName() + "." + method.getName() + "=";
}

/**
* Ignore nested fields in the {@link #current} objects.
*
* @param pathsToIgnore Dot delimited paths to ignore within the {@link #current} objects. For example {@code nested.field}.
* @return self
* @throws SnapshotMatchException if the {@link #current} objects are not nested {@link Map}s.
*/
public Snapshot ignoring(String... pathsToIgnore) throws SnapshotMatchException {
for (String path : pathsToIgnore) {
for (Object object : current) {
SnapshotUtils.deleteNestedFieldFromMap(object, path);
}
}

return this;
}
}
30 changes: 30 additions & 0 deletions src/main/java/io/github/jsonSnapshot/SnapshotUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -82,4 +84,32 @@ private static <T> HashMap<String, List<LinkedHashMap<String, Object>>> process(

return result;
}

public static void deleteNestedFieldFromMap(Object object, String path) {
deleteNestedFieldFromMap(object, Arrays.asList(path.split("\\.")));
}

private static void deleteNestedFieldFromMap(Object object, List<String> pathSegments) {
if (!(object instanceof Map)) {
throw new SnapshotMatchException("object is not a map");
}

Map map = (Map) object;
int numberOfSegments = pathSegments.size();

if (numberOfSegments == 0) {
return;
}

String key = pathSegments.get(0);

if (numberOfSegments == 1) {
if (map.containsKey(key)) {
map.put(key, "IGNORED");
}
return;
}

deleteNestedFieldFromMap(map.get(key), pathSegments.subList(1, numberOfSegments));
}
}
8 changes: 8 additions & 0 deletions src/test/resources/ignoreFieldsExpected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"topLevel": "IGNORED",
"nested": {
"field": "IGNORED",
"notIgnored": "unchanged"
},
"notIgnored": "unchanged"
}
8 changes: 8 additions & 0 deletions src/test/resources/ignoreFieldsOriginal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"topLevel": "X",
"nested": {
"field": "Y",
"notIgnored": "unchanged"
},
"notIgnored": "unchanged"
}