|
5 | 5 | import static io.a2a.spec.A2AMethods.SEND_STREAMING_MESSAGE_METHOD; |
6 | 6 | import static io.a2a.spec.AgentCard.CURRENT_PROTOCOL_VERSION; |
7 | 7 | import static io.a2a.transport.jsonrpc.context.JSONRPCContextKeys.METHOD_NAME_KEY; |
| 8 | +import static io.a2a.transport.jsonrpc.context.JSONRPCContextKeys.TENANT_KEY; |
8 | 9 | import static java.util.Collections.singletonList; |
9 | 10 | import static org.junit.jupiter.api.Assertions.assertEquals; |
10 | 11 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
@@ -108,6 +109,7 @@ public void setUp() { |
108 | 109 | when(mockRoutingContext.user()).thenReturn(null); |
109 | 110 | when(mockRequest.headers()).thenReturn(mockHeaders); |
110 | 111 | when(mockRoutingContext.body()).thenReturn(mockRequestBody); |
| 112 | + when(mockRoutingContext.normalizedPath()).thenReturn("/"); |
111 | 113 |
|
112 | 114 | // Chain the response methods properly |
113 | 115 | when(mockHttpResponse.setStatusCode(any(Integer.class))).thenReturn(mockHttpResponse); |
@@ -520,6 +522,202 @@ public void testGetExtendedCard_MethodNameSetInContext() { |
520 | 522 | assertEquals(GET_EXTENDED_AGENT_CARD_METHOD, capturedContext.getState().get(METHOD_NAME_KEY)); |
521 | 523 | } |
522 | 524 |
|
| 525 | + @Test |
| 526 | + public void testTenantExtraction_MultiSegmentPath() { |
| 527 | + // Arrange - simulate request to /test/titi |
| 528 | + when(mockRoutingContext.normalizedPath()).thenReturn("/test/titi"); |
| 529 | + String jsonRpcRequest = """ |
| 530 | + { |
| 531 | + "jsonrpc": "2.0", |
| 532 | + "id": "cd4c76de-d54c-436c-8b9f-4c2703648d64", |
| 533 | + "method": "GetTask", |
| 534 | + "params": { |
| 535 | + "name": "tasks/de38c76d-d54c-436c-8b9f-4c2703648d64", |
| 536 | + "historyLength": 10 |
| 537 | + } |
| 538 | + }"""; |
| 539 | + when(mockRequestBody.asString()).thenReturn(jsonRpcRequest); |
| 540 | + |
| 541 | + Task responseTask = Task.builder() |
| 542 | + .id("de38c76d-d54c-436c-8b9f-4c2703648d64") |
| 543 | + .contextId("context-1234") |
| 544 | + .status(new TaskStatus(TaskState.SUBMITTED)) |
| 545 | + .build(); |
| 546 | + GetTaskResponse realResponse = new GetTaskResponse("1", responseTask); |
| 547 | + when(mockJsonRpcHandler.onGetTask(any(GetTaskRequest.class), any(ServerCallContext.class))) |
| 548 | + .thenReturn(realResponse); |
| 549 | + |
| 550 | + ArgumentCaptor<ServerCallContext> contextCaptor = ArgumentCaptor.forClass(ServerCallContext.class); |
| 551 | + |
| 552 | + // Act |
| 553 | + routes.invokeJSONRPCHandler(jsonRpcRequest, mockRoutingContext); |
| 554 | + |
| 555 | + // Assert |
| 556 | + verify(mockJsonRpcHandler).onGetTask(any(GetTaskRequest.class), contextCaptor.capture()); |
| 557 | + ServerCallContext capturedContext = contextCaptor.getValue(); |
| 558 | + assertNotNull(capturedContext); |
| 559 | + assertEquals("test/titi", capturedContext.getState().get(TENANT_KEY)); |
| 560 | + } |
| 561 | + |
| 562 | + @Test |
| 563 | + public void testTenantExtraction_RootPath() { |
| 564 | + // Arrange - simulate request to / |
| 565 | + when(mockRoutingContext.normalizedPath()).thenReturn("/"); |
| 566 | + String jsonRpcRequest = """ |
| 567 | + { |
| 568 | + "jsonrpc": "2.0", |
| 569 | + "id": "cd4c76de-d54c-436c-8b9f-4c2703648d64", |
| 570 | + "method": "GetTask", |
| 571 | + "params": { |
| 572 | + "name": "tasks/de38c76d-d54c-436c-8b9f-4c2703648d64", |
| 573 | + "historyLength": 10 |
| 574 | + } |
| 575 | + }"""; |
| 576 | + when(mockRequestBody.asString()).thenReturn(jsonRpcRequest); |
| 577 | + |
| 578 | + Task responseTask = Task.builder() |
| 579 | + .id("de38c76d-d54c-436c-8b9f-4c2703648d64") |
| 580 | + .contextId("context-1234") |
| 581 | + .status(new TaskStatus(TaskState.SUBMITTED)) |
| 582 | + .build(); |
| 583 | + GetTaskResponse realResponse = new GetTaskResponse("1", responseTask); |
| 584 | + when(mockJsonRpcHandler.onGetTask(any(GetTaskRequest.class), any(ServerCallContext.class))) |
| 585 | + .thenReturn(realResponse); |
| 586 | + |
| 587 | + ArgumentCaptor<ServerCallContext> contextCaptor = ArgumentCaptor.forClass(ServerCallContext.class); |
| 588 | + |
| 589 | + // Act |
| 590 | + routes.invokeJSONRPCHandler(jsonRpcRequest, mockRoutingContext); |
| 591 | + |
| 592 | + // Assert |
| 593 | + verify(mockJsonRpcHandler).onGetTask(any(GetTaskRequest.class), contextCaptor.capture()); |
| 594 | + ServerCallContext capturedContext = contextCaptor.getValue(); |
| 595 | + assertNotNull(capturedContext); |
| 596 | + assertEquals("", capturedContext.getState().get(TENANT_KEY)); |
| 597 | + } |
| 598 | + |
| 599 | + @Test |
| 600 | + public void testTenantExtraction_SingleSegmentPath() { |
| 601 | + // Arrange - simulate request to /tenant1 |
| 602 | + when(mockRoutingContext.normalizedPath()).thenReturn("/tenant1"); |
| 603 | + String jsonRpcRequest = """ |
| 604 | + { |
| 605 | + "jsonrpc": "2.0", |
| 606 | + "id": "cd4c76de-d54c-436c-8b9f-4c2703648d64", |
| 607 | + "method": "GetTask", |
| 608 | + "params": { |
| 609 | + "name": "tasks/de38c76d-d54c-436c-8b9f-4c2703648d64", |
| 610 | + "historyLength": 10 |
| 611 | + } |
| 612 | + }"""; |
| 613 | + when(mockRequestBody.asString()).thenReturn(jsonRpcRequest); |
| 614 | + |
| 615 | + Task responseTask = Task.builder() |
| 616 | + .id("de38c76d-d54c-436c-8b9f-4c2703648d64") |
| 617 | + .contextId("context-1234") |
| 618 | + .status(new TaskStatus(TaskState.SUBMITTED)) |
| 619 | + .build(); |
| 620 | + GetTaskResponse realResponse = new GetTaskResponse("1", responseTask); |
| 621 | + when(mockJsonRpcHandler.onGetTask(any(GetTaskRequest.class), any(ServerCallContext.class))) |
| 622 | + .thenReturn(realResponse); |
| 623 | + |
| 624 | + ArgumentCaptor<ServerCallContext> contextCaptor = ArgumentCaptor.forClass(ServerCallContext.class); |
| 625 | + |
| 626 | + // Act |
| 627 | + routes.invokeJSONRPCHandler(jsonRpcRequest, mockRoutingContext); |
| 628 | + |
| 629 | + // Assert |
| 630 | + verify(mockJsonRpcHandler).onGetTask(any(GetTaskRequest.class), contextCaptor.capture()); |
| 631 | + ServerCallContext capturedContext = contextCaptor.getValue(); |
| 632 | + assertNotNull(capturedContext); |
| 633 | + assertEquals("tenant1", capturedContext.getState().get(TENANT_KEY)); |
| 634 | + } |
| 635 | + |
| 636 | + @Test |
| 637 | + public void testTenantExtraction_ThreeSegmentPath() { |
| 638 | + // Arrange - simulate request to /tenant1/api/v1 |
| 639 | + when(mockRoutingContext.normalizedPath()).thenReturn("/tenant1/api/v1"); |
| 640 | + String jsonRpcRequest = """ |
| 641 | + { |
| 642 | + "jsonrpc": "2.0", |
| 643 | + "id": "cd4c76de-d54c-436c-8b9f-4c2703648d64", |
| 644 | + "method": "GetTask", |
| 645 | + "params": { |
| 646 | + "name": "tasks/de38c76d-d54c-436c-8b9f-4c2703648d64", |
| 647 | + "historyLength": 10 |
| 648 | + } |
| 649 | + }"""; |
| 650 | + when(mockRequestBody.asString()).thenReturn(jsonRpcRequest); |
| 651 | + |
| 652 | + Task responseTask = Task.builder() |
| 653 | + .id("de38c76d-d54c-436c-8b9f-4c2703648d64") |
| 654 | + .contextId("context-1234") |
| 655 | + .status(new TaskStatus(TaskState.SUBMITTED)) |
| 656 | + .build(); |
| 657 | + GetTaskResponse realResponse = new GetTaskResponse("1", responseTask); |
| 658 | + when(mockJsonRpcHandler.onGetTask(any(GetTaskRequest.class), any(ServerCallContext.class))) |
| 659 | + .thenReturn(realResponse); |
| 660 | + |
| 661 | + ArgumentCaptor<ServerCallContext> contextCaptor = ArgumentCaptor.forClass(ServerCallContext.class); |
| 662 | + |
| 663 | + // Act |
| 664 | + routes.invokeJSONRPCHandler(jsonRpcRequest, mockRoutingContext); |
| 665 | + |
| 666 | + // Assert |
| 667 | + verify(mockJsonRpcHandler).onGetTask(any(GetTaskRequest.class), contextCaptor.capture()); |
| 668 | + ServerCallContext capturedContext = contextCaptor.getValue(); |
| 669 | + assertNotNull(capturedContext); |
| 670 | + assertEquals("tenant1/api/v1", capturedContext.getState().get(TENANT_KEY)); |
| 671 | + } |
| 672 | + |
| 673 | + @Test |
| 674 | + public void testTenantExtraction_StreamingRequest() { |
| 675 | + // Arrange - simulate streaming request to /myTenant/api |
| 676 | + when(mockRoutingContext.normalizedPath()).thenReturn("/myTenant/api"); |
| 677 | + String jsonRpcRequest = """ |
| 678 | + { |
| 679 | + "jsonrpc": "2.0", |
| 680 | + "id": "cd4c76de-d54c-436c-8b9f-4c2703648d64", |
| 681 | + "method": "SendStreamingMessage", |
| 682 | + "params": { |
| 683 | + "message": { |
| 684 | + "messageId": "message-1234", |
| 685 | + "contextId": "context-1234", |
| 686 | + "role": "ROLE_USER", |
| 687 | + "parts": [ |
| 688 | + { |
| 689 | + "text": "tell me a joke" |
| 690 | + } |
| 691 | + ], |
| 692 | + "metadata": {} |
| 693 | + }, |
| 694 | + "configuration": { |
| 695 | + "acceptedOutputModes": ["text"], |
| 696 | + "blocking": true |
| 697 | + }, |
| 698 | + "metadata": {} |
| 699 | + } |
| 700 | + }"""; |
| 701 | + when(mockRequestBody.asString()).thenReturn(jsonRpcRequest); |
| 702 | + |
| 703 | + @SuppressWarnings("unchecked") |
| 704 | + Flow.Publisher<SendStreamingMessageResponse> mockPublisher = mock(Flow.Publisher.class); |
| 705 | + when(mockJsonRpcHandler.onMessageSendStream(any(SendStreamingMessageRequest.class), |
| 706 | + any(ServerCallContext.class))).thenReturn(mockPublisher); |
| 707 | + |
| 708 | + ArgumentCaptor<ServerCallContext> contextCaptor = ArgumentCaptor.forClass(ServerCallContext.class); |
| 709 | + |
| 710 | + // Act |
| 711 | + routes.invokeJSONRPCHandler(jsonRpcRequest, mockRoutingContext); |
| 712 | + |
| 713 | + // Assert |
| 714 | + verify(mockJsonRpcHandler).onMessageSendStream(any(SendStreamingMessageRequest.class), |
| 715 | + contextCaptor.capture()); |
| 716 | + ServerCallContext capturedContext = contextCaptor.getValue(); |
| 717 | + assertNotNull(capturedContext); |
| 718 | + assertEquals("myTenant/api", capturedContext.getState().get(TENANT_KEY)); |
| 719 | + } |
| 720 | + |
523 | 721 | /** |
524 | 722 | * Helper method to set a field via reflection for testing purposes. |
525 | 723 | */ |
|
0 commit comments