From 74b0094019c70ce1bcf6d3298d7dd87e4ba11f75 Mon Sep 17 00:00:00 2001 From: "daniel.colina" Date: Mon, 10 Jul 2023 14:01:59 +0200 Subject: [PATCH] #22706 Some tests using testcontainers. --- tools/dotcms-cli/api-data-model/pom.xml | 57 ++++++- .../com/dotcms/DockerComposeResource.java | 39 +++++ .../com/dotcms/DockerComposeTestResource.java | 63 ++++++++ .../src/test/java/com/dotcms/DummyTest.java | 141 ++++++++++++++++++ .../java/com/dotcms/api/AssetAPITest.java | 59 ++++++++ .../src/test/resources/docker-compose.yaml | 70 +++++++++ 6 files changed, 424 insertions(+), 5 deletions(-) create mode 100644 tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/DockerComposeResource.java create mode 100644 tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/DockerComposeTestResource.java create mode 100644 tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/DummyTest.java create mode 100644 tools/dotcms-cli/api-data-model/src/test/resources/docker-compose.yaml diff --git a/tools/dotcms-cli/api-data-model/pom.xml b/tools/dotcms-cli/api-data-model/pom.xml index 4a6653c3db35..db3ddfcdb51b 100644 --- a/tools/dotcms-cli/api-data-model/pom.xml +++ b/tools/dotcms-cli/api-data-model/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> dotCMS-cli com.dotcms @@ -22,6 +22,9 @@ io.quarkus.platform 2.9.1.Final 3.0.0-M5 + 5.9.3 + 1.17.6 + @@ -32,6 +35,8 @@ pom import + + @@ -75,6 +80,44 @@ jackson-datatype-guava 2.13.3 + + + + + + + + + + + + + + org.testcontainers + junit-jupiter + ${testcontainers.version} + test + + + org.testcontainers + testcontainers + ${testcontainers.version} + test + + + org.testcontainers + postgresql + ${testcontainers.version} + test + + + + org.testcontainers + elasticsearch + ${testcontainers.version} + test + + io.quarkus quarkus-smallrye-openapi @@ -128,7 +171,8 @@ ${surefire-plugin.version} - org.jboss.logmanager.LogManager + org.jboss.logmanager.LogManager + ${maven.home} @@ -157,8 +201,11 @@ - ${project.build.directory}/${project.build.finalName}-runner - org.jboss.logmanager.LogManager + + ${project.build.directory}/${project.build.finalName}-runner + + org.jboss.logmanager.LogManager + ${maven.home} diff --git a/tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/DockerComposeResource.java b/tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/DockerComposeResource.java new file mode 100644 index 000000000000..b9133f9aa853 --- /dev/null +++ b/tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/DockerComposeResource.java @@ -0,0 +1,39 @@ +//package com.dotcms; +// +//import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; +//import org.testcontainers.containers.DockerComposeContainer; +//import org.testcontainers.containers.wait.strategy.Wait; +// +//import java.io.File; +//import java.time.Duration; +//import java.util.Collections; +//import java.util.Map; +// +//public class DockerComposeResource implements QuarkusTestResourceLifecycleManager { +// +// private static final int POSTGRES_SERVICE_PORT = 5432; +// private static final int ELASTICSEARCH_SERVICE_PORT = 9200; +// private static final int DOTCMS_SERVICE_PORT = 8080; +// private static final int STARTUP_TIMEOUT = 120; +// private static final boolean USE_LOCAL_COMPOSE = false; +// +// private static final DockerComposeContainer DOCKER_COMPOSE_CONTAINER = +// new DockerComposeContainer("dotcms-env", new File("src/test/resources/docker-compose.yaml")) +// .withExposedService("postgres", POSTGRES_SERVICE_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT))) +// .withExposedService("elasticsearch", ELASTICSEARCH_SERVICE_PORT, Wait.forHttp("/").forPort(ELASTICSEARCH_SERVICE_PORT).forStatusCode(200)) +// .withExposedService("dotcms", DOTCMS_SERVICE_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT))) +// .withLocalCompose(USE_LOCAL_COMPOSE); // need to be false to run on macOS +// @Override +// public Map start() { +// DOCKER_COMPOSE_CONTAINER.start(); +// String dotcmsServiceHost = DOCKER_COMPOSE_CONTAINER.getServiceHost("dotcms", DOTCMS_SERVICE_PORT); +// String dotcmsServicePort = DOCKER_COMPOSE_CONTAINER.getServicePort("dotcms", DOTCMS_SERVICE_PORT).toString(); +// +// return Collections.singletonMap("myapp.service.url", "http://" + dotcmsServiceHost + ":" + dotcmsServicePort); +// } +// +// @Override +// public void stop() { +// DOCKER_COMPOSE_CONTAINER.stop(); +// } +//} \ No newline at end of file diff --git a/tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/DockerComposeTestResource.java b/tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/DockerComposeTestResource.java new file mode 100644 index 000000000000..3c31c3e2ea32 --- /dev/null +++ b/tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/DockerComposeTestResource.java @@ -0,0 +1,63 @@ +//package com.dotcms; +// +//import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; +//import org.testcontainers.containers.DockerComposeContainer; +//import org.testcontainers.containers.wait.strategy.Wait; +//import org.testcontainers.lifecycle.Startables; +// +//import java.io.File; +//import java.time.Duration; +//import java.util.Collections; +//import java.util.Map; +//import java.util.stream.Stream; +// +//public class DockerComposeTestResource implements QuarkusTestResourceLifecycleManager { +// +// private static final DockerComposeContainer composeContainer = new DockerComposeContainer<>(new File("src/test/resources/docker-compose.yaml")) +// .withLocalCompose(true) +// +// .withServices("postgres"); +// +// static { +// Startables.deepStart(Stream.of(composeContainer)).join(); +// } +// @Override +// public Map start() { +// return Map.of( +// "quarkus.datasource.jdbc.url", composeContainer.getServiceHost("postgres", 5432) +// + ":" + composeContainer.getServicePort("postgres", 5432), +// "quarkus.datasource.username", "your_username", +// "quarkus.datasource.password", "your_password" +// ); +// } +// +// @Override +// public void stop() { +// // No explicit stop needed as Testcontainers manages the lifecycle +// } +// +// +//// private static final int SERVICE_PORT = 5432; +//// private static final int STARTUP_TIMEOUT = 60; +//// private static final DockerComposeContainer DOCKER_COMPOSE_CONTAINER = +//// new DockerComposeContainer<>(new File("src/test/resources/docker-compose.yaml")) +//// .withExposedService( +//// "dotcms-env", +//// SERVICE_PORT, +//// Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT))) +//// .withLocalCompose(true); +//// +//// @Override +//// public Map start() { +//// DOCKER_COMPOSE_CONTAINER.start(); +//// String dotcmsServiceHost = DOCKER_COMPOSE_CONTAINER.getServiceHost("dotcms-env", SERVICE_PORT); +//// String dotcmsServicePort = DOCKER_COMPOSE_CONTAINER.getServicePort("dotcms-env", SERVICE_PORT).toString(); +//// +//// return Collections.singletonMap("dotcms.service.url", "http://" + dotcmsServiceHost + ":" + dotcmsServicePort); +//// } +//// +//// @Override +//// public void stop() { +//// DOCKER_COMPOSE_CONTAINER.stop(); +//// } +//} diff --git a/tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/DummyTest.java b/tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/DummyTest.java new file mode 100644 index 000000000000..7b8ac58af7a5 --- /dev/null +++ b/tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/DummyTest.java @@ -0,0 +1,141 @@ +package com.dotcms; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.DockerComposeContainer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.junit.jupiter.Testcontainers; + +import java.io.File; +import java.time.Duration; + +@Testcontainers +public class DummyTest { + + private static final int POSTGRES_SERVICE_PORT = 5432; + private static final int ELASTICSEARCH_SERVICE_PORT = 9200; + private static final int DOTCMS_SERVICE_PORT = 8080; + private static final int STARTUP_TIMEOUT = 120; + + private static final DockerComposeContainer COMPOSE_CONTAINER = + new DockerComposeContainer("dotcms-env", new File("src/test/resources/docker-compose.yaml")) + .withExposedService("postgres", POSTGRES_SERVICE_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT))) + .withExposedService("elasticsearch", ELASTICSEARCH_SERVICE_PORT, Wait.forHttp("/").forPort(ELASTICSEARCH_SERVICE_PORT).forStatusCode(200)) + .withExposedService("dotcms", DOTCMS_SERVICE_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT))) + .withLocalCompose(false); // need to be false to run on macOS + + static { + COMPOSE_CONTAINER.start(); +// Stream.of(GENERIC_CONTAINER, COMPOSE_CONTAINER).parallel().forEach(Startable::start); +// Startables.deepStart(Stream.of(GENERIC_CONTAINER, COMPOSE_CONTAINER)).join(); + } + + @BeforeAll + public static void beforeAll() { + System.out.println("beforeAll"); + + System.out.println("Postgres address: " + COMPOSE_CONTAINER.getServiceHost("postgres", POSTGRES_SERVICE_PORT)); + System.out.println("Postgres port: " + COMPOSE_CONTAINER.getServicePort("postgres", POSTGRES_SERVICE_PORT)); + + System.out.println("Elasticsearch address: " + COMPOSE_CONTAINER.getServiceHost("elasticsearch", ELASTICSEARCH_SERVICE_PORT)); + System.out.println("Elasticsearch port: " + COMPOSE_CONTAINER.getServicePort("elasticsearch", ELASTICSEARCH_SERVICE_PORT)); + + System.out.println("DotCMS address: " + COMPOSE_CONTAINER.getServiceHost("dotcms", DOTCMS_SERVICE_PORT)); + System.out.println("DotCMS port: " + COMPOSE_CONTAINER.getServicePort("dotcms", DOTCMS_SERVICE_PORT)); + + } + + @Test + public void myTest() { + // Your test logic here + System.out.println("Hello World"); + } + + +// private static final Network NETWORK = Network.newNetwork(); +// +// @Container +// private static final ElasticsearchContainer ELASTICSEARCH_CONTAINER = +// new ElasticsearchContainer(DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.10.2")) +// .withEnv("discovery.type", "single-node") +// .withEnv("cluster.name", "elastic-cluster") +// .withEnv("bootstrap.memory_lock", "true") +// .withEnv("ES_JAVA_OPTS", "-Xms512m -Xmx1G") +// .withExposedPorts(ELASTICSEARCH_SERVICE_PORT, 9600) +// .waitingFor(Wait.forHttp("/").forPort(ELASTICSEARCH_SERVICE_PORT).forStatusCode(200)) +// .withNetwork(NETWORK) +// .withLogConsumer(new Slf4jLogConsumer(org.slf4j.LoggerFactory.getLogger("elasticsearch"))) +// .withReuse(true); + + + + + +// @Container +// public static final DockerComposeContainer dotCmsEnv = new DockerComposeContainer(new File("src/test/resources/docker-compose.yaml")) +// .withExposedService("dotcms", POSTGRES_SERVICE_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT))) +// .withLocalCompose(true); + +// @Container +// private static final DockerComposeContainer dotCmsEnv = new DockerComposeContainer(new File("src/test/resources/docker-compose.yaml")) +// .withExposedService("dotcms", 8080, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(60))) +// .withLocalCompose(true); + +// @Container +// private static final PostgreSQLContainer POSTGRES_CONTAINER = +// new PostgreSQLContainer<>(DockerImageName.parse("postgres:15")) +//// .withDatabaseName("dotcms") +//// .withUsername("dotcms") +//// .withPassword("dotcms") +// .withEnv("POSTGRES_USER", "dotcms") +// .withEnv("POSTGRES_PASSWORD", "dotcms") +// .withEnv("POSTGRES_DB", "dotcms") +// .withCommand("postgres -c 'max_connections=400' -c 'shared_buffers=128MB'") +// .withExposedPorts(POSTGRES_SERVICE_PORT) +// .waitingFor(Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT))) +// .withNetwork(NETWORK) +// .withReuse(true); + + +// @BeforeAll +// public static void setup() { +// +//// System.out.println("Postgres address: " + POSTGRES_CONTAINER.getHost()); +//// System.out.println("Postgres port: " + POSTGRES_CONTAINER.getFirstMappedPort()); +//// +//// System.out.println("Elasticsearch address: " + ELASTICSEARCH_CONTAINER.getHost()); +//// System.out.println("Elasticsearch port: " + ELASTICSEARCH_CONTAINER.getFirstMappedPort()); +//// +//// GenericContainer DOTCMS_CONTAINER = new GenericContainer<>(DockerImageName.parse("dotcms/dotcms:latest")) +//// .withEnv("CMS_JAVA_OPTS", "-Xmx1G") +//// .withEnv("TZ", "UTC") +//// .withEnv("DB_BASE_URL", "jdbc:postgresql://" + POSTGRES_CONTAINER.getHost() + ":5432/dotcms") +////// .withEnv("DB_BASE_URL", "jdbc:postgresql://" + POSTGRES_CONTAINER.getHost() + ":" + POSTGRES_CONTAINER.getFirstMappedPort() + "/dotcms") +//// .withEnv("DB_USERNAME", "dotcms") +//// .withEnv("DB_PASSWORD", "dotcms") +//// .withEnv("DOT_ES_AUTH_BASIC_PASSWORD", "admin") +//// .withEnv("DOT_INITIAL_ADMIN_PASSWORD", "admin") +//// .withEnv("DOT_ES_ENDPOINTS", "https://" + ELASTICSEARCH_CONTAINER.getHost() + ":9200") +////// .withEnv("DOT_ES_ENDPOINTS", "https://" + ELASTICSEARCH_CONTAINER.getHost() + ":" + ELASTICSEARCH_CONTAINER.getFirstMappedPort()) +////// .withEnv("CUSTOM_STARTER_URL", "https://repo.dotcms.com/artifactory/libs-release-local/com/dotcms/starter/20211201/starter-20211201.zip") +//// .withExposedPorts(8080, 8443) +//// .waitingFor(Wait.forHttp("/dotAdmin").forPort(8080).forStatusCode(200)) +//// .dependsOn(POSTGRES_CONTAINER, ELASTICSEARCH_CONTAINER) +//// .withNetwork(NETWORK) +//// .withLogConsumer(logger -> System.out.print(logger.getUtf8String())) +//// .withReuse(true); +//// +//// DOTCMS_CONTAINER.start(); +// +//// System.out.println("DotCMS address: " + DOTCMS_CONTAINER.getHost()); +//// System.out.println("DotCMS port: " + DOTCMS_CONTAINER.getFirstMappedPort()); +// +// System.out.println("DummyTest.setup"); +// +// } + +// @Test +// public void should_launch_containers_test() { +// System.out.println("DummyTest.test"); +// } +} diff --git a/tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/api/AssetAPITest.java b/tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/api/AssetAPITest.java index 1d3cd303fbb9..6a3d00667d95 100644 --- a/tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/api/AssetAPITest.java +++ b/tools/dotcms-cli/api-data-model/src/test/java/com/dotcms/api/AssetAPITest.java @@ -1,5 +1,6 @@ package com.dotcms.api; +//import com.dotcms.DockerComposeResource; import com.dotcms.api.client.RestClientFactory; import com.dotcms.api.client.ServiceManager; import com.dotcms.model.ResponseEntityView; @@ -8,22 +9,67 @@ import com.dotcms.model.asset.SearchByPathRequest; import com.dotcms.model.config.ServiceBean; import com.google.common.collect.ImmutableList; +import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; import org.apache.commons.lang3.RandomStringUtils; import org.eclipse.microprofile.config.inject.ConfigProperty; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.testcontainers.containers.DockerComposeContainer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; import javax.inject.Inject; import javax.ws.rs.NotFoundException; +import java.io.File; import java.io.IOException; +import java.time.Duration; import java.util.List; import java.util.Map; + +//@Testcontainers @QuarkusTest class AssetAPITest { + private static final int POSTGRES_SERVICE_PORT = 5432; + private static final int ELASTICSEARCH_SERVICE_PORT = 9200; + private static final int DOTCMS_SERVICE_PORT = 8080; + private static final int STARTUP_TIMEOUT = 120; + private static final boolean LOCAL_COMPOSE = false; + + private static DockerComposeContainer composeContainer; +// +// @Container +// private static final DockerComposeContainer COMPOSE_CONTAINER = +// new DockerComposeContainer(new File("src/test/resources/docker-compose.yaml")) +// .withExposedService("postgres", POSTGRES_SERVICE_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT))) +// .withExposedService("elasticsearch", ELASTICSEARCH_SERVICE_PORT, Wait.forHttp("/").forPort(ELASTICSEARCH_SERVICE_PORT).forStatusCode(200)) +// .withExposedService("dotcms", DOTCMS_SERVICE_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT))) +// .withLocalCompose(LOCAL_COMPOSE); // need to be false to run on macOS +// +// static { +// COMPOSE_CONTAINER.start(); +// } +// +// @BeforeAll +// public static void beforeAll() { +// System.out.println("beforeAll"); +// +// System.out.println("Postgres address: " + COMPOSE_CONTAINER.getServiceHost("postgres", POSTGRES_SERVICE_PORT)); +// System.out.println("Postgres port: " + COMPOSE_CONTAINER.getServicePort("postgres", POSTGRES_SERVICE_PORT)); +// +// System.out.println("Elasticsearch address: " + COMPOSE_CONTAINER.getServiceHost("elasticsearch", ELASTICSEARCH_SERVICE_PORT)); +// System.out.println("Elasticsearch port: " + COMPOSE_CONTAINER.getServicePort("elasticsearch", ELASTICSEARCH_SERVICE_PORT)); +// +// System.out.println("DotCMS address: " + COMPOSE_CONTAINER.getServiceHost("dotcms", DOTCMS_SERVICE_PORT)); +// System.out.println("DotCMS port: " + COMPOSE_CONTAINER.getServicePort("dotcms", DOTCMS_SERVICE_PORT)); +// +// } + @ConfigProperty(name = "com.dotcms.starter.site", defaultValue = "default") String siteName; @@ -36,6 +82,19 @@ class AssetAPITest { @Inject ServiceManager serviceManager; + @BeforeAll + public static void beforeAll() { + System.out.println("beforeAll"); + + composeContainer = + new DockerComposeContainer(new File("src/test/resources/docker-compose.yaml")) + .withExposedService("postgres", POSTGRES_SERVICE_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT))) + .withExposedService("elasticsearch", ELASTICSEARCH_SERVICE_PORT, Wait.forHttp("/").forPort(ELASTICSEARCH_SERVICE_PORT).forStatusCode(200)) + .withExposedService("dotcms", DOTCMS_SERVICE_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT))) + .withLocalCompose(LOCAL_COMPOSE); // need to be false to run on macOS + composeContainer.start(); + } + @BeforeEach public void setupTest() throws IOException { serviceManager.removeAll().persist( diff --git a/tools/dotcms-cli/api-data-model/src/test/resources/docker-compose.yaml b/tools/dotcms-cli/api-data-model/src/test/resources/docker-compose.yaml new file mode 100644 index 000000000000..355ac845a9db --- /dev/null +++ b/tools/dotcms-cli/api-data-model/src/test/resources/docker-compose.yaml @@ -0,0 +1,70 @@ +version: '3' +services: + postgres: + image: postgres:15 + ports: + - "5432:5432" + environment: + - POSTGRES_USER=dotcms + - POSTGRES_PASSWORD=dotcms + - POSTGRES_DB=dotcms + command: postgres -c 'max_connections=400' -c 'shared_buffers=128MB' + volumes: + - postgres-data:/var/lib/postgresql/data + networks: + - dotcms-net + + elasticsearch: + image: docker.elastic.co/elasticsearch/elasticsearch:7.15.1 + ports: + - "9200:9200" + environment: + - discovery.type=single-node + - cluster.name=elastic-cluster + - bootstrap.memory_lock=true + - "ES_JAVA_OPTS=-Xms512m -Xmx1g" + ulimits: + memlock: + soft: -1 # Set memlock to unlimited (no soft or hard limit) + hard: -1 + nofile: + soft: 65536 # Maximum number of open files for the opensearch user - set to at least 65536 + hard: 65536 + volumes: + - elasticsearch-data:/usr/share/elasticsearch/data + networks: + - dotcms-net + + dotcms: + image: dotcms/dotcms:latest + environment: + CMS_JAVA_OPTS: '-Xmx1g ' + LANG: 'C.UTF-8' + TZ: 'UTC' + DB_BASE_URL: "jdbc:postgresql://postgres/dotcms" + DB_USERNAME: "dotcms" + DB_PASSWORD: "dotcms" + DOT_ES_AUTH_BASIC_PASSWORD: 'admin' + DOT_INITIAL_ADMIN_PASSWORD: 'admin' + DOT_ES_ENDPOINTS: 'http://elasticsearch:9200' + #CUSTOM_STARTER_URL: 'https://repo.dotcms.com/artifactory/libs-release-local/com/dotcms/starter/20211201/starter-20211201.zip' + depends_on: + - elasticsearch + - postgres + volumes: + - dotcms-shared:/data/shared + #- {license_local_path}/license.zip:/data/shared/assets/license.zip + networks: + - dotcms-net + ports: + - "8080:8080" + - "8443:8443" + +volumes: + postgres-data: + elasticsearch-data: + dotcms-shared: + +networks: + dotcms-net: +