Refactor OllamaAPI to use Constants for HTTP headers and improve logging format

- Introduced a new Constants class to centralize HTTP header values.
- Updated OllamaAPI methods to utilize Constants for "Content-Type" and "Accept" headers.
- Enhanced logging statements to use parameterized messages for better performance and readability.
- Added a test for the ping method in OllamaAPIIntegrationTest to ensure connectivity.
This commit is contained in:
amithkoujalgi
2025-08-30 13:01:01 +05:30
parent f085b633af
commit cc950b893e
11 changed files with 134 additions and 60 deletions

View File

@@ -98,6 +98,13 @@ public class OllamaAPIIntegrationTest {
// image version");
}
@Test
@Order(1)
public void testPing() throws URISyntaxException, IOException, OllamaBaseException, InterruptedException {
boolean pingResponse = api.ping();
assertTrue(pingResponse, "Ping should return true");
}
@Test
@Order(2)
public void testListModelsAPI()

View File

@@ -3,40 +3,66 @@ package io.github.ollama4j.unittests.jackson;
import io.github.ollama4j.models.response.Model;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class TestModelRequestSerialization extends AbstractSerializationTest<Model> {
@Test
public void testDeserializationOfModelResponseWithOffsetTime(){
String serializedTestStringWithOffsetTime = "{\n"
+ "\"name\": \"codellama:13b\",\n"
+ "\"modified_at\": \"2023-11-04T14:56:49.277302595-07:00\",\n"
+ "\"size\": 7365960935,\n"
+ "\"digest\": \"9f438cb9cd581fc025612d27f7c1a6669ff83a8bb0ed86c94fcf4c5440555697\",\n"
+ "\"details\": {\n"
+ "\"format\": \"gguf\",\n"
+ "\"family\": \"llama\",\n"
+ "\"families\": null,\n"
+ "\"parameter_size\": \"13B\",\n"
+ "\"quantization_level\": \"Q4_0\"\n"
+ "}}";
deserialize(serializedTestStringWithOffsetTime,Model.class);
public void testDeserializationOfModelResponseWithOffsetTime() {
String serializedTestStringWithOffsetTime = "{\n" +
" \"name\": \"codellama:13b\",\n" +
" \"modified_at\": \"2023-11-04T14:56:49.277302595-07:00\",\n" +
" \"size\": 7365960935,\n" +
" \"digest\": \"9f438cb9cd581fc025612d27f7c1a6669ff83a8bb0ed86c94fcf4c5440555697\",\n" +
" \"details\": {\n" +
" \"format\": \"gguf\",\n" +
" \"family\": \"llama\",\n" +
" \"families\": null,\n" +
" \"parameter_size\": \"13B\",\n" +
" \"quantization_level\": \"Q4_0\"\n" +
" }\n" +
"}";
Model model = deserialize(serializedTestStringWithOffsetTime, Model.class);
assertNotNull(model);
assertEquals("codellama:13b", model.getName());
assertEquals("2023-11-04T21:56:49.277302595Z", model.getModifiedAt().toString());
assertEquals(7365960935L, model.getSize());
assertEquals("9f438cb9cd581fc025612d27f7c1a6669ff83a8bb0ed86c94fcf4c5440555697", model.getDigest());
assertNotNull(model.getModelMeta());
assertEquals("gguf", model.getModelMeta().getFormat());
assertEquals("llama", model.getModelMeta().getFamily());
assertNull(model.getModelMeta().getFamilies());
assertEquals("13B", model.getModelMeta().getParameterSize());
assertEquals("Q4_0", model.getModelMeta().getQuantizationLevel());
}
@Test
public void testDeserializationOfModelResponseWithZuluTime(){
String serializedTestStringWithZuluTimezone = "{\n"
+ "\"name\": \"codellama:13b\",\n"
+ "\"modified_at\": \"2023-11-04T14:56:49.277302595Z\",\n"
+ "\"size\": 7365960935,\n"
+ "\"digest\": \"9f438cb9cd581fc025612d27f7c1a6669ff83a8bb0ed86c94fcf4c5440555697\",\n"
+ "\"details\": {\n"
+ "\"format\": \"gguf\",\n"
+ "\"family\": \"llama\",\n"
+ "\"families\": null,\n"
+ "\"parameter_size\": \"13B\",\n"
+ "\"quantization_level\": \"Q4_0\"\n"
+ "}}";
deserialize(serializedTestStringWithZuluTimezone,Model.class);
public void testDeserializationOfModelResponseWithZuluTime() {
String serializedTestStringWithZuluTimezone = "{\n" +
" \"name\": \"codellama:13b\",\n" +
" \"modified_at\": \"2023-11-04T14:56:49.277302595Z\",\n" +
" \"size\": 7365960935,\n" +
" \"digest\": \"9f438cb9cd581fc025612d27f7c1a6669ff83a8bb0ed86c94fcf4c5440555697\",\n" +
" \"details\": {\n" +
" \"format\": \"gguf\",\n" +
" \"family\": \"llama\",\n" +
" \"families\": null,\n" +
" \"parameter_size\": \"13B\",\n" +
" \"quantization_level\": \"Q4_0\"\n" +
" }\n" +
"}";
Model model = deserialize(serializedTestStringWithZuluTimezone, Model.class);
assertNotNull(model);
assertEquals("codellama:13b", model.getName());
assertEquals("2023-11-04T14:56:49.277302595Z", model.getModifiedAt().toString());
assertEquals(7365960935L, model.getSize());
assertEquals("9f438cb9cd581fc025612d27f7c1a6669ff83a8bb0ed86c94fcf4c5440555697", model.getDigest());
assertNotNull(model.getModelMeta());
assertEquals("gguf", model.getModelMeta().getFormat());
assertEquals("llama", model.getModelMeta().getFamily());
assertNull(model.getModelMeta().getFamilies());
assertEquals("13B", model.getModelMeta().getParameterSize());
assertEquals("Q4_0", model.getModelMeta().getQuantizationLevel());
}
}