-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/amp 126 subscriptions db #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
213eb57
81a09fb
2766dee
96622a0
c212e21
563c964
2340100
623c0ca
32455cd
7c29de0
feb214d
e1bd78f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| gradle | ||
| gradle/wrapper | ||
| bin/* | ||
| !bin/run-in-docker.sh | ||
| .gradle | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| services: | ||
| db: | ||
| image: postgres:16-alpine | ||
| environment: | ||
| POSTGRES_DB: appdb | ||
| POSTGRES_USER: postgres | ||
| POSTGRES_PASSWORD: postgres | ||
| ports: | ||
| - "5432:5432" | ||
|
|
||
| app: | ||
| build: | ||
| dockerfile: Dockerfile | ||
| environment: | ||
| DATASOURCE_URL: jdbc:postgresql://db:5432/appdb | ||
| DATASOURCE_USERNAME: postgres | ||
| DATASOURCE_PASSWORD: postgres | ||
| depends_on: | ||
| - db | ||
| ports: | ||
| - "8082:8082" |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| dependencies { | ||
| implementation 'org.springframework.boot:spring-boot-starter-data-jpa' | ||
| implementation 'org.postgresql:postgresql' | ||
| implementation 'org.springframework.boot:spring-boot-starter-flyway' | ||
| implementation 'org.flywaydb:flyway-core' | ||
| implementation 'org.flywaydb:flyway-database-postgresql' | ||
| testImplementation("org.springframework.boot:spring-boot-testcontainers:4.0.0") | ||
| testImplementation 'org.testcontainers:postgresql:1.21.3' | ||
| testImplementation 'org.testcontainers:junit-jupiter:1.21.3' | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| tasks.register('api', Test) { | ||
| tasks.register('apiTest', Test) { | ||
| description = "Runs api tests against docker-compose stack" | ||
| group = "Verification" | ||
|
|
||
|
|
@@ -13,12 +13,12 @@ tasks.register('api', Test) { | |
| } | ||
|
|
||
| tasks.named('check') { | ||
| dependsOn tasks.named('api') | ||
| dependsOn tasks.named('apiTest') | ||
| } | ||
|
|
||
| tasks.named('build') { | ||
| dependsOn tasks.named('test') | ||
| dependsOn tasks.named('api') | ||
| dependsOn tasks.named('apiTest') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to run api tests as part of the build or after the build? Will these api test interact with CP services? |
||
| } | ||
|
|
||
| sourceSets { | ||
|
|
@@ -30,5 +30,21 @@ sourceSets { | |
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| dockerCompose { | ||
| useComposeFiles = ['docker-compose.yml'] | ||
| startedServices = ['db', 'app'] | ||
|
|
||
| buildBeforeUp = true | ||
| waitForTcpPorts = true | ||
| upAdditionalArgs = ['--wait', '--wait-timeout', '120'] | ||
|
|
||
| captureContainersOutput = true | ||
| removeOrphans = true | ||
| stopContainers = true | ||
| removeContainers = true | ||
|
|
||
| projectName = "app" | ||
|
|
||
| useDockerComposeV2 = true | ||
| dockerExecutable = 'docker' | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package uk.gov.hmcts.cp.entities; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @Entity | ||
| @Table(name = "subscriber") | ||
| @Getter | ||
| @Builder(toBuilder = true) | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class SubscriberEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.UUID) | ||
| private UUID id; | ||
|
|
||
| private String name; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package uk.gov.hmcts.cp.entities; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import uk.gov.hmcts.cp.model.EventType; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @Entity | ||
| @Table(name = "subscription") | ||
| @Getter | ||
| @Builder(toBuilder = true) | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class SubscriptionEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.UUID) | ||
| private UUID id; | ||
|
|
||
| private UUID subscriberId; | ||
| @Enumerated(EnumType.STRING) | ||
| private EventType eventType; | ||
| private String notifyUrl; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package uk.gov.hmcts.cp.mappers; | ||
|
|
||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
| import org.mapstruct.ReportingPolicy; | ||
| import uk.gov.hmcts.cp.entities.SubscriptionEntity; | ||
| import uk.gov.hmcts.cp.openapi.model.Result; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE) | ||
| public interface SubscriptionMapper { | ||
|
|
||
| @Mapping(source = "notifyUrl", target = "resultText") | ||
| Result mapSubscriptionToResult(SubscriptionEntity subscription); | ||
|
|
||
| List<Result> mapSubscriptionsToResults(List<SubscriptionEntity> subscriptions); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package uk.gov.hmcts.cp.model; | ||
|
|
||
| public enum EventType { | ||
| PCR, | ||
| CUSTODIAL_RESULT | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package uk.gov.hmcts.cp.repositories; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
| import uk.gov.hmcts.cp.entities.SubscriberEntity; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @Repository | ||
| public interface SubscriberRepository extends JpaRepository<SubscriberEntity, UUID> { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package uk.gov.hmcts.cp.repositories; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
| import uk.gov.hmcts.cp.entities.SubscriptionEntity; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @Repository | ||
| public interface SubscriptionRepository extends JpaRepository<SubscriptionEntity, UUID> { | ||
|
|
||
| List<SubscriptionEntity> getBySubscriberId(UUID subscriberId); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package uk.gov.hmcts.cp.services; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Service; | ||
| import uk.gov.hmcts.cp.entities.SubscriptionEntity; | ||
| import uk.gov.hmcts.cp.mappers.SubscriptionMapper; | ||
| import uk.gov.hmcts.cp.openapi.model.Result; | ||
| import uk.gov.hmcts.cp.repositories.SubscriptionRepository; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class SubscriptionService { | ||
|
|
||
| private final SubscriptionRepository subscriptionRepository; | ||
| private final SubscriptionMapper mapper; | ||
|
|
||
| public Result getSubscriptionById(final UUID subscriptionId) { | ||
| final SubscriptionEntity entity = subscriptionRepository.getReferenceById(subscriptionId); | ||
| return mapper.mapSubscriptionToResult(entity); | ||
| } | ||
|
|
||
| public List<Result> getSubscriptionsBySubscriber(final UUID subscriberId) { | ||
| final List<SubscriptionEntity> subscriptions = subscriptionRepository.getBySubscriberId(subscriberId); | ||
| return mapper.mapSubscriptionsToResults(subscriptions); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| server: | ||
| port: ${SERVER_PORT:8082} | ||
|
|
||
| spring: | ||
| application: | ||
| name: service-hmcts-springboot-demo-postgres | ||
| jpa: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. name looks incorrect |
||
| open-in-view: false | ||
| datasource: | ||
| url: ${DATASOURCE_URL:jdbc:postgresql://localhost:5432/appdb} | ||
| username: ${DATASOURCE_USERNAME:postgres} | ||
| password: ${DATASOURCE_PASSWORD:postgres} | ||
| flyway: | ||
| enabled: true | ||
| locations: classpath:db/migration | ||
| baseline-on-migrate: true | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| create table subscriber ( | ||
| id uuid primary key not null, | ||
| name varchar(128) not null | ||
| ); | ||
| create unique index on subscriber (name); | ||
|
|
||
| create table subscription ( | ||
| id uuid primary key not null, | ||
| subscriber_id uuid not null, | ||
| event_type varchar(32) not null, | ||
| notify_url text not null | ||
| ); | ||
|
|
||
| alter table subscription | ||
| add constraint subscription_subscriber_id_fk foreign key (subscriber_id) references subscriber (id); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package uk.gov.hmcts.cp.config; | ||
|
|
||
| import lombok.SneakyThrows; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.boot.test.util.TestPropertyValues; | ||
| import org.springframework.context.ApplicationContextInitializer; | ||
| import org.springframework.context.ConfigurableApplicationContext; | ||
| import org.testcontainers.containers.PostgreSQLContainer; | ||
|
|
||
| @Slf4j | ||
| public class TestContainersInitialise implements ApplicationContextInitializer<ConfigurableApplicationContext> { | ||
|
|
||
| private static final PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer( | ||
| "postgres") | ||
| .withDatabaseName("postgres") | ||
| .withUsername("postgres") | ||
| .withPassword("postgres"); | ||
|
|
||
|
|
||
| @SneakyThrows | ||
| @Override | ||
| public void initialize(ConfigurableApplicationContext applicationContext) { | ||
| postgreSQLContainer.start(); | ||
| TestPropertyValues.of( | ||
| "spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(), | ||
| "spring.datasource.username=" + postgreSQLContainer.getUsername(), | ||
| "spring.datasource.password=" + postgreSQLContainer.getPassword() | ||
| ).applyTo(applicationContext.getEnvironment()); | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wioll get rid of this next PR when pull in the proper api spec