Skip to content

Commit

Permalink
feat(bridge): Config PostgreSQL testcontainer for test
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfkaeser committed May 4, 2023
1 parent 3e0950f commit 15e55df
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@

package com.catenax.bpdm.bridge.dummy

import com.catenax.bpdm.bridge.dummy.util.PostgreSQLContextInitializer
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration

@SpringBootTest
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@ContextConfiguration(initializers = [PostgreSQLContextInitializer::class])
class ApplicationTests {

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* 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 org.springframework.boot.test.util.TestPropertyValues
import org.springframework.context.ApplicationContextInitializer
import org.springframework.context.ConfigurableApplicationContext
import org.testcontainers.containers.PostgreSQLContainer

/**
* When used on a spring boot test, starts a singleton postgres db container that is shared between all integration tests.
*/
class PostgreSQLContextInitializer : ApplicationContextInitializer<ConfigurableApplicationContext> {
companion object {
val postgreSQLContainer = PostgreSQLContainer("postgres:13.2")
}

override fun initialize(applicationContext: ConfigurableApplicationContext) {
postgreSQLContainer.start()
TestPropertyValues.of(
"spring.datasource.url=${postgreSQLContainer.jdbcUrl}",
"spring.datasource.username=${postgreSQLContainer.username}",
"spring.datasource.password=${postgreSQLContainer.password}"
).applyTo(applicationContext.environment)
}
}

0 comments on commit 15e55df

Please sign in to comment.