Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
public class GsonJsonProvider implements JsonProvider {

private static final Gson gson = new Gson();
private static final Type MAP_TYPE = new TypeToken<Map<String, Object>>() {}.getType();

@Override
public Map<String, Object> parse(String json) throws OAuth2ClientJsonException {
try {
Type type = new TypeToken<Map<String, Object>>() {}.getType();
return gson.fromJson(json, type);
return gson.fromJson(json, MAP_TYPE);
} catch (Exception e) {
throw new OAuth2ClientJsonException("Failed to read JSON", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
public class JacksonJsonProvider implements JsonProvider {

private static final ObjectMapper mapper = new ObjectMapper();
private static final TypeReference<Map<String, Object>> MAP_TYPE_REFERENCE = new TypeReference<>() {};

@Override
public Map<String, Object> parse(String json) throws OAuth2ClientJsonException {
try {
var typeReference = new TypeReference<Map<String, Object>>() {};
return mapper.readValue(json, typeReference);
return mapper.readValue(json, MAP_TYPE_REFERENCE);
} catch (Exception e) {
throw new OAuth2ClientJsonException("Failed to read JSON", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -68,63 +70,27 @@ protected static Stream<Arguments> 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<TestConfig> configSupplier) {
Expand All @@ -135,4 +101,14 @@ protected static String readResourceId(String resource) throws OAuth2ClientJsonE
Map<String, Object> jsonMap = JsonProvider.getInstance().parse(resource);
return (String) jsonMap.get("id");
}

private static <T> T runOrWrap(ThrowingSupplier<T> action) {
try {
return action.get();
} catch (TestAbortedException | IllegalStateException e) {
throw e;
} catch (Throwable e) {
throw new IllegalStateException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down