From 23084143abe1be5f03ec649eb4dbcb0c50d3b20e Mon Sep 17 00:00:00 2001 From: Stefan Bratanov Date: Thu, 28 Mar 2024 10:27:27 +0000 Subject: [PATCH] add deserialization test --- .../jvm/openai/DeserializationTest.java | 33 +++++++++++++++++++ .../OpenAIAssistantsApiIntegrationTest.java | 1 + .../jvm/openai/OpenAIIntegrationTest.java | 1 + .../jvm/openai/OpenAIIntegrationTestBase.java | 13 -------- .../stefanbratanov/jvm/openai/TestUtil.java | 28 ++++++++++++++++ src/test/resources/chat-completion-chunk.json | 18 ++++++++++ 6 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 src/test/java/io/github/stefanbratanov/jvm/openai/DeserializationTest.java create mode 100644 src/test/java/io/github/stefanbratanov/jvm/openai/TestUtil.java create mode 100644 src/test/resources/chat-completion-chunk.json diff --git a/src/test/java/io/github/stefanbratanov/jvm/openai/DeserializationTest.java b/src/test/java/io/github/stefanbratanov/jvm/openai/DeserializationTest.java new file mode 100644 index 0000000..b091abe --- /dev/null +++ b/src/test/java/io/github/stefanbratanov/jvm/openai/DeserializationTest.java @@ -0,0 +1,33 @@ +package io.github.stefanbratanov.jvm.openai; + +import static io.github.stefanbratanov.jvm.openai.TestUtil.getStringResource; +import static org.assertj.core.api.Assertions.assertThat; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +public class DeserializationTest { + + private final ObjectMapper objectMapper = ObjectMapperSingleton.getInstance(); + + @Test + public void deserializesChatCompletionChunk() throws JsonProcessingException { + ChatCompletionChunk result = + objectMapper.readValue( + getStringResource("/chat-completion-chunk.json"), ChatCompletionChunk.class); + + assertThat(result).isNotNull(); + assertThat(result.id()).isEqualTo("chatcmpl-xyz"); + assertThat(result.choices()) + .hasSize(1) + .first() + .satisfies( + choice -> { + assertThat(choice.index()).isZero(); + assertThat(choice.delta().role()).isEqualTo("assistant"); + assertThat(choice.delta().content()).isEqualTo(""); + assertThat(choice.finishReason()).isNull(); + }); + } +} diff --git a/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIAssistantsApiIntegrationTest.java b/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIAssistantsApiIntegrationTest.java index df798b2..9013e18 100644 --- a/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIAssistantsApiIntegrationTest.java +++ b/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIAssistantsApiIntegrationTest.java @@ -1,5 +1,6 @@ package io.github.stefanbratanov.jvm.openai; +import static io.github.stefanbratanov.jvm.openai.TestUtil.getTestResource; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIIntegrationTest.java b/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIIntegrationTest.java index 420ef1e..c0697a4 100644 --- a/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIIntegrationTest.java +++ b/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIIntegrationTest.java @@ -1,5 +1,6 @@ package io.github.stefanbratanov.jvm.openai; +import static io.github.stefanbratanov.jvm.openai.TestUtil.getTestResource; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIIntegrationTestBase.java b/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIIntegrationTestBase.java index a768119..c483d18 100644 --- a/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIIntegrationTestBase.java +++ b/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIIntegrationTestBase.java @@ -4,11 +4,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockserver.mock.OpenAPIExpectation.openAPIExpectation; -import java.net.URISyntaxException; -import java.nio.file.Path; -import java.nio.file.Paths; import java.time.Duration; -import java.util.Objects; import java.util.concurrent.*; import java.util.function.Supplier; import org.junit.jupiter.api.AfterAll; @@ -56,15 +52,6 @@ public static void cleanUp() { mockServer.stop(); } - protected Path getTestResource(String resource) { - try { - return Paths.get( - Objects.requireNonNull(OpenAIIntegrationTestBase.class.getResource(resource)).toURI()); - } catch (URISyntaxException ex) { - throw new RuntimeException(ex); - } - } - protected void awaitCondition( Supplier condition, Duration pollingInterval, Duration timeout) { ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); diff --git a/src/test/java/io/github/stefanbratanov/jvm/openai/TestUtil.java b/src/test/java/io/github/stefanbratanov/jvm/openai/TestUtil.java new file mode 100644 index 0000000..b342193 --- /dev/null +++ b/src/test/java/io/github/stefanbratanov/jvm/openai/TestUtil.java @@ -0,0 +1,28 @@ +package io.github.stefanbratanov.jvm.openai; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Objects; + +public class TestUtil { + + public static Path getTestResource(String resource) { + try { + return Paths.get(Objects.requireNonNull(TestUtil.class.getResource(resource)).toURI()); + } catch (URISyntaxException ex) { + throw new RuntimeException(ex); + } + } + + public static String getStringResource(String resource) { + try { + return Files.readString(getTestResource(resource)); + } catch (IOException ex) { + throw new UncheckedIOException(ex); + } + } +} diff --git a/src/test/resources/chat-completion-chunk.json b/src/test/resources/chat-completion-chunk.json new file mode 100644 index 0000000..e8bcd05 --- /dev/null +++ b/src/test/resources/chat-completion-chunk.json @@ -0,0 +1,18 @@ +{ + "id": "chatcmpl-xyz", + "object": "chat.completion.chunk", + "created": 1711604346, + "model": "gpt-4-0613", + "system_fingerprint": null, + "choices": [ + { + "index": 0, + "delta": { + "role": "assistant", + "content": "" + }, + "logprobs": null, + "finish_reason": null + } + ] +} \ No newline at end of file