diff --git a/README.md b/README.md index d26c876..e1db088 100644 --- a/README.md +++ b/README.md @@ -364,4 +364,3 @@ cd library && mvn spotless:apply - The File Watchers plugin can format `.java` files on save - See watcher configuration in [`watcherTasks.xml`](./.idea/watcherTasks.xml) - diff --git a/library/src/main/java/com/mastercard/developer/oauth2/http/UserAgent.java b/library/src/main/java/com/mastercard/developer/oauth2/http/UserAgent.java index 885bcef..48da140 100644 --- a/library/src/main/java/com/mastercard/developer/oauth2/http/UserAgent.java +++ b/library/src/main/java/com/mastercard/developer/oauth2/http/UserAgent.java @@ -25,7 +25,7 @@ public static String get() { String javaVer = System.getProperty("java.version", "unknown"); String osName = System.getProperty("os.name", "unknown"); String osVer = System.getProperty("os.version", "").trim(); - var runtime = "Java/" + javaVer; + String runtime = "Java/" + javaVer; String osPart = osName + (osVer.isEmpty() ? "" : " " + osVer); return String.format("%s/%s (%s; %s)", PRODUCT, VERSION, runtime, osPart); } diff --git a/library/src/main/java/com/mastercard/developer/oauth2/internal/json/GsonJsonProvider.java b/library/src/main/java/com/mastercard/developer/oauth2/internal/json/GsonJsonProvider.java index 73f3475..7400757 100644 --- a/library/src/main/java/com/mastercard/developer/oauth2/internal/json/GsonJsonProvider.java +++ b/library/src/main/java/com/mastercard/developer/oauth2/internal/json/GsonJsonProvider.java @@ -10,12 +10,12 @@ public class GsonJsonProvider implements JsonProvider { private static final Gson gson = new Gson(); + private static final Type MAP_TYPE = new TypeToken>() {}.getType(); @Override public Map parse(String json) throws OAuth2ClientJsonException { try { - Type type = new TypeToken>() {}.getType(); - return gson.fromJson(json, type); + return gson.fromJson(json, MAP_TYPE); } catch (Exception e) { throw new OAuth2ClientJsonException("Failed to read JSON", e); } diff --git a/library/src/main/java/com/mastercard/developer/oauth2/internal/json/JacksonJsonProvider.java b/library/src/main/java/com/mastercard/developer/oauth2/internal/json/JacksonJsonProvider.java index 0c1e161..26dc58d 100644 --- a/library/src/main/java/com/mastercard/developer/oauth2/internal/json/JacksonJsonProvider.java +++ b/library/src/main/java/com/mastercard/developer/oauth2/internal/json/JacksonJsonProvider.java @@ -9,12 +9,12 @@ public class JacksonJsonProvider implements JsonProvider { private static final ObjectMapper mapper = new ObjectMapper(); + private static final TypeReference> MAP_TYPE_REFERENCE = new TypeReference<>() {}; @Override public Map parse(String json) throws OAuth2ClientJsonException { try { - var typeReference = new TypeReference>() {}; - return mapper.readValue(json, typeReference); + return mapper.readValue(json, MAP_TYPE_REFERENCE); } catch (Exception e) { throw new OAuth2ClientJsonException("Failed to read JSON", e); } diff --git a/library/src/test/java/com/mastercard/developer/oauth2/test/fixtures/BaseClientTest.java b/library/src/test/java/com/mastercard/developer/oauth2/test/fixtures/BaseClientTest.java index 78fb582..b85e653 100644 --- a/library/src/test/java/com/mastercard/developer/oauth2/test/fixtures/BaseClientTest.java +++ b/library/src/test/java/com/mastercard/developer/oauth2/test/fixtures/BaseClientTest.java @@ -11,7 +11,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Named; import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.function.ThrowingSupplier; import org.junit.jupiter.params.provider.Arguments; +import org.opentest4j.TestAbortedException; public abstract class BaseClientTest extends BaseTest { @@ -68,63 +70,27 @@ protected static Stream serverAndKeyProvider() { } protected static TestConfig getMastercardConfig() { - try { - return TestConfig.getMastercardApiConfig(); - } catch (IllegalStateException e) { - throw e; - } catch (Exception e) { - throw new IllegalStateException(e); - } + return runOrWrap(TestConfig::getMastercardApiConfig); } private static TestConfig getMastercardConfigWithEcKey() { - try { - return TestConfig.getMastercardApiConfig(StaticKeys.EC_KEY_PAIR); - } catch (IllegalStateException e) { - throw e; - } catch (Exception e) { - throw new IllegalStateException(e); - } + return runOrWrap(() -> TestConfig.getMastercardApiConfig(StaticKeys.EC_KEY_PAIR)); } private static TestConfig getMastercardConfigWithRsaKey() { - try { - return TestConfig.getMastercardApiConfig(StaticKeys.RSA_KEY_PAIR); - } catch (IllegalStateException e) { - throw e; - } catch (Exception e) { - throw new IllegalStateException(e); - } + return runOrWrap(() -> TestConfig.getMastercardApiConfig(StaticKeys.RSA_KEY_PAIR)); } protected static TestConfig getFakeConfig() { - try { - return TestConfig.getFakeApiConfig(authorizationServer, resourceServer); - } catch (IllegalStateException e) { - throw e; - } catch (Exception e) { - throw new IllegalStateException(e); - } + return runOrWrap(() -> TestConfig.getFakeApiConfig(authorizationServer, resourceServer)); } private static TestConfig getFakeConfigWithEcKey() { - try { - return TestConfig.getFakeApiConfig(authorizationServer, resourceServer, StaticKeys.EC_KEY_PAIR); - } catch (IllegalStateException e) { - throw e; - } catch (Exception e) { - throw new IllegalStateException(e); - } + return runOrWrap(() -> TestConfig.getFakeApiConfig(authorizationServer, resourceServer, StaticKeys.EC_KEY_PAIR)); } private static TestConfig getFakeConfigWithRsaKey() { - try { - return TestConfig.getFakeApiConfig(authorizationServer, resourceServer, StaticKeys.RSA_KEY_PAIR); - } catch (IllegalStateException e) { - throw e; - } catch (Exception e) { - throw new IllegalStateException(e); - } + return runOrWrap(() -> TestConfig.getFakeApiConfig(authorizationServer, resourceServer, StaticKeys.RSA_KEY_PAIR)); } private static Arguments createConfigArgument(String name, Supplier configSupplier) { @@ -135,4 +101,14 @@ protected static String readResourceId(String resource) throws OAuth2ClientJsonE Map jsonMap = JsonProvider.getInstance().parse(resource); return (String) jsonMap.get("id"); } + + private static T runOrWrap(ThrowingSupplier action) { + try { + return action.get(); + } catch (TestAbortedException | IllegalStateException e) { + throw e; + } catch (Throwable e) { + throw new IllegalStateException(e); + } + } } diff --git a/library/src/test/java/com/mastercard/developer/oauth2/test/fixtures/TestConfig.java b/library/src/test/java/com/mastercard/developer/oauth2/test/fixtures/TestConfig.java index 6fce0eb..7a7c77f 100644 --- a/library/src/test/java/com/mastercard/developer/oauth2/test/fixtures/TestConfig.java +++ b/library/src/test/java/com/mastercard/developer/oauth2/test/fixtures/TestConfig.java @@ -1,5 +1,7 @@ package com.mastercard.developer.oauth2.test.fixtures; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + import com.mastercard.developer.oauth2.config.OAuth2Config; import com.mastercard.developer.oauth2.config.SecurityProfile; import com.mastercard.developer.oauth2.core.dpop.StaticDPoPKeyProvider; @@ -51,14 +53,19 @@ public static TestConfig getMastercardApiConfig(KeyPair dpopKeyPair) throws Exce String readScopes = getEnv("READ_SCOPES"); String writeScopes = getEnv("WRITE_SCOPES"); - validateEnvVariable("PRIVATE_KEY", privateKeyContent); - validateEnvVariable("CLIENT_ID", clientId); - validateEnvVariable("KID", kid); - validateEnvVariable("TOKEN_ENDPOINT", tokenEndpoint); - validateEnvVariable("ISSUER", issuer); - validateEnvVariable("API_BASE_URL", apiBaseUrlEnv); - validateEnvVariable("READ_SCOPES", readScopes); - validateEnvVariable("WRITE_SCOPES", readScopes); + try { + validateEnvVariable("PRIVATE_KEY", privateKeyContent); + validateEnvVariable("CLIENT_ID", clientId); + validateEnvVariable("KID", kid); + validateEnvVariable("TOKEN_ENDPOINT", tokenEndpoint); + validateEnvVariable("ISSUER", issuer); + validateEnvVariable("API_BASE_URL", apiBaseUrlEnv); + validateEnvVariable("READ_SCOPES", readScopes); + validateEnvVariable("WRITE_SCOPES", readScopes); + } catch (Exception e) { + // Disable the calling test when environment variables are missing + assumeTrue(false, "Test disabled: %s".formatted(e.getMessage())); + } OAuth2Config oauth2Config = OAuth2Config.builder() .securityProfile(SecurityProfile.FAPI2SP_PRIVATE_KEY_DPOP)