diff --git a/pom.xml b/pom.xml index 84bebef..516c8cd 100644 --- a/pom.xml +++ b/pom.xml @@ -99,11 +99,10 @@ springboot-modules + testContainers - 17 - 17 3.25.0 3.5.0 3.13.0 @@ -112,6 +111,7 @@ 5.10.3 5.10.3 2.7.3 + 1.21.1 \ No newline at end of file diff --git a/springboot-modules/spring-ai/pom.xml b/springboot-modules/spring-ai/pom.xml index 07d249d..7b68a7b 100644 --- a/springboot-modules/spring-ai/pom.xml +++ b/springboot-modules/spring-ai/pom.xml @@ -71,9 +71,8 @@ --> - 21 - 21 UTF-8 + 17 \ No newline at end of file diff --git a/springboot-modules/spring-ai/src/test/java/com/kodesastra/ai/advisors/AdvisorUnitTest.java b/springboot-modules/spring-ai/src/test/java/com/kodesastra/ai/advisors/AdvisorLiveTest.java similarity index 96% rename from springboot-modules/spring-ai/src/test/java/com/kodesastra/ai/advisors/AdvisorUnitTest.java rename to springboot-modules/spring-ai/src/test/java/com/kodesastra/ai/advisors/AdvisorLiveTest.java index 1367259..36e3d30 100644 --- a/springboot-modules/spring-ai/src/test/java/com/kodesastra/ai/advisors/AdvisorUnitTest.java +++ b/springboot-modules/spring-ai/src/test/java/com/kodesastra/ai/advisors/AdvisorLiveTest.java @@ -16,9 +16,9 @@ @SpringBootTest @ActiveProfiles("sa") -public class AdvisorUnitTest { +public class AdvisorLiveTest { - private static final Logger LOGGER = LoggerFactory.getLogger(AdvisorUnitTest.class); + private static final Logger LOGGER = LoggerFactory.getLogger(AdvisorLiveTest.class); @Autowired ChatModel chatModel; diff --git a/testContainers/README.md b/testContainers/README.md new file mode 100644 index 0000000..2c91d7f --- /dev/null +++ b/testContainers/README.md @@ -0,0 +1,9 @@ +# **Published Articles** + +## **Testcontainers** + +This module contains source codes related to the articles on Testcontainers. +- [**Ollama - Testcontainers Module in Java**](https://www.kodesastra.com/2025/06/ollama-testcontainers-module-in-java.html) + + + diff --git a/testContainers/pom.xml b/testContainers/pom.xml new file mode 100644 index 0000000..8631abb --- /dev/null +++ b/testContainers/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + com.kodesastra + kodesastra_blog + 1.0-SNAPSHOT + + + testContainers + + + + + org.testcontainers + testcontainers-bom + 1.21.1 + pom + import + + + + + + org.testcontainers + ollama + test + + + ch.qos.logback + logback-classic + 1.5.18 + + + ch.qos.logback + logback-core + 1.5.18 + + + org.slf4j + slf4j-api + 2.0.17 + + + + + UTF-8 + 17 + + + \ No newline at end of file diff --git a/testContainers/src/main/resources/logback.xml b/testContainers/src/main/resources/logback.xml new file mode 100644 index 0000000..4bd09a2 --- /dev/null +++ b/testContainers/src/main/resources/logback.xml @@ -0,0 +1,22 @@ + + + + + + %d{HH:mm:ss.SSS} %-5level %logger - %msg%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testContainers/src/test/java/com/ks/testcontainers/ollama/OllamaContainerLiveTest.java b/testContainers/src/test/java/com/ks/testcontainers/ollama/OllamaContainerLiveTest.java new file mode 100644 index 0000000..9230350 --- /dev/null +++ b/testContainers/src/test/java/com/ks/testcontainers/ollama/OllamaContainerLiveTest.java @@ -0,0 +1,63 @@ +package com.ks.testcontainers.ollama; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.Container; +import org.testcontainers.ollama.OllamaContainer; + +public class OllamaContainerLiveTest { + private static final Logger logger = LoggerFactory.getLogger(OllamaContainerLiveTest.class); + + static OllamaContainer ollamaContainer; + private static final String OLLAMA_IMAGE = "ollama/ollama:latest"; + private static final String LLM = "tinyllama:1.1b"; + + @BeforeAll + static void init() throws IOException, InterruptedException { + + ollamaContainer = new OllamaContainer(OLLAMA_IMAGE); + ollamaContainer.withCreateContainerCmdModifier((cmd) -> + cmd.getHostConfig().withDeviceRequests(null)); + + ollamaContainer.start(); + + ollamaContainer.execInContainer("ollama", "pull", LLM); + } + + @AfterAll + static void cleanUp() { + if (ollamaContainer != null) { + try { + ollamaContainer.stop(); + } catch (Exception e) { + logger.error("Error stopping Ollama container: {}", e.getMessage()); + } + } + } + + @Test + void test() throws IOException, InterruptedException { + String prompt = """ + Context: + The sun is a star located at the center of our solar system. + It provides the Earth with heat and light, making life possible. + The sun is composed mostly of hydrogen and helium + and generates energy through nuclear fusion." + Question: What two gases make up most of the sun? + Instructions: + Please answer strictly from the context provided in the prompt and no other additional + should be provided.Also, keep the answer short and concise." + """; + Container.ExecResult execResult = + ollamaContainer.execInContainer("ollama", "run", LLM, prompt); + assertEquals(0, execResult.getExitCode(), "Exit code should be 0"); + logger.info("Exec Result: {}", execResult.getStdout()); + } +} \ No newline at end of file