Skip to content

Commit dae0ac7

Browse files
committed
Switch to try/catch + assertEquals for JwtVendorTest
Signed-off-by: Ryan Liang <jiallian@amazon.com>
1 parent d918d7a commit dae0ac7

1 file changed

Lines changed: 15 additions & 41 deletions

File tree

src/test/java/org/opensearch/security/authtoken/jwt/JwtVendorTest.java

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ public void testCreateJwkFromSettings() throws Exception {
3939
@Test
4040
public void testCreateJwkFromSettingsWithoutSigningKey() {
4141
Settings settings = Settings.builder().put("jwt", "").build();
42-
assertThrows(
43-
RuntimeException.class,
44-
"java.lang.Exception: Settings for key is missing. Please specify at least the option signing_key with a shared secret.",
45-
() -> {
46-
try {
47-
JwtVendor.createJwkFromSettings(settings);
48-
} catch (Exception e) {
49-
throw new RuntimeException(e);
50-
}
42+
Throwable exception = Assert.assertThrows(RuntimeException.class, () -> {
43+
try {
44+
JwtVendor.createJwkFromSettings(settings);
45+
} catch (Exception e) {
46+
throw new RuntimeException(e);
5147
}
48+
});
49+
Assert.assertEquals(
50+
"java.lang.Exception: Settings for key is missing. Please specify at least the option signing_key with a shared secret.",
51+
exception.getMessage()
5252
);
5353
}
5454

@@ -135,13 +135,14 @@ public void testCreateJwtWithBadExpiry() {
135135
Settings settings = Settings.builder().put("signing_key", "abc123").put("encryption_key", claimsEncryptionKey).build();
136136
JwtVendor jwtVendor = new JwtVendor(settings, Optional.empty());
137137

138-
assertThrows(RuntimeException.class, "java.lang.Exception: The expiration time should be a positive integer", () -> {
138+
Throwable exception = Assert.assertThrows(RuntimeException.class, () -> {
139139
try {
140140
jwtVendor.createJwt(issuer, subject, audience, expirySeconds, roles, List.of());
141141
} catch (Exception e) {
142142
throw new RuntimeException(e);
143143
}
144144
});
145+
Assert.assertEquals("java.lang.Exception: The expiration time should be a positive integer", exception.getMessage());
145146
}
146147

147148
@Test
@@ -154,13 +155,14 @@ public void testCreateJwtWithBadEncryptionKey() {
154155

155156
Settings settings = Settings.builder().put("signing_key", "abc123").build();
156157

157-
assertThrows(RuntimeException.class, "java.lang.RuntimeException: encryption_key cannot be null", () -> {
158+
Throwable exception = Assert.assertThrows(RuntimeException.class, () -> {
158159
try {
159160
new JwtVendor(settings, Optional.empty()).createJwt(issuer, subject, audience, expirySeconds, roles, List.of());
160161
} catch (Exception e) {
161162
throw new RuntimeException(e);
162163
}
163164
});
165+
Assert.assertEquals("java.lang.RuntimeException: encryption_key cannot be null", exception.getMessage());
164166
}
165167

166168
@Test
@@ -174,41 +176,13 @@ public void testCreateJwtWithBadRoles() {
174176
Settings settings = Settings.builder().put("signing_key", "abc123").put("encryption_key", claimsEncryptionKey).build();
175177
JwtVendor jwtVendor = new JwtVendor(settings, Optional.empty());
176178

177-
assertThrows(RuntimeException.class, "java.lang.Exception: Roles cannot be null", () -> {
179+
Throwable exception = Assert.assertThrows(RuntimeException.class, () -> {
178180
try {
179181
jwtVendor.createJwt(issuer, subject, audience, expirySecond, roles, List.of());
180182
} catch (Exception e) {
181183
throw new RuntimeException(e);
182184
}
183185
});
184-
}
185-
186-
public static <T extends Throwable> T assertThrows(Class<T> expectedType, String expectedMessage, Runnable executable) {
187-
try {
188-
executable.run();
189-
} catch (Throwable actualException) {
190-
if (expectedType.isInstance(actualException)) {
191-
if (actualException.getMessage().equals(expectedMessage)) {
192-
return expectedType.cast(actualException);
193-
} else {
194-
String message = String.format(
195-
"Expected %s with message '%s', but got message '%s' instead",
196-
expectedType.getSimpleName(),
197-
expectedMessage,
198-
actualException.getMessage()
199-
);
200-
throw new AssertionError(message, actualException);
201-
}
202-
} else {
203-
String message = String.format(
204-
"Expected %s to be thrown, but %s was thrown instead",
205-
expectedType.getSimpleName(),
206-
actualException.getClass().getSimpleName()
207-
);
208-
throw new AssertionError(message, actualException);
209-
}
210-
}
211-
String message = String.format("Expected %s to be thrown, but nothing was thrown", expectedType.getSimpleName());
212-
throw new AssertionError(message);
186+
Assert.assertEquals("java.lang.Exception: Roles cannot be null", exception.getMessage());
213187
}
214188
}

0 commit comments

Comments
 (0)