Search in sources :

Example 1 with Request

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

the class BulkTest method cachesJsonData.

/**
 * Bulk can cache JSON data.
 * @throws Exception If some problem inside
 */
@Test
public void cachesJsonData() throws Exception {
    final Comment origin = Mockito.mock(Comment.class);
    final Request request = new FakeRequest().withBody("[{\"body\": \"hey you\"}]");
    final Iterable<Comment> comments = new Bulk<Comment>(new RtPagination<Comment>(request, new RtValuePagination.Mapping<Comment, JsonObject>() {

        @Override
        public Comment map(final JsonObject object) {
            return origin;
        }
    }));
    final Comment comment = comments.iterator().next();
    MatcherAssert.assertThat(new Comment.Smart(comment).body(), Matchers.equalTo("hey you"));
    comment.number();
    Mockito.verify(origin).number();
    Mockito.verify(origin, Mockito.never()).json();
}
Also used : Request(com.jcabi.http.Request) FakeRequest(com.jcabi.http.request.FakeRequest) JsonObject(javax.json.JsonObject) FakeRequest(com.jcabi.http.request.FakeRequest) Test(org.junit.Test)

Example 2 with Request

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

the class RtUserTest method notifications.

/**
 * Verifies the behaviour of the RtUser.notifications method.
 * @throws Exception Thrown in case of error.
 */
@Test
public void notifications() throws Exception {
    final Github github = Mockito.mock(Github.class);
    final Request request = new FakeRequest().withBody("[{\"id\":\"55898574\",\"unread\":true,\"reason\":\"mention\",\"updated_at\":\"2015-01-21T14:14:12Z\",\"last_read_at\":null,\"subject\":{\"title\":\"DyTasksTest.java:81-82: Unignore following test when #492...\",\"url\":\"https://api.github.com/repos/yegor256/thindeck/issues/508\",\"latest_comment_url\":\"https://api.github.com/repos/yegor256/thindeck/issues/comments/70843546\",\"type\":\"Issue\"},\"repository\":{\"id\":17670227,\"name\":\"thindeck\",\"full_name\":\"yegor256/thindeck\",\"owner\":{\"login\":\"yegor256\",\"id\":526301,\"avatar_url\":\"https://avatars.githubusercontent.com/u/526301?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/yegor256\",\"html_url\":\"https://github.com/yegor256\",\"followers_url\":\"https://api.github.com/users/yegor256/followers\",\"following_url\":\"https://api.github.com/users/yegor256/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/yegor256/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/yegor256/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/yegor256/subscriptions\",\"organizations_url\":\"https://api.github.com/users/yegor256/orgs\",\"repos_url\":\"https://api.github.com/users/yegor256/repos\",\"events_url\":\"https://api.github.com/users/yegor256/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/yegor256/received_events\",\"type\":\"User\",\"site_admin\":false},\"private\":false,\"html_url\":\"https://github.com/yegor256/thindeck\",\"description\":\"Web Hosting That Deploys Itself\",\"fork\":false,\"url\":\"https://api.github.com/repos/yegor256/thindeck\",\"forks_url\":\"https://api.github.com/repos/yegor256/thindeck/forks\",\"keys_url\":\"https://api.github.com/repos/yegor256/thindeck/keys{/key_id}\",\"collaborators_url\":\"https://api.github.com/repos/yegor256/thindeck/collaborators{/collaborator}\",\"teams_url\":\"https://api.github.com/repos/yegor256/thindeck/teams\",\"hooks_url\":\"https://api.github.com/repos/yegor256/thindeck/hooks\",\"issue_events_url\":\"https://api.github.com/repos/yegor256/thindeck/issues/events{/number}\",\"events_url\":\"https://api.github.com/repos/yegor256/thindeck/events\",\"assignees_url\":\"https://api.github.com/repos/yegor256/thindeck/assignees{/user}\",\"branches_url\":\"https://api.github.com/repos/yegor256/thindeck/branches{/branch}\",\"tags_url\":\"https://api.github.com/repos/yegor256/thindeck/tags\",\"blobs_url\":\"https://api.github.com/repos/yegor256/thindeck/git/blobs{/sha}\",\"git_tags_url\":\"https://api.github.com/repos/yegor256/thindeck/git/tags{/sha}\",\"git_refs_url\":\"https://api.github.com/repos/yegor256/thindeck/git/refs{/sha}\",\"trees_url\":\"https://api.github.com/repos/yegor256/thindeck/git/trees{/sha}\",\"statuses_url\":\"https://api.github.com/repos/yegor256/thindeck/statuses/{sha}\",\"languages_url\":\"https://api.github.com/repos/yegor256/thindeck/languages\",\"stargazers_url\":\"https://api.github.com/repos/yegor256/thindeck/stargazers\",\"contributors_url\":\"https://api.github.com/repos/yegor256/thindeck/contributors\",\"subscribers_url\":\"https://api.github.com/repos/yegor256/thindeck/subscribers\",\"subscription_url\":\"https://api.github.com/repos/yegor256/thindeck/subscription\",\"commits_url\":\"https://api.github.com/repos/yegor256/thindeck/commits{/sha}\",\"git_commits_url\":\"https://api.github.com/repos/yegor256/thindeck/git/commits{/sha}\",\"comments_url\":\"https://api.github.com/repos/yegor256/thindeck/comments{/number}\",\"issue_comment_url\":\"https://api.github.com/repos/yegor256/thindeck/issues/comments/{number}\",\"contents_url\":\"https://api.github.com/repos/yegor256/thindeck/contents/{+path}\",\"compare_url\":\"https://api.github.com/repos/yegor256/thindeck/compare/{base}...{head}\",\"merges_url\":\"https://api.github.com/repos/yegor256/thindeck/merges\",\"archive_url\":\"https://api.github.com/repos/yegor256/thindeck/{archive_format}{/ref}\",\"downloads_url\":\"https://api.github.com/repos/yegor256/thindeck/downloads\",\"issues_url\":\"https://api.github.com/repos/yegor256/thindeck/issues{/number}\",\"pulls_url\":\"https://api.github.com/repos/yegor256/thindeck/pulls{/number}\",\"milestones_url\":\"https://api.github.com/repos/yegor256/thindeck/milestones{/number}\",\"notifications_url\":\"https://api.github.com/repos/yegor256/thindeck/notifications{?since,all,participating}\",\"labels_url\":\"https://api.github.com/repos/yegor256/thindeck/labels{/name}\",\"releases_url\":\"https://api.github.com/repos/yegor256/thindeck/releases{/id}\"},\"url\":\"https://api.github.com/notifications/threads/55898574\",\"subscription_url\":\"https://api.github.com/notifications/threads/55898574/subscription\"}]");
    Mockito.when(github.entry()).thenReturn(request);
    final RtUser user = new RtUser(github, request);
    final List<Notification> notifications = user.notifications();
    MatcherAssert.assertThat(notifications, Matchers.notNullValue());
    MatcherAssert.assertThat(notifications.size(), Matchers.is(1));
    MatcherAssert.assertThat(notifications.get(0).number(), Matchers.is(55898574L));
}
Also used : Request(com.jcabi.http.Request) ApacheRequest(com.jcabi.http.request.ApacheRequest) FakeRequest(com.jcabi.http.request.FakeRequest) FakeRequest(com.jcabi.http.request.FakeRequest) Test(org.junit.Test)

Example 3 with Request

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

the class RtValuePaginationTest method jumpNextPage.

/**
 * RtPagination can jump to next page of results.
 * @throws Exception if there is any problem
 */
@Test
public void jumpNextPage() throws Exception {
    final String jeff = "Jeff";
    final String mark = "Mark";
    final String judy = "Judy";
    final String jessy = "Jessy";
    final MkContainer container = new MkGrizzlyContainer().next(RtValuePaginationTest.simple(jeff, mark).withHeader("Link", "</s?page=3&per_page=100>; rel=\"next\"")).next(RtValuePaginationTest.simple(judy, jessy)).start();
    final Request request = new ApacheRequest(container.home());
    final RtValuePagination<JsonObject, JsonArray> page = new RtValuePagination<JsonObject, JsonArray>(request, new RtValuePagination.Mapping<JsonObject, JsonArray>() {

        @Override
        public JsonObject map(final JsonArray object) {
            return Json.createObjectBuilder().add("id1", object.getString(0)).add("id2", object.getString(1)).build();
        }
    });
    final Iterator<JsonObject> iterator = page.iterator();
    MatcherAssert.assertThat(iterator.next().toString(), Matchers.allOf(Matchers.containsString(jeff), Matchers.containsString(mark)));
    MatcherAssert.assertThat(iterator.next().toString(), Matchers.allOf(Matchers.containsString(judy), Matchers.containsString(jessy)));
    container.stop();
}
Also used : JsonArray(javax.json.JsonArray) 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 4 with Request

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

the class RtValuePaginationTest method throwsIfNoMoreElement.

/**
 * RtValuePagination 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 String jeff = "other Jeff";
    final String mark = "other Mark";
    final MkContainer container = new MkGrizzlyContainer().next(RtValuePaginationTest.simple(jeff, mark)).start();
    try {
        final Request request = new ApacheRequest(container.home());
        final RtValuePagination<JsonObject, JsonArray> page = new RtValuePagination<JsonObject, JsonArray>(request, new RtValuePagination.Mapping<JsonObject, JsonArray>() {

            @Override
            public JsonObject map(final JsonArray object) {
                return Json.createObjectBuilder().add("id3", object.getString(0)).add("id4", object.getString(1)).build();
            }
        });
        final Iterator<JsonObject> iterator = page.iterator();
        iterator.next();
        MatcherAssert.assertThat(iterator.next(), Matchers.notNullValue());
    } finally {
        container.stop();
    }
}
Also used : JsonArray(javax.json.JsonArray) 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 5 with Request

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

the class RtPaginationTest method jumpNextPage.

/**
 * RtPagination can jump to next page of results.
 *
 * @throws Exception if there is any problem
 */
@Test
public void jumpNextPage() throws Exception {
    final MkContainer container = new MkGrizzlyContainer().next(RtPaginationTest.simple("Hi Jeff").withHeader("Link", "</s?page=3&per_page=100>; rel=\"next\"")).next(RtPaginationTest.simple("Hi Mark")).start();
    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();
    MatcherAssert.assertThat(iterator.next().toString(), Matchers.containsString("Jeff"));
    MatcherAssert.assertThat(iterator.next().toString(), Matchers.containsString("Mark"));
    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)

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