Skip to content
Draft
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 @@ -963,8 +963,7 @@ static SalesforceHttpClient createHttpClient(
int workerPoolMaxSize) {
SecurityUtils.adaptToIBMCipherNames(sslContextFactory);

// ExecutorService lifecycle is managed by SalesforceHttpClient
final SalesforceHttpClient httpClient = new SalesforceHttpClient( // NOSONAR
final SalesforceHttpClient httpClient = new SalesforceHttpClient(
context, context.getExecutorServiceManager().newThreadPool(source, "SalesforceHttpClient", workerPoolSize,
workerPoolMaxSize),
sslContextFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ protected AggregateProcessor createAggregator() throws Exception {

boolean parallel = parseBoolean(definition.getParallelProcessing(), false);
boolean shutdownThreadPool = willCreateNewThreadPool(definition, parallel);
// ExecutorService lifecycle is managed by AggregateProcessor via shutdownThreadPool flag
@SuppressWarnings("java:S2095")
ExecutorService threadPool = getConfiguredExecutorService("Aggregator", definition, parallel);
if (threadPool == null && !parallel) {
// executor service is mandatory for the Aggregator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public Processor createProcessor() throws Exception {
}
// prefer any explicit configured executor service
boolean shutdownThreadPool = willCreateNewThreadPool(definition, true);
// ExecutorService lifecycle is managed by ThreadsProcessor via shutdownThreadPool flag
@SuppressWarnings("java:S2095")
ExecutorService threadPool = getConfiguredExecutorService(name, definition, false);

// resolve what rejected policy to use
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.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
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.test.infra.openai.mock;

import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

/**
* Wrapper that makes {@link HttpClient} usable in try-with-resources. On Java 21+ HttpClient implements AutoCloseable
* natively; the instanceof check future-proofs us for when the minimum JDK is raised.
*/
final class CloseableHttpClient implements AutoCloseable {
private final HttpClient httpClient = HttpClient.newHttpClient();

<T> HttpResponse<T> send(HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler)
throws IOException, InterruptedException {
return httpClient.send(request, responseBodyHandler);
}

@Override
public void close() throws Exception {
if (httpClient instanceof AutoCloseable closeable) {
closeable.close();
}
}
}
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 @@ -17,7 +17,6 @@
package org.apache.camel.test.infra.openai.mock;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -48,35 +47,35 @@ public class OpenAIMockConversationHistoryTest {

@Test
public void testConversationHistoryAssertion() throws Exception {
HttpClient client = HttpClient.newHttpClient();
try (CloseableHttpClient hc = new CloseableHttpClient()) {
// 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 = hc.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");
}
}
}
Loading
Loading