Skip to content

Commit

Permalink
feat: use MySQL test container for integration tests (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-baiborodine committed Oct 16, 2023
1 parent 9bade3e commit d56c440
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 77 deletions.
13 changes: 13 additions & 0 deletions pom.xml
Expand Up @@ -36,6 +36,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<testcontainers.version>1.19.1</testcontainers.version>
<!-- Sonar Cloud -->
<sonar.organization>igor-baiborodine-github</sonar.organization>
<sonar.projectKey>igor-baiborodine_campsite-booking</sonar.projectKey>
Expand Down Expand Up @@ -155,6 +156,18 @@
<artifactId>easy-random-core</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
@@ -1,6 +1,6 @@
package com.kiroule.campsite.booking.api.model;

import java.time.LocalDateTime;
import java.time.Instant;
import lombok.Data;
import lombok.Generated;
import lombok.experimental.SuperBuilder;
Expand All @@ -10,7 +10,7 @@
@Generated
public abstract class DateAudit {

private LocalDateTime createdAt;
private Instant createdAt;

private LocalDateTime updatedAt;
private Instant updatedAt;
}
Expand Up @@ -3,8 +3,7 @@
import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import java.time.LocalDateTime;

import java.time.Instant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Generated;
Expand All @@ -25,9 +24,9 @@ public abstract class DateAuditEntity {

@CreatedDate
@Column(name = "created_at", nullable = false, updatable = false)
private LocalDateTime createdAt;
private Instant createdAt;

@LastModifiedDate
@Column(name = "updated_at")
private LocalDateTime updatedAt;
private Instant updatedAt;
}
27 changes: 25 additions & 2 deletions src/test/java/com/kiroule/campsite/booking/api/BaseIT.java
Expand Up @@ -4,7 +4,30 @@

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.MySQLContainer;

@SpringBootTest(webEnvironment = RANDOM_PORT)
@ActiveProfiles("in-memory-db")
public abstract class BaseIT {}
@ActiveProfiles("mysql")
public abstract class BaseIT {

private static final String MYSQL_DOCKER_IMAGE_NAME = "mysql:8-debian";
private static final String MYSQL_DATABASE_NAME = "test_campsite";

static final MySQLContainer<?> mySqlContainer;

static {
mySqlContainer =
new MySQLContainer<>(MYSQL_DOCKER_IMAGE_NAME).withDatabaseName(MYSQL_DATABASE_NAME);
mySqlContainer.start();
}

@DynamicPropertySource
static void mysqlProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", mySqlContainer::getJdbcUrl);
registry.add("spring.datasource.username", mySqlContainer::getUsername);
registry.add("spring.datasource.password", mySqlContainer::getPassword);
registry.add("spring.jpa.properties.hibernate.show_sql", () -> "true");
}
}
Expand Up @@ -37,12 +37,12 @@ class FindByUuid {
@Test
void happy_path() {
// given
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId());
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
BookingEntity bookingEntity = testDataGenerator.createBookingEntity(campsiteEntity.getId());
// when
Optional<BookingEntity> result = classUnderTest.findByUuid(booking.getUuid());
Optional<BookingEntity> result = classUnderTest.findByUuid(bookingEntity.getUuid());
// then
assertThat(result).hasValue(booking);
assertThat(result).hasValue(bookingEntity);
}
}

Expand All @@ -53,14 +53,15 @@ class FindForDateRange {
@DisplayNamePrefix("SE|-|----|-|--")
void given_booking_dates_before_range_start_date__then_no_booking_found() {
// given
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 1, 2);
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
BookingEntity bookingEntity =
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 1, 2);
// when
List<BookingEntity> result =
classUnderTest.findForDateRange(
booking.getStartDate().plusDays(3),
booking.getStartDate().plusDays(4),
campsite.getId());
bookingEntity.getStartDate().plusDays(3),
bookingEntity.getStartDate().plusDays(4),
campsiteEntity.getId());
// then
assertThat(result).isEmpty();
}
Expand All @@ -70,14 +71,15 @@ void given_booking_dates_before_range_start_date__then_no_booking_found() {
void
given_booking_start_date_before_range_start_date_and_booking_end_date_equals_to_range_start_date__then_no_booking_found() {
// given
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 1, 2);
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
BookingEntity bookingEntity =
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 1, 2);
// when
List<BookingEntity> result =
classUnderTest.findForDateRange(
booking.getStartDate().plusDays(2),
booking.getStartDate().plusDays(3),
campsite.getId());
bookingEntity.getStartDate().plusDays(2),
bookingEntity.getStartDate().plusDays(3),
campsiteEntity.getId());
// then
assertThat(result).isEmpty();
}
Expand All @@ -87,132 +89,140 @@ void given_booking_dates_before_range_start_date__then_no_booking_found() {
void
given_booking_start_date_before_range_start_date_and_booking_end_date_within_range_dates__then_booking_found() {
// given
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 1, 3);
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
BookingEntity bookingEntity =
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 1, 3);
// when
List<BookingEntity> result =
classUnderTest.findForDateRange(
booking.getStartDate().plusDays(2),
booking.getStartDate().plusDays(4),
campsite.getId());
bookingEntity.getStartDate().plusDays(2),
bookingEntity.getStartDate().plusDays(4),
campsiteEntity.getId());
// then
assertThat(result).hasSize(1).contains(booking);
assertThat(result).hasSize(1).contains(bookingEntity);
}

@Test
@DisplayNamePrefix("--|S|E---|-|--")
void
given_booking_start_date_equals_to_range_start_date_and_booking_end_date_within_range_dates__then_booking_found() {
// given
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 1, 2);
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
BookingEntity bookingEntity =
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 1, 2);
// when
List<BookingEntity> result =
classUnderTest.findForDateRange(
booking.getStartDate().plusDays(1),
booking.getStartDate().plusDays(3),
campsite.getId());
bookingEntity.getStartDate().plusDays(1),
bookingEntity.getStartDate().plusDays(3),
campsiteEntity.getId());
// then
assertThat(result).hasSize(1).contains(booking);
assertThat(result).hasSize(1).contains(bookingEntity);
}

@Test
@DisplayNamePrefix("--|-|S--E|-|--")
void given_booking_dates_within_range_dates__then_booking_found() {
// given
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 2, 3);
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
BookingEntity bookingEntity =
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 2, 3);
// when
List<BookingEntity> result =
classUnderTest.findForDateRange(
booking.getStartDate().plusDays(1),
booking.getStartDate().plusDays(4),
campsite.getId());
bookingEntity.getStartDate().plusDays(1),
bookingEntity.getStartDate().plusDays(4),
campsiteEntity.getId());
// then
assertThat(result).hasSize(1).contains(booking);
assertThat(result).hasSize(1).contains(bookingEntity);
}

@Test
@DisplayNamePrefix("--|-|---S|E|--")
void
given_booking_start_date_within_range_dates_and_booking_end_date_equals_to_range_end_date__then_booking_found() {
// given
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 2, 3);
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
BookingEntity bookingEntity =
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 2, 3);
// when
List<BookingEntity> result =
classUnderTest.findForDateRange(
booking.getStartDate().plusDays(1),
booking.getStartDate().plusDays(3),
campsite.getId());
bookingEntity.getStartDate().plusDays(1),
bookingEntity.getStartDate().plusDays(3),
campsiteEntity.getId());
// then
assertThat(result).hasSize(1).contains(booking);
assertThat(result).hasSize(1).contains(bookingEntity);
}

@Test
@DisplayNamePrefix("--|-|---S|-|E-")
void
given_booking_start_date_before_range_end_date_and_booking_end_date_after_range_end_date__then_booking_found() {
// given
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 2, 4);
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
BookingEntity bookingEntity =
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 2, 4);
// when
List<BookingEntity> result =
classUnderTest.findForDateRange(
booking.getStartDate().plusDays(1),
booking.getStartDate().plusDays(3),
campsite.getId());
bookingEntity.getStartDate().plusDays(1),
bookingEntity.getStartDate().plusDays(3),
campsiteEntity.getId());
// then
assertThat(result).hasSize(1).contains(booking);
assertThat(result).hasSize(1).contains(bookingEntity);
}

@Test
@DisplayNamePrefix("--|-|----|S|E-")
void
given_booking_start_date_equals_to_range_end_date_and_booking_end_date_after_range_end_date__then_booking_found() {
// given
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 3, 4);
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
BookingEntity bookingEntity =
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 3, 4);
// when
List<BookingEntity> result =
classUnderTest.findForDateRange(
booking.getStartDate().plusDays(1),
booking.getStartDate().plusDays(3),
campsite.getId());
bookingEntity.getStartDate().plusDays(1),
bookingEntity.getStartDate().plusDays(3),
campsiteEntity.getId());
// then
assertThat(result).hasSize(1).contains(booking);
assertThat(result).hasSize(1).contains(bookingEntity);
}

@Test
@DisplayNamePrefix("--|-|----|-|SE")
void given_booking_dates_after_range_end_date__then_no_booking_found() {
// given
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 3, 4);
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
BookingEntity bookingEntity =
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 3, 4);
// when
List<BookingEntity> result =
classUnderTest.findForDateRange(
booking.getStartDate().plusDays(1),
booking.getStartDate().plusDays(2),
campsite.getId());
bookingEntity.getStartDate().plusDays(1),
bookingEntity.getStartDate().plusDays(2),
campsiteEntity.getId());
// then
assertThat(result).hasSize(1).contains(booking);
assertThat(result).hasSize(1).contains(bookingEntity);
}

@Test
@DisplayNamePrefix("-S|-|----|-|E-")
void given_booking_dates_overlap_range_dates__then_booking_found() {
// given
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 1, 4);
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
BookingEntity bookingEntity =
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 1, 4);
// when
List<BookingEntity> result =
classUnderTest.findForDateRange(
booking.getStartDate().plusDays(2),
booking.getStartDate().plusDays(3),
campsite.getId());
bookingEntity.getStartDate().plusDays(2),
bookingEntity.getStartDate().plusDays(3),
campsiteEntity.getId());
// then
assertThat(result).hasSize(1).contains(booking);
assertThat(result).hasSize(1).contains(bookingEntity);
}
}
}

0 comments on commit d56c440

Please sign in to comment.