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();
}
}
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();
}
}
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"));
}
Aggregations