Skip to content

Commit 6e8412f

Browse files
committed
Refactored remote API JUnit tests. Removed boilerplate code from mock server route initialization.
1 parent cd7b14d commit 6e8412f

File tree

7 files changed

+174
-143
lines changed

7 files changed

+174
-143
lines changed

src/test/java/com/github/m0nk3y2k4/thetvdb/internal/resource/impl/EpisodesAPITest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import static com.github.m0nk3y2k4.thetvdb.internal.resource.impl.EpisodesAPI.get;
44
import static com.github.m0nk3y2k4.thetvdb.internal.util.http.HttpRequestMethod.GET;
5-
import static com.github.m0nk3y2k4.thetvdb.testutils.MockServerUtil.json;
5+
import static com.github.m0nk3y2k4.thetvdb.testutils.MockServerUtil.jsonResponse;
6+
import static com.github.m0nk3y2k4.thetvdb.testutils.MockServerUtil.request;
67
import static com.github.m0nk3y2k4.thetvdb.testutils.json.JSONTestUtil.JsonResource.EPISODE;
78
import static com.github.m0nk3y2k4.thetvdb.testutils.parameterized.TestRemoteAPICall.route;
89
import static org.assertj.core.api.Assertions.assertThat;
910
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
10-
import static org.mockserver.model.HttpRequest.request;
11-
import static org.mockserver.model.HttpResponse.response;
11+
import static org.junit.jupiter.params.provider.Arguments.of;
1212

1313
import java.util.function.Supplier;
1414
import java.util.stream.Stream;
@@ -29,23 +29,23 @@ class EpisodesAPITest {
2929

3030
@BeforeAll
3131
static void setUpRoutes(MockServerClient client) throws Exception {
32-
client.when(request("/episodes/2364").withMethod(GET.getName())).respond(response().withBody(json(EPISODE)));
32+
client.when(request("/episodes/2364", GET)).respond(jsonResponse(EPISODE));
3333
}
3434

3535
private static Stream<Arguments> withInvalidParameters() {
3636
return Stream.of(
37-
Arguments.of(route(con -> get(con, 0), "get() with ZERO language ID")),
38-
Arguments.of(route(con -> get(con, -12), "get() with negative language ID"))
37+
of(route(con -> get(con, 0), "get() with ZERO language ID")),
38+
of(route(con -> get(con, -12), "get() with negative language ID"))
3939
);
4040
}
4141

4242
private static Stream<Arguments> withValidParameters() {
4343
return Stream.of(
44-
Arguments.of(route(con -> get(con, 2364), "get()"), EPISODE)
44+
of(route(con -> get(con, 2364), "get()"), EPISODE)
4545
);
4646
}
4747

48-
@ParameterizedTest(name = "[{index}] Route EpisodesAPI.{0} rejected")
48+
@ParameterizedTest(name = "[{index}] Route EpisodesAPI.{0} invalid parameters rejected")
4949
@MethodSource(value = "withInvalidParameters")
5050
void invokeRoute_withInvalidParameters_verifyParameterValidation(TestRemoteAPICall route, Supplier<RemoteAPI> remoteAPI) {
5151
assertThatIllegalArgumentException().isThrownBy(() -> route.invoke(new APIConnection("OIW8E4GT58H8E4W", remoteAPI)));

src/test/java/com/github/m0nk3y2k4/thetvdb/internal/resource/impl/LanguagesAPITest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import static com.github.m0nk3y2k4.thetvdb.internal.resource.impl.LanguagesAPI.get;
44
import static com.github.m0nk3y2k4.thetvdb.internal.resource.impl.LanguagesAPI.getAllAvailable;
55
import static com.github.m0nk3y2k4.thetvdb.internal.util.http.HttpRequestMethod.GET;
6-
import static com.github.m0nk3y2k4.thetvdb.testutils.MockServerUtil.json;
6+
import static com.github.m0nk3y2k4.thetvdb.testutils.MockServerUtil.jsonResponse;
7+
import static com.github.m0nk3y2k4.thetvdb.testutils.MockServerUtil.request;
78
import static com.github.m0nk3y2k4.thetvdb.testutils.json.JSONTestUtil.JsonResource.LANGUAGE;
89
import static com.github.m0nk3y2k4.thetvdb.testutils.json.JSONTestUtil.JsonResource.LANGUAGES;
910
import static com.github.m0nk3y2k4.thetvdb.testutils.parameterized.TestRemoteAPICall.route;
1011
import static org.assertj.core.api.Assertions.assertThat;
1112
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
12-
import static org.mockserver.model.HttpRequest.request;
13-
import static org.mockserver.model.HttpResponse.response;
13+
import static org.junit.jupiter.params.provider.Arguments.of;
1414

1515
import java.util.function.Supplier;
1616
import java.util.stream.Stream;
@@ -31,26 +31,26 @@ class LanguagesAPITest {
3131

3232
@BeforeAll
3333
static void setUpRoutes(MockServerClient client) throws Exception {
34-
client.when(request("/languages").withMethod(GET.getName())).respond(response().withBody(json(LANGUAGES)));
35-
client.when(request("/languages/4584").withMethod(GET.getName())).respond(response().withBody(json(LANGUAGE)));
34+
client.when(request("/languages", GET)).respond(jsonResponse(LANGUAGES));
35+
client.when(request("/languages/4584", GET)).respond(jsonResponse(LANGUAGE));
3636
}
3737

3838
private static Stream<Arguments> withInvalidParameters() {
3939
return Stream.of(
40-
Arguments.of(route(con -> get(con, 0), "get() with ZERO language ID")),
41-
Arguments.of(route(con -> get(con, -11), "get() with negative language ID"))
40+
of(route(con -> get(con, 0), "get() with ZERO language ID")),
41+
of(route(con -> get(con, -11), "get() with negative language ID"))
4242
);
4343
}
4444

4545
@SuppressWarnings("Convert2MethodRef")
4646
private static Stream<Arguments> withValidParameters() {
4747
return Stream.of(
48-
Arguments.of(route(con -> get(con, 4584), "get()"), LANGUAGE),
49-
Arguments.of(route(con -> getAllAvailable(con), "getAllAvailable()"), LANGUAGES)
48+
of(route(con -> get(con, 4584), "get()"), LANGUAGE),
49+
of(route(con -> getAllAvailable(con), "getAllAvailable()"), LANGUAGES)
5050
);
5151
}
5252

53-
@ParameterizedTest(name = "[{index}] Route LanguagesAPI.{0} rejected")
53+
@ParameterizedTest(name = "[{index}] Route LanguagesAPI.{0} invalid parameters rejected")
5454
@MethodSource(value = "withInvalidParameters")
5555
void invokeRoute_withInvalidParameters_verifyParameterValidation(TestRemoteAPICall route, Supplier<RemoteAPI> remoteAPI) {
5656
assertThatIllegalArgumentException().isThrownBy(() -> route.invoke(new APIConnection("946FG8I5P5E56E4", remoteAPI)));

src/test/java/com/github/m0nk3y2k4/thetvdb/internal/resource/impl/SearchAPITest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
import static com.github.m0nk3y2k4.thetvdb.internal.resource.impl.SearchAPI.series;
55
import static com.github.m0nk3y2k4.thetvdb.internal.util.http.HttpRequestMethod.GET;
66
import static com.github.m0nk3y2k4.thetvdb.testutils.APITestUtil.params;
7-
import static com.github.m0nk3y2k4.thetvdb.testutils.MockServerUtil.json;
7+
import static com.github.m0nk3y2k4.thetvdb.testutils.MockServerUtil.jsonResponse;
8+
import static com.github.m0nk3y2k4.thetvdb.testutils.MockServerUtil.request;
89
import static com.github.m0nk3y2k4.thetvdb.testutils.json.JSONTestUtil.JsonResource.QUERYPARAMETERS;
910
import static com.github.m0nk3y2k4.thetvdb.testutils.json.JSONTestUtil.JsonResource.SERIESSEARCH;
1011
import static com.github.m0nk3y2k4.thetvdb.testutils.parameterized.TestRemoteAPICall.route;
1112
import static org.assertj.core.api.Assertions.assertThat;
12-
import static org.mockserver.model.HttpRequest.request;
13-
import static org.mockserver.model.HttpResponse.response;
13+
import static org.junit.jupiter.params.provider.Arguments.of;
14+
import static org.mockserver.model.Parameter.param;
1415

1516
import java.util.function.Supplier;
1617
import java.util.stream.Stream;
@@ -31,15 +32,15 @@ class SearchAPITest {
3132

3233
@BeforeAll
3334
static void setUpRoutes(MockServerClient client) throws Exception {
34-
client.when(request("/search/series").withQueryStringParameter("name", "Some Series").withMethod(GET.getName())).respond(response().withBody(json(SERIESSEARCH)));
35-
client.when(request("/search/series/params").withMethod(GET.getName())).respond(response().withBody(json(QUERYPARAMETERS)));
35+
client.when(request("/search/series", GET, param("name", "Some Series"))).respond(jsonResponse(SERIESSEARCH));
36+
client.when(request("/search/series/params", GET)).respond(jsonResponse(QUERYPARAMETERS));
3637
}
3738

3839
@SuppressWarnings("Convert2MethodRef")
3940
private static Stream<Arguments> withValidParameters() {
4041
return Stream.of(
41-
Arguments.of(route(con -> series(con, params("name", "Some Series")), "series()"), SERIESSEARCH),
42-
Arguments.of(route(con -> getAvailableSearchParameters(con), "getAvailableSearchParameters()"), QUERYPARAMETERS)
42+
of(route(con -> series(con, params("name", "Some Series")), "series()"), SERIESSEARCH),
43+
of(route(con -> getAvailableSearchParameters(con), "getAvailableSearchParameters()"), QUERYPARAMETERS)
4344
);
4445
}
4546

0 commit comments

Comments
 (0)