1
2
3
4
5 package com.jcabi.github;
6
7 import com.jcabi.http.Request;
8 import com.jcabi.http.mock.MkAnswer;
9 import com.jcabi.http.mock.MkContainer;
10 import com.jcabi.http.mock.MkGrizzlyContainer;
11 import com.jcabi.http.request.ApacheRequest;
12 import jakarta.json.Json;
13 import jakarta.json.JsonArray;
14 import jakarta.json.JsonObject;
15 import java.io.IOException;
16 import java.net.HttpURLConnection;
17 import java.util.Iterator;
18 import java.util.NoSuchElementException;
19 import org.hamcrest.MatcherAssert;
20 import org.hamcrest.Matchers;
21 import org.junit.jupiter.api.Assertions;
22 import org.junit.jupiter.api.Test;
23 import org.junit.jupiter.api.extension.ExtendWith;
24
25
26
27
28
29 @ExtendWith(RandomPort.class)
30 final class RtValuePaginationTest {
31
32
33
34
35 @Test
36 void jumpNextPage() throws IOException {
37 final String jeff = "Jeff";
38 final String mark = "Mark";
39 final String judy = "Judy";
40 final String jessy = "Jessy";
41 final MkContainer container = new MkGrizzlyContainer().next(
42 RtValuePaginationTest.simple(jeff, mark)
43 .withHeader("Link", "</s?page=3&per_page=100>; rel=\"next\"")
44 ).next(RtValuePaginationTest.simple(judy, jessy))
45 .start(RandomPort.port());
46 final Request request = new ApacheRequest(container.home());
47 final RtValuePagination<JsonObject, JsonArray> page =
48 new RtValuePagination<>(
49 request,
50 object -> Json.createObjectBuilder()
51 .add("id1", object.getString(0))
52 .add("id2", object.getString(1))
53 .build()
54 );
55 final Iterator<JsonObject> iterator = page.iterator();
56 MatcherAssert.assertThat(
57 "String does not contain expected value",
58 iterator.next().toString(),
59 Matchers.allOf(
60 Matchers.containsString(jeff),
61 Matchers.containsString(mark)
62 )
63 );
64 MatcherAssert.assertThat(
65 "String does not contain expected value",
66 iterator.next().toString(),
67 Matchers.allOf(
68 Matchers.containsString(judy),
69 Matchers.containsString(jessy)
70 )
71 );
72 container.stop();
73 }
74
75 @Test
76 void throwsIfNoMoreElement() throws IOException {
77 final String jeff = "other Jeff";
78 final String mark = "other Mark";
79 final MkContainer container = new MkGrizzlyContainer().next(
80 RtValuePaginationTest.simple(jeff, mark)
81 ).start(RandomPort.port());
82 try {
83 final Request request = new ApacheRequest(container.home());
84 final RtValuePagination<JsonObject, JsonArray> page =
85 new RtValuePagination<>(
86 request,
87 object -> Json.createObjectBuilder()
88 .add("id3", object.getString(0))
89 .add("id4", object.getString(1))
90 .build()
91 );
92 final Iterator<JsonObject> iterator = page.iterator();
93 iterator.next();
94 Assertions.assertThrows(
95 NoSuchElementException.class,
96 iterator::next,
97 "Should throw when no more elements"
98 );
99 } finally {
100 container.stop();
101 }
102 }
103
104
105
106
107
108
109
110 private static MkAnswer.Simple simple(final String one,
111 final String another
112 ) {
113 final String message = Json.createArrayBuilder()
114 .add(Json.createArrayBuilder().add(one).add(another))
115 .build().toString();
116 return new MkAnswer.Simple(HttpURLConnection.HTTP_OK, message);
117 }
118 }