Skip to content

Commit fbe429a

Browse files
committed
Add integration and unit tests for quotes backend
- Implement ApplicationIntegrationTest to cover API endpoints for quotes, including creation, retrieval, and error handling. - Refactor ApplicationTest to improve test structure and add assertions for quote retrieval and posting. - Create DatabaseTest to validate database interactions, including quote creation, retrieval, updating, and deletion. - Ensure tests utilize an in-memory H2 database for isolation and repeatability. - Add tests for default quote initialization and search functionality.
1 parent afb0312 commit fbe429a

14 files changed

Lines changed: 2373 additions & 558 deletions

File tree

.mise.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ run = [
6464
description = "Wait for infrastructure services to be ready"
6565
run = [
6666
"echo 'Waiting for infrastructure...'",
67-
"for i in {1..30}; do nc -z localhost 5432 2>/dev/null && echo 'PostgreSQL ready' && break || sleep 1; done",
67+
"for i in {1..30}; do nc -z localhost 5432 2>/dev/null && echo 'PostgreSQL (Unleash) ready' && break || sleep 1; done",
68+
"for i in {1..30}; do nc -z localhost 5433 2>/dev/null && echo 'PostgreSQL (Quotes) ready' && break || sleep 1; done",
6869
"for i in {1..30}; do curl -s http://localhost:4242/health >/dev/null && echo 'Unleash ready' && break || sleep 1; done",
6970
"echo '✅ Infrastructure ready'",
7071
]
@@ -109,11 +110,12 @@ run = [
109110
description = "Start infrastructure services (PostgreSQL, OTEL, Unleash)"
110111
run = [
111112
"echo '🏗️ Starting infrastructure services...'",
112-
"docker-compose up -d postgres otel unleash",
113+
"docker-compose up -d postgres quotes-db otel unleash",
113114
"echo '✅ Infrastructure ready:'",
114115
"echo ' 📊 Grafana: http://localhost:4300'",
115116
"echo ' 🎯 Unleash: http://localhost:4242'",
116-
"echo ' 🗄️ PostgreSQL: localhost:5432'",
117+
"echo ' 🗄️ PostgreSQL (Unleash): localhost:5432'",
118+
"echo ' 🗄️ PostgreSQL (Quotes): localhost:5433'",
117119
]
118120

119121
[tasks."infra:down"]

docker-compose.yaml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ services:
1919
volumes:
2020
- db-data:/var/lib/postgresql/data
2121

22+
quotes-db:
23+
image: postgres:16
24+
restart: always
25+
environment:
26+
POSTGRES_USER: quotes
27+
POSTGRES_PASSWORD: quotes
28+
POSTGRES_DB: quotes
29+
ports:
30+
- "5433:5432"
31+
volumes:
32+
- quotes-db-data:/var/lib/postgresql/data
33+
healthcheck:
34+
test: ["CMD-SHELL", "pg_isready -U quotes"]
35+
interval: 5s
36+
timeout: 5s
37+
retries: 5
38+
2239
unleash:
2340
image: unleashorg/unleash-server:6
2441
depends_on:
@@ -41,10 +58,16 @@ services:
4158
context: ./quotes-backend
4259
dockerfile: Dockerfile
4360
depends_on:
44-
- otel
61+
quotes-db:
62+
condition: service_healthy
63+
otel:
64+
condition: service_started
4565
environment:
4666
OTEL_EXPORTER_OTLP_ENDPOINT: http://otel:4317
4767
OTEL_SERVICE_NAME: quotes-backend
68+
DB_URL: jdbc:postgresql://quotes-db:5432/quotes
69+
DB_USERNAME: quotes
70+
DB_PASSWORD: quotes
4871
ports:
4972
- "8080:8080"
5073

@@ -78,4 +101,5 @@ services:
78101
- "3000:3000"
79102

80103
volumes:
81-
db-data:
104+
db-data:
105+
quotes-db-data:

quotes-backend/.mise.toml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ gradle = "latest"
55
[env]
66
GRADLE_OPTS = "-Dorg.gradle.daemon=true -Dorg.gradle.parallel=true"
77
JAVA_TOOL_OPTIONS = "-XX:+UseG1GC"
8+
DB_URL = "jdbc:postgresql://localhost:5433/quotes"
9+
DB_USERNAME = "quotes"
10+
DB_PASSWORD = "quotes"
811

912
[tasks.dev]
1013
description = "Start development server with auto-reload"
@@ -69,3 +72,48 @@ run = "./gradlew dependencyUpdates"
6972
[tasks."dependencies:update"]
7073
description = "Check for outdated dependencies (Gradle requires manual updates)"
7174
run = "./gradlew dependencyUpdates"
75+
[tasks."db:start"]
76+
description = "Start local PostgreSQL database"
77+
run = [
78+
"echo '🗄️ Starting quotes database...'",
79+
"docker-compose -f ../docker-compose.yaml up -d quotes-db",
80+
"echo 'Waiting for database to be ready...'",
81+
"for i in {1..30}; do nc -z localhost 5433 2>/dev/null && echo '✅ Database ready on localhost:5433' && break || sleep 1; done",
82+
]
83+
84+
[tasks."db:stop"]
85+
description = "Stop local PostgreSQL database"
86+
run = [
87+
"echo '🛑 Stopping quotes database...'",
88+
"docker-compose -f ../docker-compose.yaml stop quotes-db",
89+
]
90+
91+
[tasks."db:logs"]
92+
description = "Show database logs"
93+
run = "docker-compose -f ../docker-compose.yaml logs -f quotes-db"
94+
95+
[tasks."db:psql"]
96+
description = "Connect to database with psql"
97+
run = "docker exec -it nais-quotes-quotes-db-1 psql -U quotes -d quotes"
98+
99+
[tasks."db:reset"]
100+
description = "Reset database (WARNING: destroys all data)"
101+
run = [
102+
"echo '⚠️ This will destroy all database data!'",
103+
"read -p 'Are you sure? (yes/no): ' confirm",
104+
"if [ \"$confirm\" = \"yes\" ]; then",
105+
" docker-compose -f ../docker-compose.yaml down -v quotes-db",
106+
" docker-compose -f ../docker-compose.yaml up -d quotes-db",
107+
" echo '✅ Database reset complete'",
108+
"else",
109+
" echo '❌ Reset cancelled'",
110+
"fi",
111+
]
112+
113+
[tasks."dev:with-db"]
114+
description = "Start database and development server"
115+
run = [
116+
"mise run db:start",
117+
"echo '🚀 Starting development server...'",
118+
"./gradlew run --continuous",
119+
]

quotes-backend/.nais/app.yaml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,19 @@ spec:
6565
- emptyDir:
6666
medium: Memory
6767
mountPath: /app/docs
68-
#gcp:
69-
# sqlInstances:
70-
# - name: quotes-backend
71-
# type: POSTGRES_14
72-
# databases:
73-
# - name: quotes-backend
74-
# envVarPrefix: DB
75-
# tier: db-f1-micro
68+
gcp:
69+
sqlInstances:
70+
- name: quotes-backend
71+
type: POSTGRES_16
72+
databases:
73+
- name: quotes
74+
envVarPrefix: DB
75+
tier: db-f1-micro
76+
flags:
77+
- name: cloudsql.enable_pgaudit
78+
value: "on"
79+
- name: pgaudit.log
80+
value: "all"
7681
ingresses:
7782
{{#each ingresses as |url|}}
7883
- {{url}}

0 commit comments

Comments
 (0)