Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -17,6 +17,7 @@
package org.apache.camel.test.infra.openai.mock;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -43,7 +44,9 @@ public AudioTranscriptionRequestHandler(List<AudioTranscriptionExpectation> expe
public String handleRequest(HttpExchange exchange) throws IOException {
try {
// consume the request body
exchange.getRequestBody().readAllBytes();
try (InputStream requestBody = exchange.getRequestBody()) {
requestBody.readAllBytes();
}
LOG.debug("Processing audio transcription request (call #{})", callIndex);

if (expectations.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.test.infra.openai.mock;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -45,7 +46,10 @@ public EmbeddingRequestHandler(List<EmbeddingExpectation> expectations, ObjectMa

public String handleRequest(HttpExchange exchange) throws IOException {
try {
String requestBody = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
String requestBody;
try (InputStream is = exchange.getRequestBody()) {
requestBody = new String(is.readAllBytes(), StandardCharsets.UTF_8);
}
LOG.debug("Processing embedding request: {}", requestBody);

JsonNode rootNode = objectMapper.readTree(requestBody);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ public class OpenAIMockConversationHistoryTest {
.end()
.build();

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testConversationHistoryAssertion() throws Exception {
HttpClient client = HttpClient.newHttpClient();

// Send request with conversation history
String requestBody = "{\"messages\": [" +
"{\"role\": \"user\", \"content\": \"Hi! Can you look up user 123 and tell me about our rental policies?\"},"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class OpenAIMockEmbeddingTest {
.end()
.build();

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testExplicitEmbeddingVector() throws Exception {
HttpClient client = HttpClient.newHttpClient();
Expand Down Expand Up @@ -89,6 +91,8 @@ public void testExplicitEmbeddingVector() throws Exception {
assertTrue(usage.has("total_tokens"));
}

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testAutoGeneratedEmbedding() throws Exception {
HttpClient client = HttpClient.newHttpClient();
Expand Down Expand Up @@ -118,6 +122,8 @@ public void testAutoGeneratedEmbedding() throws Exception {
}
}

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testArrayInputFormat() throws Exception {
HttpClient client = HttpClient.newHttpClient();
Expand All @@ -126,7 +132,8 @@ public void testArrayInputFormat() throws Exception {
.uri(URI.create(openAIMock.getBaseUrl() + "/v1/embeddings"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers
.ofString("{\"input\": [\"What is Apache Camel?\"], \"model\": \"text-embedding-ada-002\"}"))
.ofString(
"{\"input\": [\"What is Apache Camel?\"], \"model\": \"text-embedding-ada-002\"}"))
.build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
Expand All @@ -142,10 +149,11 @@ public void testArrayInputFormat() throws Exception {
assertEquals(0.1, embedding.get(0).asDouble(), 0.001);
}

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testDeterministicOutput() throws Exception {
HttpClient client = HttpClient.newHttpClient();

// First request
HttpRequest request1 = HttpRequest.newBuilder()
.uri(URI.create(openAIMock.getBaseUrl() + "/v1/embeddings"))
Expand Down Expand Up @@ -181,6 +189,8 @@ public void testDeterministicOutput() throws Exception {
}
}

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testBatchEmbeddings() throws Exception {
HttpClient client = HttpClient.newHttpClient();
Expand All @@ -189,7 +199,8 @@ public void testBatchEmbeddings() throws Exception {
.uri(URI.create(openAIMock.getBaseUrl() + "/v1/embeddings"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers
.ofString("{\"input\": [\"First text\", \"Second text\"], \"model\": \"text-embedding-ada-002\"}"))
.ofString(
"{\"input\": [\"First text\", \"Second text\"], \"model\": \"text-embedding-ada-002\"}"))
.build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class OpenAIMockFailuresTest {
@RegisterExtension
public OpenAIMock openAIMock = new OpenAIMock();

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testBadRequest() throws Exception {
HttpClient client = HttpClient.newHttpClient();
Expand All @@ -46,6 +48,8 @@ public void testBadRequest() throws Exception {
Assertions.assertEquals(500, response.statusCode());
}

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testNotFound() throws Exception {
HttpClient client = HttpClient.newHttpClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class OpenAIMockMultipleToolsTest {
.withParam("longitude", "-0.13388057363742217")
.build();

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
void testInvokeToolAndThenInvokeTool() throws Exception {
HttpClient client = HttpClient.newHttpClient();
Expand All @@ -49,7 +51,8 @@ void testInvokeToolAndThenInvokeTool() throws Exception {
.uri(URI.create(openAIMock.getBaseUrl() + "/v1/chat/completions"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers
.ofString("{\"messages\": [{\"role\": \"user\", \"content\": \"What is the weather in london?\"}]}"))
.ofString(
"{\"messages\": [{\"role\": \"user\", \"content\": \"What is the weather in london?\"}]}"))
.build();

HttpResponse<String> response1 = client.send(request1, HttpResponse.BodyHandlers.ofString());
Expand All @@ -64,7 +67,8 @@ void testInvokeToolAndThenInvokeTool() throws Exception {
JsonNode toolCalls1 = message1.path("tool_calls");
Assertions.assertEquals(1, toolCalls1.size());
JsonNode toolCall1 = toolCalls1.get(0);
Assertions.assertEquals("FindsTheLatitudeAndLongitudeOfAGivenCity", toolCall1.path("function").path("name").asText());
Assertions.assertEquals("FindsTheLatitudeAndLongitudeOfAGivenCity",
toolCall1.path("function").path("name").asText());
Assertions.assertEquals("{\"name\":\"London\"}", toolCall1.path("function").path("arguments").asText());
String toolCallId1 = toolCall1.path("id").asText();
Assertions.assertEquals("tool_calls", choice1.path("finish_reason").asText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@ public class OpenAIMockReplyWithAfterToolTest {
.end()
.build();

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testReplyWithAfterTool() throws Exception {
HttpClient client = HttpClient.newHttpClient();

// First request - should trigger tool call
HttpRequest request1 = HttpRequest.newBuilder()
.uri(URI.create(openAIMock.getBaseUrl() + "/v1/chat/completions"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers
.ofString("{\"messages\": [{\"role\": \"user\", \"content\": \"What is the weather in london?\"}]}"))
.ofString(
"{\"messages\": [{\"role\": \"user\", \"content\": \"What is the weather in london?\"}]}"))
.build();

HttpResponse<String> response1 = client.send(request1, HttpResponse.BodyHandlers.ofString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ public class OpenAIMockReplyWithToolContentTest {
.end()
.build();

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testReplyWithToolContent() throws Exception {
HttpClient client = HttpClient.newHttpClient();

// First request - should trigger tool call
HttpRequest request1 = HttpRequest.newBuilder()
.uri(URI.create(openAIMock.getBaseUrl() + "/v1/chat/completions"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers
.ofString("{\"messages\": [{\"role\": \"user\", \"content\": \"Get location coordinates\"}]}"))
.ofString(
"{\"messages\": [{\"role\": \"user\", \"content\": \"Get location coordinates\"}]}"))
.build();

HttpResponse<String> response1 = client.send(request1, HttpResponse.BodyHandlers.ofString());
Expand Down Expand Up @@ -97,6 +99,7 @@ public void testReplyWithToolContent() throws Exception {
// Should contain both the tool content and the custom message
assertTrue(content.contains("{\"latitude\": \"48.8566\", \"longitude\": \"2.3522\"}"));
assertTrue(content.contains("- This is the location data I found."));
assertEquals("{\"latitude\": \"48.8566\", \"longitude\": \"2.3522\"} - This is the location data I found.", content);
assertEquals("{\"latitude\": \"48.8566\", \"longitude\": \"2.3522\"} - This is the location data I found.",
content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ public class OpenAIMockSimpleAssertionTest {
.end()
.build();

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testBothAssertionsExecuted() throws Exception {
HttpClient client = HttpClient.newHttpClient();

// First request
HttpRequest request1 = HttpRequest.newBuilder()
.uri(URI.create(openAIMock.getBaseUrl() + "/v1/chat/completions"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public class OpenAIMockTest {
.replyWith("Request asserted successfully")
.build();

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testToolResponse() throws Exception {
HttpClient client = HttpClient.newHttpClient();
Expand Down Expand Up @@ -90,6 +92,8 @@ public void testToolResponse() throws Exception {
assertEquals("{\"param1\":\"value1\"}", toolCall.path("function").path("arguments").asText());
}

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testChatResponse() throws Exception {
HttpClient client = HttpClient.newHttpClient();
Expand All @@ -115,6 +119,8 @@ public void testChatResponse() throws Exception {
assertEquals(true, message.path("tool_calls").isMissingNode());
}

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testMultipleToolCallsResponse() throws Exception {
HttpClient client = HttpClient.newHttpClient();
Expand Down Expand Up @@ -152,6 +158,8 @@ public void testMultipleToolCallsResponse() throws Exception {
assertEquals("{\"p2\":\"v2\",\"p3\":\"v3\"}", toolCall2.path("function").path("arguments").asText());
}

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testCustomResponse() throws Exception {
HttpClient client = HttpClient.newHttpClient();
Expand All @@ -168,6 +176,8 @@ public void testCustomResponse() throws Exception {
assertEquals("Custom response for: custom response", responseBody);
}

// HttpClient does not implement AutoCloseable before Java 21
@SuppressWarnings("java:S2095")
@Test
public void testToolResponseAndStop() throws Exception {
HttpClient client = HttpClient.newHttpClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.test.infra.openai.mock;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

Expand Down Expand Up @@ -44,7 +45,10 @@ public RequestHandler(List<MockExpectation> expectations, ObjectMapper objectMap

public String handleRequest(HttpExchange exchange) throws IOException {
try {
String requestBody = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
String requestBody;
try (InputStream is = exchange.getRequestBody()) {
requestBody = new String(is.readAllBytes(), StandardCharsets.UTF_8);
}
LOG.debug("Processing request: {}", requestBody);

JsonNode rootNode = objectMapper.readTree(requestBody);
Expand Down