|
| 1 | +package uk.gov.hmcts.cp.subscription.integration.repository; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import uk.gov.hmcts.cp.subscription.entities.ClientSubscriptionEntity; |
| 6 | +import uk.gov.hmcts.cp.subscription.model.EntityEventType; |
| 7 | +import uk.gov.hmcts.cp.subscription.repositories.SubscriptionRepository; |
| 8 | + |
| 9 | +import java.time.OffsetDateTime; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Optional; |
| 12 | + |
| 13 | +import org.flywaydb.core.Flyway; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.boot.test.context.SpringBootTest; |
| 17 | +import org.springframework.test.context.DynamicPropertyRegistry; |
| 18 | +import org.springframework.test.context.DynamicPropertySource; |
| 19 | +import org.testcontainers.containers.PostgreSQLContainer; |
| 20 | +import org.testcontainers.junit.jupiter.Container; |
| 21 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 22 | + |
| 23 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) |
| 24 | +@Testcontainers |
| 25 | +class SubscriptionRepositoryIntegrationTest { |
| 26 | + |
| 27 | + @Container |
| 28 | + static PostgreSQLContainer<?> POSTGRES = new PostgreSQLContainer<>("postgres:15-alpine") |
| 29 | + .withDatabaseName("ampdb") |
| 30 | + .withUsername("postgres") |
| 31 | + .withPassword("postgres"); |
| 32 | + |
| 33 | + @DynamicPropertySource |
| 34 | + static void registerPgProperties(DynamicPropertyRegistry registry) { |
| 35 | + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); |
| 36 | + registry.add("spring.datasource.username", POSTGRES::getUsername); |
| 37 | + registry.add("spring.datasource.password", POSTGRES::getPassword); |
| 38 | + registry.add("spring.datasource.driver-class-name", POSTGRES::getDriverClassName); |
| 39 | + } |
| 40 | + |
| 41 | + @Autowired |
| 42 | + private SubscriptionRepository subscriptionRepository; |
| 43 | + |
| 44 | + @Autowired(required = false) |
| 45 | + private Flyway flyway; |
| 46 | + |
| 47 | + @Test |
| 48 | + void repository_should_persist_and_read_against_postgres() { |
| 49 | + // ensure migrations ran (if Flyway is on the classpath and configured) |
| 50 | + if (flyway != null) { |
| 51 | + assertThat(flyway.info().applied()).isNotEmpty(); |
| 52 | + } |
| 53 | + |
| 54 | + ClientSubscriptionEntity entity = ClientSubscriptionEntity.builder() |
| 55 | + .notificationEndpoint("https://example.com/callback") |
| 56 | + .eventTypes(List.of(EntityEventType.PRISON_COURT_REGISTER_GENERATED)) |
| 57 | + .createdAt(OffsetDateTime.now()) |
| 58 | + .updatedAt(OffsetDateTime.now()) |
| 59 | + .build(); |
| 60 | + |
| 61 | + ClientSubscriptionEntity saved = subscriptionRepository.save(entity); |
| 62 | + assertThat(saved.getId()).isNotNull(); |
| 63 | + |
| 64 | + Optional<ClientSubscriptionEntity> fetched = subscriptionRepository.findById(saved.getId()); |
| 65 | + assertThat(fetched).isPresent(); |
| 66 | + assertThat(fetched.get().getNotificationEndpoint()).isEqualTo("https://example.com/callback"); } |
| 67 | +} |
0 commit comments