Skip to content

Commit 35ee765

Browse files
committed
Skipped real service tests when required environment variables are not set
1 parent f5b2d0e commit 35ee765

2 files changed

Lines changed: 33 additions & 50 deletions

File tree

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)