Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -49,34 +49,39 @@ public class OpenAIMockConversationHistoryTest {
@Test
public void testConversationHistoryAssertion() throws Exception {
HttpClient client = HttpClient.newHttpClient();
try {
// 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?\"},"
+
"{\"role\": \"assistant\", \"content\": \"Previous response\"}," +
"{\"role\": \"user\", \"content\": \"What's his preferred vehicle type?\"}" +
"]}";

// 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?\"},"
+
"{\"role\": \"assistant\", \"content\": \"Previous response\"}," +
"{\"role\": \"user\", \"content\": \"What's his preferred vehicle type?\"}" +
"]}";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(openAIMock.getBaseUrl() + "/v1/chat/completions"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build();

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(openAIMock.getBaseUrl() + "/v1/chat/completions"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String responseBody = response.body();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String responseBody = response.body();
ObjectMapper objectMapper = new ObjectMapper();
JsonNode responseJson = objectMapper.readTree(responseBody);

ObjectMapper objectMapper = new ObjectMapper();
JsonNode responseJson = objectMapper.readTree(responseBody);
JsonNode choice = responseJson.path("choices").get(0);
JsonNode message = choice.path("message");

JsonNode choice = responseJson.path("choices").get(0);
JsonNode message = choice.path("message");
assertEquals("assistant", message.path("role").asText());
assertEquals("SUV", message.path("content").asText());

assertEquals("assistant", message.path("role").asText());
assertEquals("SUV", message.path("content").asText());

// Verify assertion was executed
assertTrue(secondAssertionExecuted.get(), "Second assertion should have been executed");
// Verify assertion was executed
assertTrue(secondAssertionExecuted.get(), "Second assertion should have been executed");
} finally {
if (client instanceof AutoCloseable ac) {
ac.close();
}
}
}
}
Loading