From 55ecced6b8cf8163ad2a63307acf43c032653f44 Mon Sep 17 00:00:00 2001 From: Chris Simmons Date: Mon, 5 Jun 2023 10:51:07 -0700 Subject: [PATCH 1/6] Use separate authentication URL - Test refactoring - Push ClientAndServer mockServer down to TestBase - Push some common client setup down to TestBase - Separate "api" and "auth" server/client mocks --- .../java/com/gettyimages/api/ApiClient.java | 34 +++---- src/test/java/AuthFailureTest.java | 24 ++--- src/test/java/CustomRequestTest.java | 52 +++------- src/test/java/DownloadImagesTest.java | 46 +++------ src/test/java/DownloadVideosTest.java | 42 ++------ src/test/java/ImagesTest.java | 41 ++------ src/test/java/SearchImagesCreativeTest.java | 87 ++++++---------- src/test/java/SearchImagesEditorialTest.java | 98 +++++++------------ src/test/java/SearchImagesTest.java | 92 +++++++---------- src/test/java/SearchVideosCreativeTest.java | 70 +++++-------- src/test/java/SearchVideosEditorialTest.java | 74 +++++--------- src/test/java/SearchVideosTest.java | 72 +++++--------- src/test/java/TestBase.java | 49 ++++++++++ src/test/java/VideosTest.java | 41 ++------ 14 files changed, 298 insertions(+), 524 deletions(-) create mode 100644 src/test/java/TestBase.java diff --git a/src/main/java/com/gettyimages/api/ApiClient.java b/src/main/java/com/gettyimages/api/ApiClient.java index 9be594d..e95feef 100644 --- a/src/main/java/com/gettyimages/api/ApiClient.java +++ b/src/main/java/com/gettyimages/api/ApiClient.java @@ -14,14 +14,15 @@ public class ApiClient { public static String Version="2.1.1"; private String Slash = "/"; private Credentials credentials; - private static String baseUrl = "https://api.gettyimages.com/v3"; + private static String apiBaseUrl = "https://api.gettyimages.com/v3"; + private static String authenticationBaseUrl = "https://authentication.gettyimages.com"; private ApiClient(String apiKey, String apiSecret) { - credentials = Credentials.GetInstance(apiKey, apiSecret, GetOAuthBaseUrl()); + credentials = Credentials.GetInstance(apiKey, apiSecret, authenticationBaseUrl); } private ApiClient(String apiKey, String apiSecret, String userName, String userPassword) { - credentials = Credentials.GetInstance(apiKey, apiSecret, userName, userPassword, GetOAuthBaseUrl()); + credentials = Credentials.GetInstance(apiKey, apiSecret, userName, userPassword, authenticationBaseUrl); } public static ApiClient GetApiClientWithClientCredentials(String apiKey, String apiSecret) @@ -34,54 +35,49 @@ public static ApiClient GetApiClientWithResourceOwnerCredentials(String apiKey, return new ApiClient(apiKey, apiSecret, userName, password); } - private String GetOAuthBaseUrl() { - String oAuthBaseUrl = baseUrl.substring(0, baseUrl.lastIndexOf(Slash)); - return oAuthBaseUrl; - } - public Images images() { - return Images.GetInstance(credentials, baseUrl); + return Images.GetInstance(credentials, apiBaseUrl); } public Videos videos() { - return Videos.GetInstance(credentials, baseUrl); + return Videos.GetInstance(credentials, apiBaseUrl); } public SearchImages searchimages() { - return SearchImages.GetInstance(credentials, baseUrl); + return SearchImages.GetInstance(credentials, apiBaseUrl); } public SearchImagesCreative searchimagescreative() { - return SearchImagesCreative.GetInstance(credentials, baseUrl); + return SearchImagesCreative.GetInstance(credentials, apiBaseUrl); } public SearchImagesEditorial searchimageseditorial() { - return SearchImagesEditorial.GetInstance(credentials, baseUrl); + return SearchImagesEditorial.GetInstance(credentials, apiBaseUrl); } public SearchVideos searchvideos() { - return SearchVideos.GetInstance(credentials, baseUrl); + return SearchVideos.GetInstance(credentials, apiBaseUrl); } public SearchVideosCreative searchvideoscreative() { - return SearchVideosCreative.GetInstance(credentials, baseUrl); + return SearchVideosCreative.GetInstance(credentials, apiBaseUrl); } public SearchVideosEditorial searchvideoseditorial() { - return SearchVideosEditorial.GetInstance(credentials, baseUrl); + return SearchVideosEditorial.GetInstance(credentials, apiBaseUrl); } public DownloadVideos downloadvideos() { - return DownloadVideos.GetInstance(credentials, baseUrl); + return DownloadVideos.GetInstance(credentials, apiBaseUrl); } public DownloadImages downloadimages() { - return DownloadImages.GetInstance(credentials, baseUrl); + return DownloadImages.GetInstance(credentials, apiBaseUrl); } public CustomRequest customrequest() { - return CustomRequest.GetInstance(credentials, baseUrl); + return CustomRequest.GetInstance(credentials, apiBaseUrl); } } diff --git a/src/test/java/AuthFailureTest.java b/src/test/java/AuthFailureTest.java index 1dc853c..4b160d6 100644 --- a/src/test/java/AuthFailureTest.java +++ b/src/test/java/AuthFailureTest.java @@ -1,11 +1,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; -import java.lang.reflect.Field; - import com.gettyimages.api.ApiClient; import com.gettyimages.api.HttpClientErrorException; import com.gettyimages.api.HttpSystemErrorException; @@ -15,18 +12,12 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockserver.client.server.MockServerClient; -import org.mockserver.integration.ClientAndServer; import org.mockserver.model.Parameter; -public class AuthFailureTest { - private ClientAndServer mockServer; - +public class AuthFailureTest extends TestBase { @BeforeEach public void startProxy() throws Exception { - final Field field = ApiClient.class.getDeclaredField("baseUrl"); - field.setAccessible(true); - field.set(null, "http://127.0.0.1:1080/"); - mockServer = startClientAndServer(1080); + startMockServers(); } @Test @@ -38,7 +29,7 @@ void clientErrorOnAuthentication() throws Exception { .withPhrase("cat"); final HttpClientErrorException exception = assertThrows(HttpClientErrorException.class, () -> { search.executeAsync(); } ); - assertEquals(exception.getStatusCode(), statusCode); + assertEquals(statusCode, exception.getStatusCode()); } @Test @@ -50,17 +41,14 @@ void systemErrorOnAuthentication() throws Exception { .withPhrase("cat"); final HttpSystemErrorException exception = assertThrows(HttpSystemErrorException.class, () -> { search.executeAsync(); } ); - assertEquals(exception.getStatusCode(), statusCode); + assertEquals(statusCode, exception.getStatusCode()); } - @AfterEach - public void stopProxy() { - mockServer.stop(); - } + public void stopProxy() { stopMockServers(); } private void createMock(final int statusCode) { - final MockServerClient client = new MockServerClient("127.0.0.1", 1080); + final MockServerClient client = new MockServerClient("127.0.0.1", 1081); client.when(request().withMethod("POST").withPath("/oauth2/token")) .respond(response().withStatusCode(statusCode)); diff --git a/src/test/java/CustomRequestTest.java b/src/test/java/CustomRequestTest.java index 12ae0cb..be6e965 100644 --- a/src/test/java/CustomRequestTest.java +++ b/src/test/java/CustomRequestTest.java @@ -6,16 +6,10 @@ import org.apache.http.entity.ByteArrayEntity; import org.json.JSONObject; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockserver.client.server.MockServerClient; -import org.mockserver.integration.ClientAndServer; +import org.junit.jupiter.api.*; import org.mockserver.matchers.MatchType; import org.mockserver.model.Parameter; -import java.lang.reflect.Field; import java.util.Arrays; import java.util.EnumSet; import java.util.HashMap; @@ -23,32 +17,17 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; import static org.mockserver.model.JsonBody.json; -public class CustomRequestTest { - private static ClientAndServer mockServer; - +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class CustomRequestTest extends TestBase { @BeforeAll - public static void startProxy() throws Exception { - Field field = ApiClient.class.getDeclaredField("baseUrl"); - field.setAccessible(true); - field.set(null, "http://127.0.0.1:1080/"); - mockServer = startClientAndServer(1080); - MockServerClient client = new MockServerClient("127.0.0.1", 1080); - - client - .when(request() - .withMethod("POST") - .withPath("/oauth2/token") - ) - .respond(response() - .withStatusCode(200) - .withBody("{ access_token: 'client_credentials_access_token', token_type: 'Bearer', expires_in: '1800' }") - ); - client.when( + public void startProxy() throws Exception { + startMockServersAndSetupAuth(); + + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -58,7 +37,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("POST") .withPath("/downloads/images/12345") @@ -67,7 +46,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("POST") .withPath("/boards") @@ -81,7 +60,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("PUT") .withPath("/asset-changes/change-sets") @@ -90,7 +69,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("PUT") .withPath("/boards/111") @@ -104,7 +83,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("DELETE") .withPath("/boards/333") @@ -113,7 +92,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -231,9 +210,6 @@ void customRequestGetWithHeader() throws Exception { assertEquals("success", result); } - @AfterAll - public static void stopProxy() { - mockServer.stop(); - } + public void stopProxy() { stopMockServers(); } } diff --git a/src/test/java/DownloadImagesTest.java b/src/test/java/DownloadImagesTest.java index 08558bf..c77be72 100644 --- a/src/test/java/DownloadImagesTest.java +++ b/src/test/java/DownloadImagesTest.java @@ -4,42 +4,20 @@ import com.gettyimages.api.Downloads.DownloadImages; import com.gettyimages.api.Filters.FileType; import com.gettyimages.api.Filters.ProductType; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockserver.client.server.MockServerClient; -import org.mockserver.integration.ClientAndServer; +import org.junit.jupiter.api.*; import org.mockserver.model.Parameter; -import java.lang.reflect.Field; - import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; -public class DownloadImagesTest { - private static ClientAndServer mockServer; - +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class DownloadImagesTest extends TestBase { @BeforeAll - public static void startProxy() throws Exception { - Field field = ApiClient.class.getDeclaredField("baseUrl"); - field.setAccessible(true); - field.set(null, "http://127.0.0.1:1080/"); - mockServer = startClientAndServer(1080); - MockServerClient client = new MockServerClient("127.0.0.1", 1080); + public void startProxy() throws Exception { + startMockServersAndSetupAuth(); - client - .when(request() - .withMethod("POST") - .withPath("/oauth2/token") - ) - .respond(response() - .withStatusCode(200) - .withBody("{ access_token: 'client_credentials_access_token', token_type: 'Bearer', expires_in: '1800' }") - ); - client.when( + apiClientMock.when( request() .withMethod("POST") .withPath("/downloads/images/12345") @@ -49,7 +27,7 @@ public static void startProxy() throws Exception { .withHeader("Accept-Language", "de") ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("POST") .withPath("/downloads/images/12345") @@ -59,7 +37,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("POST") .withPath("/downloads/images/12345") @@ -69,7 +47,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("POST") .withPath("/downloads/images/12345") @@ -79,7 +57,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("POST") .withPath("/downloads/images/12345") @@ -139,7 +117,5 @@ void downloadImagesWithProductType() throws Exception { @AfterAll - public static void stopProxy() { - mockServer.stop(); - } + public void stopProxy() { stopMockServers(); } } diff --git a/src/test/java/DownloadVideosTest.java b/src/test/java/DownloadVideosTest.java index 4143e8c..23a6fd3 100644 --- a/src/test/java/DownloadVideosTest.java +++ b/src/test/java/DownloadVideosTest.java @@ -2,42 +2,20 @@ import com.gettyimages.api.ApiClient; import com.gettyimages.api.Downloads.DownloadVideos; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockserver.client.server.MockServerClient; -import org.mockserver.integration.ClientAndServer; +import org.junit.jupiter.api.*; import org.mockserver.model.Parameter; -import java.lang.reflect.Field; - import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; -public class DownloadVideosTest { - private static ClientAndServer mockServer; - +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class DownloadVideosTest extends TestBase { @BeforeAll - public static void startProxy() throws Exception { - Field field = ApiClient.class.getDeclaredField("baseUrl"); - field.setAccessible(true); - field.set(null, "http://127.0.0.1:1080/"); - mockServer = startClientAndServer(1080); - MockServerClient client = new MockServerClient("127.0.0.1", 1080); + public void startProxy() throws Exception { + startMockServersAndSetupAuth(); - client - .when(request() - .withMethod("POST") - .withPath("/oauth2/token") - ) - .respond(response() - .withStatusCode(200) - .withBody("{ access_token: 'client_credentials_access_token', token_type: 'Bearer', expires_in: '1800' }") - ); - client.when( + apiClientMock.when( request() .withMethod("POST") .withPath("/downloads/videos/12345") @@ -47,7 +25,7 @@ public static void startProxy() throws Exception { .withHeader("Accept-Language", "de") ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("POST") .withPath("/downloads/videos/12345") @@ -57,7 +35,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("POST") .withPath("/downloads/videos/12345") @@ -99,7 +77,5 @@ void downloadVideosWithSize() throws Exception { @AfterAll - public static void stopProxy() { - mockServer.stop(); - } + public void stopProxy() { stopMockServers(); } } diff --git a/src/test/java/ImagesTest.java b/src/test/java/ImagesTest.java index 571b8c8..399d62c 100644 --- a/src/test/java/ImagesTest.java +++ b/src/test/java/ImagesTest.java @@ -2,50 +2,29 @@ import com.gettyimages.api.ApiClient; import com.gettyimages.api.Images.Images; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockserver.client.server.MockServerClient; -import org.mockserver.integration.ClientAndServer; +import org.junit.jupiter.api.*; import org.mockserver.model.Parameter; -import java.lang.reflect.Field; import java.util.Arrays; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; -public class ImagesTest { - private static ClientAndServer mockServer; - +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class ImagesTest extends TestBase { @BeforeAll - public static void startProxy() throws Exception { - Field field = ApiClient.class.getDeclaredField("baseUrl"); - field.setAccessible(true); - field.set(null, "http://127.0.0.1:1080/"); - mockServer = startClientAndServer(1080); - MockServerClient client = new MockServerClient("127.0.0.1", 1080); + public void startProxy() throws Exception { + startMockServersAndSetupAuth(); - client - .when(request() - .withMethod("POST") - .withPath("/oauth2/token") - ) - .respond(response() - .withStatusCode(200) - .withBody("{ access_token: 'client_credentials_access_token', token_type: 'Bearer', expires_in: '1800' }") - ); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/images/12345") .withHeader("Accept-Language", "de") ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/images/12345") @@ -54,7 +33,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/images") @@ -95,7 +74,5 @@ void imagesWithIds() throws Exception { @AfterAll - public static void stopProxy() { - mockServer.stop(); - } + public void stopProxy() { stopMockServers(); } } diff --git a/src/test/java/SearchImagesCreativeTest.java b/src/test/java/SearchImagesCreativeTest.java index 443c18f..758f12f 100644 --- a/src/test/java/SearchImagesCreativeTest.java +++ b/src/test/java/SearchImagesCreativeTest.java @@ -2,51 +2,30 @@ import com.gettyimages.api.ApiClient; import com.gettyimages.api.Filters.*; import com.gettyimages.api.Search.SearchImagesCreative; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockserver.client.server.MockServerClient; -import org.mockserver.integration.ClientAndServer; +import org.junit.jupiter.api.*; import org.mockserver.model.Parameter; -import java.lang.reflect.Field; import java.util.Arrays; import java.util.EnumSet; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; -public class SearchImagesCreativeTest { - private static ClientAndServer mockServer; - +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class SearchImagesCreativeTest extends TestBase { @BeforeAll - public static void startProxy() throws Exception { - Field field = ApiClient.class.getDeclaredField("baseUrl"); - field.setAccessible(true); - field.set(null, "http://127.0.0.1:1080/"); - mockServer = startClientAndServer(1080); - MockServerClient client = new MockServerClient("127.0.0.1", 1080); - - client - .when(request() - .withMethod("POST") - .withPath("/oauth2/token") - ) - .respond(response() - .withStatusCode(200) - .withBody("{ access_token: 'client_credentials_access_token', token_type: 'Bearer', expires_in: '1800' }") - ); - client.when( + public void startProxy() throws Exception { + startMockServersAndSetupAuth(); + + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") .withHeader("Accept-Language", "de") ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -55,7 +34,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -64,7 +43,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -73,7 +52,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -82,7 +61,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -91,7 +70,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -100,7 +79,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -109,7 +88,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -118,7 +97,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -127,7 +106,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -136,7 +115,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -145,7 +124,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -154,7 +133,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -163,7 +142,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -172,7 +151,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -181,7 +160,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -190,7 +169,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -199,7 +178,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -208,7 +187,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -217,7 +196,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -226,7 +205,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -235,7 +214,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -244,7 +223,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -253,7 +232,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/creative") @@ -489,7 +468,5 @@ void searchImagesCreativeWithCustomHeader() throws Exception { } @AfterAll - public static void stopProxy() { - mockServer.stop(); - } + public void stopProxy() { stopMockServers(); } } diff --git a/src/test/java/SearchImagesEditorialTest.java b/src/test/java/SearchImagesEditorialTest.java index 34bf5ba..4b95d14 100644 --- a/src/test/java/SearchImagesEditorialTest.java +++ b/src/test/java/SearchImagesEditorialTest.java @@ -1,53 +1,31 @@ import com.gettyimages.api.ApiClient; import com.gettyimages.api.Filters.*; -import com.gettyimages.api.Search.SearchImagesCreative; import com.gettyimages.api.Search.SearchImagesEditorial; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockserver.client.server.MockServerClient; -import org.mockserver.integration.ClientAndServer; +import org.junit.jupiter.api.*; import org.mockserver.model.Parameter; -import java.lang.reflect.Field; import java.util.Arrays; import java.util.EnumSet; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; -public class SearchImagesEditorialTest { - private static ClientAndServer mockServer; - +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class SearchImagesEditorialTest extends TestBase { @BeforeAll - public static void startProxy() throws Exception { - Field field = ApiClient.class.getDeclaredField("baseUrl"); - field.setAccessible(true); - field.set(null, "http://127.0.0.1:1080/"); - mockServer = startClientAndServer(1080); - MockServerClient client = new MockServerClient("127.0.0.1", 1080); - - client - .when(request() - .withMethod("POST") - .withPath("/oauth2/token") - ) - .respond(response() - .withStatusCode(200) - .withBody("{ access_token: 'client_credentials_access_token', token_type: 'Bearer', expires_in: '1800' }") - ); - client.when( + public void startProxy() throws Exception { + startMockServersAndSetupAuth(); + + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") .withHeader("Accept-Language", "de") ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -56,7 +34,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -65,7 +43,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -74,7 +52,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -83,7 +61,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -92,7 +70,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -101,7 +79,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -110,7 +88,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -119,7 +97,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -128,7 +106,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -137,7 +115,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -146,7 +124,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -155,7 +133,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -164,7 +142,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -173,7 +151,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -182,7 +160,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -191,7 +169,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -200,7 +178,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -209,7 +187,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -218,7 +196,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -227,7 +205,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -236,7 +214,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -245,7 +223,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -254,7 +232,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -263,7 +241,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -272,7 +250,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -281,7 +259,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -290,7 +268,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -299,7 +277,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images/editorial") @@ -581,7 +559,5 @@ void searchImagesEditorialWithCustomHeader() throws Exception { } @AfterAll - public static void stopProxy() { - mockServer.stop(); - } + public void stopProxy() { stopMockServers(); } } \ No newline at end of file diff --git a/src/test/java/SearchImagesTest.java b/src/test/java/SearchImagesTest.java index 8e90742..ea79798 100644 --- a/src/test/java/SearchImagesTest.java +++ b/src/test/java/SearchImagesTest.java @@ -2,52 +2,30 @@ import com.gettyimages.api.ApiClient; import com.gettyimages.api.Filters.*; import com.gettyimages.api.Search.SearchImages; -import com.gettyimages.api.Search.SearchImagesCreative; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockserver.client.server.MockServerClient; -import org.mockserver.integration.ClientAndServer; +import org.junit.jupiter.api.*; import org.mockserver.model.Parameter; -import java.lang.reflect.Field; import java.util.Arrays; import java.util.EnumSet; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; -public class SearchImagesTest { - private static ClientAndServer mockServer; - +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class SearchImagesTest extends TestBase { @BeforeAll - public static void startProxy() throws Exception { - Field field = ApiClient.class.getDeclaredField("baseUrl"); - field.setAccessible(true); - field.set(null, "http://127.0.0.1:1080/"); - mockServer = startClientAndServer(1080); - MockServerClient client = new MockServerClient("127.0.0.1", 1080); - - client - .when(request() - .withMethod("POST") - .withPath("/oauth2/token") - ) - .respond(response() - .withStatusCode(200) - .withBody("{ access_token: 'client_credentials_access_token', token_type: 'Bearer', expires_in: '1800' }") - ); - client.when( + public void startProxy() throws Exception { + startMockServersAndSetupAuth(); + + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") .withHeader("Accept-Language", "de") ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -56,7 +34,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -65,7 +43,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -74,7 +52,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -83,7 +61,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -92,7 +70,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -101,7 +79,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -110,7 +88,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -119,7 +97,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -128,7 +106,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -137,7 +115,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -146,7 +124,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -155,7 +133,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -164,7 +142,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -173,7 +151,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -182,7 +160,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -191,7 +169,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -200,7 +178,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -209,7 +187,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -218,7 +196,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -227,7 +205,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -236,7 +214,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -245,7 +223,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -254,7 +232,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -263,7 +241,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -272,7 +250,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/images") @@ -526,7 +504,5 @@ void searchImagesWithCustomHeader() throws Exception { } @AfterAll - public static void stopProxy() { - mockServer.stop(); - } + public void stopProxy() { stopMockServers(); } } diff --git a/src/test/java/SearchVideosCreativeTest.java b/src/test/java/SearchVideosCreativeTest.java index ee3f9fc..4e1692d 100644 --- a/src/test/java/SearchVideosCreativeTest.java +++ b/src/test/java/SearchVideosCreativeTest.java @@ -1,53 +1,31 @@ import com.gettyimages.api.ApiClient; import com.gettyimages.api.Filters.*; -import com.gettyimages.api.Search.SearchImagesCreative; import com.gettyimages.api.Search.SearchVideosCreative; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockserver.client.server.MockServerClient; -import org.mockserver.integration.ClientAndServer; +import org.junit.jupiter.api.*; import org.mockserver.model.Parameter; -import java.lang.reflect.Field; import java.util.Arrays; import java.util.EnumSet; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; -public class SearchVideosCreativeTest { - private static ClientAndServer mockServer; - +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class SearchVideosCreativeTest extends TestBase { @BeforeAll - public static void startProxy() throws Exception { - Field field = ApiClient.class.getDeclaredField("baseUrl"); - field.setAccessible(true); - field.set(null, "http://127.0.0.1:1080/"); - mockServer = startClientAndServer(1080); - MockServerClient client = new MockServerClient("127.0.0.1", 1080); + public void startProxy() throws Exception { + startMockServersAndSetupAuth(); - client - .when(request() - .withMethod("POST") - .withPath("/oauth2/token") - ) - .respond(response() - .withStatusCode(200) - .withBody("{ access_token: 'client_credentials_access_token', token_type: 'Bearer', expires_in: '1800' }") - ); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") .withHeader("Accept-Language", "de") ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -56,7 +34,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -65,7 +43,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -74,7 +52,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -83,7 +61,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -92,7 +70,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -101,7 +79,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -110,7 +88,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -119,7 +97,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -128,7 +106,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -137,7 +115,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -146,7 +124,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -155,7 +133,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -164,7 +142,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -173,7 +151,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -182,7 +160,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/creative") @@ -345,7 +323,5 @@ void searchVideosCreativeWithCustomHeader() throws Exception { } @AfterAll - public static void stopProxy() { - mockServer.stop(); - } + public void stopProxy() { stopMockServers(); } } diff --git a/src/test/java/SearchVideosEditorialTest.java b/src/test/java/SearchVideosEditorialTest.java index 3ad54b2..242f3a2 100644 --- a/src/test/java/SearchVideosEditorialTest.java +++ b/src/test/java/SearchVideosEditorialTest.java @@ -1,53 +1,31 @@ import com.gettyimages.api.ApiClient; import com.gettyimages.api.Filters.*; -import com.gettyimages.api.Search.SearchVideos; import com.gettyimages.api.Search.SearchVideosEditorial; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockserver.client.server.MockServerClient; -import org.mockserver.integration.ClientAndServer; +import org.junit.jupiter.api.*; import org.mockserver.model.Parameter; -import java.lang.reflect.Field; import java.util.Arrays; import java.util.EnumSet; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; -public class SearchVideosEditorialTest { - private static ClientAndServer mockServer; - +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class SearchVideosEditorialTest extends TestBase { @BeforeAll - public static void startProxy() throws Exception { - Field field = ApiClient.class.getDeclaredField("baseUrl"); - field.setAccessible(true); - field.set(null, "http://127.0.0.1:1080/"); - mockServer = startClientAndServer(1080); - MockServerClient client = new MockServerClient("127.0.0.1", 1080); + public void startProxy() throws Exception { + startMockServersAndSetupAuth(); - client - .when(request() - .withMethod("POST") - .withPath("/oauth2/token") - ) - .respond(response() - .withStatusCode(200) - .withBody("{ access_token: 'client_credentials_access_token', token_type: 'Bearer', expires_in: '1800' }") - ); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") .withHeader("Accept-Language", "de") ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -56,7 +34,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -65,7 +43,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -74,7 +52,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -83,7 +61,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -92,7 +70,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -101,7 +79,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -110,7 +88,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -119,7 +97,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -128,7 +106,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -137,7 +115,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -146,7 +124,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -155,7 +133,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -164,7 +142,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -173,7 +151,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -182,7 +160,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -191,7 +169,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -200,7 +178,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos/editorial") @@ -381,7 +359,5 @@ void searchVideosEditorialWithCustomHeader() throws Exception { } @AfterAll - public static void stopProxy() { - mockServer.stop(); - } + public void stopProxy() { stopMockServers(); } } diff --git a/src/test/java/SearchVideosTest.java b/src/test/java/SearchVideosTest.java index d791011..b2182ab 100644 --- a/src/test/java/SearchVideosTest.java +++ b/src/test/java/SearchVideosTest.java @@ -1,53 +1,33 @@ - import com.gettyimages.api.ApiClient; import com.gettyimages.api.Filters.*; -import com.gettyimages.api.Search.SearchImages; import com.gettyimages.api.Search.SearchVideos; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockserver.client.server.MockServerClient; -import org.mockserver.integration.ClientAndServer; +import org.junit.jupiter.api.TestInstance; import org.mockserver.model.Parameter; -import java.lang.reflect.Field; import java.util.Arrays; import java.util.EnumSet; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; -public class SearchVideosTest { - private static ClientAndServer mockServer; - +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class SearchVideosTest extends TestBase { @BeforeAll - public static void startProxy() throws Exception { - Field field = ApiClient.class.getDeclaredField("baseUrl"); - field.setAccessible(true); - field.set(null, "http://127.0.0.1:1080/"); - mockServer = startClientAndServer(1080); - MockServerClient client = new MockServerClient("127.0.0.1", 1080); + public void startProxy() throws Exception { + startMockServersAndSetupAuth(); - client - .when(request() - .withMethod("POST") - .withPath("/oauth2/token") - ) - .respond(response() - .withStatusCode(200) - .withBody("{ access_token: 'client_credentials_access_token', token_type: 'Bearer', expires_in: '1800' }") - ); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") .withHeader("Accept-Language", "de") ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -56,7 +36,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -65,7 +45,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -74,7 +54,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -83,7 +63,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -92,7 +72,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -101,7 +81,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -110,7 +90,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -119,7 +99,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -128,7 +108,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -137,7 +117,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -146,7 +126,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -155,7 +135,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -164,7 +144,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -173,7 +153,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -182,7 +162,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -191,7 +171,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -200,7 +180,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/search/videos") @@ -381,7 +361,5 @@ void searchVideosWithCustomHeader() throws Exception { } @AfterAll - public static void stopProxy() { - mockServer.stop(); - } + public void stopProxy() { stopMockServers(); } } diff --git a/src/test/java/TestBase.java b/src/test/java/TestBase.java new file mode 100644 index 0000000..a4480d8 --- /dev/null +++ b/src/test/java/TestBase.java @@ -0,0 +1,49 @@ +import com.gettyimages.api.ApiClient; +import org.mockserver.client.server.MockServerClient; +import org.mockserver.integration.ClientAndServer; + +import java.lang.reflect.Field; + +import static org.mockserver.integration.ClientAndServer.startClientAndServer; +import static org.mockserver.model.HttpRequest.request; +import static org.mockserver.model.HttpResponse.response; + +public class TestBase { + private ClientAndServer apiServerMock; + protected MockServerClient apiClientMock; + private ClientAndServer authServerMock; + private MockServerClient authClientMock; + + protected void startMockServersAndSetupAuth() throws Exception { + startMockServers(); + + authClientMock + .when(request() + .withMethod("POST") + .withPath("/oauth2/token") + ) + .respond(response() + .withStatusCode(200) + .withBody("{ access_token: 'client_credentials_access_token', token_type: 'Bearer', expires_in: '1800' }") + ); + } + + protected void startMockServers() throws Exception { + final Field apiBaseUrl = ApiClient.class.getDeclaredField("apiBaseUrl"); + apiBaseUrl.setAccessible(true); + apiBaseUrl.set(null, "http://127.0.0.1:1080/"); + + final Field authenticationBaseUrl = ApiClient.class.getDeclaredField("authenticationBaseUrl"); + authenticationBaseUrl.setAccessible(true); + authenticationBaseUrl.set(null, "http://127.0.0.1:1081/"); + apiServerMock = startClientAndServer(1080); + authServerMock = startClientAndServer(1081); + apiClientMock = new MockServerClient("127.0.0.1", 1080); + authClientMock = new MockServerClient("127.0.0.1", 1081); + } + + protected void stopMockServers() { + apiServerMock.stop(); + authServerMock.stop(); + } +} diff --git a/src/test/java/VideosTest.java b/src/test/java/VideosTest.java index b6973fc..097496b 100644 --- a/src/test/java/VideosTest.java +++ b/src/test/java/VideosTest.java @@ -1,51 +1,30 @@ - - import com.gettyimages.api.ApiClient; import com.gettyimages.api.Videos.Videos; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockserver.client.server.MockServerClient; -import org.mockserver.integration.ClientAndServer; +import org.junit.jupiter.api.TestInstance; import org.mockserver.model.Parameter; -import java.lang.reflect.Field; import java.util.Arrays; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; -public class VideosTest { - private static ClientAndServer mockServer; - +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class VideosTest extends TestBase { @BeforeAll - public static void startProxy() throws Exception { - Field field = ApiClient.class.getDeclaredField("baseUrl"); - field.setAccessible(true); - field.set(null, "http://127.0.0.1:1080/"); - mockServer = startClientAndServer(1080); - MockServerClient client = new MockServerClient("127.0.0.1", 1080); - - client - .when(request() - .withMethod("POST") - .withPath("/oauth2/token") - ) - .respond(response() - .withStatusCode(200) - .withBody("{ access_token: 'client_credentials_access_token', token_type: 'Bearer', expires_in: '1800' }") - ); - client.when( + public void startProxy() throws Exception { + startMockServersAndSetupAuth(); + apiClientMock.when( request() .withMethod("GET") .withPath("/videos/12345") .withHeader("Accept-Language", "de") ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/videos/12345") @@ -54,7 +33,7 @@ public static void startProxy() throws Exception { ) ) .respond(response().withStatusCode(200).withBody("success")); - client.when( + apiClientMock.when( request() .withMethod("GET") .withPath("/videos") @@ -95,8 +74,6 @@ void videosWithIds() throws Exception { @AfterAll - public static void stopProxy() { - mockServer.stop(); - } + public void stopProxy() { stopMockServers(); } } From fcbf8f9be9ccc91dca25daa7ce75cc12defa7ba0 Mon Sep 17 00:00:00 2001 From: Chris Simmons Date: Mon, 5 Jun 2023 13:38:21 -0700 Subject: [PATCH 2/6] Run "test" target in Maven --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 32e7c2a..4a616b8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,5 +13,5 @@ jobs: with: java-version: '8' distribution: 'adopt' - - name: Build with Maven - run: mvn --batch-mode --update-snapshots verify + - name: Verify/Build/Test with Maven + run: mvn --batch-mode --update-snapshots test From af47d71014e2a0a104df30c5b1535043901dc315 Mon Sep 17 00:00:00 2001 From: Chris Simmons Date: Mon, 5 Jun 2023 13:38:21 -0700 Subject: [PATCH 3/6] Revert "Run "test" target in Maven" This reverts commit fcbf8f9be9ccc91dca25daa7ce75cc12defa7ba0. --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4a616b8..32e7c2a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,5 +13,5 @@ jobs: with: java-version: '8' distribution: 'adopt' - - name: Verify/Build/Test with Maven - run: mvn --batch-mode --update-snapshots test + - name: Build with Maven + run: mvn --batch-mode --update-snapshots verify From f0ab934712c088a6d2308772e6779f6d2bad5b60 Mon Sep 17 00:00:00 2001 From: Chris Simmons Date: Mon, 5 Jun 2023 14:55:12 -0700 Subject: [PATCH 4/6] Use GetApiClientWithClientCredentials in README We want to discourage the use of the resource owner grant --- README.md | 156 ++++++++++++++++++++++++++---------------------------- 1 file changed, 74 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index 3d4b7c4..00bf1f6 100644 --- a/README.md +++ b/README.md @@ -46,102 +46,94 @@ $ mvn install ### Search creative images with phrase, age of people, and page ```java - String apiKey = "API Key"; - String apiSecret = "API Secret"; - String userName = "Username"; - String userPassword = "Password"; - - ApiClient client = ApiClient.GetApiClientWithResourceOwnerCredentials(apiKey, apiSecret, userName, userPassword); - - try { - SearchImagesCreative search = client.searchimagescreative() - .withPhrase("cat") - .withAgeOfPeople(EnumSet.of(AgeOfPeople.CHILD, AgeOfPeople.BABY,AgeOfPeople.ADULT)) - .withPage(3); - String result = search.executeAsync(); - System.out.print(result); - - } catch (SdkException e) { - System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage()); - System.exit(-1); - } +String apiKey = "API Key"; +String apiSecret = "API Secret"; + +ApiClient client = ApiClient.GetApiClientWithClientCredentials(apiKey, apiSecret); + +try { + SearchImagesCreative search = client.searchimagescreative() + .withPhrase("cat") + .withAgeOfPeople(EnumSet.of(AgeOfPeople.CHILD, AgeOfPeople.BABY,AgeOfPeople.ADULT)) + .withPage(3); + String result = search.executeAsync(); + System.out.print(result); + +} catch (SdkException e) { + System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage()); + System.exit(-1); +} ``` ### Search editorial videos with phrase, fields, format available, and exclude nudity ```java - String apiKey = "API Key"; - String apiSecret = "API Secret"; - String userName = "Username"; - String userPassword = "Password"; - - ApiClient client = ApiClient.GetApiClientWithResourceOwnerCredentials(apiKey, apiSecret, userName, userPassword); - - try { - SearchVideosEditorial search = client.searchvideoseditorial() - .withPhrase("cat") - .withResponseFields(Arrays.asList("allowed_use","caption")) - .withFormatAvailable(FormatAvailable.HD) - .withExcludeNudity(true); - String result = search.executeAsync(); - System.out.print(result); - - } catch (SdkException e) { - System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage()); - System.exit(-1); - } +String apiKey = "API Key"; +String apiSecret = "API Secret"; + +ApiClient client = ApiClient.GetApiClientWithClientCredentials(apiKey, apiSecret); + +try { + SearchVideosEditorial search = client.searchvideoseditorial() + .withPhrase("cat") + .withResponseFields(Arrays.asList("allowed_use","caption")) + .withFormatAvailable(FormatAvailable.HD) + .withExcludeNudity(true); + String result = search.executeAsync(); + System.out.print(result); + +} catch (SdkException e) { + System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage()); + System.exit(-1); +} ``` ### Search creative images with phrase, custom parameter, and customer header ```java - String apiKey = "API Key"; - String apiSecret = "API Secret"; - String userName = "Username"; - String userPassword = "Password"; - - ApiClient client = ApiClient.GetApiClientWithResourceOwnerCredentials(apiKey, apiSecret, userName, userPassword); - - try { - SearchImagesCreative search = client.searchimagescreative() - .withPhrase("cat") - .withCustomParameter("safe_search", "true") - .withCustomHeader("gi-country-code", "CAN"); - String result = search.executeAsync(); - System.out.print(result); - - } catch (SdkException e) { - System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage()); - System.exit(-1); - } +String apiKey = "API Key"; +String apiSecret = "API Secret"; + +ApiClient client = ApiClient.GetApiClientWithClientCredentials(apiKey, apiSecret); + +try { + SearchImagesCreative search = client.searchimagescreative() + .withPhrase("cat") + .withCustomParameter("safe_search", "true") + .withCustomHeader("gi-country-code", "CAN"); + String result = search.executeAsync(); + System.out.print(result); + +} catch (SdkException e) { + System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage()); + System.exit(-1); +} ``` ### Custom Request to search images with phrase, fields, and age of people ```java - String apiKey = "API Key"; - String apiSecret = "API Secret"; - String userName = "Username"; - String userPassword = "Password"; - - ApiClient client = ApiClient.GetApiClientWithResourceOwnerCredentials(apiKey, apiSecret, userName, userPassword); - - Map params = new HashMap(); - params.put("phrase", "cat"); - params.put("fields", Arrays.asList("artist", "id")); - params.put("age_of_people", EnumSet.of(AgeOfPeople.NEWBORN,AgeOfPeople.BABY,AgeOfPeople.CHILD)); - - try { - CustomRequest search = client.customrequest() - .withMethod("GET") - .withRoute("/search/images") - .withQueryParameters(params); - String result = search.executeAsync(); - System.out.print(result); - - } catch (SdkException e) { - System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage()); - System.exit(-1); - } +String apiKey = "API Key"; +String apiSecret = "API Secret"; + +ApiClient client = ApiClient.GetApiClientWithClientCredentials(apiKey, apiSecret); + + Map params = new HashMap(); + params.put("phrase", "cat"); + params.put("fields", Arrays.asList("artist", "id")); + params.put("age_of_people", EnumSet.of(AgeOfPeople.NEWBORN,AgeOfPeople.BABY,AgeOfPeople.CHILD)); + + try { + CustomRequest search = client.customrequest() + .withMethod("GET") + .withRoute("/search/images") + .withQueryParameters(params); + String result = search.executeAsync(); + System.out.print(result); + + } catch (SdkException e) { + System.out.println("Exception occurred while searching for creative images: " + e.getLocalizedMessage()); + System.exit(-1); + } ``` For more examples, see unittests package. From 8119fcd45ddc0cf18926cecdb6869099189c98da Mon Sep 17 00:00:00 2001 From: Chris Simmons Date: Mon, 5 Jun 2023 15:08:55 -0700 Subject: [PATCH 5/6] Reduce diff --- src/test/java/AuthFailureTest.java | 4 +++- src/test/java/CustomRequestTest.java | 4 +++- src/test/java/DownloadImagesTest.java | 4 +++- src/test/java/DownloadVideosTest.java | 4 +++- src/test/java/ImagesTest.java | 4 +++- src/test/java/SearchImagesCreativeTest.java | 4 +++- src/test/java/SearchImagesEditorialTest.java | 4 +++- src/test/java/SearchImagesTest.java | 4 +++- src/test/java/SearchVideosCreativeTest.java | 4 +++- src/test/java/SearchVideosEditorialTest.java | 4 +++- src/test/java/SearchVideosTest.java | 4 +++- src/test/java/VideosTest.java | 4 +++- 12 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/test/java/AuthFailureTest.java b/src/test/java/AuthFailureTest.java index 4b160d6..9649885 100644 --- a/src/test/java/AuthFailureTest.java +++ b/src/test/java/AuthFailureTest.java @@ -45,7 +45,9 @@ void systemErrorOnAuthentication() throws Exception { } @AfterEach - public void stopProxy() { stopMockServers(); } + public void stopProxy() { + stopMockServers(); + } private void createMock(final int statusCode) { final MockServerClient client = new MockServerClient("127.0.0.1", 1081); diff --git a/src/test/java/CustomRequestTest.java b/src/test/java/CustomRequestTest.java index be6e965..1067d0f 100644 --- a/src/test/java/CustomRequestTest.java +++ b/src/test/java/CustomRequestTest.java @@ -211,5 +211,7 @@ void customRequestGetWithHeader() throws Exception { } @AfterAll - public void stopProxy() { stopMockServers(); } + public void stopProxy() { + stopMockServers(); + } } diff --git a/src/test/java/DownloadImagesTest.java b/src/test/java/DownloadImagesTest.java index c77be72..41f37fa 100644 --- a/src/test/java/DownloadImagesTest.java +++ b/src/test/java/DownloadImagesTest.java @@ -117,5 +117,7 @@ void downloadImagesWithProductType() throws Exception { @AfterAll - public void stopProxy() { stopMockServers(); } + public void stopProxy() { + stopMockServers(); + } } diff --git a/src/test/java/DownloadVideosTest.java b/src/test/java/DownloadVideosTest.java index 23a6fd3..e7d96d2 100644 --- a/src/test/java/DownloadVideosTest.java +++ b/src/test/java/DownloadVideosTest.java @@ -77,5 +77,7 @@ void downloadVideosWithSize() throws Exception { @AfterAll - public void stopProxy() { stopMockServers(); } + public void stopProxy() { + stopMockServers(); + } } diff --git a/src/test/java/ImagesTest.java b/src/test/java/ImagesTest.java index 399d62c..c810fe3 100644 --- a/src/test/java/ImagesTest.java +++ b/src/test/java/ImagesTest.java @@ -74,5 +74,7 @@ void imagesWithIds() throws Exception { @AfterAll - public void stopProxy() { stopMockServers(); } + public void stopProxy() { + stopMockServers(); + } } diff --git a/src/test/java/SearchImagesCreativeTest.java b/src/test/java/SearchImagesCreativeTest.java index 758f12f..db61c78 100644 --- a/src/test/java/SearchImagesCreativeTest.java +++ b/src/test/java/SearchImagesCreativeTest.java @@ -468,5 +468,7 @@ void searchImagesCreativeWithCustomHeader() throws Exception { } @AfterAll - public void stopProxy() { stopMockServers(); } + public void stopProxy() { + stopMockServers(); + } } diff --git a/src/test/java/SearchImagesEditorialTest.java b/src/test/java/SearchImagesEditorialTest.java index 4b95d14..d9d3415 100644 --- a/src/test/java/SearchImagesEditorialTest.java +++ b/src/test/java/SearchImagesEditorialTest.java @@ -559,5 +559,7 @@ void searchImagesEditorialWithCustomHeader() throws Exception { } @AfterAll - public void stopProxy() { stopMockServers(); } + public void stopProxy() { + stopMockServers(); + } } \ No newline at end of file diff --git a/src/test/java/SearchImagesTest.java b/src/test/java/SearchImagesTest.java index ea79798..bfc2cc9 100644 --- a/src/test/java/SearchImagesTest.java +++ b/src/test/java/SearchImagesTest.java @@ -504,5 +504,7 @@ void searchImagesWithCustomHeader() throws Exception { } @AfterAll - public void stopProxy() { stopMockServers(); } + public void stopProxy() { + stopMockServers(); + } } diff --git a/src/test/java/SearchVideosCreativeTest.java b/src/test/java/SearchVideosCreativeTest.java index 4e1692d..2b8ffdc 100644 --- a/src/test/java/SearchVideosCreativeTest.java +++ b/src/test/java/SearchVideosCreativeTest.java @@ -323,5 +323,7 @@ void searchVideosCreativeWithCustomHeader() throws Exception { } @AfterAll - public void stopProxy() { stopMockServers(); } + public void stopProxy() { + stopMockServers(); + } } diff --git a/src/test/java/SearchVideosEditorialTest.java b/src/test/java/SearchVideosEditorialTest.java index 242f3a2..7178778 100644 --- a/src/test/java/SearchVideosEditorialTest.java +++ b/src/test/java/SearchVideosEditorialTest.java @@ -359,5 +359,7 @@ void searchVideosEditorialWithCustomHeader() throws Exception { } @AfterAll - public void stopProxy() { stopMockServers(); } + public void stopProxy() { + stopMockServers(); + } } diff --git a/src/test/java/SearchVideosTest.java b/src/test/java/SearchVideosTest.java index b2182ab..931bc6f 100644 --- a/src/test/java/SearchVideosTest.java +++ b/src/test/java/SearchVideosTest.java @@ -361,5 +361,7 @@ void searchVideosWithCustomHeader() throws Exception { } @AfterAll - public void stopProxy() { stopMockServers(); } + public void stopProxy() { + stopMockServers(); + } } diff --git a/src/test/java/VideosTest.java b/src/test/java/VideosTest.java index 097496b..afa7aa6 100644 --- a/src/test/java/VideosTest.java +++ b/src/test/java/VideosTest.java @@ -74,6 +74,8 @@ void videosWithIds() throws Exception { @AfterAll - public void stopProxy() { stopMockServers(); } + public void stopProxy() { + stopMockServers(); + } } From 47db92836b279c1523737e86d684646b1495e12d Mon Sep 17 00:00:00 2001 From: Chris Simmons Date: Mon, 5 Jun 2023 15:14:34 -0700 Subject: [PATCH 6/6] Refactor mocks --- src/test/java/AuthFailureTest.java | 12 +++++------- src/test/java/TestBase.java | 4 ++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/test/java/AuthFailureTest.java b/src/test/java/AuthFailureTest.java index 9649885..bb75169 100644 --- a/src/test/java/AuthFailureTest.java +++ b/src/test/java/AuthFailureTest.java @@ -50,12 +50,10 @@ public void stopProxy() { } private void createMock(final int statusCode) { - final MockServerClient client = new MockServerClient("127.0.0.1", 1081); - - client.when(request().withMethod("POST").withPath("/oauth2/token")) - .respond(response().withStatusCode(statusCode)); - client.when(request().withMethod("GET").withPath("/search/images") - .withQueryStringParameters(new Parameter("phrase", "cat"))) - .respond(response().withStatusCode(200).withBody("success")); + authClientMock.when(request().withMethod("POST").withPath("/oauth2/token")) + .respond(response().withStatusCode(statusCode)); + apiClientMock.when(request().withMethod("GET").withPath("/search/images") + .withQueryStringParameters(new Parameter("phrase", "cat"))) + .respond(response().withStatusCode(200).withBody("success")); } } diff --git a/src/test/java/TestBase.java b/src/test/java/TestBase.java index a4480d8..cbefb35 100644 --- a/src/test/java/TestBase.java +++ b/src/test/java/TestBase.java @@ -11,8 +11,8 @@ public class TestBase { private ClientAndServer apiServerMock; protected MockServerClient apiClientMock; - private ClientAndServer authServerMock; - private MockServerClient authClientMock; + protected ClientAndServer authServerMock; + protected MockServerClient authClientMock; protected void startMockServersAndSetupAuth() throws Exception { startMockServers();