Skip to content

Commit

Permalink
add deserialization test
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Mar 28, 2024
1 parent 00fb801 commit 2308414
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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();
});
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Boolean> condition, Duration pollingInterval, Duration timeout) {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/io/github/stefanbratanov/jvm/openai/TestUtil.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
18 changes: 18 additions & 0 deletions src/test/resources/chat-completion-chunk.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}

0 comments on commit 2308414

Please sign in to comment.