diff --git a/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/ApplicationTests.kt b/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/ApplicationTests.kt index 3f6470e5f..96d57ef70 100644 --- a/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/ApplicationTests.kt +++ b/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/ApplicationTests.kt @@ -19,6 +19,9 @@ package com.catenax.bpdm.bridge.dummy +import com.catenax.bpdm.bridge.dummy.util.BpdmGateContextInitializer +import com.catenax.bpdm.bridge.dummy.util.BpdmPoolContextInitializer +import com.catenax.bpdm.bridge.dummy.util.OpenSearchContextInitializer import com.catenax.bpdm.bridge.dummy.util.PostgreSQLContextInitializer import org.junit.jupiter.api.Test import org.springframework.boot.test.context.SpringBootTest @@ -27,11 +30,12 @@ import org.springframework.test.context.ContextConfiguration @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") -@ContextConfiguration(initializers = [PostgreSQLContextInitializer::class]) +@ContextConfiguration(initializers = [PostgreSQLContextInitializer::class, OpenSearchContextInitializer::class, BpdmPoolContextInitializer::class, BpdmGateContextInitializer::class]) class ApplicationTests { @Test fun contextLoads() { + } } diff --git a/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/util/BpdmGateContextInitializer.kt b/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/util/BpdmGateContextInitializer.kt new file mode 100644 index 000000000..050fa2438 --- /dev/null +++ b/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/util/BpdmGateContextInitializer.kt @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ******************************************************************************/ + +package com.catenax.bpdm.bridge.dummy.util + + +import com.catenax.bpdm.bridge.dummy.util.BpdmPoolContextInitializer.Companion.bpdmPoolContainer +import com.catenax.bpdm.bridge.dummy.util.OpenSearchContextInitializer.Companion.openSearchContainer +import com.catenax.bpdm.bridge.dummy.util.PostgreSQLContextInitializer.Companion.postgreSQLContainer +import org.springframework.boot.test.util.TestPropertyValues +import org.springframework.context.ApplicationContextInitializer +import org.springframework.context.ConfigurableApplicationContext +import org.testcontainers.containers.GenericContainer +import org.testcontainers.lifecycle.Startable + +/** + * When used on a spring boot test, starts a singleton postgres db container that is shared between all integration tests. + */ + + +class BpdmGateContextInitializer : ApplicationContextInitializer { + + companion object { + const val BPDM_PORT = 8081 + + private val bpdmGateContainer: GenericContainer<*> = + GenericContainer("ghcr.io/catenax-ng/tx-bpdm/gate:4.0.0-alpha.4") + .dependsOn(listOf(postgreSQLContainer, openSearchContainer, bpdmPoolContainer)) + .withNetwork(postgreSQLContainer.getNetwork()) + .withExposedPorts(BPDM_PORT) + + + } + + + override fun initialize(applicationContext: ConfigurableApplicationContext) { + val postgresNetworkAlias = applicationContext.environment.getProperty("bpdm.datasource.alias") + val bpdmPoolAlias = applicationContext.environment.getProperty("bpdm.pool.alias") + val dataBase = postgreSQLContainer.getDatabaseName() + bpdmGateContainer.withEnv( + "spring.datasource.url", "jdbc:postgresql://${postgresNetworkAlias}:5432/${dataBase}?loggerLevel=OFF" + ) + .withEnv("bpdm.pool.base-url", "http://$bpdmPoolAlias:8080/api/catena") + + .withEnv( + "spring.datasource.username", postgreSQLContainer.username + ) + .withEnv( + "spring.datasource.password", postgreSQLContainer.password + ).start() + + + TestPropertyValues.of( + "bpdm.gate.base-url=http://localhost:${bpdmGateContainer.getMappedPort(BPDM_PORT)}", + ).applyTo(applicationContext.environment) + + } +} \ No newline at end of file diff --git a/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/util/BpdmPoolContextInitializer.kt b/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/util/BpdmPoolContextInitializer.kt new file mode 100644 index 000000000..302199798 --- /dev/null +++ b/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/util/BpdmPoolContextInitializer.kt @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ******************************************************************************/ + +package com.catenax.bpdm.bridge.dummy.util + + +import com.catenax.bpdm.bridge.dummy.util.OpenSearchContextInitializer.Companion.openSearchContainer +import com.catenax.bpdm.bridge.dummy.util.PostgreSQLContextInitializer.Companion.postgreSQLContainer +import org.springframework.boot.test.util.TestPropertyValues +import org.springframework.context.ApplicationContextInitializer +import org.springframework.context.ConfigurableApplicationContext +import org.testcontainers.containers.GenericContainer +import org.testcontainers.lifecycle.Startable + +/** + * When used on a spring boot test, starts a singleton postgres db container that is shared between all integration tests. + */ + + +class BpdmPoolContextInitializer : ApplicationContextInitializer { + + companion object { + const val BPDM_PORT = 8080 + + val bpdmPoolContainer: GenericContainer<*> = + GenericContainer("ghcr.io/catenax-ng/tx-bpdm/pool:4.0.0-alpha.4") + .dependsOn(listOf(postgreSQLContainer, openSearchContainer)) + .withNetwork(postgreSQLContainer.getNetwork()) + .withExposedPorts(BPDM_PORT) + + + } + + + override fun initialize(applicationContext: ConfigurableApplicationContext) { + val postgresNetworkAlias = applicationContext.environment.getProperty("bpdm.datasource.alias") + val openSearchNetworkAlias = applicationContext.environment.getProperty("bpdm.opensearch.alias") + val dataBase = postgreSQLContainer.getDatabaseName() + val bpdmAlias = applicationContext.environment.getProperty("bpdm.pool.alias") + bpdmPoolContainer.withNetworkAliases(bpdmAlias) + + bpdmPoolContainer.withEnv( + "spring.datasource.url", "jdbc:postgresql://${postgresNetworkAlias}:5432/${dataBase}?loggerLevel=OFF" + ) + .withEnv("bpdm.opensearch.host", openSearchNetworkAlias) + .withEnv( + "pdm.opensearch.port", + OpenSearchContextInitializer.OPENSEARCH_PORT.toString() + ) + .withEnv("bpdm.opensearch.scheme", "http") + .withEnv( + "spring.datasource.username", postgreSQLContainer.username + ) + .withEnv( + "spring.datasource.password", postgreSQLContainer.password + ).start() + + TestPropertyValues.of( + "bpdm.pool.base-url=http://localhost:${bpdmPoolContainer.getMappedPort(8080)}", + ).applyTo(applicationContext.environment) + + } +} \ No newline at end of file diff --git a/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/util/OpenSearchContextInitializer.kt b/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/util/OpenSearchContextInitializer.kt new file mode 100644 index 000000000..a998b8e15 --- /dev/null +++ b/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/util/OpenSearchContextInitializer.kt @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ******************************************************************************/ + +package com.catenax.bpdm.bridge.dummy.util + + +import com.catenax.bpdm.bridge.dummy.util.PostgreSQLContextInitializer.Companion.postgreSQLContainer +import com.github.dockerjava.api.model.Ulimit +import org.springframework.boot.test.util.TestPropertyValues +import org.springframework.context.ApplicationContextInitializer +import org.springframework.context.ConfigurableApplicationContext +import org.testcontainers.containers.GenericContainer +import org.testcontainers.containers.wait.strategy.HttpWaitStrategy +import org.testcontainers.lifecycle.Startable + +/** + * When used on a spring boot test, starts a singleton opensearch container that is shared between all integration tests. + */ + +class OpenSearchContextInitializer : ApplicationContextInitializer { + companion object { + const val OPENSEARCH_PORT = 9200 + val openSearchContainer: GenericContainer<*> = GenericContainer("opensearchproject/opensearch:2.1.0") + .withExposedPorts(OPENSEARCH_PORT) + .waitingFor(HttpWaitStrategy() + .forPort(OPENSEARCH_PORT) + .forStatusCodeMatching { response -> response == 200 || response == 401 } + ) + // based on sample docker-compose for development from https://opensearch.org/docs/latest/opensearch/install/docker + .withEnv("cluster.name", "cdqbridge") + .withEnv("node.name", "bpdm-opensearch") + .withEnv("bootstrap.memory_lock", "true") + .withEnv("OPENSEARCH_JAVA_OPTS", "-Xms512m -Xmx512m") + .withEnv("DISABLE_INSTALL_DEMO_CONFIG", "true") + .withEnv("DISABLE_SECURITY_PLUGIN", "true") + .withEnv("discovery.type", "single-node") + .withCreateContainerCmdModifier { cmd -> + cmd.hostConfig!!.withUlimits(arrayOf(Ulimit("nofile", 65536L, 65536L), Ulimit("memlock", -1L, -1L))) + } + .withNetwork(postgreSQLContainer.getNetwork()) + .dependsOn(listOf(postgreSQLContainer)) + } + + override fun initialize(applicationContext: ConfigurableApplicationContext) { + val openSearchAlias = applicationContext.environment.getProperty("bpdm.opensearch.alias") + openSearchContainer.withNetworkAliases(openSearchAlias) + openSearchContainer.start() + TestPropertyValues.of( + "bpdm.opensearch.host=${openSearchContainer.host}", + "bpdm.opensearch.port=${openSearchContainer.getMappedPort(OPENSEARCH_PORT)}", + "bpdm.opensearch.scheme=http", + "bpdm.opensearch.enabled=true", + ).applyTo(applicationContext.environment) + } +} \ No newline at end of file diff --git a/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/util/PostgreSQLContextInitializer.kt b/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/util/PostgreSQLContextInitializer.kt index b4c68e2eb..2cefc8ece 100644 --- a/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/util/PostgreSQLContextInitializer.kt +++ b/bpdm-bridge-dummy/src/test/kotlin/com/catenax/bpdm/bridge/dummy/util/PostgreSQLContextInitializer.kt @@ -22,6 +22,7 @@ package com.catenax.bpdm.bridge.dummy.util import org.springframework.boot.test.util.TestPropertyValues import org.springframework.context.ApplicationContextInitializer import org.springframework.context.ConfigurableApplicationContext +import org.testcontainers.containers.Network import org.testcontainers.containers.PostgreSQLContainer /** @@ -29,15 +30,23 @@ import org.testcontainers.containers.PostgreSQLContainer */ class PostgreSQLContextInitializer : ApplicationContextInitializer { companion object { + val postgreSQLContainer = PostgreSQLContainer("postgres:13.2") + .withAccessToHost(true) + .withNetwork(Network.SHARED) + + } override fun initialize(applicationContext: ConfigurableApplicationContext) { + val postgresAlias = applicationContext.environment.getProperty("bpdm.datasource.alias") + postgreSQLContainer.withNetworkAliases(postgresAlias) + postgreSQLContainer.start() TestPropertyValues.of( "spring.datasource.url=${postgreSQLContainer.jdbcUrl}", "spring.datasource.username=${postgreSQLContainer.username}", - "spring.datasource.password=${postgreSQLContainer.password}" + "spring.datasource.password=${postgreSQLContainer.password}", ).applyTo(applicationContext.environment) } } \ No newline at end of file diff --git a/bpdm-bridge-dummy/src/test/resources/application-test.properties b/bpdm-bridge-dummy/src/test/resources/application-test.properties new file mode 100644 index 000000000..927ff45af --- /dev/null +++ b/bpdm-bridge-dummy/src/test/resources/application-test.properties @@ -0,0 +1,25 @@ +################################################################################ +# Copyright (c) 2021,2023 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License, Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0. +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# SPDX-License-Identifier: Apache-2.0 +################################################################################ +bpdm.opensearch.enabled=false +bpdm.opensearch.refresh-on-write=true +bpdm.opensearch.alias=bpdm-opensearch +bpdm.datasource.alias=bpdm-postgres +bpdm.pool.alias=bpdm-pool +logging.level.root=INFO +spring.datasource.hikari.maximum-pool-size=2 \ No newline at end of file