forked from Mirror/ollama4j
		
	Refactor WithAuth integration test for improved structure and functionality
- Restored the WithAuth class and updated it to include a new general-purpose model. - Enhanced the setup method to initialize the OllamaAPI with bearer authentication. - Added tests to validate the API's behavior with correct and incorrect authentication tokens. - Updated the structured output test to reflect changes in the model and prompt. - Improved logging for better traceability during test execution.
This commit is contained in:
		@@ -1,194 +1,180 @@
 | 
				
			|||||||
//package io.github.ollama4j.integrationtests;
 | 
					package io.github.ollama4j.integrationtests;
 | 
				
			||||||
//
 | 
					
 | 
				
			||||||
//import io.github.ollama4j.OllamaAPI;
 | 
					import io.github.ollama4j.OllamaAPI;
 | 
				
			||||||
//import io.github.ollama4j.exceptions.OllamaBaseException;
 | 
					import io.github.ollama4j.exceptions.OllamaBaseException;
 | 
				
			||||||
//import io.github.ollama4j.models.response.OllamaResult;
 | 
					import io.github.ollama4j.models.response.OllamaResult;
 | 
				
			||||||
//import io.github.ollama4j.samples.AnnotatedTool;
 | 
					import io.github.ollama4j.samples.AnnotatedTool;
 | 
				
			||||||
//import io.github.ollama4j.tools.annotations.OllamaToolService;
 | 
					import io.github.ollama4j.tools.annotations.OllamaToolService;
 | 
				
			||||||
//import org.junit.jupiter.api.BeforeAll;
 | 
					import org.junit.jupiter.api.BeforeAll;
 | 
				
			||||||
//import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
 | 
					import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
 | 
				
			||||||
//import org.junit.jupiter.api.Order;
 | 
					import org.junit.jupiter.api.Order;
 | 
				
			||||||
//import org.junit.jupiter.api.Test;
 | 
					import org.junit.jupiter.api.Test;
 | 
				
			||||||
//import org.junit.jupiter.api.TestMethodOrder;
 | 
					import org.junit.jupiter.api.TestMethodOrder;
 | 
				
			||||||
//import org.slf4j.Logger;
 | 
					import org.slf4j.Logger;
 | 
				
			||||||
//import org.slf4j.LoggerFactory;
 | 
					import org.slf4j.LoggerFactory;
 | 
				
			||||||
//import org.testcontainers.containers.GenericContainer;
 | 
					import org.testcontainers.containers.GenericContainer;
 | 
				
			||||||
//import org.testcontainers.containers.NginxContainer;
 | 
					import org.testcontainers.containers.NginxContainer;
 | 
				
			||||||
//import org.testcontainers.containers.wait.strategy.Wait;
 | 
					import org.testcontainers.containers.wait.strategy.Wait;
 | 
				
			||||||
//import org.testcontainers.ollama.OllamaContainer;
 | 
					import org.testcontainers.ollama.OllamaContainer;
 | 
				
			||||||
//import org.testcontainers.utility.DockerImageName;
 | 
					import org.testcontainers.utility.DockerImageName;
 | 
				
			||||||
//import org.testcontainers.utility.MountableFile;
 | 
					import org.testcontainers.utility.MountableFile;
 | 
				
			||||||
//
 | 
					
 | 
				
			||||||
//import java.io.File;
 | 
					import java.io.File;
 | 
				
			||||||
//import java.io.FileWriter;
 | 
					import java.io.FileWriter;
 | 
				
			||||||
//import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
//import java.net.URISyntaxException;
 | 
					import java.net.URISyntaxException;
 | 
				
			||||||
//import java.time.Duration;
 | 
					import java.time.Duration;
 | 
				
			||||||
//import java.util.Arrays;
 | 
					import java.util.Arrays;
 | 
				
			||||||
//import java.util.HashMap;
 | 
					import java.util.HashMap;
 | 
				
			||||||
//import java.util.Map;
 | 
					import java.util.List;
 | 
				
			||||||
//
 | 
					import java.util.Map;
 | 
				
			||||||
//import static org.junit.jupiter.api.Assertions.*;
 | 
					
 | 
				
			||||||
//
 | 
					import static org.junit.jupiter.api.Assertions.*;
 | 
				
			||||||
//@OllamaToolService(providers = {AnnotatedTool.class})
 | 
					
 | 
				
			||||||
//@TestMethodOrder(OrderAnnotation.class)
 | 
					@OllamaToolService(providers = {AnnotatedTool.class})
 | 
				
			||||||
//@SuppressWarnings({"HttpUrlsUsage", "SpellCheckingInspection", "resource", "ResultOfMethodCallIgnored"})
 | 
					@TestMethodOrder(OrderAnnotation.class)
 | 
				
			||||||
//public class WithAuth {
 | 
					@SuppressWarnings({"HttpUrlsUsage", "SpellCheckingInspection", "resource", "ResultOfMethodCallIgnored"})
 | 
				
			||||||
//
 | 
					public class WithAuth {
 | 
				
			||||||
//    private static final Logger LOG = LoggerFactory.getLogger(WithAuth.class);
 | 
					
 | 
				
			||||||
//    private static final int NGINX_PORT = 80;
 | 
					    private static final Logger LOG = LoggerFactory.getLogger(WithAuth.class);
 | 
				
			||||||
//    private static final int OLLAMA_INTERNAL_PORT = 11434;
 | 
					    private static final int NGINX_PORT = 80;
 | 
				
			||||||
//    private static final String OLLAMA_VERSION = "0.6.1";
 | 
					    private static final int OLLAMA_INTERNAL_PORT = 11434;
 | 
				
			||||||
//    private static final String NGINX_VERSION = "nginx:1.23.4-alpine";
 | 
					    private static final String OLLAMA_VERSION = "0.6.1";
 | 
				
			||||||
//    private static final String BEARER_AUTH_TOKEN = "secret-token";
 | 
					    private static final String NGINX_VERSION = "nginx:1.23.4-alpine";
 | 
				
			||||||
//    private static final String CHAT_MODEL_LLAMA3 = "llama3";
 | 
					    private static final String BEARER_AUTH_TOKEN = "secret-token";
 | 
				
			||||||
//
 | 
					
 | 
				
			||||||
//
 | 
					    private static final String GENERAL_PURPOSE_MODEL = "gemma3:270m";
 | 
				
			||||||
//    private static OllamaContainer ollama;
 | 
					
 | 
				
			||||||
//    private static GenericContainer<?> nginx;
 | 
					
 | 
				
			||||||
//    private static OllamaAPI api;
 | 
					    private static OllamaContainer ollama;
 | 
				
			||||||
//
 | 
					    private static GenericContainer<?> nginx;
 | 
				
			||||||
//    @BeforeAll
 | 
					    private static OllamaAPI api;
 | 
				
			||||||
//    public static void setUp() {
 | 
					
 | 
				
			||||||
//        ollama = createOllamaContainer();
 | 
					    @BeforeAll
 | 
				
			||||||
//        ollama.start();
 | 
					    public static void setUp() {
 | 
				
			||||||
//
 | 
					        ollama = createOllamaContainer();
 | 
				
			||||||
//        nginx = createNginxContainer(ollama.getMappedPort(OLLAMA_INTERNAL_PORT));
 | 
					        ollama.start();
 | 
				
			||||||
//        nginx.start();
 | 
					
 | 
				
			||||||
//
 | 
					        nginx = createNginxContainer(ollama.getMappedPort(OLLAMA_INTERNAL_PORT));
 | 
				
			||||||
//        LOG.info("Using Testcontainer Ollama host...");
 | 
					        nginx.start();
 | 
				
			||||||
//
 | 
					
 | 
				
			||||||
//        api = new OllamaAPI("http://" + nginx.getHost() + ":" + nginx.getMappedPort(NGINX_PORT));
 | 
					        LOG.info("Using Testcontainer Ollama host...");
 | 
				
			||||||
//        api.setRequestTimeoutSeconds(120);
 | 
					
 | 
				
			||||||
//        api.setVerbose(true);
 | 
					        api = new OllamaAPI("http://" + nginx.getHost() + ":" + nginx.getMappedPort(NGINX_PORT));
 | 
				
			||||||
//        api.setNumberOfRetriesForModelPull(3);
 | 
					        api.setRequestTimeoutSeconds(120);
 | 
				
			||||||
//
 | 
					        api.setVerbose(true);
 | 
				
			||||||
//        String ollamaUrl = "http://" + ollama.getHost() + ":" + ollama.getMappedPort(OLLAMA_INTERNAL_PORT);
 | 
					        api.setNumberOfRetriesForModelPull(3);
 | 
				
			||||||
//        String nginxUrl = "http://" + nginx.getHost() + ":" + nginx.getMappedPort(NGINX_PORT);
 | 
					
 | 
				
			||||||
//        LOG.info(
 | 
					        String ollamaUrl = "http://" + ollama.getHost() + ":" + ollama.getMappedPort(OLLAMA_INTERNAL_PORT);
 | 
				
			||||||
//                "The Ollama service is now accessible via the Nginx proxy with bearer-auth authentication mode.\n" +
 | 
					        String nginxUrl = "http://" + nginx.getHost() + ":" + nginx.getMappedPort(NGINX_PORT);
 | 
				
			||||||
//                        "→ Ollama URL: {}\n" +
 | 
					        LOG.info(
 | 
				
			||||||
//                        "→ Proxy URL: {}",
 | 
					                "The Ollama service is now accessible via the Nginx proxy with bearer-auth authentication mode.\n" +
 | 
				
			||||||
//                ollamaUrl, nginxUrl
 | 
					                        "→ Ollama URL: {}\n" +
 | 
				
			||||||
//        );
 | 
					                        "→ Proxy URL: {}",
 | 
				
			||||||
//        LOG.info("OllamaAPI initialized with bearer auth token: {}", BEARER_AUTH_TOKEN);
 | 
					                ollamaUrl, nginxUrl
 | 
				
			||||||
//    }
 | 
					        );
 | 
				
			||||||
//
 | 
					        LOG.info("OllamaAPI initialized with bearer auth token: {}", BEARER_AUTH_TOKEN);
 | 
				
			||||||
//    private static OllamaContainer createOllamaContainer() {
 | 
					    }
 | 
				
			||||||
//        return new OllamaContainer("ollama/ollama:" + OLLAMA_VERSION).withExposedPorts(OLLAMA_INTERNAL_PORT);
 | 
					
 | 
				
			||||||
//    }
 | 
					    private static OllamaContainer createOllamaContainer() {
 | 
				
			||||||
//
 | 
					        return new OllamaContainer("ollama/ollama:" + OLLAMA_VERSION).withExposedPorts(OLLAMA_INTERNAL_PORT);
 | 
				
			||||||
//    private static String generateNginxConfig(int ollamaPort) {
 | 
					    }
 | 
				
			||||||
//        return String.format("events {}\n" +
 | 
					
 | 
				
			||||||
//                "\n" +
 | 
					    private static String generateNginxConfig(int ollamaPort) {
 | 
				
			||||||
//                "http {\n" +
 | 
					        return String.format("events {}\n" +
 | 
				
			||||||
//                "    server {\n" +
 | 
					                "\n" +
 | 
				
			||||||
//                "        listen 80;\n" +
 | 
					                "http {\n" +
 | 
				
			||||||
//                "\n" +
 | 
					                "    server {\n" +
 | 
				
			||||||
//                "        location / {\n" +
 | 
					                "        listen 80;\n" +
 | 
				
			||||||
//                "            set $auth_header $http_authorization;\n" +
 | 
					                "\n" +
 | 
				
			||||||
//                "\n" +
 | 
					                "        location / {\n" +
 | 
				
			||||||
//                "            if ($auth_header != \"Bearer secret-token\") {\n" +
 | 
					                "            set $auth_header $http_authorization;\n" +
 | 
				
			||||||
//                "                return 401;\n" +
 | 
					                "\n" +
 | 
				
			||||||
//                "            }\n" +
 | 
					                "            if ($auth_header != \"Bearer secret-token\") {\n" +
 | 
				
			||||||
//                "\n" +
 | 
					                "                return 401;\n" +
 | 
				
			||||||
//                "            proxy_pass http://host.docker.internal:%s/;\n" +
 | 
					                "            }\n" +
 | 
				
			||||||
//                "            proxy_set_header Host $host;\n" +
 | 
					                "\n" +
 | 
				
			||||||
//                "            proxy_set_header X-Real-IP $remote_addr;\n" +
 | 
					                "            proxy_pass http://host.docker.internal:%s/;\n" +
 | 
				
			||||||
//                "            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n" +
 | 
					                "            proxy_set_header Host $host;\n" +
 | 
				
			||||||
//                "            proxy_set_header X-Forwarded-Proto $scheme;\n" +
 | 
					                "            proxy_set_header X-Real-IP $remote_addr;\n" +
 | 
				
			||||||
//                "        }\n" +
 | 
					                "            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n" +
 | 
				
			||||||
//                "    }\n" +
 | 
					                "            proxy_set_header X-Forwarded-Proto $scheme;\n" +
 | 
				
			||||||
//                "}\n", ollamaPort);
 | 
					                "        }\n" +
 | 
				
			||||||
//    }
 | 
					                "    }\n" +
 | 
				
			||||||
//
 | 
					                "}\n", ollamaPort);
 | 
				
			||||||
//    public static GenericContainer<?> createNginxContainer(int ollamaPort) {
 | 
					    }
 | 
				
			||||||
//        File nginxConf;
 | 
					
 | 
				
			||||||
//        try {
 | 
					    public static GenericContainer<?> createNginxContainer(int ollamaPort) {
 | 
				
			||||||
//            File tempDir = new File(System.getProperty("java.io.tmpdir"), "nginx-auth");
 | 
					        File nginxConf;
 | 
				
			||||||
//            if (!tempDir.exists()) tempDir.mkdirs();
 | 
					        try {
 | 
				
			||||||
//
 | 
					            File tempDir = new File(System.getProperty("java.io.tmpdir"), "nginx-auth");
 | 
				
			||||||
//            nginxConf = new File(tempDir, "nginx.conf");
 | 
					            if (!tempDir.exists()) tempDir.mkdirs();
 | 
				
			||||||
//            try (FileWriter writer = new FileWriter(nginxConf)) {
 | 
					
 | 
				
			||||||
//                writer.write(generateNginxConfig(ollamaPort));
 | 
					            nginxConf = new File(tempDir, "nginx.conf");
 | 
				
			||||||
//            }
 | 
					            try (FileWriter writer = new FileWriter(nginxConf)) {
 | 
				
			||||||
//
 | 
					                writer.write(generateNginxConfig(ollamaPort));
 | 
				
			||||||
//            return new NginxContainer<>(DockerImageName.parse(NGINX_VERSION))
 | 
					            }
 | 
				
			||||||
//                    .withExposedPorts(NGINX_PORT)
 | 
					
 | 
				
			||||||
//                    .withCopyFileToContainer(
 | 
					            return new NginxContainer<>(DockerImageName.parse(NGINX_VERSION))
 | 
				
			||||||
//                            MountableFile.forHostPath(nginxConf.getAbsolutePath()),
 | 
					                    .withExposedPorts(NGINX_PORT)
 | 
				
			||||||
//                            "/etc/nginx/nginx.conf"
 | 
					                    .withCopyFileToContainer(
 | 
				
			||||||
//                    )
 | 
					                            MountableFile.forHostPath(nginxConf.getAbsolutePath()),
 | 
				
			||||||
//                    .withExtraHost("host.docker.internal", "host-gateway")
 | 
					                            "/etc/nginx/nginx.conf"
 | 
				
			||||||
//                    .waitingFor(
 | 
					                    )
 | 
				
			||||||
//                            Wait.forHttp("/")
 | 
					                    .withExtraHost("host.docker.internal", "host-gateway")
 | 
				
			||||||
//                                    .forStatusCode(401)
 | 
					                    .waitingFor(
 | 
				
			||||||
//                                    .withStartupTimeout(Duration.ofSeconds(30))
 | 
					                            Wait.forHttp("/")
 | 
				
			||||||
//                    );
 | 
					                                    .forStatusCode(401)
 | 
				
			||||||
//        } catch (IOException e) {
 | 
					                                    .withStartupTimeout(Duration.ofSeconds(30))
 | 
				
			||||||
//            throw new RuntimeException("Failed to create nginx.conf", e);
 | 
					                    );
 | 
				
			||||||
//        }
 | 
					        } catch (IOException e) {
 | 
				
			||||||
//    }
 | 
					            throw new RuntimeException("Failed to create nginx.conf", e);
 | 
				
			||||||
//
 | 
					        }
 | 
				
			||||||
//    @Test
 | 
					    }
 | 
				
			||||||
//    @Order(1)
 | 
					
 | 
				
			||||||
//    void testOllamaBehindProxy() throws InterruptedException {
 | 
					    @Test
 | 
				
			||||||
//        api.setBearerAuth(BEARER_AUTH_TOKEN);
 | 
					    @Order(1)
 | 
				
			||||||
//        assertTrue(api.ping(), "Expected OllamaAPI to successfully ping through NGINX with valid auth token.");
 | 
					    void testOllamaBehindProxy() throws InterruptedException {
 | 
				
			||||||
//    }
 | 
					        api.setBearerAuth(BEARER_AUTH_TOKEN);
 | 
				
			||||||
//
 | 
					        assertTrue(api.ping(), "Expected OllamaAPI to successfully ping through NGINX with valid auth token.");
 | 
				
			||||||
//    @Test
 | 
					    }
 | 
				
			||||||
//    @Order(1)
 | 
					
 | 
				
			||||||
//    void testWithWrongToken() throws InterruptedException {
 | 
					    @Test
 | 
				
			||||||
//        api.setBearerAuth("wrong-token");
 | 
					    @Order(1)
 | 
				
			||||||
//        assertFalse(api.ping(), "Expected OllamaAPI ping to fail through NGINX with an invalid auth token.");
 | 
					    void testWithWrongToken() throws InterruptedException {
 | 
				
			||||||
//    }
 | 
					        api.setBearerAuth("wrong-token");
 | 
				
			||||||
//
 | 
					        assertFalse(api.ping(), "Expected OllamaAPI ping to fail through NGINX with an invalid auth token.");
 | 
				
			||||||
//    @Test
 | 
					    }
 | 
				
			||||||
//    @Order(2)
 | 
					
 | 
				
			||||||
//    void testAskModelWithStructuredOutput()
 | 
					    @Test
 | 
				
			||||||
//            throws OllamaBaseException, IOException, InterruptedException, URISyntaxException {
 | 
					    @Order(2)
 | 
				
			||||||
//        api.setBearerAuth(BEARER_AUTH_TOKEN);
 | 
					    void testAskModelWithStructuredOutput()
 | 
				
			||||||
//
 | 
					            throws OllamaBaseException, IOException, InterruptedException, URISyntaxException {
 | 
				
			||||||
//        api.pullModel(CHAT_MODEL_LLAMA3);
 | 
					        api.setBearerAuth(BEARER_AUTH_TOKEN);
 | 
				
			||||||
//
 | 
					
 | 
				
			||||||
//        int timeHour = 6;
 | 
					        api.pullModel(GENERAL_PURPOSE_MODEL);
 | 
				
			||||||
//        boolean isNightTime = false;
 | 
					
 | 
				
			||||||
//
 | 
					        String prompt = "The sun is shining brightly and is directly overhead at the zenith, casting my shadow over my foot, so it must be noon.";
 | 
				
			||||||
//        String prompt = "The Sun is shining, and its " + timeHour + ". Its daytime.";
 | 
					
 | 
				
			||||||
//
 | 
					        Map<String, Object> format = new HashMap<>();
 | 
				
			||||||
//        Map<String, Object> format = new HashMap<>();
 | 
					        format.put("type", "object");
 | 
				
			||||||
//        format.put("type", "object");
 | 
					        format.put("properties", new HashMap<String, Object>() {
 | 
				
			||||||
//        format.put("properties", new HashMap<String, Object>() {
 | 
					            {
 | 
				
			||||||
//            {
 | 
					                put("isNoon", new HashMap<String, Object>() {
 | 
				
			||||||
//                put("timeHour", new HashMap<String, Object>() {
 | 
					                    {
 | 
				
			||||||
//                    {
 | 
					                        put("type", "boolean");
 | 
				
			||||||
//                        put("type", "integer");
 | 
					                    }
 | 
				
			||||||
//                    }
 | 
					                });
 | 
				
			||||||
//                });
 | 
					            }
 | 
				
			||||||
//                put("isNightTime", new HashMap<String, Object>() {
 | 
					        });
 | 
				
			||||||
//                    {
 | 
					        format.put("required", List.of("isNoon"));
 | 
				
			||||||
//                        put("type", "boolean");
 | 
					
 | 
				
			||||||
//                    }
 | 
					        OllamaResult result = api.generate(GENERAL_PURPOSE_MODEL, prompt, format);
 | 
				
			||||||
//                });
 | 
					
 | 
				
			||||||
//            }
 | 
					        assertNotNull(result);
 | 
				
			||||||
//        });
 | 
					        assertNotNull(result.getResponse());
 | 
				
			||||||
//        format.put("required", Arrays.asList("timeHour", "isNightTime"));
 | 
					        assertFalse(result.getResponse().isEmpty());
 | 
				
			||||||
//
 | 
					
 | 
				
			||||||
//        OllamaResult result = api.generate(CHAT_MODEL_LLAMA3, prompt, format);
 | 
					        assertEquals(true, result.getStructuredResponse().get("isNoon"));
 | 
				
			||||||
//
 | 
					    }
 | 
				
			||||||
//        assertNotNull(result);
 | 
					}
 | 
				
			||||||
//        assertNotNull(result.getResponse());
 | 
					 | 
				
			||||||
//        assertFalse(result.getResponse().isEmpty());
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
//        assertEquals(timeHour,
 | 
					 | 
				
			||||||
//                result.getStructuredResponse().get("timeHour"));
 | 
					 | 
				
			||||||
//        assertEquals(isNightTime,
 | 
					 | 
				
			||||||
//                result.getStructuredResponse().get("isNightTime"));
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
//        TimeOfDay timeOfDay = result.as(TimeOfDay.class);
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
//        assertEquals(timeHour, timeOfDay.getTimeHour());
 | 
					 | 
				
			||||||
//        assertEquals(isNightTime, timeOfDay.isNightTime());
 | 
					 | 
				
			||||||
//    }
 | 
					 | 
				
			||||||
//}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user