test: add unit tests for DeviceCodeOAuthFlow#739
test: add unit tests for DeviceCodeOAuthFlow#739neo1027144-creator wants to merge 1 commit intoa2aproject:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the robustness of the DeviceCodeOAuthFlow record by introducing a dedicated suite of unit tests. These tests validate the record's behavior across various construction scenarios, ensuring correct handling of both required and optional fields, proper null validation, and consistent equals and hashCode implementations. This work directly contributes to the stability and reliability of the OAuth flow mechanisms. Highlights
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds a comprehensive set of unit tests for the DeviceCodeOAuthFlow record class. The tests cover construction, null validation for required fields, handling of optional fields, and record equality. The implementation is solid. I've provided a couple of suggestions to make the tests even more robust: one to enhance the assertions in the empty scopes test for better consistency, and another to expand the equality test to include inequality checks, which will provide more complete validation of the record's behavior.
| DeviceCodeOAuthFlow flow = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, null, Map.of()); | ||
|
|
||
| assertNotNull(flow.scopes()); | ||
| assertEquals(0, flow.scopes().size()); |
There was a problem hiding this comment.
This test only asserts properties of the scopes field. For completeness and consistency with other tests in this class (like testConstruction_withAllFields), it's better to assert the state of all fields of the constructed object. This makes the test more robust against future regressions.
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());| 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()); | ||
| } |
There was a problem hiding this comment.
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 equals and hashCode methods, it's beneficial to also include checks for inequality. I suggest renaming the test to reflect its expanded scope and adding assertions for a case where the objects are expected to be unequal.
Note: You will need to add import static org.junit.jupiter.api.Assertions.assertNotEquals; for this suggestion.
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);
}|
@neo1027144-creator can you please fix the gemini code review |
1da4049 to
e751b3f
Compare
|
Sorry for the delayed response — I've just addressed the review feedback. |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces unit tests for the DeviceCodeOAuthFlow class, covering object construction, parameter validation, and basic equality. The feedback suggests enhancing the test suite by adding a test for the immutability of the scopes map to ensure defensive copying and expanding the equality and hash code tests to include more comprehensive edge cases such as type mismatches and null checks.
| assertEquals(TOKEN_URL, flow.tokenUrl()); | ||
| assertNull(flow.refreshUrl()); | ||
| assertEquals(emptyScopes, flow.scopes()); | ||
| } |
There was a problem hiding this comment.
Consider adding a test case to verify that the scopes map is immutable. Records representing configuration should ideally be protected against external mutation of their internal state. Note that the current implementation of DeviceCodeOAuthFlow is missing a defensive copy of the scopes map, which this test would help identify.
}
@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 if it performs a defensive copy
mutableScopes.put("write", "Write access");
assertNotEquals(mutableScopes.size(), flow.scopes().size(), "Record should be immutable and perform a defensive copy of the scopes map");
}| 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.
The equality and hash code tests are currently limited. For a record that serves as a core data carrier in the specification, it is important to verify that equality and hash code consistency hold for all fields, including optional ones, and that inequality is correctly handled for different field values and null/type mismatches.
@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
assertNotEquals(flow1, flow3);
assertNotEquals(flow1, new DeviceCodeOAuthFlow("https://other.com", TOKEN_URL, REFRESH_URL, SCOPES));
assertNotEquals(flow1, null);
assertNotEquals(flow1, "not a flow");
}Test cases: - Construction with all fields populated - Null refreshUrl (optional field) - Empty scopes map with full field assertions - Null validation for required fields (deviceAuthorizationUrl, tokenUrl, scopes) - Record equality, hashCode consistency, and inequality edge cases - Scopes map immutability (defensive copy via Map.copyOf) Also fix: add Map.copyOf() defensive copy in DeviceCodeOAuthFlow compact constructor Fixes a2aproject#607
355301e to
ac9f5b3
Compare
|
/gemini review |
Add unit tests for DeviceCodeOAuthFlow record class.
Test cases:
Follows the same testing patterns as FileWithBytesTest.
Fixes #607