Skip to content

Commit

Permalink
Very minor code-style improvements in test
Browse files Browse the repository at this point in the history
  • Loading branch information
newtork committed Sep 3, 2024
1 parent 7474fb1 commit ac6f36c
Showing 1 changed file with 20 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;

import com.fasterxml.jackson.core.JsonParseException;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
Expand All @@ -27,8 +27,10 @@
import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination;
import io.vavr.control.Try;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import org.apache.hc.client5.http.classic.HttpClient;
Expand All @@ -46,6 +48,8 @@
@WireMockTest
class OpenAiClientTest {
private static OpenAiClient client;
private final Function<String, InputStream> TEST_FILE_LOADER =
filename -> Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream(filename));

@BeforeEach
void setup(WireMockRuntimeInfo server) {
Expand Down Expand Up @@ -80,14 +84,15 @@ void apiVersion() {
verify(exactly(2), postRequestedFor(anyUrl()).withoutQueryParam("api-version"));
}

private static Stream<Runnable> chatCompletionCalls() {
return Stream.of(
() -> client.chatCompletion(new OpenAiChatCompletionParameters()),
() ->
client
.streamChatCompletionDeltas(new OpenAiChatCompletionParameters())
// the stream needs to be consumed to parse the response
.forEach(System.out::println));
private static Runnable[] chatCompletionCalls() {
return new Runnable[] {
() -> client.chatCompletion(new OpenAiChatCompletionParameters()),
() ->
client
.streamChatCompletionDeltas(new OpenAiChatCompletionParameters())
// the stream needs to be consumed to parse the response
.forEach(System.out::println)
};
}

@ParameterizedTest
Expand Down Expand Up @@ -166,10 +171,8 @@ void chatCompletionErrorHandling(@Nonnull final Runnable request) {

@Test
void chatCompletion() throws IOException {
try (var inputStream =
getClass().getClassLoader().getResourceAsStream("chatCompletionResponse.json")) {
try (var inputStream = TEST_FILE_LOADER.apply("chatCompletionResponse.json")) {

assert inputStream != null;
final String response = new String(inputStream.readAllBytes());
stubFor(post("/chat/completions").willReturn(okJson(response)));

Expand Down Expand Up @@ -255,10 +258,8 @@ void chatCompletion() throws IOException {

@Test
void embedding() throws IOException {
try (var inputStream =
getClass().getClassLoader().getResourceAsStream("embeddingResponse.json")) {
try (var inputStream = TEST_FILE_LOADER.apply("embeddingResponse.json")) {

assert inputStream != null;
final String response = new String(inputStream.readAllBytes());
stubFor(post("/embeddings").willReturn(okJson(response)));

Expand Down Expand Up @@ -296,12 +297,7 @@ void embedding() throws IOException {

@Test
void streamChatCompletionDeltasErrorHandling() throws IOException {
try (var inputStream =
spy(
Objects.requireNonNull(
getClass()
.getClassLoader()
.getResourceAsStream("streamChatCompletionError.txt")))) {
try (var inputStream = spy(TEST_FILE_LOADER.apply("streamChatCompletionError.txt"))) {

final var httpClient = mock(HttpClient.class);
ApacheHttpClient5Accessor.setHttpClientFactory(destination -> httpClient);
Expand Down Expand Up @@ -330,16 +326,13 @@ void streamChatCompletionDeltasErrorHandling() throws IOException {
"Failed to parse response from OpenAI model and error message: 'exceeded token rate limit'");
}

Mockito.verify(inputStream, atLeastOnce()).close();
Mockito.verify(inputStream, times(1)).close();
}
}

@Test
void streamChatCompletionDeltas() throws IOException {
try (var inputStream =
spy(
Objects.requireNonNull(
getClass().getClassLoader().getResourceAsStream("streamChatCompletion.txt")))) {
try (var inputStream = spy(TEST_FILE_LOADER.apply("streamChatCompletion.txt"))) {

final var httpClient = mock(HttpClient.class);
ApacheHttpClient5Accessor.setHttpClientFactory(destination -> httpClient);
Expand Down Expand Up @@ -448,7 +441,7 @@ void streamChatCompletionDeltas() throws IOException {
assertFilter(totalOutput.getPromptFilterResults().get(0).getContentFilterResults());
}

Mockito.verify(inputStream, atLeastOnce()).close();
Mockito.verify(inputStream, times(1)).close();
}
}

Expand Down

0 comments on commit ac6f36c

Please sign in to comment.