-
Notifications
You must be signed in to change notification settings - Fork 104
fix: issue 226 #227
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
Merged
Merged
fix: issue 226 #227
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
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
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
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,94 @@ | ||
| package io.a2a.spec; | ||
|
|
||
| import com.fasterxml.jackson.databind.DeserializationFeature; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.time.OffsetDateTime; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| public class TaskStatusTest { | ||
|
|
||
| private static final ObjectMapper OBJECT_MAPPER; | ||
|
|
||
| private static final String REPLACE_TIMESTAMP_PATTERN = ".*\"timestamp\":\"([^\"]+)\",?.*"; | ||
|
|
||
| static { | ||
| OBJECT_MAPPER = new ObjectMapper(); | ||
| OBJECT_MAPPER.registerModule(new JavaTimeModule()); | ||
| OBJECT_MAPPER.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTaskStatusWithSetTimestamp() { | ||
| TaskState state = TaskState.WORKING; | ||
| OffsetDateTime offsetDateTime = OffsetDateTime.parse("2023-10-01T12:00:00Z"); | ||
| TaskStatus status = new TaskStatus(state, offsetDateTime); | ||
|
|
||
| assertNotNull(status.timestamp()); | ||
| assertEquals(offsetDateTime, status.timestamp()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTaskStatusWithProvidedTimestamp() { | ||
| OffsetDateTime providedTimestamp = OffsetDateTime.parse("2024-01-01T00:00:00Z"); | ||
| TaskState state = TaskState.COMPLETED; | ||
| TaskStatus status = new TaskStatus(state, providedTimestamp); | ||
|
|
||
| assertEquals(providedTimestamp, status.timestamp()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTaskStatusSerializationUsesISO8601Format() throws Exception { | ||
| OffsetDateTime expectedTimestamp = OffsetDateTime.parse("2023-10-01T12:00:00.234-05:00"); | ||
| TaskState state = TaskState.WORKING; | ||
| TaskStatus status = new TaskStatus(state, expectedTimestamp); | ||
|
|
||
| String json = OBJECT_MAPPER.writeValueAsString(status); | ||
|
|
||
| String expectedJson = "{\"state\":\"working\",\"timestamp\":\"2023-10-01T12:00:00.234-05:00\"}"; | ||
| assertEquals(expectedJson, json); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTaskStatusDeserializationWithValidISO8601Format() throws Exception { | ||
| String validJson = "{" | ||
| + "\"state\": \"auth-required\"," | ||
| + "\"timestamp\": \"2023-10-01T12:00:00.10+03:00\"" | ||
| + "}"; | ||
|
|
||
| TaskStatus result = OBJECT_MAPPER.readValue(validJson, TaskStatus.class); | ||
| assertEquals(TaskState.AUTH_REQUIRED, result.state()); | ||
| assertNotNull(result.timestamp()); | ||
| assertEquals(OffsetDateTime.parse("2023-10-01T12:00:00.100+03:00"), result.timestamp()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTaskStatusDeserializationWithInvalidISO8601FormatFails() { | ||
| String invalidJson = "{" | ||
| + "\"state\": \"completed\"," | ||
| + "\"timestamp\": \"2023/10/01 12:00:00\"" | ||
| + "}"; | ||
|
|
||
| assertThrows( | ||
| com.fasterxml.jackson.databind.exc.InvalidFormatException.class, | ||
| () -> OBJECT_MAPPER.readValue(invalidJson, TaskStatus.class) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTaskStatusJsonTimestampMatchesISO8601Regex() throws Exception { | ||
| TaskState state = TaskState.WORKING; | ||
| OffsetDateTime expectedTimestamp = OffsetDateTime.parse("2023-10-01T12:00:00.234Z"); | ||
| TaskStatus status = new TaskStatus(state, expectedTimestamp); | ||
|
|
||
| String json = OBJECT_MAPPER.writeValueAsString(status); | ||
|
|
||
| String timestampValue = json.replaceAll(REPLACE_TIMESTAMP_PATTERN, "$1"); | ||
| assertEquals(expectedTimestamp, OffsetDateTime.parse(timestampValue)); | ||
| } | ||
| } |
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.
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.
Can taskStatus.getState() return null? I don't seem to see that here:
https://github.com/a2aproject/a2a-java/blob/main/spec-grpc/src/main/java/io/a2a/grpc/TaskStatus.java#L75
Let me know if I'm missing something.
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.
@ehsavoie Just bumping this question again in case you missed it.