-
Notifications
You must be signed in to change notification settings - Fork 134
test: add unit tests for DeviceCodeOAuthFlow #739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package io.a2a.spec; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Unit tests for {@link DeviceCodeOAuthFlow}. | ||
| * <p> | ||
| * Tests cover construction with valid parameters, null validation of required | ||
| * fields, and handling of the optional {@code refreshUrl} field. | ||
| * | ||
| * @see DeviceCodeOAuthFlow | ||
| */ | ||
| class DeviceCodeOAuthFlowTest { | ||
|
|
||
| private static final String DEVICE_AUTH_URL = "https://auth.example.com/device/code"; | ||
| private static final String TOKEN_URL = "https://auth.example.com/token"; | ||
| private static final String REFRESH_URL = "https://auth.example.com/refresh"; | ||
| private static final Map<String, String> SCOPES = Map.of("read", "Read access", "write", "Write access"); | ||
|
|
||
| @Test | ||
| void testConstruction_withAllFields() { | ||
| DeviceCodeOAuthFlow flow = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, REFRESH_URL, SCOPES); | ||
|
|
||
| assertEquals(DEVICE_AUTH_URL, flow.deviceAuthorizationUrl()); | ||
| assertEquals(TOKEN_URL, flow.tokenUrl()); | ||
| assertEquals(REFRESH_URL, flow.refreshUrl()); | ||
| assertEquals(SCOPES, flow.scopes()); | ||
| } | ||
|
|
||
| @Test | ||
| void testConstruction_withNullRefreshUrl() { | ||
| DeviceCodeOAuthFlow flow = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, null, SCOPES); | ||
|
|
||
| assertEquals(DEVICE_AUTH_URL, flow.deviceAuthorizationUrl()); | ||
| assertEquals(TOKEN_URL, flow.tokenUrl()); | ||
| assertNull(flow.refreshUrl()); | ||
| assertEquals(SCOPES, flow.scopes()); | ||
| } | ||
|
|
||
| @Test | ||
| void testConstruction_withEmptyScopes() { | ||
| DeviceCodeOAuthFlow flow = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, null, Map.of()); | ||
|
|
||
| assertNotNull(flow.scopes()); | ||
| assertEquals(0, flow.scopes().size()); | ||
| } | ||
|
|
||
| @Test | ||
| void testConstruction_nullDeviceAuthorizationUrl_throwsException() { | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> new DeviceCodeOAuthFlow(null, TOKEN_URL, REFRESH_URL, SCOPES)); | ||
| } | ||
|
|
||
| @Test | ||
| void testConstruction_nullTokenUrl_throwsException() { | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, null, REFRESH_URL, SCOPES)); | ||
| } | ||
|
|
||
| @Test | ||
| void testConstruction_nullScopes_throwsException() { | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, REFRESH_URL, null)); | ||
| } | ||
|
|
||
| @Test | ||
| void testEquality_sameValues() { | ||
| DeviceCodeOAuthFlow flow1 = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, REFRESH_URL, SCOPES); | ||
| DeviceCodeOAuthFlow flow2 = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, REFRESH_URL, SCOPES); | ||
|
|
||
| assertEquals(flow1, flow2); | ||
| assertEquals(flow1.hashCode(), flow2.hashCode()); | ||
| } | ||
|
Comment on lines
+74
to
+80
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test for equality is good, but it only covers the case where objects are equal. To make the test suite more robust and fully validate the behavior of the record's Note: You will need to add void testEqualityAndHashCode() {
DeviceCodeOAuthFlow flow1 = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, REFRESH_URL, SCOPES);
DeviceCodeOAuthFlow flow2 = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, REFRESH_URL, SCOPES);
DeviceCodeOAuthFlow flow3WithNullRefresh = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, null, SCOPES);
// Test for equality with same values
assertEquals(flow1, flow2);
assertEquals(flow1.hashCode(), flow2.hashCode());
// Test for inequality with different values
assertNotEquals(flow1, flow3WithNullRefresh);
} |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test only asserts properties of the
scopesfield. For completeness and consistency with other tests in this class (liketestConstruction_withAllFields), it's better to assert the state of all fields of the constructed object. This makes the test more robust against future regressions.