Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -198,6 +199,15 @@ public List<Task> getRelatedTasks() {
return params != null ? params.configuration() : null;
}

/**
* Returns the request metadata.
*
* @return the metadata, or null if not available
*/
public @Nullable Map<String, Object> getMetadata() {
return (params != null && params.metadata() != null) ? Collections.unmodifiableMap(params.metadata()) : null;
}

/**
* Returns the server call context.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.mockito.Mockito.mockStatic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -38,6 +39,7 @@ public void testInitWithoutParams() {
RequestContext context = new RequestContext.Builder().build();

assertNull(context.getMessage());
assertNull(context.getMetadata());
assertNotNull(context.getTaskId()); // Generated UUID
assertNotNull(context.getContextId()); // Generated UUID
assertNull(context.getTask());
Expand Down Expand Up @@ -70,6 +72,25 @@ public void testInitWithParamsNoIds() {
}
}

@Test
public void testInitWithParamsMetadata() {
var message = Message.builder().role(Message.Role.ROLE_USER).parts(List.of(new TextPart(""))).build();
var metadata = new HashMap<String, Object>();
metadata.put("key", "value");

var params = MessageSendParams.builder()
.message(message)
.metadata(metadata)
.build();

RequestContext context = new RequestContext.Builder()
.setParams(params)
.build();

assertEquals(metadata, context.getMetadata());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The getMetadata() method is designed to return an unmodifiable map, which is great for preventing unintended side effects. To ensure this contract is maintained over time, it's a good practice to add an assertion to this test that verifies the returned map is indeed unmodifiable.

        assertEquals(metadata, context.getMetadata());
        assertThrows(UnsupportedOperationException.class, () -> context.getMetadata().put("anotherKey", "anotherValue"));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test assertions for immutable

assertThrows(UnsupportedOperationException.class, () -> context.getMetadata().put("anotherKey", "anotherValue"));
}

@Test
public void testInitWithTaskId() {
String taskId = "task-123";
Expand Down