-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9bd3f6
commit 5dd7dbe
Showing
14 changed files
with
391 additions
and
27 deletions.
There are no files selected for viewing
27 changes: 0 additions & 27 deletions
27
liquibase-demo/src/main/java/ru/sysout/liquidemo/rest/AnimalController.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
13 changes: 13 additions & 0 deletions
13
...ainers-demo/src/main/java/ru/sysout/testcontainersdemo/TestcontainersDemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
testcontainers-demo/src/main/java/ru/sysout/testcontainersdemo/dao/AnimalRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
23 changes: 23 additions & 0 deletions
23
testcontainers-demo/src/main/java/ru/sysout/testcontainersdemo/model/Animal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
|
||
|
31 changes: 31 additions & 0 deletions
31
testcontainers-demo/src/main/resources/db/changelog/1.0/2020-20-02-create-tables.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
3 changes: 3 additions & 0 deletions
3
testcontainers-demo/src/main/resources/db/changelog/data/data/animal.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"id","name" | ||
"1","cat" | ||
"2","dog" |
22 changes: 22 additions & 0 deletions
22
testcontainers-demo/src/main/resources/db/changelog/data/initial-data.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
5 changes: 5 additions & 0 deletions
5
testcontainers-demo/src/main/resources/db/changelog/db.changelog-master.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
62 changes: 62 additions & 0 deletions
62
...ontainers-demo/src/test/java/ru/sysout/testcontainersdemo/IntegrationLiquiScriptTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
Oops, something went wrong.