-
Notifications
You must be signed in to change notification settings - Fork 142
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
Open
neo1027144-creator
wants to merge
1
commit into
a2aproject:main
Choose a base branch
from
neo1027144-creator:issue-607
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+108
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
spec/src/test/java/io/a2a/spec/DeviceCodeOAuthFlowTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package io.a2a.spec; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
| 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() { | ||
| Map<String, String> emptyScopes = Map.of(); | ||
| DeviceCodeOAuthFlow flow = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, null, emptyScopes); | ||
|
|
||
| assertEquals(DEVICE_AUTH_URL, flow.deviceAuthorizationUrl()); | ||
| assertEquals(TOKEN_URL, flow.tokenUrl()); | ||
| assertNull(flow.refreshUrl()); | ||
| assertEquals(emptyScopes, flow.scopes()); | ||
| } | ||
|
|
||
| @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 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 flow3 = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, null, SCOPES); | ||
| DeviceCodeOAuthFlow flow4 = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, null, SCOPES); | ||
|
|
||
| // Test for equality and hashCode consistency | ||
| assertEquals(flow1, flow2); | ||
| assertEquals(flow1.hashCode(), flow2.hashCode()); | ||
| assertEquals(flow3, flow4); | ||
| assertEquals(flow3.hashCode(), flow4.hashCode()); | ||
|
|
||
| // Test for inequality with different field values | ||
| assertNotEquals(flow1, flow3); | ||
| assertNotEquals(flow1, new DeviceCodeOAuthFlow("https://other.com", TOKEN_URL, REFRESH_URL, SCOPES)); | ||
| assertNotEquals(flow1, null); | ||
| assertNotEquals(flow1, "not a flow"); | ||
| } | ||
|
|
||
| @Test | ||
| void testScopesImmutability() { | ||
| Map<String, String> mutableScopes = new java.util.HashMap<>(); | ||
| mutableScopes.put("read", "Read access"); | ||
| DeviceCodeOAuthFlow flow = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, REFRESH_URL, mutableScopes); | ||
|
|
||
| // Modifying the original map should not affect the record | ||
| mutableScopes.put("write", "Write access"); | ||
| assertNotEquals(mutableScopes.size(), flow.scopes().size(), | ||
| "Record should be immutable and perform a defensive copy of the scopes map"); | ||
| } | ||
neo1027144-creator marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.