Skip to content

Commit a8b01c2

Browse files
AMP-135 - refactored and resolved the test issues.
1 parent 7525919 commit a8b01c2

File tree

15 files changed

+188
-113
lines changed

15 files changed

+188
-113
lines changed

Dockerfile

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,4 @@
1-
# ---- Base image (default fallback) ----
2-
ARG BASE_IMAGE
3-
FROM ${BASE_IMAGE:-eclipse-temurin:21}
4-
5-
# ---- Runtime arguments ----
6-
ARG JAR_FILENAME
7-
ARG JAR_FILE_PATH
8-
9-
ENV JAR_FILENAME=${JAR_FILENAME:-app.jar}
10-
ENV JAR_FILE_PATH=${JAR_FILE_PATH:-build/libs}
11-
ENV JAR_FULL_PATH=$JAR_FILE_PATH/$JAR_FILENAME
12-
13-
# ---- Set runtime ENV for Spring Boot to bind port
14-
ARG SERVER_PORT
15-
ENV SERVER_PORT=${SERVER_PORT:-8082}
16-
17-
# ---- Dependencies ----
18-
RUN apt-get update \
19-
&& apt-get install -y curl \
20-
&& rm -rf /var/lib/apt/lists/*
21-
22-
# ---- Application files ----
23-
COPY $JAR_FULL_PATH /opt/app/app.jar
24-
COPY lib/applicationinsights.json /opt/app/
25-
26-
# ---- Permissions ----
27-
RUN chmod 755 /opt/app/app.jar
28-
29-
# ---- Runtime ----
30-
EXPOSE ${SERVER_PORT:-8082}
31-
32-
# Documented runtime configuration
33-
# JWT secret for token verification (Base64-encoded HS256 key)
34-
ENV JWT_SECRET_KEY="it-must-be-a-string-secret-at-least-256-bits-long"
35-
1+
FROM eclipse-temurin:21
2+
WORKDIR /opt/app
3+
COPY build/libs/*.jar /opt/app/app.jar
364
CMD ["java", "-jar", "/opt/app/app.jar"]

Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Makefile for Docker Compose services
2+
3+
.PHONY: build up down restart logs
4+
5+
build:
6+
@echo "Building Gradle JAR..."
7+
gradle clean bootJar
8+
@echo "Building Docker image..."
9+
docker-compose build
10+
11+
up:
12+
docker-compose --env-file local.env up -d
13+
14+
down:
15+
docker-compose down
16+
17+
restart: down up
18+
19+
logs:
20+
docker-compose logs -f

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ dependencies {
4545

4646
// --- Observability / Actuator / OTEL / Prometheus ---
4747
implementation 'org.springframework.boot:spring-boot-starter-actuator'
48+
implementation 'org.hibernate.validator:hibernate-validator'
4849
implementation 'org.springframework.boot:spring-boot-starter-opentelemetry'
4950
implementation 'io.jsonwebtoken:jjwt:0.13.0'
5051
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

docker-compose.yml

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
version: '3.9'
2+
13
services:
24
postgres:
35
image: postgres:16-alpine
@@ -7,29 +9,21 @@ services:
79
POSTGRES_PASSWORD: postgres
810
ports:
911
- "5432:5432"
12+
healthcheck:
13+
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
14+
interval: 10s
15+
timeout: 5s
16+
retries: 5
1017

1118
service-hmcts-marketplace-springboot-template:
1219
env_file:
13-
- .env
20+
- local.env
1421
build:
1522
context: .
1623
dockerfile: Dockerfile
17-
args:
18-
http_proxy: ${http_proxy}
19-
https_proxy: ${https_proxy}
20-
no_proxy: ${no_proxy}
21-
BASE_IMAGE: ${BASE_IMAGE}
22-
SERVER_PORT: ${SERVER_PORT}
23-
JAR_FILENAME: ${JAR_FILENAME}
24-
JAR_FILE_PATH: ${JAR_FILE_PATH}
25-
environment:
26-
- SERVER_PORT=${SERVER_PORT:-8082}
27-
- APP_NAME_DATASOURCE_URL=${APP_NAME_DATASOURCE_URL:-jdbc:postgresql://db:5432/appdb}
28-
- APP_NAME_DATASOURCE_USERNAME=${APP_NAME_DATASOURCE_USERNAME:-app}
29-
- APP_NAME_DATASOURCE_PASSWORD=${APP_NAME_DATASOURCE_PASSWORD:-app}
3024
depends_on:
3125
postgres:
3226
condition: service_healthy
3327
ports:
34-
- "${SERVER_PORT:-8082}:${SERVER_PORT:-8082}"
28+
- "8082:8082"
3529

local.env

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# ------------------------------
2+
# Spring Boot Application
3+
# ------------------------------
4+
5+
# Application
6+
SPRING_APPLICATION_NAME=service-UPDATE-TO-BE-NAME-OF-SERVICE
7+
8+
# Datasource
9+
APP_NAME_DATASOURCE_URL=jdbc:postgresql://host.docker.internal:5432/postgres
10+
APP_NAME_DATASOURCE_USERNAME=postgres
11+
APP_NAME_DATASOURCE_PASSWORD=postgres
12+
13+
# JPA
14+
SPRING_JPA_OPEN_IN_VIEW=false
15+
16+
# Flyway
17+
FLYWAY_ENABLED=true
18+
FLYWAY_LOCATIONS=classpath:db/migration
19+
FLYWAY_BASELINE_ON_MIGRATE=true
20+
21+
# Server
22+
SERVER_PORT=8082
23+
MANAGEMENT_SERVER_PORT=8082
24+
SERVER_SHUTDOWN=graceful
25+
SERVER_HTTP2_ENABLED=true
26+
SERVER_FORWARD_HEADERS_STRATEGY=framework
27+
SERVER_COMPRESSION_ENABLED=true
28+
SERVER_COMPRESSION_MIN_RESPONSE_SIZE=2KB
29+
SERVER_COMPRESSION_MIME_TYPES=application/json
30+
31+
# Management / Actuator
32+
MANAGEMENT_ENDPOINTS_WEB_BASE_PATH=/actuator
33+
MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE=health,info,prometheus
34+
MANAGEMENT_ENDPOINT_HEALTH_PROBES_ENABLED=true
35+
36+
# Metrics / Tracing
37+
CLUSTER_NAME=local
38+
REGION=local
39+
TRACING_SAMPLER_PROBABILITY=1.0
40+
OTEL_TRACES_URL=http://localhost:4318/traces
41+
OTEL_METRICS_ENABLED=false
42+
OTEL_METRICS_URL=http://localhost:4318/metrics
43+
44+
# Azure Application Insights
45+
RPE_APPINSIGHTS_INSTRUMENTATIONKEY=00000000-0000-0000-0000-000000000000
46+
47+
# JWT
48+
JWT_SECRET_KEY=it-must-be-a-string-secret-at-least-256-bits-long
49+
JWT_FILTER_ENABLED=false
50+
51+
# Logging
52+
LOGGING_LEVEL_ROOT=INFO
53+
LOGGING_CONFIG=classpath:logback.xml
54+

src/main/java/uk/gov/hmcts/cp/controllers/ExampleController.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
package uk.gov.hmcts.cp.controllers;
22

3-
import lombok.AllArgsConstructor;
3+
import jakarta.validation.constraints.NotNull;
4+
import lombok.RequiredArgsConstructor;
45
import lombok.extern.slf4j.Slf4j;
56
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.PathVariable;
68
import org.springframework.web.bind.annotation.RestController;
79
import uk.gov.hmcts.cp.openapi.api.ExamplesApi;
810
import uk.gov.hmcts.cp.openapi.model.ExampleResponse;
911
import uk.gov.hmcts.cp.services.ExampleService;
1012

1113
@RestController
12-
@AllArgsConstructor
14+
@RequiredArgsConstructor
1315
@Slf4j
1416
public class ExampleController implements ExamplesApi {
1517

1618
private final ExampleService exampleService;
1719

1820
@Override
19-
public ResponseEntity<ExampleResponse> getExampleByExampleId(final Long exampleId) {
21+
public ResponseEntity<ExampleResponse> getExampleByExampleId(@PathVariable("example_id") @NotNull Long exampleId) {
22+
2023
log.info("getExampleByExampleId example for {}", exampleId);
2124
return ResponseEntity.ok(exampleService.getExampleById(exampleId));
2225
}

src/main/java/uk/gov/hmcts/cp/entities/ExampleEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
import lombok.NoArgsConstructor;
1212

1313
@Entity
14-
@Table(name = "example")
1514
@Getter
1615
@Builder(toBuilder = true)
1716
@NoArgsConstructor
1817
@AllArgsConstructor
18+
@Table(name = "example")
1919
public class ExampleEntity {
2020

2121
@Id

src/main/java/uk/gov/hmcts/cp/services/ExampleService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ExampleService {
1717
private final ExampleMapper exampleMapper;
1818

1919
public ExampleResponse getExampleById(final long exampleId) {
20-
ExampleEntity entity = exampleRepository.getReferenceById(exampleId);
20+
ExampleEntity entity = exampleRepository.findById(exampleId).orElseThrow();
2121
return exampleMapper.mapExampleToResponse(entity);
2222
}
2323

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
package uk.gov.hmcts.cp.config;
22

3-
import lombok.SneakyThrows;
4-
import lombok.extern.slf4j.Slf4j;
53
import org.springframework.boot.test.util.TestPropertyValues;
64
import org.springframework.context.ApplicationContextInitializer;
75
import org.springframework.context.ConfigurableApplicationContext;
86
import org.testcontainers.containers.PostgreSQLContainer;
97

10-
@Slf4j
11-
public class TestContainersInitialise implements ApplicationContextInitializer<ConfigurableApplicationContext> {
8+
public class TestContainersInitialise
9+
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
1210

13-
private static final PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer(
14-
"postgres")
15-
.withDatabaseName("postgres")
16-
.withUsername("postgres")
17-
.withPassword("postgres");
11+
private static final PostgreSQLContainer<?> postgreSQLContainer =
12+
new PostgreSQLContainer<>("postgres:15.3")
13+
.withDatabaseName("postgres")
14+
.withUsername("postgres")
15+
.withPassword("postgres")
16+
.withReuse(true);
1817

18+
static {
19+
postgreSQLContainer.start(); // start once
20+
}
1921

20-
@SneakyThrows
2122
@Override
22-
public void initialize(ConfigurableApplicationContext applicationContext) {
23-
postgreSQLContainer.start();
23+
public void initialize(ConfigurableApplicationContext context) {
24+
2425
TestPropertyValues.of(
2526
"spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
2627
"spring.datasource.username=" + postgreSQLContainer.getUsername(),
2728
"spring.datasource.password=" + postgreSQLContainer.getPassword()
28-
).applyTo(applicationContext.getEnvironment());
29-
log.info("COLING starting container");
29+
).applyTo(context.getEnvironment());
3030
}
3131
}
32-

src/test/java/uk/gov/hmcts/cp/integration/IntegrationTestBase.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
import jakarta.annotation.Resource;
44
import lombok.extern.slf4j.Slf4j;
5-
import org.springframework.beans.factory.annotation.Autowired;
65
import org.springframework.boot.test.context.SpringBootTest;
76
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
87
import org.springframework.test.context.ContextConfiguration;
98
import org.springframework.test.web.servlet.MockMvc;
109
import uk.gov.hmcts.cp.config.TestContainersInitialise;
11-
import uk.gov.hmcts.cp.entities.ExampleEntity;
12-
import uk.gov.hmcts.cp.repositories.ExampleRepository;
1310

1411
@SpringBootTest
1512
@ContextConfiguration(initializers = TestContainersInitialise.class)
@@ -19,13 +16,4 @@ public abstract class IntegrationTestBase {
1916

2017
@Resource
2118
protected MockMvc mockMvc;
22-
23-
@Autowired
24-
protected ExampleRepository exampleRepository;
25-
26-
protected ExampleEntity insertExample(String text) {
27-
ExampleEntity exampleEntity = exampleRepository.save(ExampleEntity.builder().exampleText(text).build());
28-
log.info("Inserted example:{}", exampleEntity.getId());
29-
return exampleEntity;
30-
}
3119
}

0 commit comments

Comments
 (0)