Skip to content

Commit

Permalink
testcontainers
Browse files Browse the repository at this point in the history
  • Loading branch information
myluckagain committed Oct 28, 2020
1 parent f9bd3f6 commit 5dd7dbe
Show file tree
Hide file tree
Showing 14 changed files with 391 additions and 27 deletions.

This file was deleted.

24 changes: 24 additions & 0 deletions testcontainers-demo/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Read Me First
The following was discovered as part of building this project:

* The original package name 'ru.sysout.testcontainers-demo' is invalid and this project uses 'ru.sysout.testcontainersdemo' instead.

# Getting Started

### Reference Documentation
For further reference, please consider the following sections:

* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html)
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.4.0-SNAPSHOT/maven-plugin/reference/html/)
* [Create an OCI image](https://docs.spring.io/spring-boot/docs/2.4.0-SNAPSHOT/maven-plugin/reference/html/#build-image)
* [Testcontainers Postgres Module Reference Guide](https://www.testcontainers.org/modules/databases/postgres/)
* [Spring Data JDBC](https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/)
* [Spring Data JPA](https://docs.spring.io/spring-boot/docs/2.3.4.RELEASE/reference/htmlsingle/#boot-features-jpa-and-spring-data)
* [Testcontainers](https://www.testcontainers.org/)

### Guides
The following guides illustrate how to use some features concretely:

* [Using Spring Data JDBC](https://github.com/spring-projects/spring-data-examples/tree/master/jdbc/basics)
* [Accessing Data with JPA](https://spring.io/guides/gs/accessing-data-jpa/)

115 changes: 115 additions & 0 deletions testcontainers-demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ru.sysout</groupId>
<artifactId>testcontainers-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testcontainers-demo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>11</java.version>
<testcontainers.version>1.14.3</testcontainers.version>
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.sysout.testcontainersdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestcontainersDemoApplication {

public static void main(String[] args) {
SpringApplication.run(TestcontainersDemoApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.sysout.testcontainersdemo.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import ru.sysout.testcontainersdemo.model.Animal;


public interface AnimalRepository extends JpaRepository<Animal, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.sysout.testcontainersdemo.model;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@NoArgsConstructor
@Data
@Entity
public class Animal {
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE)
private long id;
private String name;

public Animal(String name) {
this.name = name;
}
}
8 changes: 8 additions & 0 deletions testcontainers-demo/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring:
jpa:
show-sql: true
hibernate:
ddl-auto: validate
liquibase:
enabled: false
change-log: classpath:db/changelog/db.changelog-master.yaml
14 changes: 14 additions & 0 deletions testcontainers-demo/src/main/resources/db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE SEQUENCE hibernate_sequence START 100;
create table animal
(
id bigint primary key,
name varchar(255)
);

insert into animal (id, name)
values (1,'animal1'),
(2,'animal2'),
(3,'animal3');



Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
databaseChangeLog:
- changeSet:
id: 1596784098744-1
author: myluc (generated)
changes:
- createTable:
columns:
- column:
constraints:
nullable: false
primaryKey: true
primaryKeyName: animal_pkey
name: id
type: BIGINT
- column:
name: name
type: VARCHAR(255)
tableName: animal
- changeSet:
id: 1596784098744-3
author: myluc (generated)
changes:
- createSequence:
cacheSize: 1
cycle: false
dataType: bigint
incrementBy: 1
maxValue: 9223372036854775807
minValue: 1
sequenceName: hibernate_sequence
startValue: 100
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"id","name"
"1","cat"
"2","dog"
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
databaseChangeLog:
- changeSet:
id: 1596784098744-2
author: myluc (generated)
changes:
- loadData:
columns:
- column:
header: id
name: id
type: NUMERIC
- column:
header: name
name: name
type: STRING
commentLineStartsWith: '#'
encoding: UTF-8
file: data/animal.csv
quotchar: '"'
separator: ','
relativeToChangelogFile: true
tableName: animal
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
databaseChangeLog:
- includeAll:
path: db/changelog/1.0/
- includeAll:
path: db/changelog/data/
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ru.sysout.testcontainersdemo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import ru.sysout.testcontainersdemo.dao.AnimalRepository;
import ru.sysout.testcontainersdemo.model.Animal;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;


@SpringBootTest
@ContextConfiguration(initializers = {IntegrationLiquiScriptTest.Initializer.class})
@Testcontainers
public class IntegrationLiquiScriptTest {

@Autowired
AnimalRepository animalRepository;

@Container
public static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:13")
.withDatabaseName("mydb")
.withUsername("myuser")
.withPassword("mypass");

static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of(
"spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
"spring.datasource.username=" + postgreSQLContainer.getUsername(),
"spring.datasource.password=" + postgreSQLContainer.getPassword(),
"spring.liquibase.enabled=true"
).applyTo(configurableApplicationContext.getEnvironment());
}
}
@Test
@Transactional
public void animalsCountShouldBeCorrect() {
long count = animalRepository.count();
assertEquals(2, count);
}

@Test
@Transactional
public void animalsShouldBeCorrect() {
List<Animal> animals = animalRepository.findAll();
String[] actualAnimals = {"cat", "dog"};
assertArrayEquals(actualAnimals, animals.stream().map(animal -> animal.getName()).toArray());
}

}
Loading

0 comments on commit 5dd7dbe

Please sign in to comment.