diff --git a/pom.xml b/pom.xml index 6e796d5ce18cbc57d9fcb380f1abd41e0dcd5744..79070cd80ca8b28328066df437e5aca06caa33f5 100644 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,7 @@ <properties> <java.version>11</java.version> + <testcontainers.version>1.16.0</testcontainers.version> <liquibase.chagelog>db.changelog-master.yaml</liquibase.chagelog> </properties> @@ -95,15 +96,23 @@ <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> + <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> + <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-configuration-processor</artifactId> + <optional>true</optional> + </dependency> + <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jooq</artifactId> @@ -125,17 +134,23 @@ </dependency> <!-- Tests --> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>testcontainers</artifactId> + <version>${testcontainers.version}</version> + <scope>test</scope> + </dependency> <dependency> <groupId>org.testcontainers</groupId> <artifactId>postgresql</artifactId> - <version>1.16.0</version> + <version>${testcontainers.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.testcontainers</groupId> <artifactId>junit-jupiter</artifactId> - <version>1.16.0</version> + <version>${testcontainers.version}</version> <scope>test</scope> </dependency> @@ -197,10 +212,7 @@ <version>1.1.1</version> </dependency> - <dependency> - <groupId>jakarta.validation</groupId> - <artifactId>jakarta.validation-api</artifactId> - </dependency> + </dependencies> diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index d80eff7e31461e0f6ace6654f9a4113a578cda9b..9e68a99093e18128076451ff9db694196941a03d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,9 +1,8 @@ -spring.datasource.url=jdbc:postgresql://localhost:5432/test_app_db spring.datasource.driver-class-name=org.postgresql.Driver +spring.jpa.database=postgresql +spring.datasource.url=jdbc:postgresql://localhost:5432/test_app_db spring.datasource.username=postgres spring.datasource.password=postgres -spring.datasource.initialize=true - logging.level.org.springframework=INFO diff --git a/src/test/java/com/example/WeatherApp/ApplicationTests.java b/src/test/java/com/example/WeatherApp/ApplicationTests.java index 7f92906a977098ee4d2c5349719e5fd0bed9e68f..84a69012a226360c2c8896d81f71eac591d82461 100644 --- a/src/test/java/com/example/WeatherApp/ApplicationTests.java +++ b/src/test/java/com/example/WeatherApp/ApplicationTests.java @@ -1,14 +1,13 @@ package com.example.WeatherApp; import com.example.WeatherApp.config.ObjectMapperConfig; +import com.example.WeatherApp.config.TestContainersConfig; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; @SpringBootTest @AutoConfigureMockMvc -@ContextConfiguration(classes = {ObjectMapperConfig.class}) +@ContextConfiguration(classes = {ObjectMapperConfig.class, TestContainersConfig.class}) public abstract class ApplicationTests { - - } diff --git a/src/test/java/com/example/WeatherApp/config/TestContainersConfig.java b/src/test/java/com/example/WeatherApp/config/TestContainersConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..b18c2284926e3935bba268ab9a337c971fbba92b --- /dev/null +++ b/src/test/java/com/example/WeatherApp/config/TestContainersConfig.java @@ -0,0 +1,42 @@ +package com.example.WeatherApp.config; + +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.containers.wait.strategy.Wait; + +import javax.sql.DataSource; + +@TestConfiguration +@ConfigurationProperties(prefix = "testcontainers") +@Setter +public class TestContainersConfig { + + private String name; + private String username; + private String password; + + + @Bean(initMethod = "start", destroyMethod = "stop") + public PostgreSQLContainer<?> postgreSQLContainer() { + return new PostgreSQLContainer<>("postgres:14") + .withDatabaseName(name) + .withUsername(username) + .withPassword(password) + .waitingFor(Wait.forListeningPort()); + } + + @Bean + public DataSource dataSource(PostgreSQLContainer<?> postgreSQLContainer) { + return DataSourceBuilder.create() + .driverClassName(postgreSQLContainer.getDriverClassName()) + .password(postgreSQLContainer.getPassword()) + .url(postgreSQLContainer.getJdbcUrl()) + .username(postgreSQLContainer.getUsername()) + .build(); + } + +} diff --git a/src/test/java/com/example/WeatherApp/controller/CityControllerPositiveTest.java b/src/test/java/com/example/WeatherApp/controller/CityControllerPositiveTest.java index 5c031e6bd71d44eb3cd760f2b2f06d25fb532470..6d47d0f043151ad16ee6da916f59b9f4a7d6de9d 100644 --- a/src/test/java/com/example/WeatherApp/controller/CityControllerPositiveTest.java +++ b/src/test/java/com/example/WeatherApp/controller/CityControllerPositiveTest.java @@ -1,15 +1,11 @@ package com.example.WeatherApp.controller; import com.example.WeatherApp.ApplicationTests; -import com.example.WeatherApp.config.ObjectMapperConfig; import com.example.WeatherApp.dto.CityDto; import com.example.WeatherApp.dto.ResponseWrappedDto; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.jdbc.Sql; import org.springframework.test.web.servlet.MockMvc; import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; @@ -20,11 +16,10 @@ import java.nio.file.Paths; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + -@SpringBootTest -@AutoConfigureMockMvc -@ContextConfiguration(classes = {ObjectMapperConfig.class}) class CityControllerPositiveTest extends ApplicationTests { @Autowired diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 39ca310b63e38042d02435bfbb31b3d582a8ade1..4cbe38eff162211fa812da0f397f2c8fa4fc4a6a 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -1,11 +1,10 @@ -spring.datasource.url=jdbc:postgresql://localhost:5432/test_app_db_for_test -server.port=8083 -spring.datasource.username=postgres -spring.datasource.password=postgres +spring.jpa.hibernate.ddl-auto=none spring.liquibase.enabled=true spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.yaml -logging.level.org.springframework.boot.autoconfigure=DEBUG -logging.level.org.hibernate.SQL=DEBUG -logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE -logging.level.org.hibernate.type=TRACE \ No newline at end of file +spring.jpa.database=postgresql +spring.datasource.driver-class-name=org.postgresql.Driver + +testcontainers.name=test_app_db +testcontainers.username=postgres +testcontainers.password=postgres diff --git a/src/test/resources/db.changelog-master.yaml b/src/test/resources/db.changelog-master.yaml deleted file mode 100644 index a7b34874ffdc21354fe24d84de5c23e1f518b410..0000000000000000000000000000000000000000 --- a/src/test/resources/db.changelog-master.yaml +++ /dev/null @@ -1,37 +0,0 @@ -databaseChangeLog: - - logicalFilePath: db/changelog/db.changelog-cities.yaml - - changeSet: - id: 1 - author: postgres - changes: - - createTable: - tableName: cities - columns: - - column: - name: id - type: uuid - constraints: - primaryKey: true - nullable: false - - column: - name: name - type: varchar(50) - constraints: - nullable: false - - column: - name: region - type: varchar(50) - constraints: - nullable: false - - column: - name: lat - type: decimal - - column: - name: lon - type: decimal - - column: - name: creation_date_time - type: date - - column: - name: modification_date_time - type: date \ No newline at end of file