Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/pmd-ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<exclude name="UnitTestAssertionsShouldIncludeMessage"/>
<exclude name="UnitTestContainsTooManyAsserts"/>
<exclude name="PreserveStackTrace"/>
<exclude name="ImplicitFunctionalInterface"/>
</rule>
<rule ref="category/java/codestyle.xml">
<exclude name="AtLeastOneConstructor"/>
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ jobs:
gradle-version: current

- name: Gradle Build
run: gradle build -x test -x api
run: gradle build -x test

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: DAST - Build and run containerised app
run: |
docker compose -f docker/docker-compose.yml up -d
docker compose -f docker-compose.yml up -d

echo "Waiting for health endpoint..."
for i in {1..30}; do
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
gradle
gradle/wrapper
bin/*
!bin/run-in-docker.sh
.gradle
Expand Down
File renamed without changes.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ version = System.getProperty('ARTEFACT_VERSION') ?: '0.0.999'
apply {
from("$rootDir/gradle/dependencies/java-core.gradle")
from("$rootDir/gradle/dependencies/spring-core.gradle")
from("$rootDir/gradle/dependencies/spring-db.gradle")

from("$rootDir/gradle/github/repositories.gradle")
from("$rootDir/gradle/github/java.gradle")
Expand All @@ -26,7 +27,6 @@ apply {
from("$rootDir/gradle/github/jar.gradle")

from("$rootDir/gradle/tasks/apitest.gradle")
from("$rootDir/gradle/tasks/docker.gradle")
}

springBoot {
Expand All @@ -40,4 +40,7 @@ springBoot {

dependencies {
implementation('uk.gov.hmcts.cp:api-cp-crime-courthearing-cases-results:0.0.0-a65d290')

// Lets add this to the api because we need it to prevent its openapi NotNull errors
implementation("io.swagger.core.v3:swagger-annotations:2.2.41")
Copy link
Collaborator Author

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

}
21 changes: 21 additions & 0 deletions docker-compose.yml
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"
10 changes: 0 additions & 10 deletions docker/docker-compose.yml

This file was deleted.

10 changes: 10 additions & 0 deletions gradle/dependencies/spring-db.gradle
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'
}
24 changes: 20 additions & 4 deletions gradle/tasks/apitest.gradle
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"

Expand All @@ -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')
Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
Expand All @@ -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'
}
18 changes: 0 additions & 18 deletions gradle/tasks/docker.gradle

This file was deleted.

28 changes: 28 additions & 0 deletions src/main/java/uk/gov/hmcts/cp/entities/SubscriberEntity.java
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;
}
34 changes: 34 additions & 0 deletions src/main/java/uk/gov/hmcts/cp/entities/SubscriptionEntity.java
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;
}
19 changes: 19 additions & 0 deletions src/main/java/uk/gov/hmcts/cp/mappers/SubscriptionMapper.java
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);

}
6 changes: 6 additions & 0 deletions src/main/java/uk/gov/hmcts/cp/model/EventType.java
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);
}
31 changes: 31 additions & 0 deletions src/main/java/uk/gov/hmcts/cp/services/SubscriptionService.java
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);
}
}
16 changes: 16 additions & 0 deletions src/main/resources/application.yaml
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:
Copy link
Contributor

Choose a reason for hiding this comment

The 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
15 changes: 15 additions & 0 deletions src/main/resources/db/migration/V1.001__initial_schema.sql
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());
}
}

Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package uk.gov.hmcts.cp.controllers.integration;
package uk.gov.hmcts.cp.integration;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import uk.gov.hmcts.cp.config.TestContainersInitialise;

@SpringBootTest
@ContextConfiguration(initializers = TestContainersInitialise.class)
@AutoConfigureMockMvc
@Slf4j
public abstract class IntegrationTestBase {
Expand Down
Loading
Loading