Skip to content

Commit

Permalink
implement RtChecks
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Mar 16, 2023
1 parent 1866fd2 commit f90e6e4
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/jcabi/github/Checks.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
package com.jcabi.github;

import java.io.IOException;
import java.util.Collection;

/**
Expand All @@ -40,6 +41,6 @@
*/
public interface Checks {

Collection<Check> all();
Collection<Check> all() throws IOException;

}
12 changes: 12 additions & 0 deletions src/main/java/com/jcabi/github/RtCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.jcabi.github;

public class RtCheck implements Check {

private final int id;
private final String status;

RtCheck(final int identifier, final String stat) {
this.id = identifier;
this.status = stat;
}
}
63 changes: 63 additions & 0 deletions src/main/java/com/jcabi/github/RtChecks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.jcabi.github;

import com.jcabi.http.Request;
import com.jcabi.http.response.JsonResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.stream.Collectors;
import javax.json.JsonObject;
import javax.json.JsonValue;

public class RtChecks implements Checks {

private final Pull pull;
private final Request request;

RtChecks(final Request req, final Pull pr) {
this.pull = pr;
this.request = req;
}

/**
* Get all checks.
* JSON schema:
* <p>{@code
* {
* "total_count": 1,
* "check_runs": [
* {
* "id": 4,
* "status": "completed",
* ...
* }
* ]
* }
* }</p>
* @return Checks.
* @throws IOException If there is any I/O problem.
*/
@Override
public Collection<Check> all() throws IOException {
final Coordinates coords = this.pull.repo().coordinates();
return this.request.uri()
.path("/repos")
.path(coords.user())
.path(coords.repo())
.path("/commits")
.path(this.pull.head().ref())
.path("/check-runs")
.back()
.method(Request.GET).fetch().as(JsonResponse.class)
.json()
.readObject()
.getJsonArray("check_runs")
.stream()
.map(RtChecks::check)
.collect(Collectors.toList());
}

private static RtCheck check(final JsonValue value) {
final JsonObject check = value.asJsonObject();
return new RtCheck(check.getInt("id"), check.getString("status"));
}
}
113 changes: 113 additions & 0 deletions src/test/java/com/jcabi/github/RtChecksTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Copyright (c) 2013-2022, jcabi.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the jcabi.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jcabi.github;

import com.jcabi.http.mock.MkAnswer;
import com.jcabi.http.mock.MkContainer;
import com.jcabi.http.mock.MkGrizzlyContainer;
import com.jcabi.http.request.JdkRequest;
import java.io.IOException;
import java.net.HttpURLConnection;
import javax.json.Json;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;

/**
* Test case for {@link RtChecks}.
*
* @author Volodya Lombrozo (volodya.lombrozo@gmail.com)
* @since 1.5.0
*/
public final class RtChecksTest {

/**
* The rule for skipping test if there's BindException.
* @checkstyle VisibilityModifierCheck (3 lines)
*/
@Rule
public final transient RandomPort resource = new RandomPort();

@Test
public void getsAllChecks() throws IOException {
try (final MkContainer container = new MkGrizzlyContainer()
.next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, this.json()))
.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)
);
}
}

private String json() {
return Json.createObjectBuilder()
.add("total_count", Json.createValue(1))
.add("check_runs",
Json.createArrayBuilder()
.add(
Json.createObjectBuilder()
.add("id", Json.createValue(100))
.add("status", "completed")
.build()
)
)
.build()
.toString();
}

/**
* Create and return repo for testing.
* @return Repo
*/
private Repo repo() throws IOException {
final Repo repo = Mockito.mock(Repo.class);
final Pulls pulls = Mockito.mock(Pulls.class);
final Pull pull = Mockito.mock(Pull.class);
final PullRef ref = Mockito.mock(PullRef.class);
Mockito.doReturn(new Coordinates.Simple("volodya-lombrozo", "jtcop"))
.when(repo)
.coordinates();
Mockito.doReturn(pulls).when(repo).pulls();
Mockito.doReturn(pull).when(pulls).get(0);
Mockito.doReturn(repo).when(pull).repo();
Mockito.doReturn(ref).when(pull).head();
Mockito.doReturn("abcdef1").when(ref).ref();
return repo;
}


}
2 changes: 1 addition & 1 deletion src/test/java/com/jcabi/github/RtCollaboratorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void userCanBeRemoved() throws Exception {
* @param login Username to login
* @return JsonObject
*/
private static JsonValue json(final String login) {
static JsonValue json(final String login) {
return Json.createObjectBuilder()
.add("login", login)
.build();
Expand Down

0 comments on commit f90e6e4

Please sign in to comment.