|
4 | 4 | import static org.junit.jupiter.api.Assertions.assertFalse; |
5 | 5 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
6 | 6 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
7 | 8 | import static org.junit.jupiter.api.Assertions.assertTrue; |
8 | 9 |
|
9 | 10 | import java.util.List; |
|
30 | 31 | import io.a2a.spec.A2AError; |
31 | 32 | import io.a2a.spec.Event; |
32 | 33 | import io.a2a.spec.EventKind; |
| 34 | +import io.a2a.spec.InvalidParamsError; |
33 | 35 | import io.a2a.spec.Message; |
34 | 36 | import io.a2a.spec.MessageSendConfiguration; |
35 | 37 | import io.a2a.spec.MessageSendParams; |
@@ -509,4 +511,55 @@ void testAuthRequired_Resubscription() throws Exception { |
509 | 511 | ((TaskStatusUpdateEvent) completionEvent).status().state(), |
510 | 512 | "Task should be completed"); |
511 | 513 | } |
| 514 | + |
| 515 | + /** |
| 516 | + * Test: Reject SendMessage with mismatching contextId and taskId. |
| 517 | + * When a message references an existing task but provides a different contextId, |
| 518 | + * the request must be rejected with an InvalidParamsError. |
| 519 | + */ |
| 520 | + @Test |
| 521 | + void testRejectMismatchingContextId() throws Exception { |
| 522 | + // Arrange: Create an initial task to get valid identifiers |
| 523 | + CountDownLatch agentCompleted = new CountDownLatch(1); |
| 524 | + |
| 525 | + agentExecutorExecute = (context, emitter) -> { |
| 526 | + emitter.complete(); |
| 527 | + agentCompleted.countDown(); |
| 528 | + }; |
| 529 | + |
| 530 | + Message initialMessage = Message.builder() |
| 531 | + .messageId("msg-1") |
| 532 | + .role(Message.Role.ROLE_USER) |
| 533 | + .contextId("original-context") |
| 534 | + .parts(new TextPart("initial message")) |
| 535 | + .build(); |
| 536 | + |
| 537 | + MessageSendParams initialParams = MessageSendParams.builder() |
| 538 | + .message(initialMessage) |
| 539 | + .configuration(DEFAULT_CONFIG) |
| 540 | + .build(); |
| 541 | + |
| 542 | + EventKind result = requestHandler.onMessageSend(initialParams, NULL_CONTEXT); |
| 543 | + assertInstanceOf(Task.class, result); |
| 544 | + Task task = (Task) result; |
| 545 | + assertTrue(agentCompleted.await(5, TimeUnit.SECONDS)); |
| 546 | + |
| 547 | + // Act & Assert: Send a follow-up message with matching taskId but wrong contextId |
| 548 | + Message mismatchedMessage = Message.builder() |
| 549 | + .messageId("msg-2") |
| 550 | + .role(Message.Role.ROLE_USER) |
| 551 | + .taskId(task.id()) |
| 552 | + .contextId("wrong-context-does-not-exist") |
| 553 | + .parts(new TextPart("follow-up message")) |
| 554 | + .build(); |
| 555 | + |
| 556 | + MessageSendParams mismatchedParams = MessageSendParams.builder() |
| 557 | + .message(mismatchedMessage) |
| 558 | + .configuration(DEFAULT_CONFIG) |
| 559 | + .build(); |
| 560 | + |
| 561 | + InvalidParamsError error = assertThrows(InvalidParamsError.class, |
| 562 | + () -> requestHandler.onMessageSend(mismatchedParams, NULL_CONTEXT)); |
| 563 | + assertTrue(error.getMessage().contains(task.id())); |
| 564 | + } |
512 | 565 | } |
0 commit comments