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 @@ -37,6 +37,7 @@ public record ChatClientRequest(Prompt prompt, Map<String, Object> context) {
Assert.notNull(prompt, "prompt cannot be null");
Assert.notNull(context, "context cannot be null");
Assert.noNullElements(context.keySet(), "context keys cannot be null");
Assert.noNullElements(context.values(), "context values cannot be null");
}

public ChatClientRequest copy() {
Expand Down Expand Up @@ -68,12 +69,15 @@ public Builder prompt(Prompt prompt) {

public Builder context(Map<String, Object> context) {
Assert.notNull(context, "context cannot be null");
Assert.noNullElements(context.keySet(), "context keys cannot be null");
Assert.noNullElements(context.values(), "context values cannot be null");
this.context.putAll(context);
return this;
}

public Builder context(String key, Object value) {
Assert.notNull(key, "key cannot be null");
Assert.notNull(value, "value cannot be null");
this.context.put(key, value);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public record ChatClientResponse(@Nullable ChatResponse chatResponse, Map<String
public ChatClientResponse {
Assert.notNull(context, "context cannot be null");
Assert.noNullElements(context.keySet(), "context keys cannot be null");
Assert.noNullElements(context.values(), "context values cannot be null");
}

public ChatClientResponse copy() {
Expand Down Expand Up @@ -66,12 +67,15 @@ public Builder chatResponse(@Nullable ChatResponse chatResponse) {

public Builder context(Map<String, Object> context) {
Assert.notNull(context, "context cannot be null");
Assert.noNullElements(context.keySet(), "context keys cannot be null");
Assert.noNullElements(context.values(), "context values cannot be null");
this.context.putAll(context);
return this;
}

public Builder context(String key, Object value) {
Assert.notNull(key, "key cannot be null");
Assert.notNull(value, "value cannot be null");
this.context.put(key, value);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ void whenContextHasNullKeysThenThrow() {
.hasMessage("context keys cannot be null");
}

@Test
void whenContextHasNullValuesThenThrow() {
Map<String, Object> context = new HashMap<>();
context.put("key", null);

assertThatThrownBy(() -> new ChatClientRequest(new Prompt(), context))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("context values cannot be null");

assertThatThrownBy(() -> ChatClientRequest.builder().prompt(new Prompt()).context(context).build())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("context values cannot be null");
}

@Test
void whenBuilderContextMapHasNullKeyThenThrow() {
Map<String, Object> context = new HashMap<>();
context.put(null, "value");

assertThatThrownBy(() -> ChatClientRequest.builder().prompt(new Prompt()).context(context).build())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("context keys cannot be null");
}

@Test
void whenCopyThenImmutableContext() {
Map<String, Object> context = new HashMap<>();
Expand All @@ -86,6 +110,13 @@ void whenMutateThenImmutableContext() {
assertThat(copy.context()).isEqualTo(Map.of("key", "newValue"));
}

@Test
void whenBuilderAddsNullValueThenThrow() {
assertThatThrownBy(() -> ChatClientRequest.builder().prompt(new Prompt()).context("key", null).build())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("value cannot be null");
}

@Test
void whenBuilderWithMultipleContextEntriesThenSuccess() {
Prompt prompt = new Prompt("test message");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ void whenContextHasNullKeysThenThrow() {
.hasMessage("context keys cannot be null");
}

@Test
void whenContextHasNullValuesThenThrow() {
Map<String, Object> context = new HashMap<>();
context.put("key", null);

assertThatThrownBy(() -> new ChatClientResponse(null, context)).isInstanceOf(IllegalArgumentException.class)
.hasMessage("context values cannot be null");

assertThatThrownBy(() -> ChatClientResponse.builder().chatResponse(null).context(context).build())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("context values cannot be null");
}

@Test
void whenCopyThenImmutableContext() {
Map<String, Object> context = new HashMap<>();
Expand Down Expand Up @@ -120,16 +133,15 @@ void whenEmptyContextThenCreateSuccessfully() {
}

@Test
void whenContextWithNullValuesThenCreateSuccessfully() {
void whenContextWithNullValuesThenThrow() {
ChatResponse chatResponse = mock(ChatResponse.class);
Map<String, Object> context = new HashMap<>();
context.put("key1", "value1");
context.put("key2", null);

ChatClientResponse response = new ChatClientResponse(chatResponse, context);

assertThat(response.context()).containsEntry("key1", "value1");
assertThat(response.context()).containsEntry("key2", null);
assertThatThrownBy(() -> new ChatClientResponse(chatResponse, context))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("context values cannot be null");
}

@Test
Expand Down Expand Up @@ -166,6 +178,23 @@ void whenBuilderWithoutChatResponseThenCreateWithNull() {
assertThat(response.chatResponse()).isNull();
}

@Test
void whenBuilderContextMapHasNullKeyThenThrow() {
Map<String, Object> context = new HashMap<>();
context.put(null, "value");

assertThatThrownBy(() -> ChatClientResponse.builder().context(context).build())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("context keys cannot be null");
}

@Test
void whenBuilderAddsNullValueThenThrow() {
assertThatThrownBy(() -> ChatClientResponse.builder().context("key", null).build())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("value cannot be null");
}

@Test
void whenComplexObjectsInContextThenPreserveCorrectly() {
ChatResponse chatResponse = mock(ChatResponse.class);
Expand Down