From 19334d4addbd6ef4dafe5960399f9ca9a39e75cc Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Thu, 6 Apr 2023 22:46:30 +0300 Subject: [PATCH] Handle unfinished checks properly --- src/main/java/com/jcabi/github/Check.java | 19 ++- src/main/java/com/jcabi/github/RtChecks.java | 23 +++- .../java/com/jcabi/github/RtChecksTest.java | 117 +++++++++++------- 3 files changed, 111 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/jcabi/github/Check.java b/src/main/java/com/jcabi/github/Check.java index 7def3b63a..d2f3ef9f9 100644 --- a/src/main/java/com/jcabi/github/Check.java +++ b/src/main/java/com/jcabi/github/Check.java @@ -43,6 +43,11 @@ */ public interface Check { + /** + * Undefined status or conclusion. + */ + String UNDEFINED_VALUE = "undefined"; + /** * Checks whether Check was successful. * @return True if Check was successful. @@ -70,7 +75,12 @@ enum Status { /** * Completed. */ - COMPLETED("completed"); + COMPLETED("completed"), + + /** + * Undefined. If GitHub response doesn't contain the Status value. + */ + UNDEFINED(Check.UNDEFINED_VALUE); /** * Status. @@ -173,7 +183,12 @@ enum Conclusion { /** * Timed out. */ - TIMED_OUT("timed_out"); + TIMED_OUT("timed_out"), + + /** + * Undefined. If GitHub response doesn't contain the Conclusion value. + */ + UNDEFINED(Check.UNDEFINED_VALUE); /** * Conclusion. diff --git a/src/main/java/com/jcabi/github/RtChecks.java b/src/main/java/com/jcabi/github/RtChecks.java index 4e8bfd650..1c5804a1d 100644 --- a/src/main/java/com/jcabi/github/RtChecks.java +++ b/src/main/java/com/jcabi/github/RtChecks.java @@ -123,8 +123,27 @@ public Collection all() throws IOException { private static RtCheck check(final JsonValue value) { final JsonObject check = value.asJsonObject(); return new RtCheck( - check.getString("status"), - check.getString("conclusion") + RtChecks.getOrUndefined("status", check), + RtChecks.getOrUndefined("conclusion", check) ); } + + /** + * Retrieves String value from JsonObject by key or return "undefined". + * @param key Json key. + * @param check Retrieve from + * @return Json String value or "undefined". + */ + private static String getOrUndefined( + final String key, + final JsonObject check + ) { + final String res; + if (check.containsKey(key)) { + res = check.getString(key); + } else { + res = Check.UNDEFINED_VALUE; + } + return res; + } } diff --git a/src/test/java/com/jcabi/github/RtChecksTest.java b/src/test/java/com/jcabi/github/RtChecksTest.java index 4fd661978..459858dfc 100644 --- a/src/test/java/com/jcabi/github/RtChecksTest.java +++ b/src/test/java/com/jcabi/github/RtChecksTest.java @@ -35,8 +35,12 @@ import com.jcabi.http.request.JdkRequest; import java.io.IOException; import java.net.HttpURLConnection; +import java.util.Arrays; +import java.util.Collection; import java.util.Random; import javax.json.Json; +import javax.json.JsonArrayBuilder; +import javax.json.JsonObjectBuilder; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Assert; @@ -53,6 +57,11 @@ */ public final class RtChecksTest { + /** + * Conclusion key in json check. + */ + private static final String CONCLUSION_KEY = "conclusion"; + /** * The rule for skipping test if there's BindException. * @checkstyle VisibilityModifierCheck (3 lines) @@ -138,25 +147,24 @@ public void assertsOkResponse() throws IOException { } } + /** + * Checks that library can handle unfinished checks. + * @throws IOException If some I/O problem happens. + */ @Test - public void retrievesUnfinishedChecksWithoutConclusion() throws IOException { + public void retrievesUnfinishedChecksWithoutConclusion() + throws IOException { try (final MkContainer container = new MkGrizzlyContainer() .next( new MkAnswer.Simple( HttpURLConnection.HTTP_OK, - Json.createObjectBuilder() - .add("total_count", Json.createValue(1)) - .add("check_runs", - Json.createArrayBuilder() - .add( - Json.createObjectBuilder() - .add("id", Json.createValue(new Random().nextInt())) - .add("status", "completed") - .build() - ) - ) - .build() - .toString() + RtChecksTest.jsonChecks( + RtChecksTest.jsonCheck() + .add( + RtChecksTest.CONCLUSION_KEY, + Check.Conclusion.SUCCESS.value() + ) + ) ) ).start(this.resource.port()) ) { @@ -164,31 +172,31 @@ public void retrievesUnfinishedChecksWithoutConclusion() throws IOException { new JdkRequest(container.home()), this.repo().pulls().get(0) ); - MatcherAssert.assertThat(checks.all(), Matchers.hasSize(1)); - for (final Check check : checks.all()) { - MatcherAssert.assertThat(check.successful(), Matchers.is(false)); + final Collection all = checks.all(); + MatcherAssert.assertThat(all, Matchers.hasSize(1)); + for (final Check check : all) { + MatcherAssert.assertThat( + check.successful(), + Matchers.is(false) + ); } } } + /** + * Checks that library can handle unfinished checks. + * @throws IOException If some I/O problem happens. + */ @Test - public void retrievesUnfinishedChecksWithoutStatusAndConclusion() throws IOException { + public void retrievesUnfinishedChecksWithoutStatusAndConclusion() + throws IOException { try (final MkContainer container = new MkGrizzlyContainer() .next( new MkAnswer.Simple( HttpURLConnection.HTTP_OK, - Json.createObjectBuilder() - .add("total_count", Json.createValue(1)) - .add("check_runs", - Json.createArrayBuilder() - .add( - Json.createObjectBuilder() - .add("id", Json.createValue(new Random().nextInt())) - .build() - ) - ) - .build() - .toString() + RtChecksTest.jsonChecks( + RtChecksTest.jsonCheck() + ) ) ).start(this.resource.port()) ) { @@ -196,9 +204,13 @@ public void retrievesUnfinishedChecksWithoutStatusAndConclusion() throws IOExcep new JdkRequest(container.home()), this.repo().pulls().get(0) ); - MatcherAssert.assertThat(checks.all(), Matchers.hasSize(1)); - for (final Check check : checks.all()) { - MatcherAssert.assertThat(check.successful(), Matchers.is(false)); + final Collection all = checks.all(); + MatcherAssert.assertThat(all, Matchers.hasSize(1)); + for (final Check check : all) { + MatcherAssert.assertThat( + check.successful(), + Matchers.is(false) + ); } } } @@ -209,19 +221,36 @@ public void retrievesUnfinishedChecksWithoutStatusAndConclusion() throws IOExcep * @return Json response body. */ private static String jsonWithCheckRuns() { + return RtChecksTest.jsonChecks( + RtChecksTest.jsonCheck() + .add("status", Check.Status.COMPLETED.value()) + .add( + RtChecksTest.CONCLUSION_KEY, + Check.Conclusion.SUCCESS.value() + ) + ); + } + + /** + * Creates Json Check Builder. + * @return JsonObjectBuilder. + */ + private static JsonObjectBuilder jsonCheck() { + return Json.createObjectBuilder() + .add("id", Json.createValue(new Random().nextInt())); + } + + /** + * Creates json checks. + * @param checks All checks that have to be included. + * @return Json. + */ + private static String jsonChecks(final JsonObjectBuilder... checks) { + final JsonArrayBuilder all = Json.createArrayBuilder(); + Arrays.stream(checks).map(JsonObjectBuilder::build).forEach(all::add); return Json.createObjectBuilder() .add("total_count", Json.createValue(1)) - .add( - "check_runs", - Json.createArrayBuilder() - .add( - Json.createObjectBuilder() - .add("id", Json.createValue(new Random().nextInt())) - .add("status", "completed") - .add("conclusion", "success") - .build() - ) - ) + .add("check_runs", all.build()) .build() .toString(); }