Skip to content

Commit 59c878c

Browse files
authored
Merge pull request #1 from opensourcevk/main
feat: minor fix
2 parents 694a1aa + 922a68d commit 59c878c

File tree

6 files changed

+38
-56
lines changed

6 files changed

+38
-56
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,3 @@ cd library && mvn spotless:apply
364364
- The File Watchers plugin can format `.java` files on save
365365

366366
- See watcher configuration in [`watcherTasks.xml`](./.idea/watcherTasks.xml)
367-

library/src/main/java/com/mastercard/developer/oauth2/http/UserAgent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static String get() {
2525
String javaVer = System.getProperty("java.version", "unknown");
2626
String osName = System.getProperty("os.name", "unknown");
2727
String osVer = System.getProperty("os.version", "").trim();
28-
var runtime = "Java/" + javaVer;
28+
String runtime = "Java/" + javaVer;
2929
String osPart = osName + (osVer.isEmpty() ? "" : " " + osVer);
3030
return String.format("%s/%s (%s; %s)", PRODUCT, VERSION, runtime, osPart);
3131
}

library/src/main/java/com/mastercard/developer/oauth2/internal/json/GsonJsonProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
public class GsonJsonProvider implements JsonProvider {
1111

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

1415
@Override
1516
public Map<String, Object> parse(String json) throws OAuth2ClientJsonException {
1617
try {
17-
Type type = new TypeToken<Map<String, Object>>() {}.getType();
18-
return gson.fromJson(json, type);
18+
return gson.fromJson(json, MAP_TYPE);
1919
} catch (Exception e) {
2020
throw new OAuth2ClientJsonException("Failed to read JSON", e);
2121
}

library/src/main/java/com/mastercard/developer/oauth2/internal/json/JacksonJsonProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
public class JacksonJsonProvider implements JsonProvider {
1010

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

1314
@Override
1415
public Map<String, Object> parse(String json) throws OAuth2ClientJsonException {
1516
try {
16-
var typeReference = new TypeReference<Map<String, Object>>() {};
17-
return mapper.readValue(json, typeReference);
17+
return mapper.readValue(json, MAP_TYPE_REFERENCE);
1818
} catch (Exception e) {
1919
throw new OAuth2ClientJsonException("Failed to read JSON", e);
2020
}

library/src/test/java/com/mastercard/developer/oauth2/test/fixtures/BaseClientTest.java

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import org.junit.jupiter.api.BeforeEach;
1212
import org.junit.jupiter.api.Named;
1313
import org.junit.jupiter.api.extension.RegisterExtension;
14+
import org.junit.jupiter.api.function.ThrowingSupplier;
1415
import org.junit.jupiter.params.provider.Arguments;
16+
import org.opentest4j.TestAbortedException;
1517

1618
public abstract class BaseClientTest extends BaseTest {
1719

@@ -68,63 +70,27 @@ protected static Stream<Arguments> serverAndKeyProvider() {
6870
}
6971

7072
protected static TestConfig getMastercardConfig() {
71-
try {
72-
return TestConfig.getMastercardApiConfig();
73-
} catch (IllegalStateException e) {
74-
throw e;
75-
} catch (Exception e) {
76-
throw new IllegalStateException(e);
77-
}
73+
return runOrWrap(TestConfig::getMastercardApiConfig);
7874
}
7975

8076
private static TestConfig getMastercardConfigWithEcKey() {
81-
try {
82-
return TestConfig.getMastercardApiConfig(StaticKeys.EC_KEY_PAIR);
83-
} catch (IllegalStateException e) {
84-
throw e;
85-
} catch (Exception e) {
86-
throw new IllegalStateException(e);
87-
}
77+
return runOrWrap(() -> TestConfig.getMastercardApiConfig(StaticKeys.EC_KEY_PAIR));
8878
}
8979

9080
private static TestConfig getMastercardConfigWithRsaKey() {
91-
try {
92-
return TestConfig.getMastercardApiConfig(StaticKeys.RSA_KEY_PAIR);
93-
} catch (IllegalStateException e) {
94-
throw e;
95-
} catch (Exception e) {
96-
throw new IllegalStateException(e);
97-
}
81+
return runOrWrap(() -> TestConfig.getMastercardApiConfig(StaticKeys.RSA_KEY_PAIR));
9882
}
9983

10084
protected static TestConfig getFakeConfig() {
101-
try {
102-
return TestConfig.getFakeApiConfig(authorizationServer, resourceServer);
103-
} catch (IllegalStateException e) {
104-
throw e;
105-
} catch (Exception e) {
106-
throw new IllegalStateException(e);
107-
}
85+
return runOrWrap(() -> TestConfig.getFakeApiConfig(authorizationServer, resourceServer));
10886
}
10987

11088
private static TestConfig getFakeConfigWithEcKey() {
111-
try {
112-
return TestConfig.getFakeApiConfig(authorizationServer, resourceServer, StaticKeys.EC_KEY_PAIR);
113-
} catch (IllegalStateException e) {
114-
throw e;
115-
} catch (Exception e) {
116-
throw new IllegalStateException(e);
117-
}
89+
return runOrWrap(() -> TestConfig.getFakeApiConfig(authorizationServer, resourceServer, StaticKeys.EC_KEY_PAIR));
11890
}
11991

12092
private static TestConfig getFakeConfigWithRsaKey() {
121-
try {
122-
return TestConfig.getFakeApiConfig(authorizationServer, resourceServer, StaticKeys.RSA_KEY_PAIR);
123-
} catch (IllegalStateException e) {
124-
throw e;
125-
} catch (Exception e) {
126-
throw new IllegalStateException(e);
127-
}
93+
return runOrWrap(() -> TestConfig.getFakeApiConfig(authorizationServer, resourceServer, StaticKeys.RSA_KEY_PAIR));
12894
}
12995

13096
private static Arguments createConfigArgument(String name, Supplier<TestConfig> configSupplier) {
@@ -135,4 +101,14 @@ protected static String readResourceId(String resource) throws OAuth2ClientJsonE
135101
Map<String, Object> jsonMap = JsonProvider.getInstance().parse(resource);
136102
return (String) jsonMap.get("id");
137103
}
104+
105+
private static <T> T runOrWrap(ThrowingSupplier<T> action) {
106+
try {
107+
return action.get();
108+
} catch (TestAbortedException | IllegalStateException e) {
109+
throw e;
110+
} catch (Throwable e) {
111+
throw new IllegalStateException(e);
112+
}
113+
}
138114
}

library/src/test/java/com/mastercard/developer/oauth2/test/fixtures/TestConfig.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.mastercard.developer.oauth2.test.fixtures;
22

3+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
4+
35
import com.mastercard.developer.oauth2.config.OAuth2Config;
46
import com.mastercard.developer.oauth2.config.SecurityProfile;
57
import com.mastercard.developer.oauth2.core.dpop.StaticDPoPKeyProvider;
@@ -51,14 +53,19 @@ public static TestConfig getMastercardApiConfig(KeyPair dpopKeyPair) throws Exce
5153
String readScopes = getEnv("READ_SCOPES");
5254
String writeScopes = getEnv("WRITE_SCOPES");
5355

54-
validateEnvVariable("PRIVATE_KEY", privateKeyContent);
55-
validateEnvVariable("CLIENT_ID", clientId);
56-
validateEnvVariable("KID", kid);
57-
validateEnvVariable("TOKEN_ENDPOINT", tokenEndpoint);
58-
validateEnvVariable("ISSUER", issuer);
59-
validateEnvVariable("API_BASE_URL", apiBaseUrlEnv);
60-
validateEnvVariable("READ_SCOPES", readScopes);
61-
validateEnvVariable("WRITE_SCOPES", readScopes);
56+
try {
57+
validateEnvVariable("PRIVATE_KEY", privateKeyContent);
58+
validateEnvVariable("CLIENT_ID", clientId);
59+
validateEnvVariable("KID", kid);
60+
validateEnvVariable("TOKEN_ENDPOINT", tokenEndpoint);
61+
validateEnvVariable("ISSUER", issuer);
62+
validateEnvVariable("API_BASE_URL", apiBaseUrlEnv);
63+
validateEnvVariable("READ_SCOPES", readScopes);
64+
validateEnvVariable("WRITE_SCOPES", readScopes);
65+
} catch (Exception e) {
66+
// Disable the calling test when environment variables are missing
67+
assumeTrue(false, "Test disabled: %s".formatted(e.getMessage()));
68+
}
6269

6370
OAuth2Config oauth2Config = OAuth2Config.builder()
6471
.securityProfile(SecurityProfile.FAPI2SP_PRIVATE_KEY_DPOP)

0 commit comments

Comments
 (0)