Skip to content

Commit

Permalink
Handle unfinished checks properly
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Apr 6, 2023
1 parent 5f7a1c7 commit 19334d4
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 48 deletions.
19 changes: 17 additions & 2 deletions src/main/java/com/jcabi/github/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/com/jcabi/github/RtChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,27 @@ public Collection<? extends Check> 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;
}
}
117 changes: 73 additions & 44 deletions src/test/java/com/jcabi/github/RtChecksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -138,67 +147,70 @@ 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())
) {
final Checks checks = new RtChecks(
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<? extends Check> 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())
) {
final Checks checks = new RtChecks(
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<? extends Check> all = checks.all();
MatcherAssert.assertThat(all, Matchers.hasSize(1));
for (final Check check : all) {
MatcherAssert.assertThat(
check.successful(),
Matchers.is(false)
);
}
}
}
Expand All @@ -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();
}
Expand Down

0 comments on commit 19334d4

Please sign in to comment.