Skip to content

Commit

Permalink
Add MVP of JUnit adapter (#13)
Browse files Browse the repository at this point in the history
* Create adapter for JUnit
* Add support for Predicate in Junit adapter
  • Loading branch information
pawel-mikolajczyk authored May 15, 2019
1 parent d7e5ad4 commit 11733f1
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 1 deletion.
1 change: 0 additions & 1 deletion jazon-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok:1.18.6'

testCompile group: 'org.spockframework', name: 'spock-core', version: '1.2-groovy-2.4'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

import static com.zendesk.jazon.expectation.ExpectationFactory.*;

Expand All @@ -28,6 +29,8 @@ public JsonExpectation expectation(Object object) {
return expectedUnorderedArray((Set<Object>) object, this);
} else if (object == null) {
return new NullExpectation();
} else if (object instanceof Predicate) {
return new PredicateExpectation((Predicate<Object>) object);
}
throw new IllegalArgumentException();
}
Expand Down
12 changes: 12 additions & 0 deletions jazon-junit/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ext {
name = 'Jazon JUnit Adapter'
description = 'A library for test assertions on JSON payloads - for JUnit framework.'
}

apply from: '../gradle/publishing.gradle'

dependencies {
compile project(':jazon-core')
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'com.google.guava', name: 'guava', version: '27.1-jre'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.zendesk.jazon.junit;

import com.zendesk.jazon.MatchResult;
import com.zendesk.jazon.MatcherFactory;
import com.zendesk.jazon.actual.GsonActualFactory;

import static com.zendesk.jazon.util.Preconditions.checkNotNull;

public class JazonJunitAdapter {
private static final GsonActualFactory GSON_ACTUAL_FACTORY = new GsonActualFactory();
private static final MatcherFactory matcherFactory = new MatcherFactory();
private final String actualJson;

public JazonJunitAdapter(String actualJson) {
this.actualJson = checkNotNull(actualJson);
}

public static JazonJunitAdapter assertThat(String actualJson) {
return new JazonJunitAdapter(actualJson);
}

public void matches(Object expected) {
MatchResult matchResult = matcherFactory.matcher()
.expected(expected)
.actual(GSON_ACTUAL_FACTORY.actual(actualJson))
.match();
if (matchResult.ok()) {
return;
}
String mismatchMessageTemplate = "\n-----------------------------------\nJSON MISMATCH:\n%s\n-----------------------------------\n";
throw new AssertionError(String.format(mismatchMessageTemplate, matchResult.message()));
}
}
76 changes: 76 additions & 0 deletions jazon-junit/src/test/java/com/zendesk/jazon/junit/ExampleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.zendesk.jazon.junit;

import com.google.common.collect.ImmutableMap;
import org.junit.Test;

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import static com.zendesk.jazon.junit.JazonJunitAdapter.assertThat;
import static java.util.Arrays.asList;


public class ExampleTest {

@Test(expected = AssertionError.class)
public void simpleTest() {
// given
String actualJson = "{\"value\": 123}";
Map<String, Object> expectedJsonAsMap = ImmutableMap.<String, Object>builder()
.put("value", 50)
.build();

// then
assertThat(actualJson).matches(expectedJsonAsMap);
}

@Test(expected = AssertionError.class)
public void testWithNestedArray() {
// given
String actualJson = "{" +
"\"value\": 50," +
"\"tags\": [\"blue\", \"black\", \"red\"]" +
"}";

// then
assertThat(actualJson).matches(
deal(50, asList("blue", "pink", "red"))
);
}

@Test(expected = AssertionError.class)
public void testWithRootArray() {
// given
String actualJson = "[\"blue\", \"black\", \"red\"]";

// then
assertThat(actualJson).matches(asList("blue", "pink", "red"));
}

@Test
public void testRegex() {
// given
String actualJson = "[\"blue\", \"black\", \"red\"]";

// then
assertThat(actualJson).matches(
asList(
regex("bl.*"),
regex("bl.*"),
regex("r.*")
)
);
}

private Predicate<String> regex(String reg) {
return s -> s.matches(reg);
}

private static Map<String, Object> deal(int value, List<String> tags) {
return ImmutableMap.<String, Object>builder()
.put("value", value)
.put("tags", tags)
.build();
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rootProject.name = 'jazon'
include 'jazon-core'
include 'jazon-spock'
include 'jazon-junit'

0 comments on commit 11733f1

Please sign in to comment.