Search in sources :

Example 6 with Request

use of com.jcabi.http.Request in project jcabi-github by jcabi.

the class RtPaginationTest method throwsIfNoMoreElement.

/**
 * RtPagination can throw if there is no more elements in pagination.
 *
 * @throws Exception if there is any problem
 */
@Test(expected = NoSuchElementException.class)
public void throwsIfNoMoreElement() throws Exception {
    final MkContainer container = new MkGrizzlyContainer().next(simple("Hi there")).start();
    try {
        final Request request = new ApacheRequest(container.home());
        final RtPagination<JsonObject> page = new RtPagination<JsonObject>(request, new RtValuePagination.Mapping<JsonObject, JsonObject>() {

            @Override
            public JsonObject map(final JsonObject object) {
                return object;
            }
        });
        final Iterator<JsonObject> iterator = page.iterator();
        iterator.next();
        MatcherAssert.assertThat(iterator.next(), Matchers.notNullValue());
    } finally {
        container.stop();
    }
}
Also used : MkGrizzlyContainer(com.jcabi.http.mock.MkGrizzlyContainer) ApacheRequest(com.jcabi.http.request.ApacheRequest) Request(com.jcabi.http.Request) ApacheRequest(com.jcabi.http.request.ApacheRequest) JsonObject(javax.json.JsonObject) MkContainer(com.jcabi.http.mock.MkContainer) Test(org.junit.Test)

Example 7 with Request

use of com.jcabi.http.Request in project jcabi-github by jcabi.

the class RtStatusesTest method createsStatus.

/**
 * Tests creating a Status.
 *
 * @throws Exception when an Error occurs
 */
@Test
public void createsStatus() throws Exception {
    final String stateprop = "state";
    final String urlprop = "target_url";
    final String descriptionprop = "description";
    final String contextprop = "context";
    final String url = "https://ci.example.com/1000/output";
    final String description = "Build has completed successfully";
    final String context = "continuous-integration/jenkins";
    final MkContainer container = new MkGrizzlyContainer().next(new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, Json.createObjectBuilder().add(stateprop, "failure").add(urlprop, url).add(descriptionprop, description).add(contextprop, context).build().toString())).start();
    final Request entry = new ApacheRequest(container.home());
    final Statuses statuses = new RtStatuses(entry, new RtCommit(entry, new MkGithub().randomRepo(), "0abcd89jcabitest"));
    try {
        statuses.create(new Statuses.StatusCreate(Status.State.FAILURE).withTargetUrl(Optional.of(url)).withDescription(description).withContext(Optional.of(context)));
        final MkQuery request = container.take();
        MatcherAssert.assertThat(request.method(), Matchers.equalTo(Request.POST));
        final JsonObject obj = Json.createReader(new StringReader(request.body())).readObject();
        MatcherAssert.assertThat(obj.getString(stateprop), Matchers.equalTo(Status.State.FAILURE.identifier()));
        MatcherAssert.assertThat(obj.getString(contextprop), Matchers.equalTo(context));
        MatcherAssert.assertThat(obj.getString(descriptionprop), Matchers.equalTo(description));
        MatcherAssert.assertThat(obj.getString(urlprop), Matchers.equalTo(url));
    } finally {
        container.stop();
    }
}
Also used : Request(com.jcabi.http.Request) ApacheRequest(com.jcabi.http.request.ApacheRequest) FakeRequest(com.jcabi.http.request.FakeRequest) MkQuery(com.jcabi.http.mock.MkQuery) JsonObject(javax.json.JsonObject) MkContainer(com.jcabi.http.mock.MkContainer) MkGrizzlyContainer(com.jcabi.http.mock.MkGrizzlyContainer) ApacheRequest(com.jcabi.http.request.ApacheRequest) StringReader(java.io.StringReader) MkGithub(com.jcabi.github.mock.MkGithub) Test(org.junit.Test)

Example 8 with Request

use of com.jcabi.http.Request in project wring by yegor256.

the class AgGithub method push.

@Override
public String push(final Events events) throws IOException {
    final Github github = new RtGithub(this.config.getString("token"));
    final String since = DateFormatUtils.formatUTC(DateUtils.addMinutes(new Date(), -Tv.THREE), "yyyy-MM-dd'T'HH:mm:ss'Z'");
    final Request req = github.entry().uri().path("/notifications").back();
    final Iterable<JsonObject> list = new RtPagination<>(req.uri().queryParam("participating", "true").queryParam("since", since).queryParam("all", Boolean.toString(true)).back(), RtPagination.COPYING);
    final Collection<String> done = new LinkedList<>();
    for (final JsonObject event : AgGithub.safe(list)) {
        final String reason = event.getString("reason");
        if (!"mention".equals(reason)) {
            continue;
        }
        this.push(github, event, events);
        done.add(event.getString("id"));
    }
    req.uri().queryParam("last_read_at", since).back().method(Request.PUT).body().set("{}").back().fetch().as(RestResponse.class).assertStatus(HttpURLConnection.HTTP_RESET);
    if (!done.isEmpty()) {
        Logger.info(this, "%d GitHub events for @%s processed: %s", done.size(), github.users().self().login(), done);
    }
    return String.format("%d events for @%s at %s", done.size(), github.users().self().login(), DateFormatUtils.formatUTC(new Date(), "yyyy-MM-dd HH:mm:ss"));
}
Also used : RtGithub(com.jcabi.github.RtGithub) Github(com.jcabi.github.Github) RestResponse(com.jcabi.http.response.RestResponse) Request(com.jcabi.http.Request) JsonObject(javax.json.JsonObject) RtGithub(com.jcabi.github.RtGithub) Date(java.util.Date) LinkedList(java.util.LinkedList) RtPagination(com.jcabi.github.RtPagination)

Aggregations

Request (com.jcabi.http.Request)8 JsonObject (javax.json.JsonObject)7 Test (org.junit.Test)7 ApacheRequest (com.jcabi.http.request.ApacheRequest)6 MkContainer (com.jcabi.http.mock.MkContainer)5 MkGrizzlyContainer (com.jcabi.http.mock.MkGrizzlyContainer)5 FakeRequest (com.jcabi.http.request.FakeRequest)3 JsonArray (javax.json.JsonArray)2 Github (com.jcabi.github.Github)1 RtGithub (com.jcabi.github.RtGithub)1 RtPagination (com.jcabi.github.RtPagination)1 MkGithub (com.jcabi.github.mock.MkGithub)1 MkQuery (com.jcabi.http.mock.MkQuery)1 RestResponse (com.jcabi.http.response.RestResponse)1 StringReader (java.io.StringReader)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1