Skip to content

Commit be69a5b

Browse files
test(pagination): fix linting issues(2)
1 parent 4bf5576 commit be69a5b

7 files changed

Lines changed: 129 additions & 83 deletions

File tree

src/test/java/apimatic/core/EndToEndTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,23 @@ public class EndToEndTest extends MockCoreConfig {
123123
*/
124124
@Mock
125125
private Response response;
126+
/**
127+
* Base test class for end-to-end tests.
128+
* <p>
129+
* Subclasses can override this method to provide a custom response retrieval mechanism.
130+
* If overridden, ensure thread-safety and proper response management.
131+
*/
126132
protected Response getResponse() {
127133
return response;
128134
}
129135

136+
/**
137+
* Allows subclasses to customize how the response is set.
138+
* <p>
139+
* If overridden, ensure that the response is correctly handled and does not introduce memory leaks.
140+
*
141+
* @param response The response to set.
142+
*/
130143
protected void setResponse(Response response) {
131144
this.response = response;
132145
}
@@ -506,7 +519,7 @@ private ApiCall<String, CoreApiException> getApiCallGlobalErrorTemplateWithHeade
506519
.hasBinaryResponse(false).retryOption(RetryOption.DEFAULT))
507520
.build();
508521
}
509-
522+
510523
/**
511524
* Creates a global configuration instance with the provided callback.
512525
* This method is designed for extension by subclasses to customize global configuration.

src/test/java/apimatic/core/RequestBuilderTest.java

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@
6060

6161
public class RequestBuilderTest extends MockCoreConfig {
6262

63-
private static final int ELECTRONS_DEFAULT = 12;
64-
private static final int ELECTRONS_UPDATED = 14;
65-
private static final long QUERY_DEFAULT = 234L;
66-
private static final long QUERY_UPDATED = 254L;
67-
private static final double HEADER_DEFAULT = 2.14;
68-
private static final double HEADER_UPDATED = 19.95;
69-
private static final int ATOM_NUMBER = 23;
70-
private static final int ATOM_MASS = 24;
71-
private static final int BODY_A_ELECTRONS = 2;
72-
private static final int BODY_A_MASS = 4;
73-
private static final int BODY_B_ELECTRONS = 4;
74-
private static final int BODY_B_MASS = 2;
75-
private static final int BODY_B_UPDATED = 8;
63+
private static final int ELECTRONS_DEFAULT = 12;
64+
private static final int ELECTRONS_UPDATED = 14;
65+
private static final long QUERY_DEFAULT = 234L;
66+
private static final long QUERY_UPDATED = 254L;
67+
private static final double HEADER_DEFAULT = 2.14;
68+
private static final double HEADER_UPDATED = 19.95;
69+
private static final int ATOM_NUMBER = 23;
70+
private static final int ATOM_MASS = 24;
71+
private static final int BODY_A_ELECTRONS = 2;
72+
private static final int BODY_A_MASS = 4;
73+
private static final int BODY_B_ELECTRONS = 4;
74+
private static final int BODY_B_MASS = 2;
75+
private static final int BODY_B_UPDATED = 8;
7676

7777

7878
/**
@@ -173,9 +173,11 @@ public void testUpdateSimpleFormParam() throws IOException {
173173
@Test
174174
public void testUpdateComplexFormParam() throws IOException {
175175
HttpRequest.Builder localRequestBuilder = new HttpRequest.Builder().httpMethod(Method.GET)
176-
.formParam(param -> param.key("form").value(new Atom(ELECTRONS_DEFAULT, ELECTRONS_DEFAULT)));
176+
.formParam(param -> param.key("form")
177+
.value(new Atom(ELECTRONS_DEFAULT, ELECTRONS_DEFAULT)));
177178

178-
updateAndVerify(localRequestBuilder, "$request.body#/form/NumberOfElectrons", ELECTRONS_DEFAULT, ELECTRONS_UPDATED);
179+
updateAndVerify(localRequestBuilder, "$request.body#/form/NumberOfElectrons",
180+
ELECTRONS_DEFAULT, ELECTRONS_UPDATED);
179181
}
180182

181183
@Test
@@ -189,9 +191,11 @@ public void testUpdateSimpleQueryParam() throws IOException {
189191
@Test
190192
public void testUpdateComplexQueryParam() throws IOException {
191193
HttpRequest.Builder localRequestBuilder = new HttpRequest.Builder().httpMethod(Method.GET)
192-
.queryParam(param -> param.key("que").value(new Atom(ELECTRONS_DEFAULT, ELECTRONS_DEFAULT)));
194+
.queryParam(param -> param.key("que")
195+
.value(new Atom(ELECTRONS_DEFAULT, ELECTRONS_DEFAULT)));
193196

194-
updateAndVerify(localRequestBuilder, "$request.query#/que/NumberOfElectrons", ELECTRONS_DEFAULT, ELECTRONS_UPDATED);
197+
updateAndVerify(localRequestBuilder, "$request.query#/que/NumberOfElectrons",
198+
ELECTRONS_DEFAULT, ELECTRONS_UPDATED);
195199
}
196200

197201
@Test
@@ -205,9 +209,11 @@ public void testUpdateSimpleHeaderParam() throws IOException {
205209
@Test
206210
public void testUpdateComplexHeaderParam() throws IOException {
207211
HttpRequest.Builder localRequestBuilder = new HttpRequest.Builder().httpMethod(Method.GET)
208-
.headerParam(param -> param.key("head").value(new Atom(ELECTRONS_DEFAULT, ELECTRONS_DEFAULT)));
212+
.headerParam(param -> param.key("head")
213+
.value(new Atom(ELECTRONS_DEFAULT, ELECTRONS_DEFAULT)));
209214

210-
updateAndVerify(localRequestBuilder, "$request.headers#/head/NumberOfElectrons", ELECTRONS_DEFAULT, ELECTRONS_UPDATED);
215+
updateAndVerify(localRequestBuilder, "$request.headers#/head/NumberOfElectrons",
216+
ELECTRONS_DEFAULT, ELECTRONS_UPDATED);
211217
}
212218

213219
@Test
@@ -224,16 +230,20 @@ public void testUpdateComplexBodyParam() throws IOException {
224230
HttpRequest.Builder localRequestBuilder = new HttpRequest.Builder().httpMethod(Method.POST)
225231
.bodyParam(param -> param.value(new Atom(ATOM_NUMBER, ATOM_MASS)));
226232

227-
updateAndVerify(localRequestBuilder, "$request.body#/NumberOfElectrons", ATOM_NUMBER, ATOM_MASS);
233+
updateAndVerify(localRequestBuilder, "$request.body#/NumberOfElectrons",
234+
ATOM_NUMBER, ATOM_MASS);
228235
}
229236

230237
@Test
231238
public void testUpdateMultipleComplexBodyParams() throws IOException {
232239
HttpRequest.Builder localRequestBuilder = new HttpRequest.Builder().httpMethod(Method.POST)
233-
.bodyParam(param -> param.key("bodyA").value(new Atom(BODY_A_ELECTRONS, BODY_A_MASS)))
234-
.bodyParam(param -> param.key("bodyB").value(new Atom(BODY_B_ELECTRONS, BODY_B_MASS)));
240+
.bodyParam(param -> param.key("bodyA")
241+
.value(new Atom(BODY_A_ELECTRONS, BODY_A_MASS)))
242+
.bodyParam(param -> param.key("bodyB")
243+
.value(new Atom(BODY_B_ELECTRONS, BODY_B_MASS)));
235244

236-
updateAndVerify(localRequestBuilder, "$request.body#/bodyB/NumberOfElectrons", BODY_B_ELECTRONS, BODY_B_UPDATED);
245+
updateAndVerify(localRequestBuilder, "$request.body#/bodyB/NumberOfElectrons",
246+
BODY_B_ELECTRONS, BODY_B_UPDATED);
237247
}
238248

239249
@Test
@@ -244,7 +254,8 @@ public void testUpdateSimpleBodyParam() throws IOException {
244254
updateAndVerify(localRequestBuilder, "$request.body", "BodyValue", "new Value");
245255
}
246256

247-
private void updateAndVerify(HttpRequest.Builder requestBuilder, String pointer, Object oldValue, Object newValue) {
257+
private void updateAndVerify(HttpRequest.Builder requestBuilder,
258+
String pointer, Object oldValue, Object newValue) {
248259
requestBuilder.updateParameterByJsonPointer(pointer, old -> {
249260
assertEquals(oldValue.toString(), old.toString());
250261
return newValue;
@@ -289,7 +300,7 @@ public void testBodyParam() throws IOException {
289300
// verify
290301
assertEquals(coreHttpRequest.getBody(), "bodyValue");
291302
}
292-
303+
293304
@Test
294305
public void testClonedBodyParam() throws IOException {
295306
// when

src/test/java/apimatic/core/type/pagination/CursorPaginationTest.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public void testWithValidCursor() {
4242
when(paginatedData.getResponse()).thenReturn(response);
4343
when(response.getBody()).thenReturn(NEXT_CURSOR_JSON_STRING);
4444

45-
CursorPagination cursor = new CursorPagination(RESPONSE_POINTER_VALID, REQUEST_QUERY_POINTER);
45+
CursorPagination cursor = new CursorPagination(RESPONSE_POINTER_VALID,
46+
REQUEST_QUERY_POINTER);
4647

4748
Builder requestBuilder = cursor.apply(paginatedData);
4849
assertNotNull(requestBuilder);
@@ -65,7 +66,8 @@ public void testWithValidCursorAndDifferentTypeA() {
6566
when(paginatedData.getResponse()).thenReturn(response);
6667
when(response.getBody()).thenReturn(NEXT_CURSOR_JSON_INT);
6768

68-
CursorPagination cursor = new CursorPagination(RESPONSE_POINTER_VALID, REQUEST_QUERY_POINTER);
69+
CursorPagination cursor = new CursorPagination(RESPONSE_POINTER_VALID,
70+
REQUEST_QUERY_POINTER);
6971

7072
Builder requestBuilder = cursor.apply(paginatedData);
7173
assertNotNull(requestBuilder);
@@ -89,7 +91,8 @@ public void testWithValidCursorAndDifferentType() {
8991
when(paginatedData.getResponse()).thenReturn(response);
9092
when(response.getBody()).thenReturn(NEXT_CURSOR_JSON_INT);
9193

92-
CursorPagination cursor = new CursorPagination(RESPONSE_POINTER_VALID, REQUEST_QUERY_POINTER);
94+
CursorPagination cursor = new CursorPagination(RESPONSE_POINTER_VALID,
95+
REQUEST_QUERY_POINTER);
9396

9497
Builder requestBuilder = cursor.apply(paginatedData);
9598
assertNotNull(requestBuilder);
@@ -111,7 +114,8 @@ public void testWithValidCursorButMissingInFirstRequest() {
111114
when(paginatedData.getResponse()).thenReturn(response);
112115
when(response.getBody()).thenReturn(NEXT_CURSOR_JSON_STRING);
113116

114-
CursorPagination cursor = new CursorPagination(RESPONSE_POINTER_VALID, REQUEST_QUERY_POINTER);
117+
CursorPagination cursor = new CursorPagination(RESPONSE_POINTER_VALID,
118+
REQUEST_QUERY_POINTER);
115119

116120
Builder requestBuilder = cursor.apply(paginatedData);
117121
assertNull(requestBuilder);
@@ -127,7 +131,8 @@ public void testWithValidCursorFromResponseBody() {
127131
when(paginatedData.getResponse()).thenReturn(response);
128132
when(response.getBody()).thenReturn(NEXT_CURSOR_JSON_INT);
129133

130-
CursorPagination cursor = new CursorPagination(RESPONSE_POINTER_VALID, REQUEST_QUERY_POINTER);
134+
CursorPagination cursor = new CursorPagination(RESPONSE_POINTER_VALID,
135+
REQUEST_QUERY_POINTER);
131136

132137
Builder requestBuilder = cursor.apply(paginatedData);
133138
assertNotNull(requestBuilder);
@@ -150,7 +155,8 @@ public void testWithInvalidResponsePointer() {
150155
when(paginatedData.getResponse()).thenReturn(response);
151156
when(response.getBody()).thenReturn(NEXT_CURSOR_JSON_STRING);
152157

153-
CursorPagination cursor = new CursorPagination(RESPONSE_POINTER_INVALID, REQUEST_HEADER_POINTER);
158+
CursorPagination cursor = new CursorPagination(RESPONSE_POINTER_INVALID,
159+
REQUEST_HEADER_POINTER);
154160

155161
Builder requestBuilder = cursor.apply(paginatedData);
156162
assertNull(requestBuilder);
@@ -182,7 +188,8 @@ public void testWithInvalidRequestPointer() {
182188
when(paginatedData.getResponse()).thenReturn(response);
183189
when(response.getBody()).thenReturn(NEXT_CURSOR_JSON_STRING);
184190

185-
CursorPagination cursor = new CursorPagination(RESPONSE_POINTER_VALID, REQUEST_POINTER_INVALID);
191+
CursorPagination cursor = new CursorPagination(RESPONSE_POINTER_VALID,
192+
REQUEST_POINTER_INVALID);
186193

187194
assertNull(cursor.apply(paginatedData));
188195
}

src/test/java/apimatic/core/type/pagination/LinkPaginationTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@
2727
*/
2828
public class LinkPaginationTest {
2929

30-
private static final String RESPONSE_BODY_POINTER = "$response.body#/next";
31-
private static final String RESPONSE_HEADERS_POINTER = "$response.headers#/next";
32-
private static final String REQUEST_QUERY_PAGE = "$request.query#/page";
33-
private static final String REQUEST_QUERY_SIZE = "$request.query#/size";
34-
private static final String REQUEST_HEADERS_PAGE = "$request.headers#/page";
35-
private static final String NEXT_URL_SINGLE = "https://api.example.com?page=2";
36-
private static final String NEXT_URL_MULTIPLE = "https://api.example.com?page=2&size=5";
37-
private static final String NEXT_URL_ENCODED =
38-
"https://api.example.com?page%20o=2%20a&size%20q=5^%214$#";
39-
private static final String PAGE = "page";
40-
private static final String SIZE = "size";
41-
private static final String NEXT = "next";
30+
private static final String RESPONSE_BODY_POINTER = "$response.body#/next";
31+
private static final String RESPONSE_HEADERS_POINTER = "$response.headers#/next";
32+
private static final String REQUEST_QUERY_PAGE = "$request.query#/page";
33+
private static final String REQUEST_QUERY_SIZE = "$request.query#/size";
34+
private static final String REQUEST_HEADERS_PAGE = "$request.headers#/page";
35+
private static final String NEXT_URL_SINGLE = "https://api.example.com?page=2";
36+
private static final String NEXT_URL_MULTIPLE = "https://api.example.com?page=2&size=5";
37+
private static final String NEXT_URL_ENCODED =
38+
"https://api.example.com?page%20o=2%20a&size%20q=5^%214$#";
39+
private static final String PAGE = "page";
40+
private static final String SIZE = "size";
41+
private static final String NEXT = "next";
4242

4343

4444
/**

src/test/java/apimatic/core/type/pagination/OffsetPaginationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class OffsetPaginationTest {
3232
private static final int PAGE_SIZE = 100;
3333
private static final int OFFSET_PLUS_PAGE = INITIAL_OFFSET + PAGE_SIZE;
3434
private static final int OFFSET_VAL = 1;
35-
private static final int OFFSET_VAL_PLUS_ONE = OFFSET_VAL + PAGE_SIZE;
35+
private static final int OFFSET_VAL_PLUS_ONE = OFFSET_VAL + PAGE_SIZE;
3636
private static final int OFFSET_STRING = 5;
3737
private static final int OFFSET_STRING_PLUS_PAGE = OFFSET_STRING + PAGE_SIZE;
3838
private static final int NUMERIC_OFFSET = 5;

src/test/java/apimatic/core/type/pagination/PaginatedDataTest.java

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public void testInvalidPaginationWithFailingResponse() throws IOException {
7373
@Test
7474
public void testInvalidPaginationWithFailingResponseAsync()
7575
throws IOException, InterruptedException, ExecutionException {
76-
Runnable call1 = () -> when(getResponse().getStatusCode()).thenReturn(NOT_FOUND_STATUS_CODE);
76+
Runnable call1 = () -> when(getResponse()
77+
.getStatusCode()).thenReturn(NOT_FOUND_STATUS_CODE);
7778
PaginatedData<String, PageWrapper<String, RecordPage>,
7879
RecordPage, CoreApiException> paginatedData = getPaginatedData(call1, null, null,
7980
new LinkPagination("$response.body#/next_link"));
@@ -88,17 +89,27 @@ public void testInvalidPaginationWithFailingResponseAsync()
8889
assertFalse(hasNext);
8990
}
9091

92+
/**
93+
* Provides invalid pagination test cases to verify handling of edge cases and null responses.
94+
*
95+
* @return A list of invalid pagination test cases, each represented as an object array.
96+
*/
9197
@Parameters
9298
public static List<Object[]> invalidPaginationTestCases() {
9399
return Arrays.asList(new Object[][] {
94-
{ "{\"data\":[],\"next_link\":\"https://localhost:3000/path?page=2\"}" },
95-
{ "{\"data\":null,\"next_link\":\"https://localhost:3000/path?page=2\"}" },
96-
{ null }
100+
{"{\"data\":[],\"next_link\":\"https://localhost:3000/path?page=2\"}"},
101+
{"{\"data\":null,\"next_link\":\"https://localhost:3000/path?page=2\"}"},
102+
{null}
97103
});
98104
}
99105

100106
private final String responseBody;
101107

108+
/**
109+
* Constructs a PaginatedDataTest with the given response body.
110+
*
111+
* @param responseBody the response body string to be used in the test
112+
*/
102113
public PaginatedDataTest(final String responseBody) {
103114
this.responseBody = responseBody;
104115
}
@@ -412,7 +423,7 @@ private void verifyData(PaginatedData<String, PageWrapper<String, RecordPage>,
412423
assertEquals(expectedNextLink, p.getResult().getNextLink());
413424
assertEquals(expectedData, p.getResult().getData());
414425
assertEquals(expectedData, p.getItems());
415-
assertEquals(200, p.getStatusCode());
426+
assertEquals(SUCCESS_STATUS_CODE, p.getStatusCode());
416427
assertEquals(getHttpHeaders(), p.getHeaders());
417428
if (p.getCursorInput() != null && pageOffset > 0) {
418429
assertEquals(expectedPages.get(pageOffset - 1).getPageInfo(), p.getCursorInput());
@@ -427,7 +438,7 @@ private void verifyData(PaginatedData<String, PageWrapper<String, RecordPage>,
427438
}
428439

429440
if (p.getOffsetInput() != -1) {
430-
assertEquals(pageOffset * 3, p.getOffsetInput());
441+
assertEquals(pageOffset * PAGE_SIZE, p.getOffsetInput());
431442
}
432443
pageOffset++;
433444
}
@@ -437,31 +448,30 @@ private void verifyData(PaginatedData<String, PageWrapper<String, RecordPage>,
437448
}
438449

439450
private PaginatedData<String, PageWrapper<String, RecordPage>,
440-
RecordPage, CoreApiException> getPaginatedData(Runnable call1, Runnable call2,
441-
Runnable call3,
442-
PaginationStrategy... pagination) throws IOException {
443-
when(getResponse().getHeaders()).thenReturn(getHttpHeaders());
444-
Callback pageCallback = new Callback() {
445-
private int callNumber = 1;
446-
@Override
447-
public void onBeforeRequest(Request request) {
448-
if (callNumber == 1) {
449-
call1.run();
450-
}
451-
else if (callNumber == 2) {
452-
call2.run();
453-
}
454-
else if (callNumber == 3) {
455-
call3.run();
456-
callNumber = 0;
457-
}
458-
callNumber++;
459-
}
460-
@Override
461-
public void onAfterResponse(Context context) {
462-
// Does nothing
451+
RecordPage, CoreApiException> getPaginatedData(Runnable call1, Runnable call2,
452+
Runnable call3,
453+
PaginationStrategy... pagination) throws IOException {
454+
when(getResponse().getHeaders()).thenReturn(getHttpHeaders());
455+
Callback pageCallback = new Callback() {
456+
private int callNumber = 1;
457+
@Override
458+
public void onBeforeRequest(Request request) {
459+
if (callNumber == 1) {
460+
call1.run();
461+
} else if (callNumber == 2) {
462+
call2.run();
463+
} else if (callNumber == PAGE_SIZE) {
464+
call3.run();
465+
callNumber = 0;
463466
}
464-
};
467+
callNumber++;
468+
}
469+
@Override
470+
public void onAfterResponse(Context context) {
471+
// Does nothing
472+
}
473+
};
474+
465475
return new ApiCall.Builder<RecordPage, CoreApiException>()
466476
.globalConfig(getGlobalConfig(pageCallback))
467477
.requestBuilder(requestBuilder -> requestBuilder.server("https://localhost:3000")
@@ -472,7 +482,7 @@ public void onAfterResponse(Context context) {
472482
.formParam(param -> param.key("limit").value(1).isRequired(false))
473483
.queryParam(param -> param.key("page").value(1).isRequired(false))
474484
.headerParam(param -> param.key("size").value(2).isRequired(false))
475-
.headerParam(param -> param.key("size").value(3).isRequired(false))
485+
.headerParam(param -> param.key("size").value(PAGE_SIZE).isRequired(false))
476486
.headerParam(param -> param.key("accept").value("application/json"))
477487
.httpMethod(Method.GET))
478488
.responseHandler(responseHandler -> responseHandler

0 commit comments

Comments
 (0)