Skip to content

Commit

Permalink
fix NPE in json replace filter
Browse files Browse the repository at this point in the history
  • Loading branch information
sokomishalov committed Nov 25, 2021
1 parent 01c8a81 commit 5e87528
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ public BodyFilter replace(final UnaryOperator<String> replacementFunction) {
return filter(context -> context.map(path, (node, config) -> node == null ? NullNode.getInstance() : new TextNode(replacementFunction.apply(node.toString()))));
}

public BodyFilter replace(
final Pattern pattern, final String replacement) {

public BodyFilter replace(final Pattern pattern, final String replacement) {
return filter(context -> context.map(path, (node, config) -> {
if (node == null) {
return NullNode.getInstance();
}

final Matcher matcher = pattern.matcher(node.toString());

if (matcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.zalando.logbook.json;

import com.fasterxml.jackson.databind.node.DoubleNode;
import com.google.common.io.Resources;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Configuration.Defaults;
Expand All @@ -10,6 +9,7 @@
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;

import lombok.SneakyThrows;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.zalando.logbook.BodyFilter;
Expand All @@ -28,13 +28,14 @@
import static org.zalando.logbook.json.JsonBodyFilters.accessToken;
import static org.zalando.logbook.json.JsonPathBodyFilters.jsonPath;

@SuppressWarnings("UnstableApiUsage")
class JsonPathBodyFiltersTest {

private final String type = "application/json";
private static final String JSON = "application/json";
private final String student;

@SuppressWarnings("UnstableApiUsage")
JsonPathBodyFiltersTest() throws IOException {
@SneakyThrows
JsonPathBodyFiltersTest() {
this.student = Resources.toString(getResource("student.json"), UTF_8);
}

Expand Down Expand Up @@ -64,7 +65,7 @@ void deletesNumberAndString() {
final BodyFilter unit = jsonPath("$.id").delete()
.tryMerge(jsonPath("$.name").delete());

with(requireNonNull(unit).filter(type, student))
with(requireNonNull(unit).filter(JSON, student))
.assertNotDefined("id")
.assertNotDefined("name");
}
Expand All @@ -73,79 +74,79 @@ void deletesNumberAndString() {
void deletesArray() {
final BodyFilter unit = jsonPath("$.friends").delete();

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertNotDefined("friends");
}

@Test
void deletesObject() {
final BodyFilter unit = jsonPath("$.grades").delete();

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertNotDefined("grades");
}

@Test
void replacesArrayWithString() {
final BodyFilter unit = jsonPath("$.friends").replace("XXX");

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("friends", "XXX");
}

@Test
void replacesNumberWithString() {
final BodyFilter unit = jsonPath("$.id").replace("XXX");

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("id", "XXX");
}

@Test
void replacesArrayWithNumber() {
final BodyFilter unit = jsonPath("$.friends").replace(0.0);

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("friends", 0.0);
}

@Test
void replacesNumberWithNumbers() {
final BodyFilter unit = jsonPath("$.grades.English").replace(1.0);

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("grades.English", 1.0);
}

@Test
void replacesArrayWithBoolean() {
final BodyFilter unit = jsonPath("$.friends").replace(false);

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("friends", false);
}

@Test
void replacesNumberWithBoolean() {
final BodyFilter unit = jsonPath("$.id").replace(true);

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("id", true);
}

@Test
void replacesStringDynamically() {
final BodyFilter unit = jsonPath("$.name").replace(compile("^(\\w).+"), "$1.");

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("name", "A.");
}

@Test
void replacesArrayDynamically() {
final BodyFilter unit = jsonPath("$.friends.*.name").replace(compile("^(\\w).+"), "$1.");

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("friends[0].name", "B.")
.assertEquals("friends[1].name", "C.");
}
Expand All @@ -154,15 +155,15 @@ void replacesArrayDynamically() {
void fallsBackTorReplaceArrayAsString() {
final BodyFilter unit = jsonPath("$.friends").replace(compile("([A-Z])[a-z]+"), "$1.");

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("friends", "[{\"id\":2,\"name\":\"B.\"},{\"id\":3,\"name\":\"C.\"}]");
}

@Test
void replacesObjectDynamically() {
final BodyFilter unit = jsonPath("$.grades.*").replace("XXX");

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("grades.Math", "XXX")
.assertEquals("grades.English", "XXX")
.assertEquals("grades.Science", "XXX")
Expand All @@ -173,23 +174,23 @@ void replacesObjectDynamically() {
void replacesValuesDynamically() {
final BodyFilter unit = jsonPath("$.name").replace(String::toUpperCase);

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("name", "ALICE");
}

@Test
void replacesValuesDynamicallyWithNullValue() {
final BodyFilter unit = jsonPath("$.nickname").replace(String::toUpperCase);

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("nickname", null);
}

@Test
void replacesArrayValuesDynamically() {
final BodyFilter unit = jsonPath("$.friends.*.name").replace(String::toUpperCase);

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("friends[0].name", "BOB")
.assertEquals("friends[1].name", "CHARLIE");
}
Expand All @@ -198,23 +199,23 @@ void replacesArrayValuesDynamically() {
void fallsBackTorReplaceObjectAsString() {
final BodyFilter unit = jsonPath("$.grades").replace(compile("(\\d+)\\.\\d+"), "$1.X");

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("grades", "{\"Math\":1.X,\"English\":2.X,\"Science\":1.X,\"PE\":4.X}");
}

@Test
void leavesNonMatchingNumberInPlace() {
final BodyFilter unit = jsonPath("$.id").replace(compile("\\s+"), "XXX");

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("id", 1);
}

@Test
void leavesNonMatchingStringInPlace() {
final BodyFilter unit = jsonPath("$.name").replace(compile("\\s+"), "XXX");

with(unit.filter(type, student))
with(unit.filter(JSON, student))
.assertEquals("name", "Alice");
}

Expand Down Expand Up @@ -247,14 +248,14 @@ void doesNotFailWhenBodyIsUnwrappedArray() throws IOException {

final BodyFilter unit = jsonPath("$.name").replace(String::toUpperCase);

unit.filter(type, cars);
unit.filter(JSON, cars);
}

@Test
void doesNotFailWhenBodyIsEmpty() {
final BodyFilter unit = jsonPath("$.id").replace(compile("\\s+"), "XXX");

String actual = unit.filter(type, "");
String actual = unit.filter(JSON, "");

assertThat(actual).isEmpty();
}
Expand All @@ -264,7 +265,17 @@ void shouldReturnSameBodyWhenBodyIsInvalidJson() {
String invalidBody = "{\"id\": 1, \"name\": \"Alice\",}";
final BodyFilter unit = jsonPath("$.id").replace(compile("\\s+"), "XXX");

String actual = unit.filter(type, invalidBody);
String actual = unit.filter(JSON, invalidBody);

assertThat(actual).isEqualTo(invalidBody);
}

@Test
void shouldNotFailWhenThereAreNullNodes() {
String invalidBody = "{\"name\":null}";
final BodyFilter unit = jsonPath("$.name").replace(compile("\\s+"), "XXX");

String actual = unit.filter(JSON, invalidBody);

assertThat(actual).isEqualTo(invalidBody);
}
Expand Down

0 comments on commit 5e87528

Please sign in to comment.