Skip to content

Feature/spring test postgres ability to customize docker image #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 11 additions & 6 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ You need to add the next dependency:
<dependency>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-test-jpa</artifactId>
<version>0.2</version>
<version>0.5</version>
</dependency>
----

Expand Down Expand Up @@ -74,7 +74,7 @@ in your integration test, you can use PostgresTcExtension.
<dependency>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-test-postgres</artifactId>
<version>0.2</version>
<version>0.5</version>
</dependency>
----

Expand Down Expand Up @@ -140,6 +140,11 @@ image:./images/pg-annotations.png[postgres annotation system]

You can build a necessary test configuration by using annotations which you need. Meta-annotations is a beautiful mechanism which will you a making configuration in a declarative style.

By default, the *latest* version of PostgreSQL docker image will be used for test. But it can be overridden by setting necessary version via _resources/tc-extension.properties_ file:
[source, properties]
----
tc-extension.postgres.docker-image-version=13.7-alpine
----

### MySql Extension

Expand All @@ -151,7 +156,7 @@ in the configuration to use this datasource in tests.
<dependency>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-test-mysql</artifactId>
<version>0.2</version>
<version>0.5</version>
</dependency>
----

Expand Down Expand Up @@ -196,7 +201,7 @@ and configure SpringBoot properties to work with this container.
<dependency>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-test-rabbitmq</artifactId>
<version>0.2</version>
<version>0.5</version>
</dependency>
----

Expand Down Expand Up @@ -308,7 +313,7 @@ You need to use the next dependency:
<dependency>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-test-activemq</artifactId>
<version>0.2</version>
<version>0.5</version>
</dependency>
----

Expand Down Expand Up @@ -401,7 +406,7 @@ web server with a mocked external controller and send HTTP requests to this serv
<dependency>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-test-web</artifactId>
<version>0.2</version>
<version>0.5</version>
</dependency>
----

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.4</version>
<version>0.5</version>
<packaging>pom</packaging>

<name>spring-boot-extensions-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-test-activemq/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.4</version>
<version>0.5</version>
</parent>

<artifactId>spring-test-activemq</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-test-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.4</version>
<version>0.5</version>
</parent>

<artifactId>spring-test-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-test-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.4</version>
<version>0.5</version>
</parent>

<artifactId>spring-test-jpa</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-test-mysql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.4</version>
<version>0.5</version>
</parent>

<artifactId>spring-test-mysql</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions spring-test-postgres/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.4</version>
<version>0.5</version>
</parent>

<artifactId>spring-test-postgres</artifactId>
Expand Down Expand Up @@ -40,7 +40,7 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.11.4</version>
<version>1.17.2</version>
<scope>compile</scope>
</dependency>
<!-- TestContainers -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.jupiter.tools.spring.test.postgres.extension;

import org.hibernate.dialect.PostgreSQL10Dialect;
import org.hibernate.dialect.PostgreSQL9Dialect;
import org.junit.jupiter.api.extension.Extension;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

import java.io.IOException;
import java.util.Properties;

/**
* Created on 06.08.2018.
Expand All @@ -14,13 +19,25 @@ public class PostgresTcExtension implements Extension {
static {
System.out.println("Start Postgres testcontainers extension...\n");

PostgreSQLContainer postgres = new PostgreSQLContainer();
PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:" + getDockerImageName());
postgres.start();

System.setProperty("spring.datasource.driver-class-name", postgres.getDriverClassName());
System.setProperty("spring.datasource.url", postgres.getJdbcUrl());
System.setProperty("spring.datasource.username", postgres.getUsername());
System.setProperty("spring.datasource.password", postgres.getPassword());
System.setProperty("spring.jpa.properties.hibernate.dialect", PostgreSQL9Dialect.class.getCanonicalName());
System.setProperty("spring.jpa.properties.hibernate.dialect", PostgreSQL10Dialect.class.getCanonicalName());
}

private static String getDockerImageName() {
try {
Properties props = new Properties();
props.load(PostgresTcExtension.class.getResourceAsStream("/tc-extension.properties"));
return props.getProperty("tc-extension.postgres.docker-image-version", "latest");
} catch (Exception e) {
e.printStackTrace();
return "latest";
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.jupiter.tools.spring.test.postgres.extension;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.StoredProcedureQuery;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
* Created on 13.06.2022.
*
* @author Sergey Nikolaev
*/
@DataJpaTest
@ExtendWith(PostgresTcExtensionCustomVersionTest.InitializePropertiesExtension.class)
@ExtendWith(SpringExtension.class)
@ExtendWith(PostgresTcExtension.class)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class PostgresTcExtensionCustomVersionTest {

@PersistenceContext
private EntityManager entityManager;

@Test
void testCustomPostgresVersion() {
// Arrange
StoredProcedureQuery query = entityManager.createStoredProcedureQuery("version");
// Act
query.execute();
// Assert
Object result = query.getSingleResult();
Assertions.assertThat(result.toString()).contains("PostgreSQL 12");
}

public static class InitializePropertiesExtension<writer> implements Extension, AfterAllCallback {
public static final String SOURCE;
static {
SOURCE = PostgresTcExtensionCustomVersionTest.class.getResource("/").getPath() + "tc-extension.properties";
try(FileWriter writer = new FileWriter(SOURCE)) {
writer.write("tc-extension.postgres.docker-image-version=12");
} catch (IOException e) {
e.printStackTrace();
Assertions.fail("Fail create tc-extension.properties");
}
}

@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
try {
new File(SOURCE).delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,16 @@ void testStoredFunc() {
int rnd = (int) resultList.get(0);
Assertions.assertThat(rnd).isEqualTo(123);
}

@Test
void testCustomPostgresVersion() {
// Arrange
StoredProcedureQuery query = entityManager.createStoredProcedureQuery("version");
// Act
query.execute();
// Assert
Object result = query.getSingleResult();
Assertions.assertThat(result.toString()).contains("PostgreSQL 12");
}

}
2 changes: 1 addition & 1 deletion spring-test-rabbitmq/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.4</version>
<version>0.5</version>
</parent>

<artifactId>spring-test-rabbitmq</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-test-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.4</version>
<version>0.5</version>
</parent>

<artifactId>spring-test-web</artifactId>
Expand Down