|
1 | 1 | /*
|
2 |
| - * Copyright 2017 LINE Corporation |
| 2 | + * Copyright 2020 LINE Corporation |
3 | 3 | *
|
4 | 4 | * LINE Corporation licenses this file to you under the Apache License,
|
5 | 5 | * version 2.0 (the "License"); you may not use this file except in compliance
|
|
38 | 38 |
|
39 | 39 | import java.io.IOException;
|
40 | 40 | import java.net.URL;
|
41 |
| -import java.util.ArrayList; |
42 |
| -import java.util.Iterator; |
43 | 41 | import java.util.List;
|
| 42 | +import java.util.function.Predicate; |
44 | 43 |
|
45 |
| -import org.testng.annotations.DataProvider; |
46 |
| -import org.testng.annotations.Test; |
| 44 | +import org.junit.jupiter.api.Order; |
| 45 | +import org.junit.jupiter.params.ParameterizedTest; |
| 46 | +import org.junit.jupiter.params.provider.Arguments; |
| 47 | +import org.junit.jupiter.params.provider.MethodSource; |
47 | 48 |
|
48 | 49 | import com.fasterxml.jackson.databind.JsonNode;
|
49 | 50 | import com.fasterxml.jackson.databind.ObjectMapper;
|
50 | 51 | import com.google.common.base.Equivalence;
|
51 |
| -import com.google.common.base.Predicate; |
| 52 | +import com.google.common.collect.ImmutableList; |
52 | 53 |
|
53 |
| -public final class JsonPatchGenerationTest { |
| 54 | +class JsonPatchGenerationTest { |
54 | 55 |
|
| 56 | + private static final ObjectMapper MAPPER = new ObjectMapper(); |
55 | 57 | private static final Equivalence<JsonNode> EQUIVALENCE = JsonNumEquals.getInstance();
|
56 | 58 |
|
57 |
| - private final JsonNode testData; |
| 59 | + @Order(1) |
| 60 | + @ParameterizedTest |
| 61 | + @MethodSource("patches") |
| 62 | + void patchAppliesCleanly(JsonNode source, JsonNode target) { |
| 63 | + final JsonPatch patch = JsonPatch.generate(source, target, ReplaceMode.SAFE); |
| 64 | + final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(target)::apply; |
| 65 | + final JsonNode actual = patch.apply(source); |
58 | 66 |
|
59 |
| - public JsonPatchGenerationTest() throws IOException { |
60 |
| - final String resource = "/jsonpatch/diff/diff.json"; |
61 |
| - final URL url = getClass().getResource(resource); |
62 |
| - final ObjectMapper objectMapper = new ObjectMapper(); |
63 |
| - testData = objectMapper.readTree(url); |
| 67 | + assertThat(predicate.test(actual)) |
| 68 | + .withFailMessage("Generated patch failed to apply\nexpected: %s\nactual: %s", target, actual) |
| 69 | + .isTrue(); |
64 | 70 | }
|
65 | 71 |
|
66 |
| - @DataProvider |
67 |
| - public Iterator<Object[]> getPatchesOnly() { |
68 |
| - final List<Object[]> list = new ArrayList<>(); |
69 |
| - for (final JsonNode node : testData) { |
70 |
| - list.add(new Object[] { node.get("first"), node.get("second") }); |
71 |
| - } |
| 72 | + @Order(2) |
| 73 | + @ParameterizedTest |
| 74 | + @MethodSource("literalPatches") |
| 75 | + void expectedPatches(String message, JsonNode source, JsonNode target, JsonNode expected) { |
| 76 | + final JsonNode actual = JsonPatch.generate(source, target, ReplaceMode.SAFE).toJson(); |
| 77 | + final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(expected)::apply; |
72 | 78 |
|
73 |
| - return list.iterator(); |
| 79 | + assertThat(predicate.test(actual)) |
| 80 | + .withFailMessage("Patch is not what was expected\nscenario: %s\nexpected: %s\nactual: %s\n", |
| 81 | + message, expected, actual) |
| 82 | + .isTrue(); |
74 | 83 | }
|
75 | 84 |
|
76 |
| - @Test(dataProvider = "getPatchesOnly") |
77 |
| - public void generatedPatchAppliesCleanly(final JsonNode source, final JsonNode target) { |
78 |
| - final JsonPatch patch = JsonPatch.generate(source, target, ReplaceMode.SAFE); |
79 |
| - final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(target); |
80 |
| - final JsonNode actual = patch.apply(source); |
| 85 | + private static List<Arguments> patches() throws Exception { |
| 86 | + final ImmutableList.Builder<Arguments> arguments = ImmutableList.builder(); |
81 | 87 |
|
82 |
| - assertThat(predicate.apply(actual)).overridingErrorMessage( |
83 |
| - "Generated patch failed to apply\nexpected: %s\nactual: %s", |
84 |
| - target, actual |
85 |
| - ).isTrue(); |
| 88 | + for (JsonNode node : getNode()) { |
| 89 | + arguments.add(Arguments.of(node.get("first"), node.get("second"))); |
| 90 | + } |
| 91 | + |
| 92 | + return arguments.build(); |
86 | 93 | }
|
87 | 94 |
|
88 |
| - @DataProvider |
89 |
| - public Iterator<Object[]> getLiteralPatches() { |
90 |
| - final List<Object[]> list = new ArrayList<>(); |
91 |
| - for (final JsonNode node : testData) { |
| 95 | + private static List<Arguments> literalPatches() throws Exception { |
| 96 | + final ImmutableList.Builder<Arguments> arguments = ImmutableList.builder(); |
| 97 | + |
| 98 | + for (JsonNode node : getNode()) { |
92 | 99 | if (!node.has("patch")) {
|
93 | 100 | continue;
|
94 | 101 | }
|
95 |
| - list.add(new Object[] { |
| 102 | + |
| 103 | + arguments.add(Arguments.of( |
96 | 104 | node.get("message").textValue(), node.get("first"),
|
97 |
| - node.get("second"), node.get("patch") |
98 |
| - }); |
| 105 | + node.get("second"), node.get("patch"))); |
99 | 106 | }
|
100 | 107 |
|
101 |
| - return list.iterator(); |
| 108 | + return arguments.build(); |
102 | 109 | }
|
103 | 110 |
|
104 |
| - @Test(dataProvider = "getLiteralPatches", |
105 |
| - dependsOnMethods = "generatedPatchAppliesCleanly") |
106 |
| - public void generatedPatchesAreWhatIsExpected(final String message, |
107 |
| - final JsonNode source, final JsonNode target, |
108 |
| - final JsonNode expected) { |
109 |
| - final JsonNode actual = JsonPatch.generate(source, target, ReplaceMode.SAFE).toJson(); |
110 |
| - final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(expected); |
111 |
| - assertThat(predicate.apply(actual)).overridingErrorMessage( |
112 |
| - "patch is not what was expected\nscenario: %s\n" + |
113 |
| - "expected: %s\nactual: %s\n", message, expected, actual |
114 |
| - ).isTrue(); |
| 111 | + private static JsonNode getNode() throws IOException { |
| 112 | + final String resource = "/jsonpatch/diff/diff.json"; |
| 113 | + final URL url = JsonPatchGenerationTest.class.getResource(resource); |
| 114 | + return MAPPER.readTree(url); |
115 | 115 | }
|
116 | 116 | }
|
0 commit comments