Files
ollama4j/src/test/java/io/github/ollama4j/unittests/TestOllamaChatRequestBuilder.java
amithkoujalgi 7788f954d6 Improve error handling and code clarity across modules
Enhanced error handling for image URL loading in OllamaChatRequestBuilder, ensuring exceptions are thrown and logged appropriately. Updated test cases to reflect new exception behavior. Improved documentation and code clarity in WeatherTool and test classes. Refactored JSON parsing in response models for conciseness. Minor cleanup in pom.xml and integration test comments for better maintainability.
2025-09-18 01:22:12 +05:30

65 lines
2.3 KiB
Java

/*
* Ollama4j - Java library for interacting with Ollama server.
* Copyright (c) 2025 Amith Koujalgi and contributors.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
*
*/
package io.github.ollama4j.unittests;
import static org.junit.jupiter.api.Assertions.*;
import io.github.ollama4j.models.chat.OllamaChatMessage;
import io.github.ollama4j.models.chat.OllamaChatMessageRole;
import io.github.ollama4j.models.chat.OllamaChatRequest;
import io.github.ollama4j.models.chat.OllamaChatRequestBuilder;
import java.util.Collections;
import org.junit.jupiter.api.Test;
class TestOllamaChatRequestBuilder {
@Test
void testResetClearsMessagesButKeepsModelAndThink() {
OllamaChatRequestBuilder builder =
OllamaChatRequestBuilder.getInstance("my-model")
.withThinking(true)
.withMessage(OllamaChatMessageRole.USER, "first");
OllamaChatRequest beforeReset = builder.build();
assertEquals("my-model", beforeReset.getModel());
assertTrue(beforeReset.isThink());
assertEquals(1, beforeReset.getMessages().size());
builder.reset();
OllamaChatRequest afterReset = builder.build();
assertEquals("my-model", afterReset.getModel());
assertTrue(afterReset.isThink());
assertNotNull(afterReset.getMessages());
assertEquals(0, afterReset.getMessages().size());
}
@Test
void testImageUrlFailuresThrowExceptionAndBuilderRemainsUsable() {
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance("m");
String invalidUrl = "ht!tp:/bad_url"; // clearly invalid URL format
// Exception should be thrown for invalid URL
assertThrows(
Exception.class,
() -> {
builder.withMessage(
OllamaChatMessageRole.USER, "hi", Collections.emptyList(), invalidUrl);
});
OllamaChatRequest req =
builder.withMessage(OllamaChatMessageRole.USER, "hello", Collections.emptyList())
.build();
assertNotNull(req.getMessages());
assert (!req.getMessages().isEmpty());
OllamaChatMessage msg = req.getMessages().get(0);
assertNotNull(msg.getContent());
}
}