Skip to content

Commit

Permalink
Expect OK response from GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Mar 27, 2023
1 parent 9dd5d2e commit 6e665bb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/main/java/com/jcabi/github/RtChecks.java
Expand Up @@ -31,7 +31,9 @@

import com.jcabi.http.Request;
import com.jcabi.http.response.JsonResponse;
import com.jcabi.http.response.RestResponse;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Collection;
import java.util.stream.Collectors;
import javax.json.JsonObject;
Expand Down Expand Up @@ -96,7 +98,10 @@ public Collection<Check> all() throws IOException {
.path(this.pull.head().ref())
.path("/check-runs")
.back()
.method(Request.GET).fetch().as(JsonResponse.class)
.method(Request.GET).fetch()
.as(RestResponse.class)
.assertStatus(HttpURLConnection.HTTP_OK)
.as(JsonResponse.class)
.json()
.readObject()
.getJsonArray("check_runs")
Expand Down
54 changes: 52 additions & 2 deletions src/test/java/com/jcabi/github/RtChecksTest.java
Expand Up @@ -39,6 +39,7 @@
import javax.json.Json;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -70,7 +71,7 @@ public void getsAllChecks() throws IOException {
.next(
new MkAnswer.Simple(
HttpURLConnection.HTTP_OK,
RtChecksTest.json()
RtChecksTest.jsonWithCheckRuns()
)
)
.start(this.resource.port())) {
Expand All @@ -85,12 +86,54 @@ public void getsAllChecks() throws IOException {
}
}

@Test
public void returnsEmptyChecksIfTheyAreAbsent() throws IOException {
try (final MkContainer container = new MkGrizzlyContainer()
.next(
new MkAnswer.Simple(
HttpURLConnection.HTTP_OK,
RtChecksTest.jsonWithoutCheckRuns()
)
)
.start(this.resource.port())) {
final Checks checks = new RtChecks(
new JdkRequest(container.home()),
this.repo().pulls().get(0)
);
MatcherAssert.assertThat(
checks.all(),
Matchers.iterableWithSize(1)
);
}
}

@Test
public void assertsOkResponse() throws IOException {
try (final MkContainer container = new MkGrizzlyContainer()
.next(
new MkAnswer.Simple(
HttpURLConnection.HTTP_NOT_FOUND,
RtChecksTest.jsonWithCheckRuns()
)
).start(this.resource.port())
) {
final Checks checks = new RtChecks(
new JdkRequest(container.home()),
this.repo().pulls().get(0)
);
Assert.assertThrows(
AssertionError.class,
checks::all
);
}
}

/**
* Creates json response body.
*
* @return Json response body.
*/
private static String json() {
private static String jsonWithCheckRuns() {
return Json.createObjectBuilder()
.add("total_count", Json.createValue(1))
.add(
Expand All @@ -108,6 +151,13 @@ private static String json() {
.toString();
}

private static String jsonWithoutCheckRuns() {
return Json.createObjectBuilder()
.add("total_count", Json.createValue(1))
.build()
.toString();
}

/**
* Create and return repo for testing.
* @return Repo
Expand Down

0 comments on commit 6e665bb

Please sign in to comment.