-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1866fd2
commit f90e6e4
Showing
5 changed files
with
191 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters