Skip to content

Commit

Permalink
Migrate TestNG tests to JUnit 5 (line#474)
Browse files Browse the repository at this point in the history
This PR migrates TestNG tests to JUnit 5.

#### Changes:
- Migrate tests to JUnit 5
- Merge operation tests
- Merge operation serialization tests
- Remove TestNG dependency and task.
  • Loading branch information
KarboniteKream authored Jan 30, 2020
1 parent 375cfbd commit d8714c8
Show file tree
Hide file tree
Showing 26 changed files with 275 additions and 921 deletions.
5 changes: 0 additions & 5 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,6 @@ This product depends on Spring Framework, distributed by Pivotal Software, Inc:
* License: licenses/LICENSE.spring.al20.txt (Apache License v2.0)
* Homepage: https://spring.io/

This product depends on TestNG, distributed by Cédric Beust:

* License: licenses/LICENSE.testng.al20.txt (Apache License v2.0)
* Homepage: http://testng.org/doc/

This product depends on Apache Thrift, distributed by Apache Software Foundation:

* License: licenses/LICENSE.thrift.al20.txt (Apache License v2.0)
Expand Down
24 changes: 0 additions & 24 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,7 @@ dependencies {

// JSON-path
compile 'com.jayway.jsonpath:json-path'

// TestNG (for JSON patch tests)
testCompile 'org.testng:testng'
}

// Run the JSON patch test cases.
task testNg(type: Test,
group: 'Verification',
description: 'Runs the TestNG unit tests.',
dependsOn: tasks.shadedTestClasses) {

useTestNG()
include '/com/linecorp/centraldogma/internal/jsonpatch/**'

scanForTestClasses = false
testClassesDirs = tasks.shadedTest.testClassesDirs
classpath = testClassesDirs

// Set the class path as late as possible so that the 'shadedTest' task has the correct classpath.
doFirst {
classpath += project.files(configurations.shadedTestRuntime.resolve())
}
}
tasks.shadedTest.finalizedBy tasks.testNg
tasks.check.dependsOn tasks.testNg

if (tasks.findByName('trimShadedJar')) {
tasks.trimShadedJar.configure {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 LINE Corporation
* Copyright 2020 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
Expand Down Expand Up @@ -38,79 +38,79 @@

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Equivalence;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;

public final class JsonPatchGenerationTest {
class JsonPatchGenerationTest {

private static final ObjectMapper MAPPER = new ObjectMapper();
private static final Equivalence<JsonNode> EQUIVALENCE = JsonNumEquals.getInstance();

private final JsonNode testData;
@Order(1)
@ParameterizedTest
@MethodSource("patches")
void patchAppliesCleanly(JsonNode source, JsonNode target) {
final JsonPatch patch = JsonPatch.generate(source, target, ReplaceMode.SAFE);
final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(target)::apply;
final JsonNode actual = patch.apply(source);

public JsonPatchGenerationTest() throws IOException {
final String resource = "/jsonpatch/diff/diff.json";
final URL url = getClass().getResource(resource);
final ObjectMapper objectMapper = new ObjectMapper();
testData = objectMapper.readTree(url);
assertThat(predicate.test(actual))
.withFailMessage("Generated patch failed to apply\nexpected: %s\nactual: %s", target, actual)
.isTrue();
}

@DataProvider
public Iterator<Object[]> getPatchesOnly() {
final List<Object[]> list = new ArrayList<>();
for (final JsonNode node : testData) {
list.add(new Object[] { node.get("first"), node.get("second") });
}
@Order(2)
@ParameterizedTest
@MethodSource("literalPatches")
void expectedPatches(String message, JsonNode source, JsonNode target, JsonNode expected) {
final JsonNode actual = JsonPatch.generate(source, target, ReplaceMode.SAFE).toJson();
final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(expected)::apply;

return list.iterator();
assertThat(predicate.test(actual))
.withFailMessage("Patch is not what was expected\nscenario: %s\nexpected: %s\nactual: %s\n",
message, expected, actual)
.isTrue();
}

@Test(dataProvider = "getPatchesOnly")
public void generatedPatchAppliesCleanly(final JsonNode source, final JsonNode target) {
final JsonPatch patch = JsonPatch.generate(source, target, ReplaceMode.SAFE);
final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(target);
final JsonNode actual = patch.apply(source);
private static List<Arguments> patches() throws Exception {
final ImmutableList.Builder<Arguments> arguments = ImmutableList.builder();

assertThat(predicate.apply(actual)).overridingErrorMessage(
"Generated patch failed to apply\nexpected: %s\nactual: %s",
target, actual
).isTrue();
for (JsonNode node : getNode()) {
arguments.add(Arguments.of(node.get("first"), node.get("second")));
}

return arguments.build();
}

@DataProvider
public Iterator<Object[]> getLiteralPatches() {
final List<Object[]> list = new ArrayList<>();
for (final JsonNode node : testData) {
private static List<Arguments> literalPatches() throws Exception {
final ImmutableList.Builder<Arguments> arguments = ImmutableList.builder();

for (JsonNode node : getNode()) {
if (!node.has("patch")) {
continue;
}
list.add(new Object[] {

arguments.add(Arguments.of(
node.get("message").textValue(), node.get("first"),
node.get("second"), node.get("patch")
});
node.get("second"), node.get("patch")));
}

return list.iterator();
return arguments.build();
}

@Test(dataProvider = "getLiteralPatches",
dependsOnMethods = "generatedPatchAppliesCleanly")
public void generatedPatchesAreWhatIsExpected(final String message,
final JsonNode source, final JsonNode target,
final JsonNode expected) {
final JsonNode actual = JsonPatch.generate(source, target, ReplaceMode.SAFE).toJson();
final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(expected);
assertThat(predicate.apply(actual)).overridingErrorMessage(
"patch is not what was expected\nscenario: %s\n" +
"expected: %s\nactual: %s\n", message, expected, actual
).isTrue();
private static JsonNode getNode() throws IOException {
final String resource = "/jsonpatch/diff/diff.json";
final URL url = JsonPatchGenerationTest.class.getResource(resource);
return MAPPER.readTree(url);
}
}
Loading

0 comments on commit d8714c8

Please sign in to comment.