|
| 1 | +package io.a2a.spec; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 7 | + |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +/** |
| 13 | + * Unit tests for {@link DeviceCodeOAuthFlow}. |
| 14 | + * <p> |
| 15 | + * Tests cover construction with valid parameters, null validation of required |
| 16 | + * fields, and handling of the optional {@code refreshUrl} field. |
| 17 | + * |
| 18 | + * @see DeviceCodeOAuthFlow |
| 19 | + */ |
| 20 | +class DeviceCodeOAuthFlowTest { |
| 21 | + |
| 22 | + private static final String DEVICE_AUTH_URL = "https://auth.example.com/device/code"; |
| 23 | + private static final String TOKEN_URL = "https://auth.example.com/token"; |
| 24 | + private static final String REFRESH_URL = "https://auth.example.com/refresh"; |
| 25 | + private static final Map<String, String> SCOPES = Map.of("read", "Read access", "write", "Write access"); |
| 26 | + |
| 27 | + @Test |
| 28 | + void testConstruction_withAllFields() { |
| 29 | + DeviceCodeOAuthFlow flow = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, REFRESH_URL, SCOPES); |
| 30 | + |
| 31 | + assertEquals(DEVICE_AUTH_URL, flow.deviceAuthorizationUrl()); |
| 32 | + assertEquals(TOKEN_URL, flow.tokenUrl()); |
| 33 | + assertEquals(REFRESH_URL, flow.refreshUrl()); |
| 34 | + assertEquals(SCOPES, flow.scopes()); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void testConstruction_withNullRefreshUrl() { |
| 39 | + DeviceCodeOAuthFlow flow = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, null, SCOPES); |
| 40 | + |
| 41 | + assertEquals(DEVICE_AUTH_URL, flow.deviceAuthorizationUrl()); |
| 42 | + assertEquals(TOKEN_URL, flow.tokenUrl()); |
| 43 | + assertNull(flow.refreshUrl()); |
| 44 | + assertEquals(SCOPES, flow.scopes()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void testConstruction_withEmptyScopes() { |
| 49 | + DeviceCodeOAuthFlow flow = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, null, Map.of()); |
| 50 | + |
| 51 | + assertNotNull(flow.scopes()); |
| 52 | + assertEquals(0, flow.scopes().size()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testConstruction_nullDeviceAuthorizationUrl_throwsException() { |
| 57 | + assertThrows(IllegalArgumentException.class, |
| 58 | + () -> new DeviceCodeOAuthFlow(null, TOKEN_URL, REFRESH_URL, SCOPES)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testConstruction_nullTokenUrl_throwsException() { |
| 63 | + assertThrows(IllegalArgumentException.class, |
| 64 | + () -> new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, null, REFRESH_URL, SCOPES)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testConstruction_nullScopes_throwsException() { |
| 69 | + assertThrows(IllegalArgumentException.class, |
| 70 | + () -> new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, REFRESH_URL, null)); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testEquality_sameValues() { |
| 75 | + DeviceCodeOAuthFlow flow1 = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, REFRESH_URL, SCOPES); |
| 76 | + DeviceCodeOAuthFlow flow2 = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, REFRESH_URL, SCOPES); |
| 77 | + |
| 78 | + assertEquals(flow1, flow2); |
| 79 | + assertEquals(flow1.hashCode(), flow2.hashCode()); |
| 80 | + } |
| 81 | +} |
0 commit comments