Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions embedded-db2/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
=== embedded-db2

==== Maven dependency

.pom.xml
[source,xml]
----
<dependency>
<groupId>com.playtika.testcontainers</groupId>
<artifactId>embedded-db2</artifactId>
<scope>test</scope>
</dependency>
----

==== Consumes (via `bootstrap.properties`)

* `embedded.db2.enabled` `(true|false, default is true)`
* `embedded.db2.reuseContainer` `(true|false, default is false)`
* `embedded.db2.dockerImage` `(default is 'ibmcom/db2:latest')`
** Image versions: https://hub.docker.com/r/ibmcom/db2
* `embedded.db2.user` `(default is db2inst1)`
* `embedded.db2.password` `(default is foobar1234)`
* `embedded.db2.database` `(default is test)`
* `embedded.db2.acceptLicence` `(default is 'false')`
* `embedded.db2.startupLogCheckRegex`
* `embedded.db2.initScriptPath`

==== Produces

* `embedded.db2.host`
* `embedded.db2.port`
* `embedded.db2.database`
* `embedded.db2.user`
* `embedded.db2.password`
43 changes: 43 additions & 0 deletions embedded-db2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>testcontainers-spring-boot-parent</artifactId>
<groupId>com.playtika.testcontainers</groupId>
<version>2.0.15-SNAPSHOT</version>
<relativePath>../testcontainers-spring-boot-parent</relativePath>
</parent>

<artifactId>embedded-db2</artifactId>

<dependencies>
<dependency>
<groupId>com.playtika.testcontainers</groupId>
<artifactId>testcontainers-common</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>db2</artifactId>
<version>${testcontainers.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>jcc</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Playtika
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.playtika.test.db2;

import com.playtika.test.common.properties.CommonContainerProperties;
import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.FieldDefaults;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotBlank;

@Data
@Validated
@EqualsAndHashCode(callSuper = true)
@ConfigurationProperties("embedded.db2")
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Db2Properties extends CommonContainerProperties {
public static final String BEAN_NAME_EMBEDDED_DB2 = "embeddedDb2";

@NotBlank
String dockerImage = "ibmcom/db2";

@NotBlank
String user = "db2inst1";
@NotBlank
String password = "foobar1234";
@NotBlank
String database = "test";

boolean acceptLicence = false;

String startupLogCheckRegex;
String initScriptPath;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Playtika
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.playtika.test.db2;

import com.playtika.test.common.spring.DockerPresenceBootstrapConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.testcontainers.containers.Db2Container;
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
import org.testcontainers.containers.wait.strategy.WaitStrategy;

import java.util.LinkedHashMap;

import static com.playtika.test.common.utils.ContainerUtils.configureCommonsAndStart;
import static org.testcontainers.shaded.com.google.common.base.Strings.isNullOrEmpty;

@Slf4j
@Configuration
@ConditionalOnExpression("${embedded.containers.enabled:true}")
@AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class)
@ConditionalOnProperty(name = "embedded.db2.enabled", matchIfMissing = true)
@EnableConfigurationProperties(Db2Properties.class)
public class EmbeddedDb2BootstrapConfiguration {

@Bean(name = Db2Properties.BEAN_NAME_EMBEDDED_DB2, destroyMethod = "stop")
public Db2Container db2(ConfigurableEnvironment environment,
Db2Properties properties) {
log.info("Starting db2 server. Docker image: {}", properties.getDockerImage());

Db2Container db2Container = new Db2Container(properties.getDockerImage())
.withDatabaseName(properties.getDatabase())
.withUsername(properties.getUser())
.withPassword(properties.getPassword())
.withInitScript(properties.getInitScriptPath());

String startupLogCheckRegex = properties.getStartupLogCheckRegex();
if (!isNullOrEmpty(startupLogCheckRegex)) {
WaitStrategy waitStrategy = new LogMessageWaitStrategy()
.withRegEx(startupLogCheckRegex);
db2Container.setWaitStrategy(waitStrategy);
}

if (properties.isAcceptLicence()) {
db2Container.acceptLicense();
}

db2Container = (Db2Container) configureCommonsAndStart(db2Container, properties, log);
registerDb2Environment(db2Container, environment, properties);

return db2Container;
}

private void registerDb2Environment(Db2Container db2Container,
ConfigurableEnvironment environment,
Db2Properties properties) {
Integer mappedPort = db2Container.getMappedPort(Db2Container.DB2_PORT);
String host = db2Container.getContainerIpAddress();

LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put("embedded.db2.port", mappedPort);
map.put("embedded.db2.host", host);
map.put("embedded.db2.database", properties.getDatabase());
map.put("embedded.db2.user", properties.getUser());
map.put("embedded.db2.password", properties.getPassword());

String jdbcURL = "jdbc:db2://{}:{}/{}";
log.info("Started db2 server. Connection details: {}, " +
"JDBC connection url: " + jdbcURL, map, host, mappedPort, properties.getDatabase());

MapPropertySource propertySource = new MapPropertySource("embeddedDb2Info", map);
environment.getPropertySources().addFirst(propertySource);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Playtika
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.playtika.test.db2;

import com.playtika.test.common.spring.DependsOnPostProcessor;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@ConditionalOnClass(DataSource.class)
@ConditionalOnExpression("${embedded.containers.enabled:true}")
@ConditionalOnProperty(name = "embedded.db2.enabled", matchIfMissing = true)
@AutoConfigureAfter(name = "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration")
public class EmbeddedDb2DependenciesAutoConfiguration {

@Bean
public static BeanFactoryPostProcessor datasourceDb2DependencyPostProcessor() {
return new DependsOnPostProcessor(DataSource.class, new String[]{Db2Properties.BEAN_NAME_EMBEDDED_DB2});
}
}
5 changes: 5 additions & 0 deletions embedded-db2/src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.playtika.test.db2.EmbeddedDb2BootstrapConfiguration

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.playtika.test.db2.EmbeddedDb2DependenciesAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.playtika.test.db2;

import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.testcontainers.containers.Db2Container;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(
classes = EmbeddedDb2DependenciesAutoConfigurationTest.TestConfiguration.class,
properties = {
"embedded.db2.enabled=true"
}
)
class EmbeddedDb2DependenciesAutoConfigurationTest {

@Autowired(required = false)
private JdbcTemplate jdbcTemplate;

@Autowired
private Db2Container db2Container;

@Test
void injectedJdbs() {
assertThat(jdbcTemplate).isNotNull();
}

@Test
@SneakyThrows
void testCreateDb() {
Map<String, Object> map = jdbcTemplate.queryForMap("SELECT first_name, last_name FROM users WHERE first_name = 'Sam'");

assertThat(map)
.containsKey("first_name")
.containsKey("last_name")
.extractingByKey("last_name").isEqualTo("Brannen");
}

@EnableAutoConfiguration
@Configuration
static class TestConfiguration {
}

}
4 changes: 4 additions & 0 deletions embedded-db2/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver
spring.datasource.url=jdbc:db2://${embedded.db2.host}:${embedded.db2.port}/${embedded.db2.database}
spring.datasource.username=${embedded.db2.user}
spring.datasource.password=${embedded.db2.password}
7 changes: 7 additions & 0 deletions embedded-db2/src/test/resources/bootstrap.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
embedded.db2.database=database
embedded.db2.user=db2inst1
embedded.db2.password=foobar1234
embedded.db2.accept-licence=true
embedded.db2.docker-image=ibmcom/db2:11.5.0.0a
embedded.db2.wait-timeout-in-seconds=180
embedded.db2.init-script-path=initScript.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ibmcom/db2:11.5.0.0a
Copy link
Contributor Author

@IlyasYOY IlyasYOY Aug 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I remove this file? I must be here because of DB2 licensing
I don't how how to remove this, if I remove this then CI would fail

7 changes: 7 additions & 0 deletions embedded-db2/src/test/resources/initScript.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE users (
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL
);
INSERT INTO
users(first_name, last_name)
values('Sam', 'Brannen');
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<module>embedded-prometheus</module>
<module>embedded-grafana</module>
<module>embedded-consul</module>
<module>embedded-db2</module>
<module>embedded-artifactory</module>
</modules>

Expand Down
5 changes: 5 additions & 0 deletions testcontainers-spring-boot-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@
<artifactId>embedded-artifactory</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.playtika.testcontainers</groupId>
<artifactId>embedded-db2</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Loading