Skip to content

Commit

Permalink
Use JsonAssert for request assertions
Browse files Browse the repository at this point in the history
Closes gh-41
  • Loading branch information
sjohnr committed Apr 17, 2024
1 parent 1d24061 commit b9f407a
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 12 deletions.
1 change: 1 addition & 0 deletions api/github/github-api.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {

testImplementation "org.assertj:assertj-core"
testImplementation "org.mockito:mockito-core"
testImplementation "com.jayway.jsonpath:json-path-assert"
testImplementation "com.squareup.okhttp3:mockwebserver"
}

Expand Down
21 changes: 15 additions & 6 deletions api/github/src/test/java/com/github/api/GitHubApiTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import com.jayway.jsonassert.JsonAssert;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -32,6 +33,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.CoreMatchers.is;

public class GitHubApiTests {

Expand Down Expand Up @@ -102,8 +104,12 @@ public void createReleaseWhenValidParametersThenSuccess() throws Exception {
assertThat(recordedRequest.getHeader("Accept")).isEqualTo("application/json");
assertThat(recordedRequest.getHeader("Content-Type")).isEqualTo("application/json");
assertThat(recordedRequest.getHeader("Authorization")).isEqualTo("Bearer %s".formatted(AUTH_TOKEN));
assertThat(recordedRequest.getBody().readString(Charset.defaultCharset()))
.isEqualTo(string("CreateReleaseRequest.json"));

var json = JsonAssert.with(recordedRequest.getBody().readString(Charset.defaultCharset()));
json.assertThat("$.tag_name", is("1.0.0"));
json.assertThat("$.draft", is(false));
json.assertThat("$.prerelease", is(false));
json.assertThat("$.generate_release_notes", is(false));
}

@Test
Expand All @@ -120,8 +126,10 @@ public void createMilestoneWhenValidParametersThenSuccess() throws Exception {
assertThat(recordedRequest.getHeader("Accept")).isEqualTo("application/json");
assertThat(recordedRequest.getHeader("Content-Type")).isEqualTo("application/json");
assertThat(recordedRequest.getHeader("Authorization")).isEqualTo("Bearer %s".formatted(AUTH_TOKEN));
assertThat(recordedRequest.getBody().readString(Charset.defaultCharset()))
.isEqualTo(string("CreateMilestoneRequest.json"));

var json = JsonAssert.with(recordedRequest.getBody().readString(Charset.defaultCharset()));
json.assertThat("$.title", is("1.0.0"));
json.assertThat("$.due_on", is("2022-05-04T12:00:00Z"));
}

@Test
Expand Down Expand Up @@ -212,8 +220,9 @@ public void closeMilestoneWhenValidParametersThenSuccess() throws Exception {
assertThat(recordedRequest.getPath()).isEqualTo("/repos/spring-projects/spring-security/milestones/191");
assertThat(recordedRequest.getHeader("Accept")).isEqualTo("application/json");
assertThat(recordedRequest.getHeader("Authorization")).isEqualTo("Bearer %s".formatted(AUTH_TOKEN));
assertThat(recordedRequest.getBody().readString(Charset.defaultCharset()))
.isEqualTo(string("UpdateMilestoneRequest.json"));

var json = JsonAssert.with(recordedRequest.getBody().readString(Charset.defaultCharset()));
json.assertThat("$.state", is("closed"));
}

private static MockResponse json(String path) throws IOException {
Expand Down
1 change: 0 additions & 1 deletion api/github/src/test/resources/CreateMilestoneRequest.json

This file was deleted.

1 change: 0 additions & 1 deletion api/github/src/test/resources/CreateReleaseRequest.json

This file was deleted.

1 change: 0 additions & 1 deletion api/github/src/test/resources/UpdateMilestoneRequest.json

This file was deleted.

1 change: 1 addition & 0 deletions api/sagan/sagan-api.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {

testImplementation "org.assertj:assertj-core"
testImplementation "org.mockito:mockito-core"
testImplementation "com.jayway.jsonpath:json-path-assert"
testImplementation "com.squareup.okhttp3:mockwebserver"
}

Expand Down
11 changes: 9 additions & 2 deletions api/sagan/src/test/java/io/spring/api/SaganApiTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Comparator;
import java.util.Objects;

import com.jayway.jsonassert.JsonAssert;
import io.spring.api.Release.ReleaseStatus;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
Expand All @@ -33,6 +34,7 @@
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;

/**
* @author Steve Riesenberg
Expand Down Expand Up @@ -154,8 +156,13 @@ public void createReleaseWhenValidParametersThenSuccess() throws Exception {
assertThat(recordedRequest.getHeader("Accept")).isNull();
assertThat(recordedRequest.getHeader("Content-Type")).isEqualTo("application/json");
assertThat(recordedRequest.getHeader("Authorization")).isEqualTo("Basic %s".formatted(AUTH_TOKEN));
assertThat(recordedRequest.getBody().readString(Charset.defaultCharset()))
.isEqualTo(string("CreateReleaseRequest.json"));

var json = JsonAssert.with(recordedRequest.getBody().readString(Charset.defaultCharset()));
json.assertThat("$.version", is("6.1.0"));
json.assertThat("$.referenceDocUrl", is("https://docs.spring.io/spring-security/reference/{version}/index.html"));
json.assertThat("$.apiDocUrl", is("https://docs.spring.io/spring-security/site/docs/{version}/api/"));
json.assertThat("$.status", is("GENERAL_AVAILABILITY"));
json.assertThat("$.current", is(true));
}

@Test
Expand Down
1 change: 0 additions & 1 deletion api/sagan/src/test/resources/CreateReleaseRequest.json

This file was deleted.

1 change: 1 addition & 0 deletions dependencies/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ dependencies {
api "org.assertj:assertj-core:3.25.3"
api "org.mockito:mockito-core:5.11.0"
api "com.squareup.okhttp3:mockwebserver:4.12.0"
api "com.jayway.jsonpath:json-path-assert:2.9.0"
}
}

0 comments on commit b9f407a

Please sign in to comment.