From 57c8b46dc45e3d19eb439c2124ea8ed11762b331 Mon Sep 17 00:00:00 2001 From: Ivan Vakhrushev Date: Thu, 28 Nov 2024 23:50:50 +0400 Subject: [PATCH] Add demo app with Testcontainers URL --- settings.gradle.kts | 2 + .../console-demo-app/build.gradle.kts | 2 +- .../h2-demo-app/build.gradle.kts | 2 +- .../kotlin-demo-app/build.gradle.kts | 2 +- .../postgres-demo-app/build.gradle.kts | 2 +- .../postgres-tc-url-demo-app/build.gradle.kts | 21 ++++++ ...tgresTestcontainersUrlDemoApplication.java | 25 +++++++ .../src/main/resources/application.yaml | 8 +++ ...stcontainersUrlDemoApplicationRunTest.java | 33 +++++++++ ...sTestcontainersUrlDemoApplicationTest.java | 72 +++++++++++++++++++ 10 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 spring-boot-integration/postgres-tc-url-demo-app/build.gradle.kts create mode 100644 spring-boot-integration/postgres-tc-url-demo-app/src/main/java/io/github/mfvanek/pg/spring/postgres/tc/url/PostgresTestcontainersUrlDemoApplication.java create mode 100644 spring-boot-integration/postgres-tc-url-demo-app/src/main/resources/application.yaml create mode 100644 spring-boot-integration/postgres-tc-url-demo-app/src/test/java/io/github/mfvanek/pg/spring/postgres/tc/url/PostgresTestcontainersUrlDemoApplicationRunTest.java create mode 100644 spring-boot-integration/postgres-tc-url-demo-app/src/test/java/io/github/mfvanek/pg/spring/postgres/tc/url/PostgresTestcontainersUrlDemoApplicationTest.java diff --git a/settings.gradle.kts b/settings.gradle.kts index 75dab1d4..3eb510fe 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -18,3 +18,5 @@ findProject(":spring-boot-integration:kotlin-demo-app")?.name = "kotlin-demo-app include("pg-index-health-bom") include("pg-index-health-logger") include("pg-index-health-core") +include("spring-boot-integration:postgres-tc-url-demo-app") +findProject(":spring-boot-integration:postgres-tc-url-demo-app")?.name = "postgres-tc-url-demo-app" diff --git a/spring-boot-integration/console-demo-app/build.gradle.kts b/spring-boot-integration/console-demo-app/build.gradle.kts index 1810954a..ad50acbe 100644 --- a/spring-boot-integration/console-demo-app/build.gradle.kts +++ b/spring-boot-integration/console-demo-app/build.gradle.kts @@ -10,8 +10,8 @@ ext["assertj.version"] = libs.versions.assertj.get() ext["junit-jupiter.version"] = libs.versions.junit.get() dependencies { - implementation(project(":spring-boot-integration:pg-index-health-test-starter")) implementation(libs.spring.boot.starter.root) testImplementation(libs.spring.boot.starter.test) + testImplementation(project(":spring-boot-integration:pg-index-health-test-starter")) } diff --git a/spring-boot-integration/h2-demo-app/build.gradle.kts b/spring-boot-integration/h2-demo-app/build.gradle.kts index 8e279adf..fe6e25cf 100644 --- a/spring-boot-integration/h2-demo-app/build.gradle.kts +++ b/spring-boot-integration/h2-demo-app/build.gradle.kts @@ -10,10 +10,10 @@ ext["assertj.version"] = libs.versions.assertj.get() ext["junit-jupiter.version"] = libs.versions.junit.get() dependencies { - implementation(project(":spring-boot-integration:pg-index-health-test-starter")) implementation(libs.spring.boot.starter.data.jdbc) runtimeOnly("com.h2database:h2") testImplementation(libs.spring.boot.starter.test) + testImplementation(project(":spring-boot-integration:pg-index-health-test-starter")) } diff --git a/spring-boot-integration/kotlin-demo-app/build.gradle.kts b/spring-boot-integration/kotlin-demo-app/build.gradle.kts index 82301387..70b1f477 100644 --- a/spring-boot-integration/kotlin-demo-app/build.gradle.kts +++ b/spring-boot-integration/kotlin-demo-app/build.gradle.kts @@ -19,7 +19,6 @@ ext["junit-jupiter.version"] = libs.versions.junit.get() dependencies { implementation("org.jetbrains.kotlin:kotlin-reflect") - implementation(project(":spring-boot-integration:pg-index-health-test-starter")) implementation(project(":pg-index-health-testing")) implementation(libs.spring.boot.starter.data.jdbc) implementation(platform(libs.testcontainers.bom)) @@ -28,6 +27,7 @@ dependencies { runtimeOnly(libs.postgresql) testImplementation(libs.spring.boot.starter.test) + testImplementation(project(":spring-boot-integration:pg-index-health-test-starter")) detektPlugins(libs.detekt.formatting) detektPlugins(libs.detekt.libraries) diff --git a/spring-boot-integration/postgres-demo-app/build.gradle.kts b/spring-boot-integration/postgres-demo-app/build.gradle.kts index 2bef5d93..b36dd62f 100644 --- a/spring-boot-integration/postgres-demo-app/build.gradle.kts +++ b/spring-boot-integration/postgres-demo-app/build.gradle.kts @@ -11,7 +11,6 @@ ext["assertj.version"] = libs.versions.assertj.get() ext["junit-jupiter.version"] = libs.versions.junit.get() dependencies { - implementation(project(":spring-boot-integration:pg-index-health-test-starter")) implementation(project(":pg-index-health-testing")) implementation(libs.spring.boot.starter.data.jdbc) implementation(platform(libs.testcontainers.bom)) @@ -20,6 +19,7 @@ dependencies { runtimeOnly(libs.postgresql) testImplementation(libs.spring.boot.starter.test) + testImplementation(project(":spring-boot-integration:pg-index-health-test-starter")) } lombok { diff --git a/spring-boot-integration/postgres-tc-url-demo-app/build.gradle.kts b/spring-boot-integration/postgres-tc-url-demo-app/build.gradle.kts new file mode 100644 index 00000000..b5653a12 --- /dev/null +++ b/spring-boot-integration/postgres-tc-url-demo-app/build.gradle.kts @@ -0,0 +1,21 @@ +plugins { + id("pg-index-health.java-application") + alias(libs.plugins.spring.boot.gradlePlugin) + alias(libs.plugins.spring.dependency.management) +} + +ext["commons-lang3.version"] = libs.versions.commons.lang3.get() +ext["assertj.version"] = libs.versions.assertj.get() +// ext["mockito.version"] = libs.versions.mockito.get() +ext["junit-jupiter.version"] = libs.versions.junit.get() + +dependencies { + implementation(platform(libs.testcontainers.bom)) + implementation("org.testcontainers:postgresql") + implementation(libs.spring.boot.starter.data.jdbc) + + runtimeOnly(libs.postgresql) + + testImplementation(libs.spring.boot.starter.test) + testImplementation(project(":spring-boot-integration:pg-index-health-test-starter")) +} diff --git a/spring-boot-integration/postgres-tc-url-demo-app/src/main/java/io/github/mfvanek/pg/spring/postgres/tc/url/PostgresTestcontainersUrlDemoApplication.java b/spring-boot-integration/postgres-tc-url-demo-app/src/main/java/io/github/mfvanek/pg/spring/postgres/tc/url/PostgresTestcontainersUrlDemoApplication.java new file mode 100644 index 00000000..8b4c78c7 --- /dev/null +++ b/spring-boot-integration/postgres-tc-url-demo-app/src/main/java/io/github/mfvanek/pg/spring/postgres/tc/url/PostgresTestcontainersUrlDemoApplication.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2019-2024. Ivan Vakhrushev and others. + * https://github.com/mfvanek/pg-index-health + * + * This file is a part of "pg-index-health" - a Java library for + * analyzing and maintaining indexes health in PostgreSQL databases. + * + * Licensed under the Apache License 2.0 + */ + +package io.github.mfvanek.pg.spring.postgres.tc.url; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class PostgresTestcontainersUrlDemoApplication { + + /** + * Demo application with PostgreSQL datasource. + */ + public static void main(final String[] args) { + SpringApplication.run(PostgresTestcontainersUrlDemoApplication.class, args); + } +} diff --git a/spring-boot-integration/postgres-tc-url-demo-app/src/main/resources/application.yaml b/spring-boot-integration/postgres-tc-url-demo-app/src/main/resources/application.yaml new file mode 100644 index 00000000..a38b0930 --- /dev/null +++ b/spring-boot-integration/postgres-tc-url-demo-app/src/main/resources/application.yaml @@ -0,0 +1,8 @@ +debug: false + +spring: + main: + banner-mode: off + datasource: + url: jdbc:tc:postgresql:17.2:///demo_for_pg_index_health_starter + driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver diff --git a/spring-boot-integration/postgres-tc-url-demo-app/src/test/java/io/github/mfvanek/pg/spring/postgres/tc/url/PostgresTestcontainersUrlDemoApplicationRunTest.java b/spring-boot-integration/postgres-tc-url-demo-app/src/test/java/io/github/mfvanek/pg/spring/postgres/tc/url/PostgresTestcontainersUrlDemoApplicationRunTest.java new file mode 100644 index 00000000..a99771a4 --- /dev/null +++ b/spring-boot-integration/postgres-tc-url-demo-app/src/test/java/io/github/mfvanek/pg/spring/postgres/tc/url/PostgresTestcontainersUrlDemoApplicationRunTest.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019-2024. Ivan Vakhrushev and others. + * https://github.com/mfvanek/pg-index-health + * + * This file is a part of "pg-index-health" - a Java library for + * analyzing and maintaining indexes health in PostgreSQL databases. + * + * Licensed under the Apache License 2.0 + */ + +package io.github.mfvanek.pg.spring.postgres.tc.url; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; + +@ExtendWith(OutputCaptureExtension.class) +class PostgresTestcontainersUrlDemoApplicationRunTest { + + @Test + void applicationShouldRun(final CapturedOutput output) { + assertThatCode(() -> PostgresTestcontainersUrlDemoApplication.main(new String[]{})) + .doesNotThrowAnyException(); + assertThat(output.getAll()) + .contains("Starting PostgresTestcontainersUrlDemoApplication using Java") + .contains("Container is started (JDBC URL: jdbc:postgresql://localhost:") + .contains("Started PostgresTestcontainersUrlDemoApplication in"); + } +} diff --git a/spring-boot-integration/postgres-tc-url-demo-app/src/test/java/io/github/mfvanek/pg/spring/postgres/tc/url/PostgresTestcontainersUrlDemoApplicationTest.java b/spring-boot-integration/postgres-tc-url-demo-app/src/test/java/io/github/mfvanek/pg/spring/postgres/tc/url/PostgresTestcontainersUrlDemoApplicationTest.java new file mode 100644 index 00000000..f3d18345 --- /dev/null +++ b/spring-boot-integration/postgres-tc-url-demo-app/src/test/java/io/github/mfvanek/pg/spring/postgres/tc/url/PostgresTestcontainersUrlDemoApplicationTest.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2019-2024. Ivan Vakhrushev and others. + * https://github.com/mfvanek/pg-index-health + * + * This file is a part of "pg-index-health" - a Java library for + * analyzing and maintaining indexes health in PostgreSQL databases. + * + * Licensed under the Apache License 2.0 + */ + +package io.github.mfvanek.pg.spring.postgres.tc.url; + +import com.zaxxer.hikari.HikariDataSource; +import io.github.mfvanek.pg.connection.PgConnection; +import io.github.mfvanek.pg.core.checks.common.DatabaseCheckOnHost; +import io.github.mfvanek.pg.core.checks.common.Diagnostic; +import io.github.mfvanek.pg.model.dbobject.DbObject; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ActiveProfiles; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +@ActiveProfiles("test") +@SpringBootTest +class PostgresTestcontainersUrlDemoApplicationTest { + + @Autowired + private ApplicationContext applicationContext; + + @Autowired + private Environment environment; + + @Autowired + private List> checks; + + @Test + void contextLoadsAndDoesNotContainPgIndexHealthBeans() { + assertThat(applicationContext.getBean("dataSource")) + .isInstanceOf(HikariDataSource.class); + + assertThat(applicationContext.getBean("pgConnection")) + .isInstanceOf(PgConnection.class); + + assertThat(environment.getProperty("spring.datasource.url")) + .isNotBlank() + .isEqualTo("jdbc:tc:postgresql:17.2:///demo_for_pg_index_health_starter"); + } + + @Test + void checksShouldWork() { + assertThat(checks) + .hasSameSizeAs(Diagnostic.values()); + + checks.stream() + .filter(DatabaseCheckOnHost::isStatic) + .forEach(c -> { + assertThat(c.check()) + .as(c.getDiagnostic().name()) + .isEmpty(); + + assertThat(c.getHost().getPgUrl()) + .startsWith("jdbc:postgresql://localhost:") + .endsWith("/test?loggerLevel=OFF"); + }); + } +}