diff --git a/AssessmentDelivery.Web/Web.AzureRelease.config b/AssessmentDelivery.Web/Web.AzureRelease.config index 5d4ef0fc..98d29692 100644 --- a/AssessmentDelivery.Web/Web.AzureRelease.config +++ b/AssessmentDelivery.Web/Web.AzureRelease.config @@ -3,7 +3,7 @@ diff --git a/AssessmentDelivery.Web/Web.Release.config b/AssessmentDelivery.Web/Web.Release.config index 2b77e7d7..a3f6ea69 100644 --- a/AssessmentDelivery.Web/Web.Release.config +++ b/AssessmentDelivery.Web/Web.Release.config @@ -3,7 +3,7 @@ diff --git a/AssessmentDelivery.Web/Web.config b/AssessmentDelivery.Web/Web.config index d080a2a4..d6e92b53 100644 --- a/AssessmentDelivery.Web/Web.config +++ b/AssessmentDelivery.Web/Web.config @@ -9,7 +9,7 @@
- + diff --git a/db/schema/000_create_database.sql b/db/schema/000_create_database.sql index ed725088..3998c543 100644 --- a/db/schema/000_create_database.sql +++ b/db/schema/000_create_database.sql @@ -10,18 +10,6 @@ IF DB_ID('AssessmentDelivery') IS NULL CREATE DATABASE AssessmentDelivery; GO -IF NOT EXISTS (SELECT 1 FROM sys.server_principals WHERE name = 'ad_app') - CREATE LOGIN ad_app WITH PASSWORD = 'Northw1nd!Dev2011', CHECK_POLICY = OFF; -GO - -USE AssessmentDelivery; -GO - -IF NOT EXISTS (SELECT 1 FROM sys.database_principals WHERE name = 'ad_app') -BEGIN - CREATE USER ad_app FOR LOGIN ad_app; - ALTER ROLE db_datareader ADD MEMBER ad_app; - ALTER ROLE db_datawriter ADD MEMBER ad_app; - GRANT EXECUTE TO ad_app; -END -GO +-- MBA-3823: the shared ad_app login (docs/as-is HB-28, MR-07) is retired. Each component now has its +-- own login, created after the tables exist by db/security/036_component_logins.sql; legacy write denial +-- for the single-writer coexistence is db/security/040_single_writer.sql. diff --git a/db/schema/016_relay_checkpoint.sql b/db/schema/016_relay_checkpoint.sql new file mode 100644 index 00000000..207fef18 --- /dev/null +++ b/db/schema/016_relay_checkpoint.sql @@ -0,0 +1,16 @@ +-- 016_relay_checkpoint.sql +-- MBA-3823: idempotency ledger for the scheduling-service outbox relay. One row per applied outbox event, +-- written in the same transaction as the relay's Booking/Waitlist/ExamSession/AuditLog changes, so a +-- replayed event (relay killed mid-batch and restarted) finds its EventId here and is a no-op. +USE AssessmentDelivery; +GO +IF OBJECT_ID('dbo.RelayCheckpoint') IS NOT NULL DROP TABLE dbo.RelayCheckpoint; +GO +CREATE TABLE dbo.RelayCheckpoint ( + EventId UNIQUEIDENTIFIER NOT NULL CONSTRAINT PK_RelayCheckpoint PRIMARY KEY, + EventSeq BIGINT NOT NULL, + EventType VARCHAR(40) NOT NULL, + AppliedAt DATETIME2(3) NOT NULL CONSTRAINT DF_RelayCheckpoint_AppliedAt DEFAULT (SYSUTCDATETIME()) +); +CREATE INDEX IX_RelayCheckpoint_EventSeq ON dbo.RelayCheckpoint (EventSeq); +GO diff --git a/db/security/036_component_logins.sql b/db/security/036_component_logins.sql new file mode 100644 index 00000000..fe610ecd --- /dev/null +++ b/db/security/036_component_logins.sql @@ -0,0 +1,70 @@ +-- 036_component_logins.sql +-- MBA-3823: one login per component instead of the shared ad_app (docs/as-is HB-28, MR-07). +-- Legacy logins keep the broad role membership ad_app had (reader/writer/EXECUTE) so read pages and +-- non-booking tables (Result, ResultUpload, AuditLog, Candidate...) behave as before; 040_single_writer.sql +-- then DENYs the booking/seat writes. ad_sched_relay is the only writer of Booking/Waitlist/seat columns. +-- Local-development passwords only (same convention as 035); Azure uses contained users + secrets. +-- Applied after the seed by db/apply.sh / db/apply.ps1. Server logins survive the DB rebuild, so idempotent. +USE master; +GO +IF NOT EXISTS (SELECT 1 FROM sys.server_principals WHERE name = 'ad_portal') + CREATE LOGIN ad_portal WITH PASSWORD = 'P0rtal!Dev2026', CHECK_POLICY = OFF, DEFAULT_DATABASE = AssessmentDelivery; +GO +IF NOT EXISTS (SELECT 1 FROM sys.server_principals WHERE name = 'ad_booking_api') + CREATE LOGIN ad_booking_api WITH PASSWORD = 'B00kingApi!Dev2026', CHECK_POLICY = OFF, DEFAULT_DATABASE = AssessmentDelivery; +GO +IF NOT EXISTS (SELECT 1 FROM sys.server_principals WHERE name = 'ad_results') + CREATE LOGIN ad_results WITH PASSWORD = 'Resu1ts!Dev2026', CHECK_POLICY = OFF, DEFAULT_DATABASE = AssessmentDelivery; +GO +IF NOT EXISTS (SELECT 1 FROM sys.server_principals WHERE name = 'ad_capacity_sync') + CREATE LOGIN ad_capacity_sync WITH PASSWORD = 'CapSync!Dev2026', CHECK_POLICY = OFF, DEFAULT_DATABASE = AssessmentDelivery; +GO +IF NOT EXISTS (SELECT 1 FROM sys.server_principals WHERE name = 'ad_sched_relay') + CREATE LOGIN ad_sched_relay WITH PASSWORD = 'Sch3dRelay!Dev2026', CHECK_POLICY = OFF, DEFAULT_DATABASE = AssessmentDelivery; +GO +USE AssessmentDelivery; +GO +-- portal (AssessmentDelivery.Web, EF6) +IF NOT EXISTS (SELECT 1 FROM sys.database_principals WHERE name = 'ad_portal') + CREATE USER ad_portal FOR LOGIN ad_portal; +ALTER ROLE db_datareader ADD MEMBER ad_portal; +ALTER ROLE db_datawriter ADD MEMBER ad_portal; +GRANT EXECUTE TO ad_portal; +GO +-- booking-api (Java 8, JdbcTemplate) +IF NOT EXISTS (SELECT 1 FROM sys.database_principals WHERE name = 'ad_booking_api') + CREATE USER ad_booking_api FOR LOGIN ad_booking_api; +ALTER ROLE db_datareader ADD MEMBER ad_booking_api; +ALTER ROLE db_datawriter ADD MEMBER ad_booking_api; +GRANT EXECUTE TO ad_booking_api; +GO +-- results-service (Java 8) +IF NOT EXISTS (SELECT 1 FROM sys.database_principals WHERE name = 'ad_results') + CREATE USER ad_results FOR LOGIN ad_results; +ALTER ROLE db_datareader ADD MEMBER ad_results; +ALTER ROLE db_datawriter ADD MEMBER ad_results; +GRANT EXECUTE TO ad_results; +GO +-- capacity-sync (Java 8) +IF NOT EXISTS (SELECT 1 FROM sys.database_principals WHERE name = 'ad_capacity_sync') + CREATE USER ad_capacity_sync FOR LOGIN ad_capacity_sync; +ALTER ROLE db_datareader ADD MEMBER ad_capacity_sync; +ALTER ROLE db_datawriter ADD MEMBER ad_capacity_sync; +GRANT EXECUTE TO ad_capacity_sync; +GO +-- scheduling-service outbox relay: named-table rights only (no roles, no EXECUTE) +IF NOT EXISTS (SELECT 1 FROM sys.database_principals WHERE name = 'ad_sched_relay') + CREATE USER ad_sched_relay FOR LOGIN ad_sched_relay; +GRANT SELECT, INSERT, UPDATE, DELETE ON dbo.Booking TO ad_sched_relay; +GRANT SELECT, INSERT, UPDATE, DELETE ON dbo.Waitlist TO ad_sched_relay; +GRANT SELECT, UPDATE ON dbo.ExamSession TO ad_sched_relay; +GRANT SELECT, INSERT ON dbo.AuditLog TO ad_sched_relay; +GRANT SELECT, INSERT ON dbo.RelayCheckpoint TO ad_sched_relay; +GRANT SELECT ON dbo.Candidate TO ad_sched_relay; +-- SET IDENTITY_INSERT dbo.Booking ON: relayed bookings keep the BookingId scheduling-service assigned +-- (>= routing.modern-booking-id-floor), so v1 URLs and Result.BookingId resolve the same row on both sides. +GRANT ALTER ON dbo.Booking TO ad_sched_relay; +GO +-- reconciliation reads RelayCheckpoint through the read-only importer login +GRANT SELECT ON dbo.RelayCheckpoint TO ad_sched_import; +GO diff --git a/db/security/040_single_writer.sql b/db/security/040_single_writer.sql new file mode 100644 index 00000000..b8789b6f --- /dev/null +++ b/db/security/040_single_writer.sql @@ -0,0 +1,49 @@ +-- 040_single_writer.sql +-- MBA-3823: scheduling-service is the single writer of bookings and seat counts. Every legacy login loses +-- direct write access to the contested data (docs/as-is/data-ownership.md; HB-01, HB-02, HB-03, HB-04, +-- HB-05, HB-29; MR-01, MR-02, MR-07, MR-17). Booking/capacity writes for all centres go through the routing +-- gateway (:8000) to scheduling-service; its outbox relay (ad_sched_relay) projects them back here. +-- Legacy read pages keep rendering from these tables. ad_sched_import stays read-only (035). +-- DENY beats the db_datawriter / GRANT EXECUTE membership from 036, so writes fail with error 229/230. +USE AssessmentDelivery; +GO +DENY INSERT, UPDATE, DELETE ON dbo.Booking TO ad_portal; +DENY INSERT, UPDATE, DELETE ON dbo.Waitlist TO ad_portal; +DENY UPDATE ON dbo.ExamSession (Capacity, SeatsAvailable, AccommodationCapacity) TO ad_portal; +DENY EXECUTE ON dbo.usp_BookSeat TO ad_portal; +DENY EXECUTE ON dbo.usp_CancelBooking TO ad_portal; +DENY EXECUTE ON dbo.usp_ChangeCapacity TO ad_portal; +DENY EXECUTE ON dbo.usp_RescheduleBooking TO ad_portal; +DENY INSERT, UPDATE, DELETE ON dbo.RelayCheckpoint TO ad_portal; +GO +DENY INSERT, UPDATE, DELETE ON dbo.Booking TO ad_booking_api; +DENY INSERT, UPDATE, DELETE ON dbo.Waitlist TO ad_booking_api; +DENY UPDATE ON dbo.ExamSession (Capacity, SeatsAvailable, AccommodationCapacity) TO ad_booking_api; +DENY EXECUTE ON dbo.usp_BookSeat TO ad_booking_api; +DENY EXECUTE ON dbo.usp_CancelBooking TO ad_booking_api; +DENY EXECUTE ON dbo.usp_ChangeCapacity TO ad_booking_api; +DENY EXECUTE ON dbo.usp_RescheduleBooking TO ad_booking_api; +DENY INSERT, UPDATE, DELETE ON dbo.RelayCheckpoint TO ad_booking_api; +GO +DENY INSERT, UPDATE, DELETE ON dbo.Booking TO ad_results; +DENY INSERT, UPDATE, DELETE ON dbo.Waitlist TO ad_results; +DENY UPDATE ON dbo.ExamSession (Capacity, SeatsAvailable, AccommodationCapacity) TO ad_results; +DENY EXECUTE ON dbo.usp_BookSeat TO ad_results; +DENY EXECUTE ON dbo.usp_CancelBooking TO ad_results; +DENY EXECUTE ON dbo.usp_ChangeCapacity TO ad_results; +DENY EXECUTE ON dbo.usp_RescheduleBooking TO ad_results; +DENY INSERT, UPDATE, DELETE ON dbo.RelayCheckpoint TO ad_results; +GO +DENY INSERT, UPDATE, DELETE ON dbo.Booking TO ad_capacity_sync; +DENY INSERT, UPDATE, DELETE ON dbo.Waitlist TO ad_capacity_sync; +DENY UPDATE ON dbo.ExamSession (Capacity, SeatsAvailable, AccommodationCapacity) TO ad_capacity_sync; +DENY EXECUTE ON dbo.usp_BookSeat TO ad_capacity_sync; +DENY EXECUTE ON dbo.usp_CancelBooking TO ad_capacity_sync; +DENY EXECUTE ON dbo.usp_ChangeCapacity TO ad_capacity_sync; +DENY EXECUTE ON dbo.usp_RescheduleBooking TO ad_capacity_sync; +DENY INSERT, UPDATE, DELETE ON dbo.RelayCheckpoint TO ad_capacity_sync; +GO +-- a leftover shared login from an older apply must not keep a side door open +IF EXISTS (SELECT 1 FROM sys.database_principals WHERE name = 'ad_app') + DROP USER ad_app; +GO diff --git a/db/triggers/032_trg_Booking_SeatSync.sql b/db/triggers/032_trg_Booking_SeatSync.sql index c8addb9b..a3f2ce57 100644 --- a/db/triggers/032_trg_Booking_SeatSync.sql +++ b/db/triggers/032_trg_Booking_SeatSync.sql @@ -4,6 +4,12 @@ migration) instead of going through usp_BookSeat. usp_BookSeat already adjusts the counter for portal bookings. 2013-04 migration cutover + + MBA-3823: returns early for writes made by the scheduling-service relay + (SESSION_CONTEXT writer = scheduling-relay). The relay projects exactly the + seat count scheduling-service decided, so the extra non-PORTAL decrement + (docs/as-is HB-01 / HB-04, MR-01) must not fire on top of it. Every other + writer keeps the legacy behaviour. */ USE AssessmentDelivery; GO @@ -15,6 +21,8 @@ AS BEGIN SET NOCOUNT ON; + IF CONVERT(NVARCHAR(64), SESSION_CONTEXT(N'writer')) = N'scheduling-relay' RETURN; + UPDATE s SET s.SeatsAvailable = s.SeatsAvailable - x.Cnt FROM dbo.ExamSession s diff --git a/db/triggers/033_trg_ExamSession_Update.sql b/db/triggers/033_trg_ExamSession_Update.sql index ee7f59d3..da8fd1a5 100644 --- a/db/triggers/033_trg_ExamSession_Update.sql +++ b/db/triggers/033_trg_ExamSession_Update.sql @@ -2,6 +2,12 @@ trg_ExamSession_Update - maintains ExamSession.LastModified. (Not every caller remembers to set it - the Java services in particular.) 2014-11 + + MBA-3823: for writes made by the scheduling-service relay + (SESSION_CONTEXT writer = scheduling-relay) only the LastModified stamp runs. + The newest-first demotion, waitlist bump and SeatsAvailable recompute + (docs/as-is HB-05 / HB-16, MR-01) are skipped because the relay already + writes the bookings, waitlist rows and seat count scheduling-service decided. */ USE AssessmentDelivery; GO @@ -21,6 +27,8 @@ BEGIN FROM dbo.ExamSession s JOIN inserted i ON i.SessionId = s.SessionId; + IF CONVERT(NVARCHAR(64), SESSION_CONTEXT(N'writer')) = N'scheduling-relay' RETURN; + IF UPDATE(Capacity) BEGIN DECLARE @b TABLE (BookingId INT PRIMARY KEY, SessionId INT, CandidateId INT, BookedAt DATETIME); diff --git a/docs/adr/0003-single-writer-outbox-relay.md b/docs/adr/0003-single-writer-outbox-relay.md new file mode 100644 index 00000000..1e1752ee --- /dev/null +++ b/docs/adr/0003-single-writer-outbox-relay.md @@ -0,0 +1,63 @@ +# ADR 0003: scheduling-service is the single writer; legacy tables are an outbox projection (MBA-3823, M4) + +Status: accepted. Builds on [ADR 0002](0002-ported-rule-decisions.md) and the M3 routing gateway. + +## Context + +`ExamSession.SeatsAvailable` has several writers with different formulas: the portal stored procedures, booking-api's +double decrement (HB-01) and leaks (HB-02), capacity-sync's nightly recompute (HB-03, MR-17) and the two triggers +(HB-04, HB-05, HB-16). That is the root of the seat drift in [MR-01 / MR-02](../as-is/migration-risks.md). All +components share the high-privilege `ad_app` login (HB-28, MR-07), so nothing stops a new writer appearing. + +## Decisions + +1. **One writer.** Only scheduling-service decides bookings, waitlist, attendance and capacity. The gateway sends + booking/capacity traffic for every centre to it (`routing.modern-centers=*`, `routing.coexistence-mode=relay`). + Centre-less portal pages (`/Booking/MyBookings` without `centerCode`, candidate pages, capacity audit) and every + legacy reader keep reading `AssessmentDelivery`. +2. **Per-component logins** (`db/security/036_component_logins.sql`): `ad_portal`, `ad_booking_api`, `ad_results`, + `ad_capacity_sync`, `ad_sched_import` (read-only, M2) and `ad_sched_relay`; `ad_app` is no longer created and its + database user is dropped (HB-28, MR-07). Azure configs carry placeholders only. +3. **Legacy write denial** (`db/security/040_single_writer.sql`, deliberate change to HB-01/HB-02/HB-29 paths): legacy + logins get `DENY INSERT/UPDATE/DELETE` on `dbo.Booking` and `dbo.Waitlist`, `DENY UPDATE` on + `ExamSession(Capacity, SeatsAvailable, AccommodationCapacity)` and `DENY EXECUTE` on `usp_BookSeat`, + `usp_CancelBooking`, `usp_ChangeCapacity` and `usp_RescheduleBooking`. A direct legacy write now fails with SQL + Server error 229/230; booking-api maps it to `403 LEGACY_WRITE_DENIED` (send writes through `:8000`). +4. **Transactional outbox** (`V2__outbox.sql`, `outbox_event`, `event_seq` identity = monotonic sequence): written in the + same transaction as `book`, `cancel`, `reschedule`, `recordAttendance` and `changeCapacity`. Event types + `BookingCreated`, `BookingWaitlisted`, `BookingCancelled`, `BookingRescheduled`, `AttendanceRecorded`, + `CapacityChanged`, plus `InventoryBaselined` written by a `mode=reset` snapshot import so the first relay pass + projects the derived seat counts. Each event carries the full resulting state of the touched bookings and + sessions (state transfer, not deltas), so applying it twice is harmless. +5. **Idempotent relay** (`com.northwind.scheduling.relay`, login `ad_sched_relay`, `SESSION_CONTEXT(N'writer') = + N'scheduling-relay'`): one legacy transaction per event upserts `Booking`, `Waitlist`, `ExamSession` and one + `AuditLog` row, then inserts the event id into `dbo.RelayCheckpoint`; an event already checkpointed is a no-op. + The outbox row is marked relayed afterwards, so a crash between the two only causes a no-op replay. +6. **Booking ids.** Modern booking ids are already disjoint from legacy ids (imported rows keep the legacy id, new rows + start at `routing.modern-booking-id-floor=50000000`), so the relay writes `dbo.Booking.BookingId = booking_id` + with `SET IDENTITY_INSERT` on first relay and stores it in `booking.legacy_booking_id`. `Result.BookingId`, v1 URLs + and results-service attendance keep working with the same number. +7. **Trigger neutralization for relayed writes only** (deliberate change, HB-01/HB-04 in `032_trg_Booking_SeatSync`, + HB-05/HB-16 in `033_trg_ExamSession_Update`, MR-01/MR-14): both triggers return early when the writer is + `scheduling-relay`; `trg_ExamSession_Update` still stamps `LastModified`. Any other writer still gets the legacy + side effects (characterized by `LegacySingleWriterIT`). +8. **Remaining legacy writers.** results-service calls `POST /api/v2/bookings/{bookingId}/attendance` instead of + `UPDATE dbo.Booking SET Status = 'ATTENDED'`; capacity-sync's `runSync` is disabled by `capacity.sync.enabled=false` + and only writes its `CAPACITY_SYNC` audit row with `SKIPPED` (HB-03, MR-17; deliberate change); booking-api keeps + its v1 contract behind the gateway, which already serves v1 for modern centres. +9. **Reconciliation replaces capacity-sync.** `GET /internal/reconciliation` and `java -jar scheduling-service.jar + reconcile` compare every session (Capacity, SeatsAvailable, BOOKED/ATTENDED/WAITLISTED counts, accommodation seats) + and every booking (session, status, accommodation code) and include `db/checks/seat-drift.sql`; the report is + written to `reports/reconciliation-.md`. Exit code 0 only with 0 mismatches and 0 drift rows. Attendance needs a service credential: header `X-Service-Token`, compared in constant time with `scheduling.attendance.service-token` (env `SCHEDULING_ATTENDANCE_SERVICE_TOKEN`). The comparison fails closed when the property is unset. results-service sends `scheduling.service-token`, and the gateway returns 404 for `/api/v2/bookings/*/attendance`. The other v2 writes stay open like the legacy no-auth model (MR-16), but attendance writes ATTENDED into legacy, so results integrity depends on it. + +## Not changed + +Other legacy oddities stay as documented (e.g. HB-30 destructive `db/apply.*`, HB-31 sessions never closed, HB-17 +audit actor inconsistencies). The relay writes `AuditLog.Actor = scheduling-relay` for its own rows only. + +## Consequences + +- Legacy availability is now exactly `Capacity - (BOOKED + ATTENDED)` as decided by scheduling-service; seat drift + is 0 after mixed traffic (see `reports/`). +- Legacy readers lag by the relay delay (poll `scheduling.relay.poll-ms=250`; p95 well under 2 s locally). +- FU-3 of ADR 0002 (retire capacity-sync and `SeatsAvailable`) is now possible. diff --git a/docs/runbooks/scheduling-service.md b/docs/runbooks/scheduling-service.md index 2b070725..a96f96b7 100644 --- a/docs/runbooks/scheduling-service.md +++ b/docs/runbooks/scheduling-service.md @@ -126,33 +126,50 @@ jq -c 'select(.operation) | {durationMs,operation,centerCode,sessionId,traceId}' is down. Bookings already in `Scheduling` keep working; snapshot refresh (`POST /api/v2/internal/import/sessions`) and reference reads that need legacy fail until it is back. -### Outbox lag (after M4) - -Not applicable until M4 adds the outbox and the `scheduling_outbox_lag_seconds` / `scheduling_outbox_pending` gauges. -Then: lag rising with pending rising means the relay is stuck or the legacy write-back target is down (check its -readiness and relay ERROR logs by `traceId`); lag rising with pending flat means one poison message (find the oldest -pending row and its error). While lag is high, legacy channels see stale availability: consider rolling the affected -centres back (§5) until the relay catches up. +### Outbox lag (relay, MBA-3823) + +Every booking/capacity change writes an `outbox_event` row in the same transaction (`event_seq` = order). The relay +(`com.northwind.scheduling.relay`, login `ad_sched_relay`, `scheduling.relay.*` / `SCHEDULING_RELAY_*`) polls every +`scheduling.relay.poll-ms` (250 ms) and applies each event to `AssessmentDelivery` in one transaction, recording the +event id in `dbo.RelayCheckpoint` ([ADR 0003](../adr/0003-single-writer-outbox-relay.md)). Gauges: +`scheduling_outbox_pending`, `scheduling_outbox_lag_seconds`; `GET /internal/relay` shows +`total/pending/lastSeq/lastRelayedSeq/oldestPendingAt` plus applied/skipped counters. + +- Lag rising with pending rising: the relay is stuck or `AssessmentDelivery` is down/locked. Check the relay WARN logs + (`relay poll failed`, `relay event ... failed`) and SQL Server. +- Lag rising with pending flat: one poison event. `SELECT TOP 5 event_seq, event_type, attempts, last_error FROM + outbox_event WHERE relayed_at IS NULL ORDER BY event_seq` in `Scheduling`. Fix the cause and let it retry; never + delete outbox rows. +- Restarting the service (or killing it mid-batch) is safe: an event already checkpointed is skipped, so replays write + nothing twice. +- While lag is high, legacy readers (portal pages, booking-api `GET`, results) show stale availability. Writes are not + affected (scheduling-service is the only writer). ### Non-zero reconciliation report -The legacy estate has no single seat formula and several writers drift `ExamSession.SeatsAvailable` -([MR-01, MR-02](../as-is/migration-risks.md), HB-01..HB-05, nightly capacity-sync masking partner drift MR-17), so a -difference between modern and legacy availability is expected for some sessions and is not by itself an incident. - -1. Run the legacy drift check: `sqlcmd -C -S localhost -U sa -d AssessmentDelivery -i db/checks/seat-drift.sql` and - compare the listed sessions with `GET /api/v2/sessions/{id}` (modern availability is derived from bookings, - [ADR 0002](../adr/0002-ported-rule-decisions.md)). -2. Sessions that differ only by legacy counter drift: record them, no action on the modern side. -3. Bookings present on one side only, or a session over capacity in `Scheduling`: stop new modern bookings for that - centre (roll back, §5), keep the evidence (`bookingId`, `traceId` from the logs), and escalate to the scheduling - owners. Do not hand-edit `Scheduling` rows. -4. For a centre that is not (yet) served by modern, refresh its snapshot with - `curl -X POST 'localhost:8090/api/v2/internal/import/sessions?mode=merge'` (never `mode=reset` in production: it - re-copies the inventory). +`GET /internal/reconciliation` (or `java -jar target/scheduling-service-*.jar reconcile`, exit 0 only when +clean) compares every session (Capacity, SeatsAvailable, BOOKED/ATTENDED/WAITLISTED, accommodation +seats used) and every booking (session, status, accommodation code) between `Scheduling` and `AssessmentDelivery`, +runs `db/checks/seat-drift.sql`, and writes `reports/reconciliation-.md`. It replaces capacity-sync's +nightly recompute, which is disabled (`capacity.sync.enabled=false`, HB-03, MR-17). + +With the relay live and the legacy write denial in place (`db/security/040_single_writer.sql`) the expected result is +0 mismatches and 0 drift rows. If not: + +1. Check relay lag first (above). Mismatches that disappear once `pending` is 0 are lag, not drift. +2. A legacy-only booking or a SeatsAvailable/count difference after the relay caught up means something wrote + `AssessmentDelivery` outside the relay: check that `040_single_writer.sql` is applied + (`SELECT * FROM sys.database_permissions WHERE state = 'D'`) and that no component still uses `sa` or `ad_app`. + Legacy writers otherwise reintroduce HB-01..HB-05 drift ([MR-01, MR-02](../as-is/migration-risks.md)). +3. A modern-only booking: the relay has not applied it yet or failed on it (see poison event above). +4. Do not hand-edit either database; keep the report and escalate to the scheduling owners. ## 5. Roll a centre back to legacy (routing allow-list, M3) +> **Single writer (MBA-3823).** Once `db/security/040_single_writer.sql` is applied, routing a centre back to legacy +> only moves its *reads*: legacy booking/capacity writes stay denied for every centre, so the steps below do not restore +> legacy writes on their own. To hand writes back to legacy use §6 (it is all-or-nothing, not per centre). + Which backend serves a centre is decided by the routing allow-list `scheduling.routing.modern-centers` (env `SCHEDULING_ROUTING_MODERN_CENTERS`, comma-separated centre codes, `*` = all; everything else goes to `SCHEDULING_ROUTING_DEFAULT_TARGET`, default `legacy`). The M3 routing layer consults it per request. @@ -170,3 +187,40 @@ Which backend serves a centre is decided by the routing allow-list `scheduling.r 3. Verify: `routing/resolve` returns `"target":"legacy"`, and `rate(scheduling_booking_duration_seconds_count{centerCode="SFO01"}[5m])` drops to 0 while legacy portal/booking-api traffic for the centre resumes. 4. Bookings taken by modern for that centre stay in `Scheduling`; reconcile them (§4) before routing the centre back. + +## 6. Single-writer cutover and rollback (MBA-3823) + +> Attendance credential. `POST /api/v2/bookings/{id}/attendance` answers 401 unless `X-Service-Token` equals +> `scheduling.attendance.service-token` (`SCHEDULING_ATTENDANCE_SERVICE_TOKEN`; unset = all refused). results-service must send the +> same value (`SCHEDULING_SERVICE_TOKEN`). If they differ, result ingestion fails with 502 `SCHEDULING_UNAVAILABLE`; check with +> `curl -i -X POST -H "X-Service-Token: $SCHEDULING_SERVICE_TOKEN" http://localhost:8090/api/v2/bookings/0/attendance` +> (401 = mismatch, 404 `BOOKING_NOT_FOUND` = token accepted; booking 0 never exists, so nothing is written). The gateway returns 404 for attendance on :8000, so results-service calls :8090 directly. + +Cutover (order matters: bookings legacy takes between the snapshot and the denial would be missing from `Scheduling`, +because the importer only copies bookings when the `Scheduling` DB has no sessions or on a RESET import): + +1. Apply `db/security/036_component_logins.sql` and `db/security/040_single_writer.sql` (legacy writes now fail with a + permission error; booking-api answers `403 LEGACY_WRITE_DENIED`). +2. Re-import with the legacy estate quiesced: drop/recreate the `Scheduling` DB (or run the RESET import) and start + scheduling-service with the relay on. The import emits `InventoryBaselined`, which the relay projects once. +3. `GET /internal/reconciliation` must report 0 mismatches and 0 seat-drift rows. +4. Only then set `routing.coexistence-mode=relay` and `routing.modern-centers=*` in the gateway. + +Rollback to legacy writes (all centres at once): + +1. Set `routing.modern-centers=` (empty) in the gateway so no new centre-routed writes reach scheduling-service; wait + for `GET /internal/relay` `pending` = 0 so every modern change is in `AssessmentDelivery` (same `BookingId`s). +2. Revoke the denial: for each legacy login, `REVOKE INSERT, UPDATE, DELETE ON dbo.Booking / dbo.Waitlist`, + `REVOKE UPDATE ON dbo.ExamSession (Capacity, SeatsAvailable, AccommodationCapacity)` and + `REVOKE EXECUTE` on the four booking procedures (the inverse of `040_single_writer.sql`). +3. Hand attendance back: set `results.scheduling.base-url=` (empty) in results-service and restart it, so + `AttendanceRecorder` uses the legacy direct `UPDATE` again (it needs step 2). +4. Hand the modern-owned ids back: the gateway routes any `BookingId >= routing.modern-booking-id-floor` to + scheduling-service regardless of the allow-list, so raise the floor above every id in use (e.g. + `routing.modern-booking-id-floor=9223372036854775807`); those bookings are then served by legacy, which already holds + them from the relay. The floor is read at gateway startup (only `routing.modern-centers` hot-reloads), so restart + the gateway after editing it. Verify `GET :8000/api/v1/bookings/` answers with `X-Route: legacy`. +5. Only now stop scheduling-service and, if seat recomputation is wanted again, set `capacity.sync.enabled=true` in + capacity-sync. Legacy drift (HB-01..HB-05) returns with the legacy writers. +6. Before routing writes to modern again, restore `routing.modern-booking-id-floor=50000000` (gateway restart) and + repeat the cutover from step 2: `Scheduling` must be re-imported. diff --git a/reports/attendance-auth-live-check.txt b/reports/attendance-auth-live-check.txt new file mode 100644 index 00000000..914fa01b --- /dev/null +++ b/reports/attendance-auth-live-check.txt @@ -0,0 +1,42 @@ +# MBA-3823 attendance service-token live check, 2026-11-11T22:04:39Z, head 5c4dcbd + +$ curl -X POST :8000/api/v2/bookings {sessionId:154, candidateNumber:NW00000960} +HTTP/1.1 201 Created +X-Route: modern +{"bookingId":50000004,"status":"BOOKED","waitlistPosition":null,"booking":{"bookingId":50000004,"sessionId":154,"candidateNumber":"NW00000960","candidateName":"Amelia Zielinski","examCode":"MHL-RN1"," +booking 50000004: Scheduling=BOOKED legacy=BOOKED + +$ curl -X POST :8090/api/v2/bookings/50000004/attendance (no token) +HTTP/1.1 401 +{"error":"UNAUTHORIZED","message":"X-Service-Token missing or invalid","legacyCode":null} + +$ curl -X POST :8090/api/v2/bookings/50000004/attendance -H 'X-Service-Token: wrong' +HTTP/1.1 401 +{"error":"UNAUTHORIZED","message":"X-Service-Token missing or invalid","legacyCode":null} + +$ curl -X POST :8000/api/v2/bookings/50000004/attendance (no token, via gateway) +HTTP/1.1 404 Not Found +X-Route: modern +{"error":"NOT_FOUND","message":"scheduling-service internal endpoints are not exposed through the gateway"} + +$ curl -X POST :8000/api/v2/bookings/50000004/attendance -H 'X-Service-Token: ' (valid token, via gateway) +HTTP/1.1 404 Not Found +X-Route: modern +{"error":"NOT_FOUND","message":"scheduling-service internal endpoints are not exposed through the gateway"} +after rejected calls: Scheduling=BOOKED legacy=BOOKED AttendanceRecorded events for 50000004=0 + +$ curl -X POST :8000/api/v1/results {bookingId:50000004, score:72} +HTTP/1.1 201 Created +X-Route: legacy +{ +after result: Scheduling=ATTENDED legacy=ATTENDED AttendanceRecorded events for 50000004=1 +legacy audit: Booking|ATTENDED|scheduling-service + +$ curl :8090/internal/relay +{"total":8,"pending":0,"lastSeq":8,"lastRelayedSeq":8,"oldestPendingAt":null} + +$ curl :8090/internal/reconciliation +{'generatedAt': '2026-11-11T22:04:45', 'sessionsCompared': 462, 'bookingsCompared': 3591, 'sessionMismatches': 0, 'bookingMismatches': 0, 'seatDrift': 0, 'reportFile': 'C:\\Users\\Administrator\\repos\\SampleMvcWebApp\\reports\\reconciliation-20261111-220445.md'} + +$ sqlcmd -d AssessmentDelivery -i db/checks/seat-drift.sql +rows: 0 diff --git a/reports/reconciliation-20261111-205122-legacy-baseline.md b/reports/reconciliation-20261111-205122-legacy-baseline.md new file mode 100644 index 00000000..a13c92d9 --- /dev/null +++ b/reports/reconciliation-20261111-205122-legacy-baseline.md @@ -0,0 +1,35 @@ +# Reconciliation: Scheduling vs AssessmentDelivery + +Generated 2026-11-11T20:51:22 by scheduling-service `reconcile` (MBA-3823). + +| check | result | +|---|---| +| sessions compared | 462 | +| bookings compared | 3627 | +| session field mismatches | 2 | +| booking mismatches | 0 | +| **total mismatches** | **2** | +| `db/checks/seat-drift.sql` rows | 2 | + +Per session: Capacity, SeatsAvailable (Scheduling derives Capacity - (BOOKED + ATTENDED), floored at 0; legacy is the stored counter), BOOKED / ATTENDED / WAITLISTED counts and accommodation seats used (BOOKED + ATTENDED with ET25/ET50). Per booking: session, status and accommodation code, keyed on the legacy BookingId (`booking.legacy_booking_id`). + +## Session mismatches + +| SessionId | field | Scheduling | AssessmentDelivery | +|---|---|---|---| +| 155 | seatsAvailable | 8 | 1 | +| 158 | seatsAvailable | 4 | 0 | + +## Booking mismatches + +None. + +## `db/checks/seat-drift.sql` + +Source: `db/checks/seat-drift.sql`. 2 rows: + +| SessionId | ExamCode | CenterCode | StartsAt | Capacity | SeatsAvailable | ActiveBookings | Drift | +|---|---|---|---|---|---|---|---| +| 155 | HPI-ACC2 | DUB01 | 2026-11-12T13:30 | 16 | 1 | 8 | 7 | +| 158 | BCB-AGL | LDS01 | 2026-11-14T09:00 | 11 | 0 | 7 | 4 | + diff --git a/reports/reconciliation-20261111-205430.md b/reports/reconciliation-20261111-205430.md new file mode 100644 index 00000000..e40aecba --- /dev/null +++ b/reports/reconciliation-20261111-205430.md @@ -0,0 +1,26 @@ +# Reconciliation: Scheduling vs AssessmentDelivery + +Generated 2026-11-11T20:54:30 by scheduling-service `reconcile` (MBA-3823). + +| check | result | +|---|---| +| sessions compared | 462 | +| bookings compared | 3627 | +| session field mismatches | 0 | +| booking mismatches | 0 | +| **total mismatches** | **0** | +| `db/checks/seat-drift.sql` rows | 0 | + +Per session: Capacity, SeatsAvailable (Scheduling derives Capacity - (BOOKED + ATTENDED), floored at 0; legacy is the stored counter), BOOKED / ATTENDED / WAITLISTED counts and accommodation seats used (BOOKED + ATTENDED with ET25/ET50). Per booking: session, status and accommodation code, keyed on the legacy BookingId (`booking.legacy_booking_id`). + +## Session mismatches + +None. + +## Booking mismatches + +None. + +## `db/checks/seat-drift.sql` + +Source: `db/checks/seat-drift.sql`. 0 rows (no drift). diff --git a/reports/seat-drift-legacy-baseline.txt b/reports/seat-drift-legacy-baseline.txt new file mode 100644 index 00000000..1dbf03c2 --- /dev/null +++ b/reports/seat-drift-legacy-baseline.txt @@ -0,0 +1,6 @@ +Changed database context to 'AssessmentDelivery'. +SessionId|ExamCode|CenterCode|StartsAt|Capacity|SeatsAvailable|ActiveBookings|Drift +---------|--------|----------|--------|--------|--------------|--------------|----- +155|HPI-ACC2|DUB01|2026-11-12 13:30:00.000|16|1|8|7 +158|BCB-AGL|LDS01|2026-11-14 09:00:00.000|11|0|7|4 + diff --git a/reports/seat-drift-m4-relay.txt b/reports/seat-drift-m4-relay.txt new file mode 100644 index 00000000..5bff63ed --- /dev/null +++ b/reports/seat-drift-m4-relay.txt @@ -0,0 +1,4 @@ +Changed database context to 'AssessmentDelivery'. + + +(0 rows affected) diff --git a/scripts/mixed-traffic.ps1 b/scripts/mixed-traffic.ps1 index c4d93e16..5cc9604c 100644 --- a/scripts/mixed-traffic.ps1 +++ b/scripts/mixed-traffic.ps1 @@ -91,6 +91,13 @@ Write-Log "sessions: $($sessionIds -join ' ')" Write-Log "candidates: $($candidates[0].Number) .. $($candidates[$total - 1].Number)" $portalIds = New-Object System.Collections.ArrayList +$modernPortalIds = New-Object System.Collections.ArrayList + +function Test-ModernRedirect($r) { + $route = "$($r.Headers['X-Route'])" + $uri = "$($r.BaseResponse.ResponseUri)" + return ($route -eq 'modern') -or ($uri -match '/app/') +} $partnerIds = New-Object System.Collections.ArrayList $stats = @{ PortalOk = 0; PortalFail = 0; PartnerOk = 0; PartnerFail = 0 } @@ -98,6 +105,15 @@ function Add-PortalBooking($cand, [string]$sessionId) { try { $r = Invoke-PortalForm "$PortalUrl/Booking/Book?sessionId=$sessionId&candidateNumber=$($cand.Number)" "$PortalUrl/Booking/Book" @{ SessionId = $sessionId; CandidateNumber = $cand.Number; AccommodationCode = '' } + if (Test-ModernRedirect $r) { + # behind the routing gateway (MBA-3822/3823) the portal form redirects to the modern UI; complete the booking + # the way /app/book does (POST /api/v2/bookings, channel PORTAL) + $body = @{ sessionId = [int]$sessionId; candidateNumber = $cand.Number; channel = 'PORTAL' } | ConvertTo-Json -Compress + $r = Invoke-WebRequest "$PortalUrl/api/v2/bookings" -Method Post -ContentType 'application/json' -Body $body -UseBasicParsing -TimeoutSec 60 + $id = ($r.Content | ConvertFrom-Json).bookingId + [void]$modernPortalIds.Add([int]$id); [void]$portalIds.Add([int]$id); $stats.PortalOk++ + return + } $m = [regex]::Match($r.Content, 'Booking (\d+) (confirmed|has been placed on the waitlist)') if ($m.Success) { $id = $m.Groups[1].Value } else { @@ -139,6 +155,12 @@ $cPortal = 0; $cPartner = 0 for ($k = 0; $k -lt $Cancels -and $k -lt $portalIds.Count; $k++) { $id = $portalIds[$k] try { + if ($modernPortalIds.Contains($id)) { + # modern UI cancel (/app/my-bookings -> DELETE /api/v2/bookings/{id}) + $r = Invoke-WebRequest "$PortalUrl/api/v2/bookings/$($id)?reason=mixed-traffic" -Method Delete -UseBasicParsing -TimeoutSec 60 + if ($r.StatusCode -eq 200) { $cPortal++ } + continue + } $r = Invoke-PortalForm "$PortalUrl/Booking/Cancel/$id" "$PortalUrl/Booking/Cancel/$id" @{ Reason = 'mixed-traffic'; CandidateNumber = '' } if ($r.Content -match "Booking $id cancelled") { $cPortal++ } } catch { } diff --git a/scripts/run-legacy.ps1 b/scripts/run-legacy.ps1 index 8a6273a8..1826cb57 100644 --- a/scripts/run-legacy.ps1 +++ b/scripts/run-legacy.ps1 @@ -329,6 +329,8 @@ function Find-ServiceJvm([string]$name) { } function Start-Services([string]$jdk) { + # MBA-3823: results-service sends this as X-Service-Token (same dev value as run-scheduling-service.ps1) + if (-not $env:SCHEDULING_SERVICE_TOKEN) { $env:SCHEDULING_SERVICE_TOKEN = 'dev-attendance-token-not-for-prod' } $java = Join-Path $jdk 'bin\java.exe' foreach ($s in $services) { $jar = Join-Path $root "services\$($s.Name)\target\$($s.Name).jar" @@ -453,7 +455,7 @@ function Show-Urls { booking-api http://localhost:8081/api/v1/sessions results-service http://localhost:8082/api/v1/results/candidate/{candidateNumber} capacity-sync http://localhost:8083/sync/last - SQL Server $server database AssessmentDelivery login ad_app + SQL Server $server database AssessmentDelivery logins ad_portal / ad_booking_api / ad_results / ad_capacity_sync (writes denied: 040_single_writer.sql) logs and pids: .run\ stop: scripts\run-legacy.ps1 -Stop diff --git a/scripts/run-legacy.sh b/scripts/run-legacy.sh index e8c0031e..49a067af 100755 --- a/scripts/run-legacy.sh +++ b/scripts/run-legacy.sh @@ -118,6 +118,8 @@ pid_alive() { [ -f "$1" ] && kill -0 "$(cat "$1")" 2>/dev/null; } port_open() { (exec 3<>"/dev/tcp/127.0.0.1/$1") 2>/dev/null; } start_services() { + # MBA-3823: results-service sends this as X-Service-Token (same dev value as run-scheduling-service.sh) + export SCHEDULING_SERVICE_TOKEN="${SCHEDULING_SERVICE_TOKEN:-dev-attendance-token-not-for-prod}" mkdir -p "$RUN_DIR" [ -f "$RUN_DIR/.gitignore" ] || echo "*" > "$RUN_DIR/.gitignore" local entry name port jar @@ -153,7 +155,7 @@ print_urls() { booking-api http://localhost:8081/api/v1/sessions results-service http://localhost:8082/api/v1/results/candidate/{candidateNumber} capacity-sync http://localhost:8083/sync/last - SQL Server localhost,1433 db AssessmentDelivery login ad_app + SQL Server localhost,1433 db AssessmentDelivery logins ad_portal / ad_booking_api / ad_results / ad_capacity_sync (writes denied: 040_single_writer.sql) logs and pids: .run/ portal (:8080) is Windows-only (.NET Framework 4.8) - run scripts/run-portal.ps1 on Windows diff --git a/scripts/run-scheduling-service.ps1 b/scripts/run-scheduling-service.ps1 index 18ce42e3..1c11b7f1 100644 --- a/scripts/run-scheduling-service.ps1 +++ b/scripts/run-scheduling-service.ps1 @@ -205,6 +205,10 @@ function Start-Service8090([string]$jdk) { $env:SCHEDULING_LEGACY_DB_USER = 'ad_sched_import' $env:SCHEDULING_LEGACY_DB_PASSWORD = $ImportPassword $env:SCHEDULING_IMPORT_ON_STARTUP = 'true' + $env:SCHEDULING_RELAY_DB_URL = "jdbc:sqlserver://$($Server -replace ',', ':');databaseName=AssessmentDelivery;encrypt=true;trustServerCertificate=true" + if (-not $env:SCHEDULING_RELAY_ENABLED) { $env:SCHEDULING_RELAY_ENABLED = 'true' } + # MBA-3823 attendance credential; run-legacy.ps1 gives results-service the same dev value + if (-not $env:SCHEDULING_ATTENDANCE_SERVICE_TOKEN) { $env:SCHEDULING_ATTENDANCE_SERVICE_TOKEN = 'dev-attendance-token-not-for-prod' } $java = Join-Path $jdk 'bin\java.exe' Write-Step "starting on :$port (log $logFile)" $p = Start-Process -FilePath $java -ArgumentList '-jar', "`"$runJar`"" -WorkingDirectory $root -WindowStyle Hidden ` diff --git a/scripts/run-scheduling-service.sh b/scripts/run-scheduling-service.sh index 119cd2a0..1b91bba5 100755 --- a/scripts/run-scheduling-service.sh +++ b/scripts/run-scheduling-service.sh @@ -86,6 +86,7 @@ start() { cp "$SVC/target/scheduling-service.jar" "$RUN_DIR/scheduling-service.jar" log "starting on :$PORT (log $LOG_FILE)" SCHEDULING_IMPORT_ON_STARTUP="${SCHEDULING_IMPORT_ON_STARTUP:-true}" \ + SCHEDULING_ATTENDANCE_SERVICE_TOKEN="${SCHEDULING_ATTENDANCE_SERVICE_TOKEN:-dev-attendance-token-not-for-prod}" \ SCHEDULING_DB_PASSWORD="${SCHEDULING_DB_PASSWORD:-$MSSQL_SA_PASSWORD}" \ nohup "$JAVA_HOME/bin/java" -jar "$RUN_DIR/scheduling-service.jar" >"$LOG_FILE" 2>&1 & echo $! >"$PID_FILE" diff --git a/services/booking-api/src/main/java/com/northwind/assessment/booking/web/ApiExceptionHandler.java b/services/booking-api/src/main/java/com/northwind/assessment/booking/web/ApiExceptionHandler.java index f72af165..72f466b5 100644 --- a/services/booking-api/src/main/java/com/northwind/assessment/booking/web/ApiExceptionHandler.java +++ b/services/booking-api/src/main/java/com/northwind/assessment/booking/web/ApiExceptionHandler.java @@ -32,10 +32,28 @@ public ResponseEntity> handleBadInput(Exception ex) { @ExceptionHandler(DataAccessException.class) public ResponseEntity> handleDb(DataAccessException ex) { + if (isPermissionDenied(ex)) { + // MBA-3823: db/security/040_single_writer.sql denies booking/seat writes to ad_booking_api + log.warn("Legacy write denied: {}", ex.getMostSpecificCause().getMessage()); + return body(HttpStatus.FORBIDDEN, "LEGACY_WRITE_DENIED", "Booking and capacity writes are owned by " + + "scheduling-service; send them through the routing gateway (:8000)"); + } log.error("Database error", ex); return body(HttpStatus.INTERNAL_SERVER_ERROR, "DB_ERROR", ex.getMostSpecificCause().getMessage()); } + // SQL Server 229/230: The permission was denied on the object/column + static boolean isPermissionDenied(DataAccessException ex) { + Throwable t = ex.getMostSpecificCause(); + if (t instanceof java.sql.SQLException) { + int code = ((java.sql.SQLException) t).getErrorCode(); + if (code == 229 || code == 230) { + return true; + } + } + return t != null && t.getMessage() != null && t.getMessage().contains("permission was denied"); + } + static ResponseEntity> body(HttpStatus status, String error, String message) { Map m = new LinkedHashMap(); m.put("error", error); diff --git a/services/booking-api/src/main/resources/application-azure.properties b/services/booking-api/src/main/resources/application-azure.properties index 85739f84..1fa0320c 100644 --- a/services/booking-api/src/main/resources/application-azure.properties +++ b/services/booking-api/src/main/resources/application-azure.properties @@ -1,7 +1,7 @@ # booking-api - Azure App Service (activate with --spring.profiles.active=azure) # Real values come from App Service application settings. Do not commit secrets. spring.datasource.url=jdbc:sqlserver://.database.windows.net:1433;database=AssessmentDelivery;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30; -spring.datasource.username= +spring.datasource.username= spring.datasource.password=${AZURE_SQL_PASSWORD} spring.jackson.serialization.indent-output=false diff --git a/services/booking-api/src/main/resources/application.properties b/services/booking-api/src/main/resources/application.properties index 21ee2ac6..faa6f708 100644 --- a/services/booking-api/src/main/resources/application.properties +++ b/services/booking-api/src/main/resources/application.properties @@ -2,8 +2,8 @@ server.port=8081 spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=AssessmentDelivery -spring.datasource.username=ad_app -spring.datasource.password=${AD_DB_PASSWORD:Northw1nd!Dev2011} +spring.datasource.username=ad_booking_api +spring.datasource.password=${AD_DB_PASSWORD:B00kingApi!Dev2026} spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.tomcat.max-active=20 spring.datasource.tomcat.test-on-borrow=true diff --git a/services/booking-api/src/test/java/com/northwind/assessment/booking/web/ApiExceptionHandlerTest.java b/services/booking-api/src/test/java/com/northwind/assessment/booking/web/ApiExceptionHandlerTest.java new file mode 100644 index 00000000..91d28a75 --- /dev/null +++ b/services/booking-api/src/test/java/com/northwind/assessment/booking/web/ApiExceptionHandlerTest.java @@ -0,0 +1,43 @@ +package com.northwind.assessment.booking.web; + +import static org.junit.Assert.assertEquals; + +import java.sql.SQLException; +import java.util.Map; + +import org.junit.Test; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.jdbc.UncategorizedSQLException; + +public class ApiExceptionHandlerTest { + + private final ApiExceptionHandler handler = new ApiExceptionHandler(); + + @Test + public void permissionDeniedFromSingleWriterDenyMapsTo403() { + SQLException denied = new SQLException( + "The INSERT permission was denied on the object 'Booking', database 'AssessmentDelivery', schema 'dbo'.", + "S0005", 229); + ResponseEntity> r = handler.handleDb( + new UncategorizedSQLException("insertBooking", "INSERT INTO dbo.Booking ...", denied)); + assertEquals(HttpStatus.FORBIDDEN, r.getStatusCode()); + assertEquals("LEGACY_WRITE_DENIED", r.getBody().get("error")); + } + + @Test + public void columnPermissionDeniedAlsoMapsTo403() { + SQLException denied = new SQLException("The UPDATE permission was denied on the column 'SeatsAvailable' " + + "of the object 'ExamSession'", "S0005", 230); + assertEquals(HttpStatus.FORBIDDEN, handler.handleDb( + new UncategorizedSQLException("decrement", "UPDATE dbo.ExamSession ...", denied)).getStatusCode()); + } + + @Test + public void otherDatabaseErrorsStay500() { + ResponseEntity> r = handler.handleDb(new DataIntegrityViolationException("dup")); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, r.getStatusCode()); + assertEquals("DB_ERROR", r.getBody().get("error")); + } +} diff --git a/services/capacity-sync/src/main/java/com/northwind/assessment/capacity/CapacitySyncService.java b/services/capacity-sync/src/main/java/com/northwind/assessment/capacity/CapacitySyncService.java index 8ca14ef9..a820d9af 100644 --- a/services/capacity-sync/src/main/java/com/northwind/assessment/capacity/CapacitySyncService.java +++ b/services/capacity-sync/src/main/java/com/northwind/assessment/capacity/CapacitySyncService.java @@ -17,6 +17,11 @@ * * Written 2016 after the "phantom full sessions" incident; runs nightly and can be * kicked off by hand from /sync/run. + * + * MBA-3823: scheduling-service is now the single writer of seat counts and its outbox relay projects them + * into ExamSession.SeatsAvailable. This job's different formula (docs/as-is HB-03, MR-02, MR-17) would + * reintroduce drift, so writes are off unless capacity.sync.enabled=true; a disabled run only records a + * SKIPPED CAPACITY_SYNC audit row. Drift is reported by scheduling-service's reconciliation instead. */ @Service public class CapacitySyncService { @@ -39,6 +44,9 @@ public class CapacitySyncService { @Value("${capacity.sync.audit-actor:capacity-sync}") private String actor; + @Value("${capacity.sync.enabled:false}") + private boolean enabled; + private volatile SyncRun lastRun = new SyncRun(); public synchronized SyncRun runSync(String trigger) { @@ -46,6 +54,16 @@ public synchronized SyncRun runSync(String trigger) { run.setTrigger(trigger); run.setStatus("RUNNING"); run.setStartedAt(new SimpleDateFormat(TS).format(new Date())); + if (!enabled) { + run.setStatus("SKIPPED"); + run.setMessage("capacity.sync.enabled=false: SeatsAvailable is written by scheduling-service's relay; " + + "use GET /api/v2/internal/reconciliation on scheduling-service for drift"); + run.setFinishedAt(new SimpleDateFormat(TS).format(new Date())); + audit(run); + lastRun = run; + LOG.info("Capacity sync ({}) skipped: capacity.sync.enabled=false", trigger); + return run; + } try { List> sessions = jdbc.queryForList(SELECT_OPEN_SESSIONS); int updated = 0; @@ -96,7 +114,8 @@ private void audit(SyncRun run) { jdbc.update("INSERT INTO dbo.AuditLog (EntityName, EntityId, Action, Detail, Actor) VALUES (?, ?, ?, ?, ?)", "ExamSession", 0, "CAPACITY_SYNC", "trigger=" + run.getTrigger() + " status=" + run.getStatus() + " checked=" + run.getSessionsChecked() - + " updated=" + run.getSessionsUpdated(), actor); + + " updated=" + run.getSessionsUpdated() + + ("SKIPPED".equals(run.getStatus()) ? " reason=capacity.sync.enabled=false" : ""), actor); } catch (RuntimeException e) { LOG.warn("Could not write audit row for capacity sync: {}", e.getMessage()); } diff --git a/services/capacity-sync/src/main/resources/application-azure.properties b/services/capacity-sync/src/main/resources/application-azure.properties index 9d0df93e..754d5759 100644 --- a/services/capacity-sync/src/main/resources/application-azure.properties +++ b/services/capacity-sync/src/main/resources/application-azure.properties @@ -1,7 +1,7 @@ # capacity-sync - Azure App Service (activate with --spring.profiles.active=azure) # Real values come from App Service application settings. Do not commit secrets. spring.datasource.url=jdbc:sqlserver://.database.windows.net:1433;database=AssessmentDelivery;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30; -spring.datasource.username= +spring.datasource.username= spring.datasource.password=${AZURE_SQL_PASSWORD} spring.jackson.serialization.indent-output=false diff --git a/services/capacity-sync/src/main/resources/application.properties b/services/capacity-sync/src/main/resources/application.properties index affd28fa..8b21e465 100644 --- a/services/capacity-sync/src/main/resources/application.properties +++ b/services/capacity-sync/src/main/resources/application.properties @@ -2,8 +2,8 @@ server.port=8083 spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=AssessmentDelivery -spring.datasource.username=ad_app -spring.datasource.password=${AD_DB_PASSWORD:Northw1nd!Dev2011} +spring.datasource.username=ad_capacity_sync +spring.datasource.password=${AD_DB_PASSWORD:CapSync!Dev2026} spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.tomcat.max-active=5 spring.datasource.tomcat.test-on-borrow=true @@ -15,3 +15,5 @@ spring.jackson.serialization.indent-output=true logging.level.com.northwind=INFO capacity.sync.audit-actor=capacity-sync +# MBA-3823: scheduling-service owns SeatsAvailable; true restores the legacy nightly write (HB-03) +capacity.sync.enabled=false diff --git a/services/capacity-sync/src/test/java/com/northwind/assessment/capacity/CapacitySyncServiceTest.java b/services/capacity-sync/src/test/java/com/northwind/assessment/capacity/CapacitySyncServiceTest.java index af61f484..93685ea0 100644 --- a/services/capacity-sync/src/test/java/com/northwind/assessment/capacity/CapacitySyncServiceTest.java +++ b/services/capacity-sync/src/test/java/com/northwind/assessment/capacity/CapacitySyncServiceTest.java @@ -3,7 +3,17 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.mockito.Matchers.anyVararg; +import static org.mockito.Matchers.contains; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.startsWith; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + import org.junit.Test; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.util.ReflectionTestUtils; public class CapacitySyncServiceTest { @@ -14,4 +24,23 @@ public void lastRunIsNeverRunBeforeFirstSync() { assertNull(last.getStartedAt()); assertEquals(0, last.getSessionsChecked()); } + + /** MBA-3823: with capacity.sync.enabled=false (the default) the run writes nothing but its audit row. */ + @Test + public void disabledSyncSkipsWritesAndAuditsTheSkip() { + JdbcTemplate jdbc = mock(JdbcTemplate.class); + CapacitySyncService svc = new CapacitySyncService(); + ReflectionTestUtils.setField(svc, "jdbc", jdbc); + ReflectionTestUtils.setField(svc, "actor", "capacity-sync"); + ReflectionTestUtils.setField(svc, "enabled", false); + + SyncRun run = svc.runSync("MANUAL"); + + assertEquals("SKIPPED", run.getStatus()); + assertEquals(run.getStatus(), svc.getLastRun().getStatus()); + verify(jdbc, never()).queryForList(startsWith("SELECT s.SessionId")); + verify(jdbc, never()).update(startsWith("UPDATE dbo.ExamSession"), (Object[]) anyVararg()); + verify(jdbc).update(startsWith("INSERT INTO dbo.AuditLog"), eq("ExamSession"), eq(0), eq("CAPACITY_SYNC"), + contains("status=SKIPPED"), eq("capacity-sync")); + } } diff --git a/services/results-service/src/main/java/com/northwind/ad/results/service/AttendanceRecorder.java b/services/results-service/src/main/java/com/northwind/ad/results/service/AttendanceRecorder.java new file mode 100644 index 00000000..57ecf757 --- /dev/null +++ b/services/results-service/src/main/java/com/northwind/ad/results/service/AttendanceRecorder.java @@ -0,0 +1,98 @@ +package com.northwind.ad.results.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Component; +import org.springframework.web.client.HttpStatusCodeException; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; + +import com.northwind.ad.results.web.ApiException; + +/** + * Marks a booking ATTENDED once its result is stored. + * + * MBA-3823: scheduling-service is the single writer of booking status, so attendance goes to + * {@code POST /api/v2/bookings/{bookingId}/attendance}; its outbox relay then projects ATTENDED into + * dbo.Booking (results-service's login is denied UPDATE on dbo.Booking by db/security/040_single_writer.sql). + * With {@code results.scheduling.base-url} empty the legacy direct UPDATE is used, which only works on a + * database without 040. The call carries the service credential {@code scheduling.service-token} as + * {@code X-Service-Token}; scheduling-service refuses attendance without it. + */ +@Component +public class AttendanceRecorder { + + private static final Logger LOG = LoggerFactory.getLogger(AttendanceRecorder.class); + + private final JdbcTemplate jdbc; + static final String TOKEN_HEADER = "X-Service-Token"; + + private final String baseUrl; + private final String serviceToken; + private final RestTemplate rest; + + @Autowired + public AttendanceRecorder(JdbcTemplate jdbc, + @Value("${results.scheduling.base-url:}") String baseUrl, + @Value("${results.scheduling.timeout-ms:5000}") int timeoutMs, + @Value("${scheduling.service-token:}") String serviceToken) { + this(jdbc, baseUrl, serviceToken, restTemplate(timeoutMs)); + } + + AttendanceRecorder(JdbcTemplate jdbc, String baseUrl, String serviceToken, RestTemplate rest) { + this.jdbc = jdbc; + this.baseUrl = baseUrl == null ? "" : baseUrl.trim().replaceAll("/+$", ""); + this.serviceToken = serviceToken == null ? "" : serviceToken; + this.rest = rest; + } + + public boolean usesSchedulingService() { + return !baseUrl.isEmpty(); + } + + public void recordAttended(int bookingId) { + if (!usesSchedulingService()) { + jdbc.update("UPDATE dbo.Booking SET Status = 'ATTENDED', LastModified = GETDATE() " + + "WHERE BookingId = ? AND Status = 'BOOKED'", bookingId); + return; + } + String url = baseUrl + "/api/v2/bookings/" + bookingId + "/attendance"; + try { + HttpHeaders headers = new HttpHeaders(); + headers.set(TOKEN_HEADER, serviceToken); + rest.exchange(url, HttpMethod.POST, new HttpEntity(headers), String.class); + LOG.info("Attendance for booking {} recorded by scheduling-service", bookingId); + } catch (HttpStatusCodeException e) { + HttpStatus status = e.getStatusCode(); + if (status == HttpStatus.NOT_FOUND) { + throw new ApiException(HttpStatus.NOT_FOUND, "BOOKING_NOT_FOUND", + "Booking " + bookingId + " not found in scheduling-service"); + } + if (status == HttpStatus.CONFLICT) { + throw new ApiException(HttpStatus.CONFLICT, "BOOKING_NOT_ACTIVE", + "scheduling-service refused attendance for booking " + bookingId + ": " + + e.getResponseBodyAsString()); + } + throw new ApiException(HttpStatus.BAD_GATEWAY, "SCHEDULING_UNAVAILABLE", + "scheduling-service returned " + status.value() + " for booking " + bookingId); + } catch (RestClientException e) { + throw new ApiException(HttpStatus.BAD_GATEWAY, "SCHEDULING_UNAVAILABLE", + "scheduling-service unreachable at " + baseUrl + ": " + e.getMessage()); + } + } + + private static RestTemplate restTemplate(int timeoutMs) { + SimpleClientHttpRequestFactory f = new SimpleClientHttpRequestFactory(); + f.setConnectTimeout(timeoutMs); + f.setReadTimeout(timeoutMs); + return new RestTemplate(f); + } +} diff --git a/services/results-service/src/main/java/com/northwind/ad/results/service/ResultIngestService.java b/services/results-service/src/main/java/com/northwind/ad/results/service/ResultIngestService.java index 1f709ab2..4d42055a 100644 --- a/services/results-service/src/main/java/com/northwind/ad/results/service/ResultIngestService.java +++ b/services/results-service/src/main/java/com/northwind/ad/results/service/ResultIngestService.java @@ -26,6 +26,9 @@ public class ResultIngestService { @Autowired private JdbcTemplate jdbc; + @Autowired + private AttendanceRecorder attendance; + @Value("${results.audit-actor:results-service}") private String actor; @@ -93,11 +96,12 @@ public IngestOutcome ingest(int bookingId, int score, Date scoredAt, String sour } return compareWithStored(existing, bookingId, score, scoredAt, sourceFile, true); } - jdbc.update("UPDATE dbo.Booking SET Status = 'ATTENDED', LastModified = GETDATE() " - + "WHERE BookingId = ? AND Status = 'BOOKED'", bookingId); jdbc.update("INSERT INTO dbo.AuditLog (EntityName, EntityId, Action, Detail, Actor) VALUES (?, ?, ?, ?, ?)", "Result", resultId, "INGEST", "booking=" + bookingId + " score=" + score + (sourceFile != null ? " file=" + sourceFile : ""), actor); + // last step before commit: scheduling-service attendance is not part of this transaction (it is idempotent, + // so a retry after a failed commit re-records it harmlessly) + attendance.recordAttended(bookingId); LOG.info("Result {} for booking {} score {} passed={}", resultId, bookingId, score, passed); return new IngestOutcome(IngestOutcome.Status.CREATED, bookingId, resultId, score, storedAt, null, null, passed); diff --git a/services/results-service/src/main/resources/application-azure.properties b/services/results-service/src/main/resources/application-azure.properties index 2ac4c62f..5e7baefb 100644 --- a/services/results-service/src/main/resources/application-azure.properties +++ b/services/results-service/src/main/resources/application-azure.properties @@ -1,8 +1,11 @@ # results-service - Azure App Service (activate with --spring.profiles.active=azure) # Real values come from App Service application settings. Do not commit secrets. spring.datasource.url=jdbc:sqlserver://.database.windows.net:1433;database=AssessmentDelivery;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30; -spring.datasource.username= +spring.datasource.username= spring.datasource.password=${AZURE_SQL_PASSWORD} +results.scheduling.base-url=${SCHEDULING_BASE_URL} +scheduling.service-token=${SCHEDULING_SERVICE_TOKEN} + spring.jackson.serialization.indent-output=false logging.level.com.northwind=WARN diff --git a/services/results-service/src/main/resources/application.properties b/services/results-service/src/main/resources/application.properties index a7706296..76945080 100644 --- a/services/results-service/src/main/resources/application.properties +++ b/services/results-service/src/main/resources/application.properties @@ -2,8 +2,8 @@ server.port=8082 spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=AssessmentDelivery -spring.datasource.username=ad_app -spring.datasource.password=${AD_DB_PASSWORD:Northw1nd!Dev2011} +spring.datasource.username=ad_results +spring.datasource.password=${AD_DB_PASSWORD:Resu1ts!Dev2026} spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.tomcat.max-active=20 spring.datasource.tomcat.test-on-borrow=true @@ -18,3 +18,10 @@ logging.level.com.northwind=INFO spring.http.multipart.max-file-size=5MB spring.http.multipart.max-request-size=5MB results.audit-actor=results-service +# MBA-3823: attendance is recorded by scheduling-service (single writer); its relay projects ATTENDED into +# dbo.Booking. Empty = legacy direct UPDATE (only for a pre-coexistence database without 040_single_writer.sql). +results.scheduling.base-url=${SCHEDULING_BASE_URL:http://localhost:8090} +results.scheduling.timeout-ms=5000 +# Service credential sent as X-Service-Token (= scheduling.attendance.service-token on scheduling-service). +# No default: the run scripts set a dev value, Azure the env var. +scheduling.service-token=${SCHEDULING_SERVICE_TOKEN:} diff --git a/services/results-service/src/test/java/com/northwind/ad/results/it/AbstractResultsIT.java b/services/results-service/src/test/java/com/northwind/ad/results/it/AbstractResultsIT.java index e2f141dc..038fac7c 100644 --- a/services/results-service/src/test/java/com/northwind/ad/results/it/AbstractResultsIT.java +++ b/services/results-service/src/test/java/com/northwind/ad/results/it/AbstractResultsIT.java @@ -26,13 +26,16 @@ public abstract class AbstractResultsIT { @Autowired protected TestRestTemplate rest; + /** sa: fixtures write dbo.Booking, which results-service's login is denied (040_single_writer.sql). */ + protected final JdbcTemplate sa = SqlServerDb.saJdbc(); + /** A fresh BOOKED booking with no result. */ protected Fixture newBooking() { String candidateNumber = String.format("NW%08d", NEXT_CANDIDATE.getAndIncrement()); - int sessionId = jdbc.queryForObject( + int sessionId = sa.queryForObject( "SELECT TOP 1 SessionId FROM dbo.ExamSession WHERE Status = 'OPEN' AND SeatsAvailable > 0 " + "ORDER BY SessionId", Integer.class); - int bookingId = jdbc.queryForObject( + int bookingId = sa.queryForObject( "SET NOCOUNT ON; INSERT INTO dbo.Booking (SessionId, CandidateId, Status, Channel) " + "SELECT ?, CandidateId, 'BOOKED', 'PARTNER' FROM dbo.Candidate WHERE CandidateNumber = ?; " + "SELECT CAST(SCOPE_IDENTITY() AS INT);", Integer.class, sessionId, candidateNumber); diff --git a/services/results-service/src/test/java/com/northwind/ad/results/it/AttendanceViaSchedulingTest.java b/services/results-service/src/test/java/com/northwind/ad/results/it/AttendanceViaSchedulingTest.java new file mode 100644 index 00000000..13bf122b --- /dev/null +++ b/services/results-service/src/test/java/com/northwind/ad/results/it/AttendanceViaSchedulingTest.java @@ -0,0 +1,49 @@ +package com.northwind.ad.results.it; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.springframework.dao.DataAccessException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +/** + * MBA-3823: results-service no longer writes dbo.Booking. A posted result asks scheduling-service to record + * attendance (stubbed here together with the relay projection), and the service's own login is denied the + * direct UPDATE it used to run. + */ +public class AttendanceViaSchedulingTest extends AbstractResultsIT { + + @Test + @SuppressWarnings("rawtypes") + public void postedResultRecordsAttendanceInSchedulingServiceThenLegacy() { + Fixture f = newBooking(); + Map body = new HashMap(); + body.put("bookingId", f.bookingId); + body.put("score", 655); + ResponseEntity r = rest.postForEntity("/api/v1/results", body, Map.class); + + assertEquals(HttpStatus.CREATED, r.getStatusCode()); + assertTrue(SchedulingStub.get().attendanceCalls().contains(f.bookingId)); + assertEquals("ATTENDED", booking(f.bookingId).get("Status")); + assertEquals(1, resultCount(f.bookingId)); + } + + @Test + public void resultsLoginCannotUpdateBookingStatusDirectly() { + Fixture f = newBooking(); + try { + jdbc.update("UPDATE dbo.Booking SET Status = 'ATTENDED' WHERE BookingId = ?", f.bookingId); + fail("ad_results must be denied UPDATE on dbo.Booking"); + } catch (DataAccessException e) { + assertTrue(e.getMostSpecificCause().getMessage(), + e.getMostSpecificCause().getMessage().contains("permission was denied")); + } + assertEquals("BOOKED", booking(f.bookingId).get("Status")); + } +} diff --git a/services/results-service/src/test/java/com/northwind/ad/results/it/SchedulingStub.java b/services/results-service/src/test/java/com/northwind/ad/results/it/SchedulingStub.java new file mode 100644 index 00000000..7c6211c3 --- /dev/null +++ b/services/results-service/src/test/java/com/northwind/ad/results/it/SchedulingStub.java @@ -0,0 +1,83 @@ +package com.northwind.ad.results.it; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpServer; + +/** + * Stands in for scheduling-service + its outbox relay in the results-service ITs: POST + * /api/v2/bookings/{id}/attendance records the call, then projects ATTENDED into dbo.Booking the way the relay + * does (sa connection, SESSION_CONTEXT writer = scheduling-relay so trigger side effects are skipped). + */ +public final class SchedulingStub { + + private static final Pattern ATTENDANCE = Pattern.compile("^/api/v2/bookings/(\\d+)/attendance$"); + private static SchedulingStub instance; + + private final HttpServer server; + /** Service credential the stub expects, like scheduling.attendance.service-token. */ + public static final String TOKEN = "it-attendance-token"; + + private final List attendanceCalls = new CopyOnWriteArrayList(); + + private SchedulingStub() throws IOException { + server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); + server.createContext("/", this::handle); + server.start(); + } + + public static synchronized SchedulingStub get() { + if (instance == null) { + try { + instance = new SchedulingStub(); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + return instance; + } + + public String baseUrl() { + return "http://127.0.0.1:" + server.getAddress().getPort(); + } + + public List attendanceCalls() { + return attendanceCalls; + } + + private void handle(HttpExchange ex) throws IOException { + Matcher m = ATTENDANCE.matcher(ex.getRequestURI().getPath()); + int code; + String body; + if ("POST".equals(ex.getRequestMethod()) && m.matches() + && !TOKEN.equals(ex.getRequestHeaders().getFirst("X-Service-Token"))) { + code = 401; + body = "{\"error\":\"UNAUTHORIZED\"}"; + } else if ("POST".equals(ex.getRequestMethod()) && m.matches()) { + int bookingId = Integer.parseInt(m.group(1)); + attendanceCalls.add(bookingId); + int n = SqlServerDb.saJdbc().update("EXEC sp_set_session_context N'writer', N'scheduling-relay'; " + + "UPDATE dbo.Booking SET Status = 'ATTENDED' WHERE BookingId = ? AND Status IN ('BOOKED','ATTENDED')", + bookingId); + code = n == 1 ? 200 : 409; + body = "{\"bookingId\":" + bookingId + ",\"status\":\"" + (n == 1 ? "ATTENDED" : "CONFLICT") + "\"}"; + } else { + code = 404; + body = "{}"; + } + byte[] bytes = body.getBytes(StandardCharsets.UTF_8); + ex.getResponseHeaders().add("Content-Type", "application/json"); + ex.sendResponseHeaders(code, bytes.length); + try (OutputStream os = ex.getResponseBody()) { + os.write(bytes); + } + } +} diff --git a/services/results-service/src/test/java/com/northwind/ad/results/it/ScoreFileIdempotencyTest.java b/services/results-service/src/test/java/com/northwind/ad/results/it/ScoreFileIdempotencyTest.java index b703a9ca..8a09b1f4 100644 --- a/services/results-service/src/test/java/com/northwind/ad/results/it/ScoreFileIdempotencyTest.java +++ b/services/results-service/src/test/java/com/northwind/ad/results/it/ScoreFileIdempotencyTest.java @@ -192,13 +192,13 @@ public void replayedFileNeverInsertsRowsThatFailedTheFirstTime() { @SuppressWarnings("rawtypes") public void replayAuditsAConflictThatWasNotAuditedOnTheFirstUpload() throws Exception { Fixture f = newBooking(); - jdbc.update("UPDATE dbo.Booking SET Status = 'CANCELLED' WHERE BookingId = ?", f.bookingId); + sa.update("UPDATE dbo.Booking SET Status = 'CANCELLED' WHERE BookingId = ?", f.bookingId); String file = csv(f.csvRow(999, "2026-11-10T07:30:00")); ResponseEntity first = upload("NWTEST_LATE_BOOKING.csv", file); assertEquals(1, ((List) first.getBody().get("errors")).size()); // the booking becomes active and gets a different score through the REST API - jdbc.update("UPDATE dbo.Booking SET Status = 'BOOKED' WHERE BookingId = ?", f.bookingId); + sa.update("UPDATE dbo.Booking SET Status = 'BOOKED' WHERE BookingId = ?", f.bookingId); Map post = new java.util.HashMap(); post.put("bookingId", f.bookingId); post.put("score", 610); diff --git a/services/results-service/src/test/java/com/northwind/ad/results/it/SqlServerDb.java b/services/results-service/src/test/java/com/northwind/ad/results/it/SqlServerDb.java index cc0963e6..da1d67d8 100644 --- a/services/results-service/src/test/java/com/northwind/ad/results/it/SqlServerDb.java +++ b/services/results-service/src/test/java/com/northwind/ad/results/it/SqlServerDb.java @@ -2,6 +2,9 @@ import java.io.File; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DriverManagerDataSource; + import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; @@ -11,7 +14,8 @@ /** * One SQL Server 2022 container per test JVM, built with the real db/apply.sh - * (schema, procs, triggers, seed). The service connects as ad_app, like in production. + * (schema, procs, triggers, seed, security). The service connects as ad_results, like in production, so the + * MBA-3823 write denial (db/security/040_single_writer.sql) applies; fixtures write as sa. */ public final class SqlServerDb { @@ -54,14 +58,28 @@ public static String jdbcUrl() { + ";databaseName=AssessmentDelivery"; } - /** Points the Spring datasource at the container as ad_app. */ + private static JdbcTemplate sa; + + /** sysadmin connection for fixtures (results-service's own login may not write dbo.Booking). */ + public static synchronized JdbcTemplate saJdbc() { + if (sa == null) { + DriverManagerDataSource ds = new DriverManagerDataSource(jdbcUrl(), "sa", SA_PASSWORD); + ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); + sa = new JdbcTemplate(ds); + } + return sa; + } + + /** Points the Spring datasource at the container as ad_results and attendance at the scheduling stub. */ public static class Initializer implements ApplicationContextInitializer { @Override public void initialize(ConfigurableApplicationContext ctx) { EnvironmentTestUtils.addEnvironment(ctx, "spring.datasource.url=" + jdbcUrl(), - "spring.datasource.username=ad_app", - "spring.datasource.password=Northw1nd!Dev2011"); + "spring.datasource.username=ad_results", + "spring.datasource.password=Resu1ts!Dev2026", + "results.scheduling.base-url=" + SchedulingStub.get().baseUrl(), + "scheduling.service-token=" + SchedulingStub.TOKEN); } } } diff --git a/services/results-service/src/test/java/com/northwind/ad/results/service/AttendanceRecorderTest.java b/services/results-service/src/test/java/com/northwind/ad/results/service/AttendanceRecorderTest.java new file mode 100644 index 00000000..35193340 --- /dev/null +++ b/services/results-service/src/test/java/com/northwind/ad/results/service/AttendanceRecorderTest.java @@ -0,0 +1,81 @@ +package com.northwind.ad.results.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.header; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; +import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus; +import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; + +import org.junit.Test; +import org.mockito.Matchers; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.web.client.MockRestServiceServer; +import org.springframework.web.client.RestTemplate; + +import com.northwind.ad.results.web.ApiException; + +public class AttendanceRecorderTest { + + private final JdbcTemplate jdbc = mock(JdbcTemplate.class); + private final RestTemplate rest = new RestTemplate(); + private final MockRestServiceServer server = MockRestServiceServer.createServer(rest); + + @Test + public void postsAttendanceToSchedulingServiceAndNeverWritesBooking() { + server.expect(requestTo("http://sched:8090/api/v2/bookings/50000007/attendance")) + .andExpect(method(HttpMethod.POST)) + .andExpect(header("X-Service-Token", "tok-1")) + .andRespond(withSuccess()); + new AttendanceRecorder(jdbc, "http://sched:8090/", "tok-1", rest).recordAttended(50000007); + server.verify(); + verifyZeroInteractions(jdbc); + } + + @Test + public void mapsSchedulingConflictToBookingNotActive() { + server.expect(requestTo("http://sched:8090/api/v2/bookings/12/attendance")) + .andRespond(withStatus(HttpStatus.CONFLICT)); + assertError(new AttendanceRecorder(jdbc, "http://sched:8090", "tok-1", rest), 12, "BOOKING_NOT_ACTIVE"); + } + + @Test + public void mapsSchedulingNotFoundAndOutage() { + server.expect(requestTo("http://sched:8090/api/v2/bookings/13/attendance")) + .andRespond(withStatus(HttpStatus.NOT_FOUND)); + server.expect(requestTo("http://sched:8090/api/v2/bookings/14/attendance")) + .andRespond(withStatus(HttpStatus.SERVICE_UNAVAILABLE)); + AttendanceRecorder r = new AttendanceRecorder(jdbc, "http://sched:8090", "tok-1", rest); + assertError(r, 13, "BOOKING_NOT_FOUND"); + assertError(r, 14, "SCHEDULING_UNAVAILABLE"); + } + + @Test + public void rejectedServiceTokenSurfacesAsSchedulingUnavailable() { + server.expect(requestTo("http://sched:8090/api/v2/bookings/15/attendance")) + .andExpect(header("X-Service-Token", "stale")) + .andRespond(withStatus(HttpStatus.UNAUTHORIZED)); + assertError(new AttendanceRecorder(jdbc, "http://sched:8090", "stale", rest), 15, "SCHEDULING_UNAVAILABLE"); + } + + @Test + public void emptyBaseUrlKeepsLegacyDirectUpdate() { + new AttendanceRecorder(jdbc, "", "", rest).recordAttended(42); + verify(jdbc).update(Matchers.startsWith("UPDATE dbo.Booking SET Status = 'ATTENDED'"), Matchers.eq(42)); + } + + private static void assertError(AttendanceRecorder r, int bookingId, String error) { + try { + r.recordAttended(bookingId); + fail("expected " + error); + } catch (ApiException e) { + assertEquals(error, e.getError()); + } + } +} diff --git a/services/routing-gateway/config/routing.properties b/services/routing-gateway/config/routing.properties index 80a99eeb..fa91f70d 100644 --- a/services/routing-gateway/config/routing.properties +++ b/services/routing-gateway/config/routing.properties @@ -1,5 +1,7 @@ # Strangler allow-list (MBA-3822): TestCenter.CenterCode values whose booking + capacity traffic goes to # scheduling-service. Empty = everything goes to legacy. Reloaded within ~1 s of saving this file # (ConfigFileWatcher) or on POST /actuator/refresh. Rollback = remove the code and save. -# Until M4 only routing.test-centers (BOS01, SEA01, AKL01) are accepted unless routing.coexistence-mode=relay. -routing.modern-centers= +# MBA-3823 (M4): routing.coexistence-mode=relay is the default, so any centre may be listed; * = every centre. +# scheduling-service is then the single writer of bookings and seat counts (db/security/040_single_writer.sql +# denies the legacy logins) and its outbox relay projects each change back into AssessmentDelivery. +routing.modern-centers=* diff --git a/services/routing-gateway/src/main/java/com/northwind/routing/policy/AllowList.java b/services/routing-gateway/src/main/java/com/northwind/routing/policy/AllowList.java index 94f07353..71ffe4b7 100644 --- a/services/routing-gateway/src/main/java/com/northwind/routing/policy/AllowList.java +++ b/services/routing-gateway/src/main/java/com/northwind/routing/policy/AllowList.java @@ -39,8 +39,10 @@ public boolean isEmpty() { return centers.isEmpty(); } + /** {@code *} (only permitted with {@code routing.coexistence-mode=relay}, MBA-3823) matches every centre. */ public boolean contains(String centerCode) { - return centerCode != null && centers.contains(centerCode.trim().toUpperCase(Locale.ROOT)); + return centerCode != null && (centers.contains(AllowListPolicy.ALL) + || centers.contains(centerCode.trim().toUpperCase(Locale.ROOT))); } @Override diff --git a/services/routing-gateway/src/main/java/com/northwind/routing/policy/AllowListPolicy.java b/services/routing-gateway/src/main/java/com/northwind/routing/policy/AllowListPolicy.java index d490baff..43b9720c 100644 --- a/services/routing-gateway/src/main/java/com/northwind/routing/policy/AllowListPolicy.java +++ b/services/routing-gateway/src/main/java/com/northwind/routing/policy/AllowListPolicy.java @@ -9,10 +9,13 @@ * Coexistence guard (MBA-3822). Until M4 (relay of writes between the two databases) is live, only the * demo/test centres in {@code routing.test-centers} may be allow-listed; any other centre is refused unless * {@code routing.coexistence-mode=relay}. See services/routing-gateway/README.md. + *

MBA-3823 sets {@code relay}: scheduling-service is the single writer and its outbox relay keeps the legacy + * tables in step, so any centre, or {@code *} for all of them, may be allow-listed. */ public final class AllowListPolicy { public static final String RELAY = "relay"; + public static final String ALL = "*"; private AllowListPolicy() { } diff --git a/services/routing-gateway/src/main/java/com/northwind/routing/route/RouteDecider.java b/services/routing-gateway/src/main/java/com/northwind/routing/route/RouteDecider.java index b5fc51dd..45565701 100644 --- a/services/routing-gateway/src/main/java/com/northwind/routing/route/RouteDecider.java +++ b/services/routing-gateway/src/main/java/com/northwind/routing/route/RouteDecider.java @@ -1,5 +1,7 @@ package com.northwind.routing.route; +import com.northwind.routing.policy.AllowListPolicy; + import java.util.Locale; import java.util.Set; @@ -29,6 +31,7 @@ public static RouteDecision decide(Set allowList, String center, Long bo return RouteDecision.legacy(null, allowList.isEmpty() ? "allow-list empty" : "centre unresolved"); } String c = center.trim().toUpperCase(Locale.ROOT); - return allowList.contains(c) ? RouteDecision.modern(c, "allow-listed") : RouteDecision.legacy(c, "not allow-listed"); + boolean listed = allowList.contains(AllowListPolicy.ALL) || allowList.contains(c); + return listed ? RouteDecision.modern(c, "allow-listed") : RouteDecision.legacy(c, "not allow-listed"); } } diff --git a/services/routing-gateway/src/main/java/com/northwind/routing/web/StranglerFilter.java b/services/routing-gateway/src/main/java/com/northwind/routing/web/StranglerFilter.java index 378576a7..3747dd34 100644 --- a/services/routing-gateway/src/main/java/com/northwind/routing/web/StranglerFilter.java +++ b/services/routing-gateway/src/main/java/com/northwind/routing/web/StranglerFilter.java @@ -157,7 +157,7 @@ private Mono reschedule(ServerWebExchange ex, GatewayFilterChain chain, Ap private Mono modern(ServerWebExchange ex, GatewayFilterChain chain) { ServerHttpRequest req = ex.getRequest(); - if (internalPath(req.getPath().value())) { + if (internalPath(req.getPath().value()) || attendancePath(req.getPath().value())) { return writeJson(ex, HttpStatus.NOT_FOUND.value(), V1Shapes.error("NOT_FOUND", "scheduling-service internal endpoints are not exposed through the gateway"), RouteDecision.modern(null, "internal endpoint blocked")); @@ -181,8 +181,17 @@ private Mono modern(ServerWebExchange ex, GatewayFilterChain chain) { } static boolean internalPath(String rawPath) { + return normalised(rawPath).startsWith("/api/v2/internal"); + } + + /** MBA-3823: attendance is service-to-service (results-service calls :8090 directly), never from outside. */ + static boolean attendancePath(String rawPath) { + return normalised(rawPath).matches("/api/v2/bookings/[^/]+/attendance/?"); + } + + private static String normalised(String rawPath) { String p = UriUtils.decode(rawPath, StandardCharsets.UTF_8).replaceAll(";[^/]*", "").replaceAll("/{2,}", "/"); - return p.toLowerCase(Locale.ROOT).startsWith("/api/v2/internal"); + return p.toLowerCase(Locale.ROOT); } /** Resolves the request's centre (only when it can matter) and applies {@link RouteDecider}. */ diff --git a/services/routing-gateway/src/main/resources/application.yml b/services/routing-gateway/src/main/resources/application.yml index 6325fbf1..06579b20 100644 --- a/services/routing-gateway/src/main/resources/application.yml +++ b/services/routing-gateway/src/main/resources/application.yml @@ -32,9 +32,10 @@ routing: config-file: ${ROUTING_CONFIG_FILE:./config/routing.properties} config-poll-interval: 1s resolve-cache-ttl: 30s - # Until M4 only these test/demo centres may be allow-listed, unless coexistence-mode=relay (M4 sets it). + # Only these test/demo centres may be allow-listed unless coexistence-mode=relay. MBA-3823 (M4) sets relay: + # scheduling-service is the single writer and its outbox relay projects every change into AssessmentDelivery. test-centers: BOS01,SEA01,AKL01 - coexistence-mode: ${ROUTING_COEXISTENCE_MODE:none} + coexistence-mode: ${ROUTING_COEXISTENCE_MODE:relay} # bookings created by scheduling-service get ids from here up (ownership by booking origin) modern-booking-id-floor: 50000000 diff --git a/services/routing-gateway/src/test/java/com/northwind/routing/AllowListPolicyTest.java b/services/routing-gateway/src/test/java/com/northwind/routing/AllowListPolicyTest.java index 658b6675..1aadfd8e 100644 --- a/services/routing-gateway/src/test/java/com/northwind/routing/AllowListPolicyTest.java +++ b/services/routing-gateway/src/test/java/com/northwind/routing/AllowListPolicyTest.java @@ -29,4 +29,15 @@ void productionCentreIsRefusedUnlessRelay() { .isInstanceOf(IllegalStateException.class).hasMessageContaining("[LON01]").hasMessageContaining("relay"); assertThat(AllowListPolicy.validate(List.of("LON01"), TEST_CENTERS, "RELAY")).containsExactly("LON01"); } + + @Test + void wildcardMeansEveryCentreAndNeedsRelay() { + assertThatThrownBy(() -> AllowListPolicy.validate(List.of("*"), TEST_CENTERS, "none")) + .isInstanceOf(IllegalStateException.class).hasMessageContaining("relay"); + assertThat(AllowListPolicy.validate(List.of(" * "), TEST_CENTERS, "relay")).containsExactly("*"); + assertThat(com.northwind.routing.route.RouteDecider.decide(java.util.Set.of("*"), "lon01", null, 50_000_000L).modern()) + .isTrue(); + assertThat(com.northwind.routing.route.RouteDecider.decide(java.util.Set.of("*"), null, null, 50_000_000L).modern()) + .isFalse(); + } } diff --git a/services/routing-gateway/src/test/java/com/northwind/routing/GatewayIntegrationTest.java b/services/routing-gateway/src/test/java/com/northwind/routing/GatewayIntegrationTest.java index 9bea2b17..b092422d 100644 --- a/services/routing-gateway/src/test/java/com/northwind/routing/GatewayIntegrationTest.java +++ b/services/routing-gateway/src/test/java/com/northwind/routing/GatewayIntegrationTest.java @@ -22,7 +22,8 @@ * scheduling-service (:8090). BOS01 is allow-listed, CHI01 is not. */ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, - properties = {"routing.config-poll-interval=200ms", "routing.resolve-cache-ttl=0s"}) + properties = {"routing.config-poll-interval=200ms", "routing.resolve-cache-ttl=0s", + "routing.coexistence-mode=none"}) class GatewayIntegrationTest { static final Path CONFIG; @@ -214,6 +215,14 @@ void internalSchedulingEndpointsAreNotExposed() { web.get().uri("/api/v2//Internal/routing/resolve?centerCode=BOS01").exchange().expectStatus().isNotFound(); } + @Test + void attendanceIsNotExposedThroughTheGateway() { + web.post().uri("/api/v2/bookings/50000001/attendance").header("X-Service-Token", "anything").exchange() + .expectStatus().isNotFound().expectHeader().valueEquals("X-Route", "modern") + .expectBody().jsonPath("$.error").isEqualTo("NOT_FOUND"); + web.post().uri("/api/v2/bookings/1200/Attendance").exchange().expectStatus().isNotFound(); + } + @Test void v2WritesWhoseCentreCannotBeResolvedAreRefused() { web.post().uri("/api/v2/bookings").contentType(MediaType.APPLICATION_JSON) diff --git a/services/routing-gateway/src/test/java/com/northwind/routing/StartupGuardTest.java b/services/routing-gateway/src/test/java/com/northwind/routing/StartupGuardTest.java index 92fb4418..4c4886a4 100644 --- a/services/routing-gateway/src/test/java/com/northwind/routing/StartupGuardTest.java +++ b/services/routing-gateway/src/test/java/com/northwind/routing/StartupGuardTest.java @@ -21,7 +21,7 @@ private static ConfigurableApplicationContext start(String... args) { @Test void refusesProductionCentreWithoutRelay() { - assertThatThrownBy(() -> start("--routing.modern-centers=LON01")) + assertThatThrownBy(() -> start("--routing.modern-centers=LON01", "--routing.coexistence-mode=none")) .hasRootCauseInstanceOf(IllegalStateException.class) .rootCause().hasMessageContaining("LON01").hasMessageContaining("coexistence-mode=relay"); } diff --git a/services/routing-gateway/src/test/java/com/northwind/routing/web/InternalPathTest.java b/services/routing-gateway/src/test/java/com/northwind/routing/web/InternalPathTest.java index d1e2997a..1bb9dc9c 100644 --- a/services/routing-gateway/src/test/java/com/northwind/routing/web/InternalPathTest.java +++ b/services/routing-gateway/src/test/java/com/northwind/routing/web/InternalPathTest.java @@ -15,4 +15,14 @@ void internalEndpointsAreRecognisedInAnySpelling() { assertThat(StranglerFilter.internalPath("/api/v2/centers/BOS01/sessions")).isFalse(); assertThat(StranglerFilter.internalPath("/api/v2/bookings")).isFalse(); } + + @Test + void attendanceIsRecognisedInAnySpelling() { + assertThat(StranglerFilter.attendancePath("/api/v2/bookings/50000007/attendance")).isTrue(); + assertThat(StranglerFilter.attendancePath("/api/v2//bookings/7/Attendance/")).isTrue(); + assertThat(StranglerFilter.attendancePath("/api/v2/bookings/7/%61ttendance")).isTrue(); + assertThat(StranglerFilter.attendancePath("/api/v2/bookings;x=1/7/attendance")).isTrue(); + assertThat(StranglerFilter.attendancePath("/api/v2/bookings/7")).isFalse(); + assertThat(StranglerFilter.attendancePath("/api/v2/bookings/7/reschedule")).isFalse(); + } } diff --git a/services/scheduling-service/api/openapi.yaml b/services/scheduling-service/api/openapi.yaml index 5a51c9a5..4b968f25 100644 --- a/services/scheduling-service/api/openapi.yaml +++ b/services/scheduling-service/api/openapi.yaml @@ -16,6 +16,7 @@ info: |---|---|---| | `VALIDATION` | 400 | null | | `BAD_REQUEST` | 400 | null | + | `UNAUTHORIZED` | 401 | null | | `CANDIDATE_NOT_FOUND` | 404 | null | | `SESSION_NOT_FOUND` | 404 | null | | `BOOKING_NOT_FOUND` | 404 | null | @@ -235,6 +236,81 @@ paths: summary: Get a booking tags: - Bookings + /api/v2/bookings/{bookingId}/attendance: + post: + operationId: attendance + parameters: + - in: path + name: bookingId + required: true + schema: + type: integer + format: int32 + responses: + "200": + content: + application/json: + schema: + $ref: "#/components/schemas/BookingDto" + description: OK + "400": + content: + application/json: + examples: + BAD_REQUEST: + summary: BAD_REQUEST + value: + error: BAD_REQUEST + message: Failed to convert value of type 'java.lang.String' to + required type 'int' + schema: + $ref: "#/components/schemas/ApiError" + description: "Invalid input: BAD_REQUEST" + "401": + content: + application/json: + examples: + UNAUTHORIZED: + summary: UNAUTHORIZED + value: + error: UNAUTHORIZED + legacyCode: null + message: X-Service-Token missing or invalid + schema: + $ref: "#/components/schemas/ServiceTokenError" + description: "Unauthorized: UNAUTHORIZED" + "404": + content: + application/json: + examples: + BOOKING_NOT_FOUND: + summary: BOOKING_NOT_FOUND + value: + error: BOOKING_NOT_FOUND + legacyCode: null + message: Booking 99999 not found + schema: + $ref: "#/components/schemas/ApiError" + description: "Not found: BOOKING_NOT_FOUND" + "409": + content: + application/json: + examples: + NOT_CANCELLABLE: + summary: NOT_CANCELLABLE (legacy 50004) + value: + error: NOT_CANCELLABLE + legacyCode: 50004 + message: Booking 1201 is CANCELLED + schema: + $ref: "#/components/schemas/ApiError" + description: "Rule violation: NOT_CANCELLABLE" + security: + - serviceToken: [] + summary: Record attendance (BOOKED -> ATTENDED; already ATTENDED is a no-op). + Called by results-service on result ingestion + tags: + - Bookings /api/v2/bookings/{bookingId}/reschedule: post: operationId: reschedule @@ -1172,6 +1248,32 @@ components: example: 163 required: - newSessionId + ServiceTokenError: + type: object + description: 401 body when X-Service-Token is missing or invalid + properties: + error: + type: string + description: Symbolic error code (booking-api vocabulary) + enum: + - UNAUTHORIZED + example: UNAUTHORIZED + legacyCode: + type: integer + format: int32 + description: "50000 + legacy proc RAISERROR state (ODD-11): 50001..50006,\ + \ see the table in the API description; null when the rule had no proc\ + \ error. Present on /api/v2 rule errors, absent for BAD_REQUEST and on\ + \ /api/v1." + nullable: true + example: null + message: + type: string + description: Human-readable message + example: X-Service-Token missing or invalid + required: + - error + - message SessionDto: type: object properties: @@ -1281,3 +1383,10 @@ components: - candidateName - candidateNumber - position + securitySchemes: + serviceToken: + description: Service credential (scheduling.attendance.service-token); results-service + only + in: header + name: X-Service-Token + type: apiKey diff --git a/services/scheduling-service/frontend/tsconfig.tsbuildinfo b/services/scheduling-service/frontend/tsconfig.tsbuildinfo index d7aa7f4c..8edb3781 100644 --- a/services/scheduling-service/frontend/tsconfig.tsbuildinfo +++ b/services/scheduling-service/frontend/tsconfig.tsbuildinfo @@ -1 +1 @@ -{"root":["./src/App.tsx","./src/api.ts","./src/candidate.ts","./src/main.tsx","./src/components/CapacityEditor.tsx","./src/components/Toast.tsx","./src/components/WaitlistTable.tsx","./src/components/ui.tsx","./src/pages/AdminCenter.tsx","./src/pages/AdminHome.tsx","./src/pages/AdminSession.tsx","./src/pages/Book.tsx","./src/pages/FindSessions.tsx","./src/pages/MyBookings.tsx","./src/pages/Reschedule.tsx"],"version":"5.6.3"} \ No newline at end of file +{"root":["./src/app.tsx","./src/api.contract.ts","./src/api.ts","./src/candidate.ts","./src/main.tsx","./src/components/capacityeditor.tsx","./src/components/toast.tsx","./src/components/waitlisttable.tsx","./src/components/ui.tsx","./src/generated/openapi.ts","./src/pages/admincenter.tsx","./src/pages/adminhome.tsx","./src/pages/adminsession.tsx","./src/pages/book.tsx","./src/pages/findsessions.tsx","./src/pages/mybookings.tsx","./src/pages/reschedule.tsx"],"version":"5.6.3"} \ No newline at end of file diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/SchedulingServiceApplication.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/SchedulingServiceApplication.java index 080798a2..7ddff790 100644 --- a/services/scheduling-service/src/main/java/com/northwind/scheduling/SchedulingServiceApplication.java +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/SchedulingServiceApplication.java @@ -7,6 +7,9 @@ @SpringBootApplication public class SchedulingServiceApplication { public static void main(String[] args) { + if (args.length > 0 && "reconcile".equals(args[0])) { + System.exit(com.northwind.scheduling.reconciliation.ReconcileCommand.run(SchedulingServiceApplication.class, args)); + } SpringApplication.run(SchedulingServiceApplication.class, args); } } diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/domain/ErrorCode.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/domain/ErrorCode.java index e61dcb02..dd2746dc 100644 --- a/services/scheduling-service/src/main/java/com/northwind/scheduling/domain/ErrorCode.java +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/domain/ErrorCode.java @@ -9,6 +9,8 @@ public enum ErrorCode { VALIDATION(400, 0), BAD_REQUEST(400, 0), + /** MBA-3823: attendance without a valid X-Service-Token. */ + UNAUTHORIZED(401, 0), CANDIDATE_NOT_FOUND(404, 0), SESSION_NOT_FOUND(404, 0), BOOKING_NOT_FOUND(404, 0), diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/importer/SnapshotImporter.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/importer/SnapshotImporter.java index 2325a17c..9bae79d2 100644 --- a/services/scheduling-service/src/main/java/com/northwind/scheduling/importer/SnapshotImporter.java +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/importer/SnapshotImporter.java @@ -1,5 +1,7 @@ package com.northwind.scheduling.importer; +import com.northwind.scheduling.outbox.OutboxRepository; + import java.time.Clock; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; @@ -42,7 +44,10 @@ public static Mode parse(String v) { private final SnapshotWriter writer; private final Clock clock; - public SnapshotImporter(LegacyReferenceSource source, SnapshotWriter writer, Clock clock) { + private final OutboxRepository outbox; + + public SnapshotImporter(LegacyReferenceSource source, SnapshotWriter writer, Clock clock, OutboxRepository outbox) { + this.outbox = outbox; this.source = source; this.writer = writer; this.clock = clock; @@ -65,6 +70,10 @@ public synchronized ImportReport run(Mode mode) { sessions = writer.insertSessions(source.sessions(), started); bookings = writer.insertBookings(source.bookings()); waitlist = writer.insertWaitlist(source.waitlist()); + writer.markImportedLegacyIds(); + // MBA-3823: scheduling-service is the single seat writer; project its derived SeatsAvailable for every + // session once, so counters the legacy writers left drifted (HB-01/HB-02/HB-03) are corrected too + outbox.appendInventoryBaseline("scheduling-service"); } else { Set have = writer.existingSessionIds(); List fresh = source.sessions().stream().filter(s -> !have.contains(s.id())).toList(); diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/importer/SnapshotWriter.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/importer/SnapshotWriter.java index 1b3c8ef6..9af7a2ce 100644 --- a/services/scheduling-service/src/main/java/com/northwind/scheduling/importer/SnapshotWriter.java +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/importer/SnapshotWriter.java @@ -133,7 +133,14 @@ public Set existingSessionIds() { } /** Wipes the seat inventory (sessions, bookings, waitlist, capacity history). Snapshots are kept. */ + /** imported rows already exist in dbo.Booking under the same id (MBA-3823 relay id map) */ + public void markImportedLegacyIds() { + jdbc.update("UPDATE booking SET legacy_booking_id = booking_id WHERE legacy_booking_id IS NULL"); + } + public void clearInventory() { + // events for the inventory being replaced are moot: the re-import reads the legacy rows they targeted + jdbc.update("DELETE FROM outbox_event WHERE relayed_at IS NULL"); jdbc.update("DELETE FROM capacity_change"); jdbc.update("DELETE FROM waitlist_entry"); jdbc.update("UPDATE booking SET rescheduled_from = NULL WHERE rescheduled_from IS NOT NULL"); diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/outbox/OutboxEvent.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/outbox/OutboxEvent.java new file mode 100644 index 00000000..eab774b7 --- /dev/null +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/outbox/OutboxEvent.java @@ -0,0 +1,8 @@ +package com.northwind.scheduling.outbox; + +import java.time.LocalDateTime; +import java.util.UUID; + +public record OutboxEvent(long seq, UUID eventId, OutboxEventType type, int aggregateId, OutboxPayload payload, + String actor, LocalDateTime createdAt) { +} diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/outbox/OutboxEventType.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/outbox/OutboxEventType.java new file mode 100644 index 00000000..db9cc4dd --- /dev/null +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/outbox/OutboxEventType.java @@ -0,0 +1,30 @@ +package com.northwind.scheduling.outbox; + +/** Outbox event types (MBA-3823); ck_outbox_event_type in V2__outbox.sql lists the same names. */ +public enum OutboxEventType { + BookingCreated("Booking", "BOOK"), + BookingCancelled("Booking", "CANCEL"), + BookingRescheduled("Booking", "RESCHEDULE"), + BookingWaitlisted("Booking", "WAITLIST"), + AttendanceRecorded("Booking", "ATTENDED"), + CapacityChanged("ExamSession", "CAPACITY_CHANGE"), + /** after a snapshot import: SeatsAvailable for every session (scheduling-service is the single seat writer) */ + InventoryBaselined("ExamSession", "SEAT_BASELINE"); + + private final String aggregateType; + private final String auditAction; + + OutboxEventType(String aggregateType, String auditAction) { + this.aggregateType = aggregateType; + this.auditAction = auditAction; + } + + public String aggregateType() { + return aggregateType; + } + + /** dbo.AuditLog.Action the relay writes for this event */ + public String auditAction() { + return auditAction; + } +} diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/outbox/OutboxPayload.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/outbox/OutboxPayload.java new file mode 100644 index 00000000..567ecb21 --- /dev/null +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/outbox/OutboxPayload.java @@ -0,0 +1,21 @@ +package com.northwind.scheduling.outbox; + +import java.util.List; + +/** JSON body of outbox_event.payload: resulting state, not a delta (timestamps are ISO local date-times). */ +public record OutboxPayload(String type, String detail, List bookings, List sessions) { + + public OutboxPayload { + bookings = bookings == null ? List.of() : List.copyOf(bookings); + sessions = sessions == null ? List.of() : List.copyOf(sessions); + } + + /** waitlistPosition null = no waitlist row for this booking */ + public record BookingState(int bookingId, int sessionId, int candidateId, String status, String accommodationCode, + String channel, String externalRef, String bookedAt, String cancelledAt, + String cancelReason, Integer waitlistPosition, String waitlistAddedAt) { + } + + public record SessionState(int sessionId, int capacity, int accommodationCapacity, int seatsAvailable) { + } +} diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/outbox/OutboxRepository.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/outbox/OutboxRepository.java new file mode 100644 index 00000000..735b34dd --- /dev/null +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/outbox/OutboxRepository.java @@ -0,0 +1,142 @@ +package com.northwind.scheduling.outbox; + +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Repository; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * Transactional outbox (MBA-3823, V2__outbox.sql). {@link #append} runs inside the caller's transaction on the + * Scheduling database, so the event commits or rolls back with the change it describes. The payload is the + * resulting state of every affected booking and session, which makes the relay a state projection: applying an + * event again writes the same values. + */ +@Repository +public class OutboxRepository { + + private final JdbcTemplate jdbc; + private final NamedParameterJdbcTemplate named; + private final ObjectMapper json; + + public OutboxRepository(JdbcTemplate jdbc, ObjectMapper json) { + this.jdbc = jdbc; + this.named = new NamedParameterJdbcTemplate(jdbc); + this.json = json; + } + + /** Appends one event; returns its sequence number. */ + public long append(OutboxEventType type, int aggregateId, Collection bookingIds, + Collection sessionIds, String actor, String detail) { + OutboxPayload payload = new OutboxPayload(type.name(), detail, bookingStates(bookingIds), sessionStates(sessionIds)); + String body; + try { + body = json.writeValueAsString(payload); + } catch (JsonProcessingException e) { + throw new IllegalStateException("outbox payload for " + type, e); + } + Long seq = jdbc.queryForObject("SET NOCOUNT ON; INSERT INTO outbox_event (event_type, aggregate_type, aggregate_id, " + + "payload, actor) VALUES (?, ?, ?, ?, ?); SELECT CAST(SCOPE_IDENTITY() AS BIGINT);", Long.class, + type.name(), type.aggregateType(), aggregateId, body, actor); + return seq == null ? -1 : seq; + } + + /** All sessions in one event: SeatsAvailable for every session as scheduling-service derives it. */ + public long appendInventoryBaseline(String actor) { + List ids = jdbc.queryForList("SELECT session_id FROM exam_session ORDER BY session_id", Integer.class); + return append(OutboxEventType.InventoryBaselined, 0, List.of(), ids, actor, "sessions=" + ids.size()); + } + + public List pending(int limit) { + return jdbc.query("SELECT TOP (?) event_seq, event_id, event_type, aggregate_id, payload, actor, created_at " + + "FROM outbox_event WHERE relayed_at IS NULL ORDER BY event_seq", (rs, i) -> new OutboxEvent( + rs.getLong("event_seq"), UUID.fromString(rs.getString("event_id")), + OutboxEventType.valueOf(rs.getString("event_type")), rs.getInt("aggregate_id"), + read(rs.getString("payload")), rs.getString("actor"), rs.getTimestamp("created_at").toLocalDateTime()), + limit); + } + + /** Marks an event relayed and stores the legacy BookingIds the relay assigned (same id, IDENTITY_INSERT). */ + public void markRelayed(OutboxEvent e) { + jdbc.update("UPDATE outbox_event SET relayed_at = SYSUTCDATETIME(), attempts = attempts + 1, last_error = NULL " + + "WHERE event_seq = ? AND relayed_at IS NULL", e.seq()); + for (OutboxPayload.BookingState b : e.payload().bookings()) { + jdbc.update("UPDATE booking SET legacy_booking_id = ? WHERE booking_id = ? AND legacy_booking_id IS NULL", + b.bookingId(), b.bookingId()); + } + } + + public void markFailed(OutboxEvent e, String error) { + String msg = error == null ? "unknown" : error.length() > 1000 ? error.substring(0, 1000) : error; + jdbc.update("UPDATE outbox_event SET attempts = attempts + 1, last_error = ? WHERE event_seq = ?", msg, e.seq()); + } + + public Map stats() { + return jdbc.queryForMap("SELECT COUNT(*) AS total, " + + "SUM(CASE WHEN relayed_at IS NULL THEN 1 ELSE 0 END) AS pending, " + + "MAX(event_seq) AS lastSeq, MAX(CASE WHEN relayed_at IS NOT NULL THEN event_seq END) AS lastRelayedSeq, " + + "MIN(CASE WHEN relayed_at IS NULL THEN created_at END) AS oldestPendingAt FROM outbox_event"); + } + + private OutboxPayload read(String body) { + try { + return json.readValue(body, OutboxPayload.class); + } catch (JsonProcessingException e) { + throw new IllegalStateException("unreadable outbox payload", e); + } + } + + private List bookingStates(Collection ids) { + if (ids == null || ids.isEmpty()) { + return List.of(); + } + return named.query("SELECT b.booking_id, b.session_id, b.candidate_id, b.status, b.accommodation_code, b.channel, " + + "b.external_ref, b.booked_at, b.cancelled_at, b.cancel_reason, w.position, w.added_at " + + "FROM booking b LEFT JOIN waitlist_entry w ON w.booking_id = b.booking_id " + + "WHERE b.booking_id IN (:ids) ORDER BY b.booking_id", + new MapSqlParameterSource("ids", new ArrayList<>(new LinkedHashSet<>(ids))), + (rs, i) -> new OutboxPayload.BookingState(rs.getInt("booking_id"), rs.getInt("session_id"), + rs.getInt("candidate_id"), rs.getString("status"), rs.getString("accommodation_code"), + rs.getString("channel"), rs.getString("external_ref"), ts(rs.getTimestamp("booked_at")), + ts(rs.getTimestamp("cancelled_at")), rs.getString("cancel_reason"), + (Integer) rs.getObject("position"), ts(rs.getTimestamp("added_at")))); + } + + private List sessionStates(Collection ids) { + if (ids == null || ids.isEmpty()) { + return List.of(); + } + Map out = new LinkedHashMap<>(); + // chunked: SQL Server allows ~2100 parameters per statement + List all = new ArrayList<>(new LinkedHashSet<>(ids)); + for (int from = 0; from < all.size(); from += 1000) { + List chunk = all.subList(from, Math.min(all.size(), from + 1000)); + named.query("SELECT s.session_id, s.capacity, s.accommodation_capacity, " + + "(SELECT COUNT(*) FROM booking b WHERE b.session_id = s.session_id AND b.status IN ('BOOKED','ATTENDED')) AS occupied " + + "FROM exam_session s WHERE s.session_id IN (:ids) ORDER BY s.session_id", + new MapSqlParameterSource("ids", chunk), rs -> { + int capacity = rs.getInt("capacity"); + int occupied = rs.getInt("occupied"); + int id = rs.getInt("session_id"); + out.put(id, new OutboxPayload.SessionState(id, capacity, rs.getInt("accommodation_capacity"), + Math.max(0, capacity - occupied))); + }); + } + return new ArrayList<>(out.values()); + } + + private static String ts(Timestamp t) { + return t == null ? null : t.toLocalDateTime().toString(); + } +} diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/persistence/SchedulingRepository.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/persistence/SchedulingRepository.java index 195f7045..44cd23d0 100644 --- a/services/scheduling-service/src/main/java/com/northwind/scheduling/persistence/SchedulingRepository.java +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/persistence/SchedulingRepository.java @@ -239,6 +239,11 @@ public void markCancelled(int bookingId, String reason, LocalDateTime now) { + "WHERE booking_id = ?").params(Timestamp.valueOf(now), reason, Timestamp.valueOf(now), bookingId).update(); } + public void markAttended(int bookingId, LocalDateTime now) { + db.sql("UPDATE booking SET status = 'ATTENDED', last_modified = ? WHERE booking_id = ? AND status = 'BOOKED'") + .params(Timestamp.valueOf(now), bookingId).update(); + } + public void markWaitlisted(int bookingId, LocalDateTime now) { db.sql("UPDATE booking SET status = 'WAITLISTED', last_modified = ? WHERE booking_id = ?") .params(Timestamp.valueOf(now), bookingId).update(); diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/ReconcileCommand.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/ReconcileCommand.java new file mode 100644 index 00000000..06c2b448 --- /dev/null +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/ReconcileCommand.java @@ -0,0 +1,39 @@ +package com.northwind.scheduling.reconciliation; + +import java.util.Arrays; + +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; + +/** + * CLI {@code java -jar scheduling-service.jar reconcile [--spring.props...]} (scripts/reconcile.ps1|.sh): starts the + * context without the web server, relay or start-up import, writes reports/reconciliation-<ts>.md and exits + * 0 when there are no mismatches and seat-drift.sql returned no rows, else 1. + */ +public final class ReconcileCommand { + + private ReconcileCommand() { + } + + public static int run(Class app, String[] args) { + try (ConfigurableApplicationContext ctx = new SpringApplicationBuilder(app).web(WebApplicationType.NONE) + .run(commandLine(args))) { + Reconciliation r = ctx.getBean(ReconciliationService.class).reconcile(true); + System.out.printf("reconcile: sessions=%d bookings=%d mismatches=%d (session fields %d, bookings %d) " + + "seat-drift rows=%d report=%s%n", r.sessionsCompared(), r.bookingsCompared(), + r.mismatches(), r.sessionMismatches().size(), r.bookingMismatches().size(), r.seatDrift().size(), + r.reportFile()); + return r.mismatches() == 0 && r.seatDrift().isEmpty() ? 0 : 1; + } + } + + /** Read-only overrides go on the command line: default properties would lose to application.yml. */ + static String[] commandLine(String[] args) { + String[] fixed = {"--scheduling.relay.enabled=false", "--scheduling.import.on-startup=false", + "--spring.main.banner-mode=off"}; + String[] out = Arrays.copyOf(fixed, fixed.length + args.length - 1); + System.arraycopy(args, 1, out, fixed.length, args.length - 1); + return out; + } +} diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/Reconciliation.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/Reconciliation.java new file mode 100644 index 00000000..d8226771 --- /dev/null +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/Reconciliation.java @@ -0,0 +1,32 @@ +package com.northwind.scheduling.reconciliation; + +import java.util.List; + +/** Result of comparing Scheduling with AssessmentDelivery (MBA-3823). */ +public record Reconciliation(String generatedAt, int sessionsCompared, int bookingsCompared, + List sessionMismatches, List bookingMismatches, + List seatDrift, String reportFile) { + + public int mismatches() { + return sessionMismatches.size() + bookingMismatches.size(); + } + + /** one row per differing field: capacity, seatsAvailable, booked, attended, waitlisted, accommodationUsed */ + public record SessionMismatch(int sessionId, String field, Integer scheduling, Integer legacy) { + } + + /** status/session differences; missing side = null */ + public record BookingMismatch(int bookingId, Integer schedulingSessionId, String schedulingStatus, + String schedulingAccommodation, Integer legacySessionId, String legacyStatus, + String legacyAccommodation) { + } + + /** db/checks/seat-drift.sql row */ + public record SeatDriftRow(int sessionId, String examCode, String centerCode, String startsAt, int capacity, + int seatsAvailable, int activeBookings, int drift) { + } + + public record SessionCounts(int capacity, int seatsAvailable, int booked, int attended, int waitlisted, + int accommodationUsed) { + } +} diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/ReconciliationController.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/ReconciliationController.java new file mode 100644 index 00000000..b1052f98 --- /dev/null +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/ReconciliationController.java @@ -0,0 +1,37 @@ +package com.northwind.scheduling.reconciliation; + +import java.util.Map; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.northwind.scheduling.outbox.OutboxRepository; + +/** + * Operator endpoints (MBA-3823), outside /api/v2 and the public OpenAPI spec; the routing gateway does not expose + * them, call scheduling-service on :8090 directly. + */ +@RestController +public class ReconciliationController { + + private final ReconciliationService reconciliation; + private final OutboxRepository outbox; + + public ReconciliationController(ReconciliationService reconciliation, OutboxRepository outbox) { + this.reconciliation = reconciliation; + this.outbox = outbox; + } + + /** Compares Scheduling with AssessmentDelivery; write=true also writes reports/reconciliation-<ts>.md */ + @GetMapping("/internal/reconciliation") + public Reconciliation reconcile(@RequestParam(defaultValue = "true") boolean write) { + return reconciliation.reconcile(write); + } + + /** Outbox backlog: total / pending events, last and last relayed sequence, oldest pending timestamp. */ + @GetMapping("/internal/relay") + public Map relay() { + return outbox.stats(); + } +} diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/ReconciliationReport.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/ReconciliationReport.java new file mode 100644 index 00000000..73218be6 --- /dev/null +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/ReconciliationReport.java @@ -0,0 +1,74 @@ +package com.northwind.scheduling.reconciliation; + +import java.nio.file.Path; + +/** Markdown for reports/reconciliation-<timestamp>.md (MBA-3823). */ +final class ReconciliationReport { + + private static final int MAX_ROWS = 200; + + private ReconciliationReport() { + } + + static String markdown(Reconciliation r, Path seatDriftSql) { + StringBuilder sb = new StringBuilder(); + sb.append("# Reconciliation: Scheduling vs AssessmentDelivery\n\n"); + sb.append("Generated ").append(r.generatedAt()).append(" by scheduling-service `reconcile` (MBA-3823).\n\n"); + sb.append("| check | result |\n|---|---|\n"); + sb.append("| sessions compared | ").append(r.sessionsCompared()).append(" |\n"); + sb.append("| bookings compared | ").append(r.bookingsCompared()).append(" |\n"); + sb.append("| session field mismatches | ").append(r.sessionMismatches().size()).append(" |\n"); + sb.append("| booking mismatches | ").append(r.bookingMismatches().size()).append(" |\n"); + sb.append("| **total mismatches** | **").append(r.mismatches()).append("** |\n"); + sb.append("| `db/checks/seat-drift.sql` rows | ").append(r.seatDrift().size()).append(" |\n\n"); + sb.append("Per session: Capacity, SeatsAvailable (Scheduling derives Capacity - (BOOKED + ATTENDED), floored at 0; ") + .append("legacy is the stored counter), BOOKED / ATTENDED / WAITLISTED counts and accommodation seats used ") + .append("(BOOKED + ATTENDED with ET25/ET50). Per booking: session, status and accommodation code, keyed on the legacy BookingId ") + .append("(`booking.legacy_booking_id`).\n\n"); + + sb.append("## Session mismatches\n\n"); + if (r.sessionMismatches().isEmpty()) { + sb.append("None.\n\n"); + } else { + sb.append("| SessionId | field | Scheduling | AssessmentDelivery |\n|---|---|---|---|\n"); + r.sessionMismatches().stream().limit(MAX_ROWS).forEach(m -> sb.append("| ").append(m.sessionId()).append(" | ") + .append(m.field()).append(" | ").append(n(m.scheduling())).append(" | ").append(n(m.legacy())).append(" |\n")); + more(sb, r.sessionMismatches().size()); + } + + sb.append("## Booking mismatches\n\n"); + if (r.bookingMismatches().isEmpty()) { + sb.append("None.\n\n"); + } else { + sb.append("| BookingId | Scheduling session | Scheduling status | Scheduling accommodation | legacy session | legacy status | legacy accommodation |\n|---|---|---|---|---|---|---|\n"); + r.bookingMismatches().stream().limit(MAX_ROWS).forEach(m -> sb.append("| ").append(m.bookingId()).append(" | ") + .append(n(m.schedulingSessionId())).append(" | ").append(n(m.schedulingStatus())).append(" | ").append(n(m.schedulingAccommodation())).append(" | ") + .append(n(m.legacySessionId())).append(" | ").append(n(m.legacyStatus())).append(" | ").append(n(m.legacyAccommodation())).append(" |\n")); + more(sb, r.bookingMismatches().size()); + } + + sb.append("## `db/checks/seat-drift.sql`\n\n"); + sb.append("Source: `db/checks/").append(seatDriftSql.getFileName()).append("`. "); + if (r.seatDrift().isEmpty()) { + sb.append("0 rows (no drift).\n"); + } else { + sb.append(r.seatDrift().size()).append(" rows:\n\n"); + sb.append("| SessionId | ExamCode | CenterCode | StartsAt | Capacity | SeatsAvailable | ActiveBookings | Drift |\n") + .append("|---|---|---|---|---|---|---|---|\n"); + r.seatDrift().stream().limit(MAX_ROWS).forEach(d -> sb.append("| ").append(d.sessionId()).append(" | ") + .append(d.examCode()).append(" | ").append(d.centerCode()).append(" | ").append(d.startsAt()) + .append(" | ").append(d.capacity()).append(" | ").append(d.seatsAvailable()).append(" | ") + .append(d.activeBookings()).append(" | ").append(d.drift()).append(" |\n")); + more(sb, r.seatDrift().size()); + } + return sb.toString(); + } + + private static void more(StringBuilder sb, int size) { + sb.append(size > MAX_ROWS ? "\n... " + (size - MAX_ROWS) + " more rows\n\n" : "\n"); + } + + private static String n(Object o) { + return o == null ? "(missing)" : o.toString(); + } +} diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/ReconciliationService.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/ReconciliationService.java new file mode 100644 index 00000000..e02ebc60 --- /dev/null +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/reconciliation/ReconciliationService.java @@ -0,0 +1,204 @@ +package com.northwind.scheduling.reconciliation; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.Clock; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.stream.Collectors; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.stereotype.Service; + +import com.northwind.scheduling.reconciliation.Reconciliation.BookingMismatch; +import com.northwind.scheduling.reconciliation.Reconciliation.SeatDriftRow; +import com.northwind.scheduling.reconciliation.Reconciliation.SessionCounts; +import com.northwind.scheduling.reconciliation.Reconciliation.SessionMismatch; + +/** + * Compares Scheduling (the single writer) with AssessmentDelivery (the relay's projection) per session and per + * booking, runs db/checks/seat-drift.sql, and writes reports/reconciliation-<timestamp>.md (MBA-3823). + * Replaces capacity-sync's nightly SeatsAvailable rewrite (HB-03, MR-17) with a report. Legacy reads use the + * read-only importer login. + */ +@Service +public class ReconciliationService { + + static final String SESSION_SQL_MODERN = "SELECT s.session_id AS id, s.capacity AS cap, " + + "SUM(CASE WHEN b.status = 'BOOKED' THEN 1 ELSE 0 END) AS booked, " + + "SUM(CASE WHEN b.status = 'ATTENDED' THEN 1 ELSE 0 END) AS attended, " + + "SUM(CASE WHEN b.status = 'WAITLISTED' THEN 1 ELSE 0 END) AS waitlisted, " + + "SUM(CASE WHEN b.status IN ('BOOKED','ATTENDED') AND b.accommodation_code IN ('ET25','ET50') THEN 1 ELSE 0 END) AS acc " + + "FROM exam_session s LEFT JOIN booking b ON b.session_id = s.session_id GROUP BY s.session_id, s.capacity"; + static final String SESSION_SQL_LEGACY = "SELECT s.SessionId AS id, s.Capacity AS cap, s.SeatsAvailable AS seats, " + + "SUM(CASE WHEN b.Status = 'BOOKED' THEN 1 ELSE 0 END) AS booked, " + + "SUM(CASE WHEN b.Status = 'ATTENDED' THEN 1 ELSE 0 END) AS attended, " + + "SUM(CASE WHEN b.Status = 'WAITLISTED' THEN 1 ELSE 0 END) AS waitlisted, " + + "SUM(CASE WHEN b.Status IN ('BOOKED','ATTENDED') AND b.AccommodationCode IN ('ET25','ET50') THEN 1 ELSE 0 END) AS acc " + + "FROM dbo.ExamSession s LEFT JOIN dbo.Booking b ON b.SessionId = s.SessionId " + + "GROUP BY s.SessionId, s.Capacity, s.SeatsAvailable"; + private static final DateTimeFormatter FILE_TS = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss"); + + private final JdbcTemplate scheduling; + private final JdbcTemplate legacy; + private final Clock clock; + private final Path reportDir; + private final Path seatDriftSql; + + @org.springframework.beans.factory.annotation.Autowired + public ReconciliationService(JdbcTemplate scheduling, Clock clock, + @Value("${scheduling.legacy.url}") String url, + @Value("${scheduling.legacy.username}") String user, + @Value("${scheduling.legacy.password}") String password, + @Value("${scheduling.reconciliation.report-dir:reports}") String reportDir, + @Value("${scheduling.reconciliation.seat-drift-sql:db/checks/seat-drift.sql}") String seatDriftSql) { + this(scheduling, legacyJdbc(url, user, password), clock, resolve(reportDir), resolve(seatDriftSql)); + } + + public ReconciliationService(JdbcTemplate scheduling, JdbcTemplate legacy, Clock clock, Path reportDir, + Path seatDriftSql) { + this.scheduling = scheduling; + this.legacy = legacy; + this.clock = clock; + this.reportDir = reportDir; + this.seatDriftSql = seatDriftSql; + } + + public Reconciliation reconcile(boolean writeReport) { + LocalDateTime now = LocalDateTime.now(clock).truncatedTo(ChronoUnit.SECONDS); + Map modern = sessions(scheduling, SESSION_SQL_MODERN, true); + Map old = sessions(legacy, SESSION_SQL_LEGACY, false); + List sm = compareSessions(modern, old); + Map mb = new TreeMap<>(); + scheduling.query("SELECT COALESCE(legacy_booking_id, booking_id) AS id, session_id, status, accommodation_code FROM booking", + rs -> { mb.put(rs.getInt("id"), new Object[] {rs.getInt("session_id"), rs.getString("status"), blank(rs.getString("accommodation_code"))}); }); + Map lb = new TreeMap<>(); + legacy.query("SELECT BookingId, SessionId, Status, AccommodationCode FROM dbo.Booking", + rs -> { lb.put(rs.getInt("BookingId"), new Object[] {rs.getInt("SessionId"), rs.getString("Status"), blank(rs.getString("AccommodationCode"))}); }); + List bm = compareBookings(mb, lb); + List drift = seatDrift(); + Path file = writeReport ? reportDir.resolve("reconciliation-" + FILE_TS.format(now) + ".md") : null; + Reconciliation r = new Reconciliation(now.toString(), union(modern, old).size(), + union(mb, lb).size(), sm, bm, drift, file == null ? null : file.toString()); + if (file != null) { + try { + Files.createDirectories(reportDir); + Files.writeString(file, ReconciliationReport.markdown(r, seatDriftSql), StandardCharsets.UTF_8); + } catch (IOException e) { + throw new IllegalStateException("cannot write " + file, e); + } + } + return r; + } + + static List compareSessions(Map modern, Map old) { + List out = new ArrayList<>(); + for (Integer id : new TreeSet<>(union(modern, old))) { + SessionCounts m = modern.get(id); + SessionCounts l = old.get(id); + if (m == null || l == null) { + out.add(new SessionMismatch(id, "session", m == null ? null : 1, l == null ? null : 1)); + continue; + } + field(out, id, "capacity", m.capacity(), l.capacity()); + field(out, id, "seatsAvailable", m.seatsAvailable(), l.seatsAvailable()); + field(out, id, "booked", m.booked(), l.booked()); + field(out, id, "attended", m.attended(), l.attended()); + field(out, id, "waitlisted", m.waitlisted(), l.waitlisted()); + field(out, id, "accommodationUsed", m.accommodationUsed(), l.accommodationUsed()); + } + return out; + } + + private static String blank(String v) { + return v == null || v.isBlank() ? null : v.trim(); + } + + static List compareBookings(Map modern, Map old) { + List out = new ArrayList<>(); + for (Integer id : new TreeSet<>(union(modern, old))) { + Object[] m = modern.get(id); + Object[] l = old.get(id); + if (m == null || l == null || !Objects.equals(m[0], l[0]) || !Objects.equals(m[1], l[1]) + || !Objects.equals(m[2], l[2])) { + out.add(new BookingMismatch(id, m == null ? null : (Integer) m[0], m == null ? null : (String) m[1], + m == null ? null : (String) m[2], l == null ? null : (Integer) l[0], + l == null ? null : (String) l[1], l == null ? null : (String) l[2])); + } + } + return out; + } + + /** Runs db/checks/seat-drift.sql (its USE/GO lines are sqlcmd syntax and are dropped). */ + List seatDrift() { + String sql; + try { + sql = Files.readString(seatDriftSql, StandardCharsets.UTF_8); + } catch (IOException e) { + throw new IllegalStateException("cannot read " + seatDriftSql, e); + } + String query = sql.lines().filter(l -> !l.trim().matches("(?i)(GO|USE\\s+\\w+;?)")) + .collect(Collectors.joining("\n")); + return legacy.query(query, (rs, i) -> new SeatDriftRow(rs.getInt("SessionId"), rs.getString("ExamCode"), + rs.getString("CenterCode"), String.valueOf(rs.getTimestamp("StartsAt").toLocalDateTime()), + rs.getInt("Capacity"), rs.getInt("SeatsAvailable"), rs.getInt("ActiveBookings"), rs.getInt("Drift"))); + } + + private static Map sessions(JdbcTemplate jdbc, String sql, boolean derivedSeats) { + Map out = new TreeMap<>(); + jdbc.query(sql, rs -> { + int cap = rs.getInt("cap"); + int booked = rs.getInt("booked"); + int attended = rs.getInt("attended"); + int seats = derivedSeats ? Math.max(0, cap - booked - attended) : rs.getInt("seats"); + out.put(rs.getInt("id"), new SessionCounts(cap, seats, booked, attended, rs.getInt("waitlisted"), + rs.getInt("acc"))); + }); + return out; + } + + private static void field(List out, int id, String name, int m, int l) { + if (m != l) { + out.add(new SessionMismatch(id, name, m, l)); + } + } + + private static Set union(Map a, Map b) { + Set s = new TreeSet<>(a.keySet()); + s.addAll(b.keySet()); + return s; + } + + /** relative paths: as given (run scripts start in the repo root), else from the module dir (../../) */ + /** Relative paths are resolved against the repository root (nearest ancestor holding db/checks), else the cwd. */ + static Path resolve(String p) { + Path path = Path.of(p); + if (path.isAbsolute()) { + return path; + } + for (Path dir = Path.of("").toAbsolutePath(); dir != null; dir = dir.getParent()) { + if (Files.isDirectory(dir.resolve("db").resolve("checks"))) { + return dir.resolve(p).normalize(); + } + } + return path; + } + + private static JdbcTemplate legacyJdbc(String url, String user, String password) { + DriverManagerDataSource ds = new DriverManagerDataSource(url, user, password); + ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); + return new JdbcTemplate(ds); + } +} diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/relay/LegacyRelay.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/relay/LegacyRelay.java new file mode 100644 index 00000000..c3bf3669 --- /dev/null +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/relay/LegacyRelay.java @@ -0,0 +1,118 @@ +package com.northwind.scheduling.relay; + +import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.transaction.support.TransactionTemplate; + +import com.northwind.scheduling.outbox.OutboxEvent; +import com.northwind.scheduling.outbox.OutboxPayload; + +/** + * Applies one outbox event to AssessmentDelivery as ad_sched_relay (MBA-3823), in one legacy transaction: + *

    + *
  1. {@code SESSION_CONTEXT(N'writer') = N'scheduling-relay'} so trg_Booking_SeatSync / trg_ExamSession_Update + * skip their side effects (HB-01, HB-04, HB-05; 032/033 return early);
  2. + *
  3. if dbo.RelayCheckpoint already has the event id the event is a no-op (replay after a crash);
  4. + *
  5. ExamSession Capacity/AccommodationCapacity/SeatsAvailable, Booking rows (new ones keep scheduling-service's + * BookingId via IDENTITY_INSERT), Waitlist rows, one AuditLog row and the checkpoint row.
  6. + *
+ */ +public class LegacyRelay { + + private static final Logger log = LoggerFactory.getLogger(LegacyRelay.class); + public static final String WRITER = "scheduling-relay"; + + private final JdbcTemplate jdbc; + private final TransactionTemplate tx; + + public LegacyRelay(JdbcTemplate legacyJdbc) { + this.jdbc = legacyJdbc; + this.tx = new TransactionTemplate(new DataSourceTransactionManager(legacyJdbc.getDataSource())); + } + + /** @return true if applied now, false if the checkpoint shows it was applied before */ + public boolean apply(OutboxEvent e) { + Boolean applied = tx.execute(status -> { + jdbc.execute("EXEC sp_set_session_context @key = N'writer', @value = N'" + WRITER + "'"); + Integer done = jdbc.queryForObject("SELECT COUNT(*) FROM dbo.RelayCheckpoint WITH (UPDLOCK, HOLDLOCK) " + + "WHERE EventId = ?", Integer.class, e.eventId().toString()); + if (done != null && done > 0) { + return false; + } + OutboxPayload p = e.payload(); + for (OutboxPayload.SessionState s : p.sessions()) { + jdbc.update("UPDATE dbo.ExamSession SET Capacity = ?, AccommodationCapacity = ?, SeatsAvailable = ? " + + "WHERE SessionId = ? AND (Capacity <> ? OR AccommodationCapacity <> ? OR SeatsAvailable <> ?)", + s.capacity(), s.accommodationCapacity(), s.seatsAvailable(), s.sessionId(), + s.capacity(), s.accommodationCapacity(), s.seatsAvailable()); + } + for (OutboxPayload.BookingState b : p.bookings()) { + upsertBooking(b); + } + for (OutboxPayload.BookingState b : p.bookings()) { + upsertWaitlist(b); + } + jdbc.update("INSERT INTO dbo.AuditLog (EntityName, EntityId, Action, Detail, Actor) VALUES (?, ?, ?, ?, ?)", + e.type().aggregateType(), e.aggregateId(), e.type().auditAction(), + truncate("outbox seq=" + e.seq() + " event=" + e.eventId() + " " + e.type() + + (p.detail() == null ? "" : " " + p.detail()), 1000), + truncate(e.actor(), 60)); + jdbc.update("INSERT INTO dbo.RelayCheckpoint (EventId, EventSeq, EventType) VALUES (?, ?, ?)", + e.eventId().toString(), e.seq(), e.type().name()); + return true; + }); + if (Boolean.TRUE.equals(applied)) { + log.debug("relayed outbox seq={} {} {}", e.seq(), e.type(), e.eventId()); + } + return Boolean.TRUE.equals(applied); + } + + private void upsertBooking(OutboxPayload.BookingState b) { + int n = jdbc.update("UPDATE dbo.Booking SET SessionId = ?, Status = ?, AccommodationCode = ?, Channel = ?, " + + "ExternalRef = ?, CancelledAt = ?, CancelReason = ?, LastModified = GETDATE() WHERE BookingId = ?", + b.sessionId(), b.status(), b.accommodationCode(), b.channel(), b.externalRef(), ts(b.cancelledAt()), + b.cancelReason(), b.bookingId()); + if (n > 0) { + return; + } + jdbc.update("SET IDENTITY_INSERT dbo.Booking ON; " + + "INSERT INTO dbo.Booking (BookingId, SessionId, CandidateId, Status, AccommodationCode, Channel, " + + "ExternalRef, BookedAt, CancelledAt, CancelReason) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?); " + + "SET IDENTITY_INSERT dbo.Booking OFF;", + b.bookingId(), b.sessionId(), b.candidateId(), b.status(), b.accommodationCode(), b.channel(), + b.externalRef(), ts(b.bookedAt()), ts(b.cancelledAt()), b.cancelReason()); + } + + private void upsertWaitlist(OutboxPayload.BookingState b) { + if (b.waitlistPosition() == null) { + jdbc.update("DELETE FROM dbo.Waitlist WHERE BookingId = ?", b.bookingId()); + return; + } + int n = jdbc.update("UPDATE dbo.Waitlist SET SessionId = ?, CandidateId = ?, Position = ? WHERE BookingId = ?", + b.sessionId(), b.candidateId(), b.waitlistPosition(), b.bookingId()); + if (n == 0) { + jdbc.update("INSERT INTO dbo.Waitlist (SessionId, CandidateId, BookingId, Position, AddedAt) " + + "VALUES (?, ?, ?, ?, COALESCE(?, GETDATE()))", + b.sessionId(), b.candidateId(), b.bookingId(), b.waitlistPosition(), ts(b.waitlistAddedAt())); + } + } + + public List checkpointedEventIds() { + return jdbc.queryForList("SELECT CONVERT(VARCHAR(36), EventId) FROM dbo.RelayCheckpoint ORDER BY EventSeq", + String.class); + } + + static Timestamp ts(String iso) { + return iso == null ? null : Timestamp.valueOf(LocalDateTime.parse(iso)); + } + + private static String truncate(String s, int max) { + return s == null || s.length() <= max ? s : s.substring(0, max); + } +} diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/relay/RelayConfig.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/relay/RelayConfig.java new file mode 100644 index 00000000..292abb07 --- /dev/null +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/relay/RelayConfig.java @@ -0,0 +1,33 @@ +package com.northwind.scheduling.relay; + +import java.util.Properties; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.scheduling.annotation.EnableScheduling; + +/** + * AssessmentDelivery connection for the outbox relay, as ad_sched_relay (db/security/036_component_logins.sql). + * Not a Spring DataSource bean, so the Scheduling DataSource stays the only auto-configured one. + */ +@Configuration +@EnableScheduling +@ConditionalOnProperty(name = "scheduling.relay.enabled", havingValue = "true") +public class RelayConfig { + + @Bean + LegacyRelay legacyRelay(@Value("${scheduling.relay.url}") String url, + @Value("${scheduling.relay.username}") String user, + @Value("${scheduling.relay.password}") String password) { + DriverManagerDataSource ds = new DriverManagerDataSource(url, user, password); + ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); + Properties p = new Properties(); + p.setProperty("loginTimeout", "5"); + ds.setConnectionProperties(p); + return new LegacyRelay(new JdbcTemplate(ds)); + } +} diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/relay/RelayWorker.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/relay/RelayWorker.java new file mode 100644 index 00000000..692f149c --- /dev/null +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/relay/RelayWorker.java @@ -0,0 +1,103 @@ +package com.northwind.scheduling.relay; + +import java.sql.Timestamp; +import java.time.Duration; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.Map; +import java.util.concurrent.atomic.AtomicLong; + +import io.micrometer.core.instrument.Gauge; +import io.micrometer.core.instrument.MeterRegistry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import com.northwind.scheduling.outbox.OutboxEvent; +import com.northwind.scheduling.outbox.OutboxRepository; + +/** + * Polls outbox_event in event_seq order and hands each pending event to {@link LegacyRelay} (MBA-3823). An event is + * marked relayed in Scheduling only after its legacy transaction committed; a crash in between leaves it pending and + * the replay is a checkpointed no-op. A failing event stops the batch so later events never overtake it. + */ +@Component +@ConditionalOnProperty(name = "scheduling.relay.enabled", havingValue = "true") +public class RelayWorker { + + private static final Logger log = LoggerFactory.getLogger(RelayWorker.class); + + private final OutboxRepository outbox; + private final LegacyRelay relay; + private final int batchSize; + private final AtomicLong applied = new AtomicLong(); + private final AtomicLong skipped = new AtomicLong(); + private final AtomicLong pending = new AtomicLong(); + private final AtomicLong lagMillis = new AtomicLong(); + + public RelayWorker(OutboxRepository outbox, LegacyRelay relay, + @Value("${scheduling.relay.batch-size:200}") int batchSize, + ObjectProvider meters) { + this.outbox = outbox; + this.relay = relay; + this.batchSize = batchSize; + meters.ifAvailable(r -> { + Gauge.builder("scheduling.outbox.pending", pending, AtomicLong::get) + .description("Outbox events not yet relayed to AssessmentDelivery").register(r); + Gauge.builder("scheduling.outbox.lag", lagMillis, v -> v.get() / 1000.0).baseUnit("seconds") + .description("Age of the oldest unrelayed outbox event").register(r); + }); + } + + @Scheduled(fixedDelayString = "${scheduling.relay.poll-ms:250}", initialDelayString = "${scheduling.relay.poll-ms:250}") + public void poll() { + try { + relayPending(); + refreshGauges(); + } catch (RuntimeException e) { + log.warn("relay poll failed: {}", e.getMessage()); + } + } + + /** One pass over the pending events; returns how many were processed (applied or replay no-ops). */ + public synchronized int relayPending() { + int processed = 0; + for (OutboxEvent e : outbox.pending(batchSize)) { + try { + if (relay.apply(e)) { + applied.incrementAndGet(); + } else { + skipped.incrementAndGet(); + } + } catch (RuntimeException ex) { + log.error("relay of outbox seq={} {} failed: {}", e.seq(), e.type(), ex.getMessage()); + outbox.markFailed(e, ex.getMessage()); + break; + } + outbox.markRelayed(e); + processed++; + } + return processed; + } + + public long appliedCount() { + return applied.get(); + } + + public long skippedCount() { + return skipped.get(); + } + + void refreshGauges() { + Map st = outbox.stats(); + pending.set(st.get("pending") == null ? 0 : ((Number) st.get("pending")).longValue()); + Object oldest = st.get("oldestPendingAt"); + LocalDateTime at = oldest instanceof Timestamp t ? t.toLocalDateTime() + : oldest instanceof LocalDateTime l ? l : null; + lagMillis.set(at == null ? 0 : Math.max(0, Duration.between(at, LocalDateTime.now(ZoneOffset.UTC)).toMillis())); + } +} diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/service/SchedulingService.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/service/SchedulingService.java index 2d40c68d..e5e55eb7 100644 --- a/services/scheduling-service/src/main/java/com/northwind/scheduling/service/SchedulingService.java +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/service/SchedulingService.java @@ -1,5 +1,8 @@ package com.northwind.scheduling.service; +import com.northwind.scheduling.outbox.OutboxEventType; +import com.northwind.scheduling.outbox.OutboxRepository; + import java.time.Clock; import java.time.LocalDate; import java.time.LocalDateTime; @@ -42,11 +45,13 @@ public record CapacityOutcome(CapacityChange.Plan plan, Rows.Session session) { private final AccommodationFloorPolicy floorPolicy; private final Clock clock; private final String actor; + private final OutboxRepository outbox; public SchedulingService(SchedulingRepository repo, RetakeEligibilityPolicy retakePolicy, AccommodationFloorPolicy floorPolicy, Clock clock, - @Value("${scheduling.actor:scheduling-service}") String actor) { + @Value("${scheduling.actor:scheduling-service}") String actor, OutboxRepository outbox) { this.repo = repo; + this.outbox = outbox; this.retakePolicy = retakePolicy; this.floorPolicy = floorPolicy; this.clock = clock; @@ -157,6 +162,10 @@ private BookingOutcome book(BookCommand cmd, Integer rescheduledFrom) { position = BookingRules.nextWaitlistPosition(repo.maxWaitlistPosition(sessionId)); repo.insertWaitlist(bookingId, position, now); } + if (rescheduledFrom == null) { + outbox.append(status == BookingStatus.BOOKED ? OutboxEventType.BookingCreated : OutboxEventType.BookingWaitlisted, + bookingId, List.of(bookingId), List.of(sessionId), actor, "channel=" + channel); + } return new BookingOutcome(bookingId, status, position, sessionId); } @@ -171,6 +180,30 @@ public Rows.Booking cancel(int bookingId, String reason) { if (b.status() == BookingStatus.WAITLISTED) { repo.deleteWaitlistEntry(bookingId); } + outbox.append(OutboxEventType.BookingCancelled, bookingId, List.of(bookingId), List.of(b.sessionId()), actor, + "reason=" + reason); + return booking(bookingId); + } + + /** + * MBA-3823: results-service reports attendance here instead of updating dbo.Booking. BOOKED becomes ATTENDED + * (one AttendanceRecorded event); an already ATTENDED booking is a no-op, so ingestion retries are safe. + */ + @Transactional + public Rows.Booking recordAttendance(int bookingId) { + Rows.Booking b = booking(bookingId); + repo.lockSession(b.sessionId()); + b = booking(bookingId); + if (b.status() == BookingStatus.ATTENDED) { + return b; + } + if (b.status() != BookingStatus.BOOKED) { + // reuses NOT_CANCELLABLE ("booking is not BOOKED") so the published error vocabulary stays unchanged + throw new SchedulingException(ErrorCode.NOT_CANCELLABLE, + "Booking " + bookingId + " is " + b.status() + ", cannot record attendance"); + } + repo.markAttended(bookingId, now()); + outbox.append(OutboxEventType.AttendanceRecorded, bookingId, List.of(bookingId), List.of(b.sessionId()), actor, null); return booking(bookingId); } @@ -196,8 +229,11 @@ public BookingOutcome reschedule(int bookingId, Integer newSessionId) { if (old.status() == BookingStatus.WAITLISTED) { repo.deleteWaitlistEntry(bookingId); } - return book(new BookCommand(newSessionId, old.candidateNumber(), old.accommodationCode(), old.externalRef(), - old.channel()), bookingId); + BookingOutcome moved = book(new BookCommand(newSessionId, old.candidateNumber(), old.accommodationCode(), + old.externalRef(), old.channel()), bookingId); + outbox.append(OutboxEventType.BookingRescheduled, moved.bookingId(), List.of(bookingId, moved.bookingId()), + List.of(old.sessionId(), newSessionId), actor, "from=" + bookingId + " to=" + moved.bookingId()); + return moved; } /** usp_ChangeCapacity + trg_ExamSession_Update demotion (ODD-02 kept, ODD-06 floor per ADR 0002). */ @@ -219,6 +255,10 @@ public CapacityOutcome changeCapacity(int sessionId, Integer newCapacity, String repo.insertWaitlist(d.bookingId(), d.position(), now); } repo.insertCapacityChange(plan, reason, actor, now); + outbox.append(OutboxEventType.CapacityChanged, sessionId, + plan.demotions().stream().map(CapacityChange.Demotion::bookingId).toList(), List.of(sessionId), actor, + "capacity=" + plan.newCapacity() + " demoted=" + plan.demotions().size() + + (reason == null ? "" : " reason=" + reason)); return new CapacityOutcome(plan, session(sessionId)); } } diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/web/AttendanceTokenGuard.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/web/AttendanceTokenGuard.java new file mode 100644 index 00000000..6f3cf5f5 --- /dev/null +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/web/AttendanceTokenGuard.java @@ -0,0 +1,33 @@ +package com.northwind.scheduling.web; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import com.northwind.scheduling.domain.ErrorCode; +import com.northwind.scheduling.domain.SchedulingException; + +/** + * MBA-3823: attendance writes ATTENDED into legacy through the relay, so only a caller holding the service credential + * (results-service) may record it. Fails closed: with scheduling.attendance.service-token unset every call is refused. + */ +@Component +public class AttendanceTokenGuard { + + public static final String HEADER = "X-Service-Token"; + + private final byte[] expected; + + public AttendanceTokenGuard(@Value("${scheduling.attendance.service-token:}") String token) { + this.expected = token == null || token.isBlank() ? null : token.getBytes(StandardCharsets.UTF_8); + } + + public void check(String presented) { + if (expected == null || presented == null + || !MessageDigest.isEqual(expected, presented.getBytes(StandardCharsets.UTF_8))) { + throw new SchedulingException(ErrorCode.UNAUTHORIZED, HEADER + " missing or invalid"); + } + } +} diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/web/OpenApiConfig.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/web/OpenApiConfig.java index bfa99702..30204577 100644 --- a/services/scheduling-service/src/main/java/com/northwind/scheduling/web/OpenApiConfig.java +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/web/OpenApiConfig.java @@ -30,6 +30,8 @@ import io.swagger.v3.oas.models.media.StringSchema; import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.responses.ApiResponses; +import io.swagger.v3.oas.models.security.SecurityRequirement; +import io.swagger.v3.oas.models.security.SecurityScheme; import io.swagger.v3.oas.models.servers.Server; import io.swagger.v3.oas.models.tags.Tag; @@ -44,6 +46,8 @@ public class OpenApiConfig { static final String API_VERSION = "2.0.0"; static final String ERROR_SCHEMA = "ApiError"; + /** 401 body of service-token operations; kept out of ApiError so its enum stays compatible with published baselines. */ + static final String SERVICE_TOKEN_ERROR_SCHEMA = "ServiceTokenError"; static final Set SESSION_TIMES = Set.of("startsAt", "sessionStartsAt"); static final String LOCAL_DATE_TIME_PATTERN = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}(:\\d{2}(\\.\\d+)?)?$"; @@ -60,6 +64,7 @@ public class OpenApiConfig { ErrorCode.NOT_ELIGIBLE)), Map.entry("booking", List.of(ErrorCode.BAD_REQUEST, ErrorCode.BOOKING_NOT_FOUND)), Map.entry("cancel", List.of(ErrorCode.BAD_REQUEST, ErrorCode.BOOKING_NOT_FOUND, ErrorCode.NOT_CANCELLABLE)), + Map.entry("attendance", List.of(ErrorCode.BAD_REQUEST, ErrorCode.UNAUTHORIZED, ErrorCode.BOOKING_NOT_FOUND, ErrorCode.NOT_CANCELLABLE)), Map.entry("reschedule", List.of(ErrorCode.VALIDATION, ErrorCode.BOOKING_NOT_FOUND, ErrorCode.SESSION_NOT_FOUND, ErrorCode.NOT_CANCELLABLE, ErrorCode.SAME_SESSION, ErrorCode.DIFFERENT_EXAM, ErrorCode.SESSION_NOT_OPEN, ErrorCode.DUPLICATE_BOOKING, @@ -84,6 +89,8 @@ public class OpenApiConfig { "Book a seat (WAITLISTED when the session is full); returns the legacy {bookingId, status} plus the live booking and session"}), Map.entry("booking", new String[] {"Bookings", "Get a booking"}), Map.entry("cancel", new String[] {"Bookings", "Cancel a booking (frees the seat, nobody is promoted, ODD-09)"}), + Map.entry("attendance", new String[] {"Bookings", + "Record attendance (BOOKED -> ATTENDED; already ATTENDED is a no-op). Called by results-service on result ingestion"}), Map.entry("reschedule", new String[] {"Bookings", "Reschedule a booking to another session of the same exam (cancel + book, new booking id, ODD-05)"}), Map.entry("candidate", new String[] {"Candidates", "Get a candidate profile"}), @@ -92,6 +99,10 @@ public class OpenApiConfig { static final Set CREATED = Set.of("book", "reschedule"); + /** Operations that need the service credential (MBA-3823). */ + static final String SERVICE_TOKEN = "serviceToken"; + static final Set SECURED = Set.of("attendance"); + /** Query parameters declared optional on the controller that SchedulingService rejects with VALIDATION when missing. */ static final Map> REQUIRED_PARAMS = Map.of("eligibility", Set.of("examCode")); @@ -135,6 +146,7 @@ private static Map errorExamples() { Map msg = new LinkedHashMap<>(); msg.put(ErrorCode.VALIDATION, "sessionId and candidateNumber are required"); msg.put(ErrorCode.BAD_REQUEST, "Failed to convert value of type 'java.lang.String' to required type 'int'"); + msg.put(ErrorCode.UNAUTHORIZED, "X-Service-Token missing or invalid"); msg.put(ErrorCode.CANDIDATE_NOT_FOUND, "Candidate NW00009999 not found"); msg.put(ErrorCode.SESSION_NOT_FOUND, "Session 99999 not found"); msg.put(ErrorCode.BOOKING_NOT_FOUND, "Booking 99999 not found"); @@ -191,24 +203,36 @@ null when the rule never existed as a proc error. Malformed input (unreadable JS .servers(List.of(new Server().url("/").description("scheduling-service (default :8090)"))) .tags(List.of(new Tag().name("Bookings"), new Tag().name("Candidates"), new Tag().name("Capacity"), new Tag().name("Reference data"), new Tag().name("Sessions"))) - .components(new Components()); + .components(new Components().addSecuritySchemes(SERVICE_TOKEN, new SecurityScheme() + .type(SecurityScheme.Type.APIKEY).in(SecurityScheme.In.HEADER).name(AttendanceTokenGuard.HEADER) + .description("Service credential (scheduling.attendance.service-token); results-service only"))); } static Schema errorSchema() { - List codes = Arrays.stream(ErrorCode.values()).map(Enum::name).toList(); + return errorSchema(Arrays.stream(ErrorCode.values()).filter(c -> c != ErrorCode.UNAUTHORIZED).toList(), + "Error body of every 4xx answer (ApiExceptionHandler)", "No accommodation seat available on session 155"); + } + + static Schema serviceTokenErrorSchema() { + return errorSchema(List.of(ErrorCode.UNAUTHORIZED), "401 body when X-Service-Token is missing or invalid", + "X-Service-Token missing or invalid"); + } + + static Schema errorSchema(List errorCodes, String description, String exampleMessage) { + List codes = errorCodes.stream().map(Enum::name).toList(); IntegerSchema legacy = new IntegerSchema(); legacy.setNullable(true); legacy.setDescription("50000 + legacy proc RAISERROR state (ODD-11): 50001..50006, see the table in the API " + "description; null when the rule had no proc error. Present on /api/v2 rule errors, absent for BAD_REQUEST " + "and on /api/v1."); - legacy.setExample(50003); + legacy.setExample(codes.contains("NO_ACCOMMODATION_SEAT") ? 50003 : null); StringSchema error = new StringSchema(); - error._enum(codes).description("Symbolic error code (booking-api vocabulary)").example("NO_ACCOMMODATION_SEAT"); + error._enum(codes).description("Symbolic error code (booking-api vocabulary)").example(codes.contains("NO_ACCOMMODATION_SEAT") ? "NO_ACCOMMODATION_SEAT" : codes.get(0)); Schema s = new ObjectSchema() - .description("Error body of every 4xx answer (ApiExceptionHandler)") + .description(description) .addProperty("error", error) .addProperty("message", new StringSchema().description("Human-readable message") - .example("No accommodation seat available on session 155")) + .example(exampleMessage)) .addProperty("legacyCode", legacy); s.setRequired(List.of("error", "message")); return s; @@ -228,6 +252,7 @@ void document(OpenAPI api) { schemas.forEach(this::documentSchema); } api.getComponents().addSchemas(ERROR_SCHEMA, errorSchema()); + api.getComponents().addSchemas(SERVICE_TOKEN_ERROR_SCHEMA, serviceTokenErrorSchema()); if (api.getPaths() == null) { return; } @@ -236,7 +261,7 @@ void document(OpenAPI api) { @SuppressWarnings({"rawtypes", "unchecked"}) void documentSchema(String name, Schema schema) { - if (ERROR_SCHEMA.equals(name) || schema.getProperties() == null) { + if (ERROR_SCHEMA.equals(name) || SERVICE_TOKEN_ERROR_SCHEMA.equals(name) || schema.getProperties() == null) { return; } Map props = schema.getProperties(); @@ -315,6 +340,9 @@ void documentOperation(Operation op) { Set requiredParams = REQUIRED_PARAMS.getOrDefault(id, Set.of()); op.getParameters().stream().filter(p -> requiredParams.contains(p.getName())).forEach(p -> p.setRequired(true)); } + if (SECURED.contains(id)) { + op.addSecurityItem(new SecurityRequirement().addList(SERVICE_TOKEN)); + } ApiResponses responses = op.getResponses(); if (responses == null) { return; @@ -329,13 +357,15 @@ void documentOperation(Operation op) { byStatus.computeIfAbsent(c.httpStatus(), k -> new ArrayList<>()).add(c); } byStatus.forEach((status, codes) -> { - MediaType mt = new MediaType().schema(new Schema<>().$ref("#/components/schemas/" + ERROR_SCHEMA)); + MediaType mt = new MediaType().schema(new Schema<>().$ref("#/components/schemas/" + + (status == 401 ? SERVICE_TOKEN_ERROR_SCHEMA : ERROR_SCHEMA))); for (ErrorCode c : codes) { mt.addExamples(c.name(), new Example().summary(c.name() + (c.legacyCode() == null ? "" : " (legacy " + c.legacyCode() + ")")).value(ERROR_EXAMPLES.get(c.name()))); } String text = switch (status) { case 400 -> "Invalid input"; + case 401 -> "Unauthorized"; case 404 -> "Not found"; default -> "Rule violation"; }; diff --git a/services/scheduling-service/src/main/java/com/northwind/scheduling/web/SchedulingApiController.java b/services/scheduling-service/src/main/java/com/northwind/scheduling/web/SchedulingApiController.java index e3e58d31..23c9e697 100644 --- a/services/scheduling-service/src/main/java/com/northwind/scheduling/web/SchedulingApiController.java +++ b/services/scheduling-service/src/main/java/com/northwind/scheduling/web/SchedulingApiController.java @@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -25,6 +26,8 @@ import com.northwind.scheduling.service.SchedulingService; import com.northwind.scheduling.web.Dtos.*; +import io.swagger.v3.oas.annotations.Parameter; + /** Public scheduling API, base /api/v2. Errors: {error, message, legacyCode} (ApiExceptionHandler). */ @RestController @RequestMapping("/api/v2") @@ -34,10 +37,13 @@ public class SchedulingApiController { private final SchedulingService service; private final RetakeEligibilityPolicy retakePolicy; private final String defaultChannel; + private final AttendanceTokenGuard attendanceGuard; public SchedulingApiController(SchedulingService service, RetakeEligibilityPolicy retakePolicy, - @Value("${scheduling.default-channel:PORTAL}") String defaultChannel) { + @Value("${scheduling.default-channel:PORTAL}") String defaultChannel, + AttendanceTokenGuard attendanceGuard) { this.service = service; + this.attendanceGuard = attendanceGuard; this.retakePolicy = retakePolicy; this.defaultChannel = defaultChannel; } @@ -121,6 +127,18 @@ public BookingDto cancel(@PathVariable int bookingId, @RequestParam(required = f return BookingDto.of(service.cancel(bookingId, reason == null || reason.isBlank() ? "Cancelled by candidate" : reason)); } + /** + * MBA-3823: results-service records attendance here; the outbox relay projects it to dbo.Booking. Needs the + * service credential (X-Service-Token); the gateway does not expose it. + */ + @PostMapping("/bookings/{bookingId}/attendance") + public BookingDto attendance(@PathVariable int bookingId, + @Parameter(hidden = true) @RequestHeader(value = AttendanceTokenGuard.HEADER, required = false) + String serviceToken) { + attendanceGuard.check(serviceToken); + return BookingDto.of(service.recordAttendance(bookingId)); + } + @PostMapping("/bookings/{bookingId}/reschedule") public ResponseEntity reschedule(@PathVariable int bookingId, @RequestBody(required = false) RescheduleRequest req) { diff --git a/services/scheduling-service/src/main/resources/application.yml b/services/scheduling-service/src/main/resources/application.yml index 6b6cfa69..cee608c5 100644 --- a/services/scheduling-service/src/main/resources/application.yml +++ b/services/scheduling-service/src/main/resources/application.yml @@ -72,6 +72,23 @@ scheduling: # Same vocabulary as legacy CK_Booking_Channel so the M4 outbox can write it back unchanged. default-channel: PORTAL actor: ${SCHEDULING_ACTOR:scheduling-service} + # MBA-3823: service credential for POST /api/v2/bookings/{id}/attendance (header X-Service-Token, results-service). + # No default: unset = every attendance call is refused (fail closed); the run scripts set a dev value. + attendance: + service-token: ${SCHEDULING_ATTENDANCE_SERVICE_TOKEN:} + # MBA-3823 outbox relay: projects every outbox_event into AssessmentDelivery as ad_sched_relay + # (db/security/036_component_logins.sql). Local-dev password like scheduling.legacy; Azure uses the env var. + relay: + enabled: ${SCHEDULING_RELAY_ENABLED:true} + url: ${SCHEDULING_RELAY_DB_URL:jdbc:sqlserver://localhost:1433;databaseName=AssessmentDelivery;encrypt=true;trustServerCertificate=true} + username: ${SCHEDULING_RELAY_DB_USER:ad_sched_relay} + password: ${SCHEDULING_RELAY_DB_PASSWORD:Sch3dRelay!Dev2026} + poll-ms: ${SCHEDULING_RELAY_POLL_MS:250} + batch-size: ${SCHEDULING_RELAY_BATCH_SIZE:200} + # GET /internal/reconciliation and the `reconcile` CLI write reports/reconciliation-.md + reconciliation: + report-dir: ${SCHEDULING_RECONCILIATION_REPORT_DIR:reports} + seat-drift-sql: ${SCHEDULING_SEAT_DRIFT_SQL:db/checks/seat-drift.sql} logging: level: diff --git a/services/scheduling-service/src/main/resources/db/migration/V2__outbox.sql b/services/scheduling-service/src/main/resources/db/migration/V2__outbox.sql new file mode 100644 index 00000000..d136372e --- /dev/null +++ b/services/scheduling-service/src/main/resources/db/migration/V2__outbox.sql @@ -0,0 +1,28 @@ +-- MBA-3823 transactional outbox. Every booking/capacity change made by SchedulingService appends one row here in +-- the same transaction (SchedulingService -> OutboxRepository.append), so a committed change always has its event +-- and a rolled-back one never does. event_seq is the monotonically increasing relay order; event_id is the +-- idempotency key the relay records in AssessmentDelivery.dbo.RelayCheckpoint (db/schema/016_relay_checkpoint.sql). +-- payload = JSON snapshot of the resulting state of the affected bookings (incl. waitlist position) and sessions +-- (capacity, accommodation capacity, derived SeatsAvailable), so applying an event twice gives the same rows. +CREATE TABLE outbox_event ( + event_seq BIGINT IDENTITY(1,1) NOT NULL CONSTRAINT pk_outbox_event PRIMARY KEY, + event_id UNIQUEIDENTIFIER NOT NULL CONSTRAINT df_outbox_event_id DEFAULT (NEWID()) + CONSTRAINT uq_outbox_event_id UNIQUE, + event_type VARCHAR(40) NOT NULL CONSTRAINT ck_outbox_event_type CHECK (event_type IN ( + 'BookingCreated', 'BookingCancelled', 'BookingRescheduled', 'BookingWaitlisted', + 'AttendanceRecorded', 'CapacityChanged', 'InventoryBaselined')), + aggregate_type VARCHAR(20) NOT NULL CONSTRAINT ck_outbox_aggregate_type CHECK (aggregate_type IN ('Booking', 'ExamSession')), + aggregate_id INT NOT NULL, + payload NVARCHAR(MAX) NOT NULL, + actor VARCHAR(60) NOT NULL, + created_at DATETIME2(3) NOT NULL CONSTRAINT df_outbox_event_created_at DEFAULT (SYSUTCDATETIME()), + relayed_at DATETIME2(3) NULL, + attempts INT NOT NULL CONSTRAINT df_outbox_event_attempts DEFAULT (0), + last_error NVARCHAR(1000) NULL +); +CREATE INDEX ix_outbox_event_pending ON outbox_event (event_seq) WHERE relayed_at IS NULL; + +-- legacy dbo.Booking.BookingId, set by the relay on first projection (imported bookings: their legacy id) +ALTER TABLE booking ADD legacy_booking_id INT NULL; +GO +CREATE UNIQUE INDEX uq_booking_legacy_booking_id ON booking (legacy_booking_id) WHERE legacy_booking_id IS NOT NULL; diff --git a/services/scheduling-service/src/test/java/com/northwind/scheduling/SqlServerTestBase.java b/services/scheduling-service/src/test/java/com/northwind/scheduling/SqlServerTestBase.java index bb11b42f..66bbf87d 100644 --- a/services/scheduling-service/src/test/java/com/northwind/scheduling/SqlServerTestBase.java +++ b/services/scheduling-service/src/test/java/com/northwind/scheduling/SqlServerTestBase.java @@ -40,6 +40,8 @@ static void props(DynamicPropertyRegistry r) { r.add("spring.datasource.username", SQL::getUsername); r.add("spring.datasource.password", SQL::getPassword); r.add("scheduling.import.on-startup", () -> "true"); + // outbox rows are still written; RelayIT drives the relay against a real AssessmentDelivery + r.add("scheduling.relay.enabled", () -> "false"); } /** Seed-shaped fixture: centre SFO01, exam MHL-RN1 (sessions 154/163) and HPI-ACC2 (155), etc. */ diff --git a/services/scheduling-service/src/test/java/com/northwind/scheduling/reconciliation/ReconcileCommandTest.java b/services/scheduling-service/src/test/java/com/northwind/scheduling/reconciliation/ReconcileCommandTest.java new file mode 100644 index 00000000..d5edbb51 --- /dev/null +++ b/services/scheduling-service/src/test/java/com/northwind/scheduling/reconciliation/ReconcileCommandTest.java @@ -0,0 +1,21 @@ +package com.northwind.scheduling.reconciliation; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class ReconcileCommandTest { + + @Test + void readOnlyOverridesAreCommandLineArgsSoTheyBeatApplicationYml() { + String[] out = ReconcileCommand.commandLine(new String[] {"reconcile", "--scheduling.reconciliation.report-dir=out"}); + assertThat(out).containsExactly("--scheduling.relay.enabled=false", "--scheduling.import.on-startup=false", + "--spring.main.banner-mode=off", "--scheduling.reconciliation.report-dir=out"); + } + + @Test + void userArgsAfterTheVerbCanStillOverride() { + String[] out = ReconcileCommand.commandLine(new String[] {"reconcile"}); + assertThat(out).hasSize(3); + } +} diff --git a/services/scheduling-service/src/test/java/com/northwind/scheduling/relay/LegacyEstateDb.java b/services/scheduling-service/src/test/java/com/northwind/scheduling/relay/LegacyEstateDb.java new file mode 100644 index 00000000..4485013d --- /dev/null +++ b/services/scheduling-service/src/test/java/com/northwind/scheduling/relay/LegacyEstateDb.java @@ -0,0 +1,153 @@ +package com.northwind.scheduling.relay; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; + +import org.testcontainers.DockerClientFactory; +import org.testcontainers.containers.MSSQLServerContainer; + +/** + * A real AssessmentDelivery for the MBA-3823 ITs, built from the repo's db/ scripts (schema, procs, triggers, seed, + * security - the db/apply.ps1 order) under the name {@value #DB}, so a live local estate is never touched. + *

Target: env {@code SCHEDULING_IT_SQL_URL} (e.g. {@code jdbc:sqlserver://localhost:1433;encrypt=true; + * trustServerCertificate=true}) + {@code SCHEDULING_IT_SQL_USER}/{@code _PASSWORD} (sysadmin, default sa / + * Str0ng!Passw0rd) - the Windows SQL Express from scripts/run-legacy.ps1; else Testcontainers when Docker runs; + * else {@link #available()} is false and the ITs are skipped. + */ +public final class LegacyEstateDb { + + public static final String DB = "AssessmentDelivery_RelayIT"; + public static final String SCHEDULING_DB = "Scheduling_RelayIT"; + private static final String[] FOLDERS = {"schema", "procs", "triggers", "seed", "security"}; + + private static String baseUrl; + private static String user; + private static String password; + private static boolean applied; + + private LegacyEstateDb() { + } + + public static synchronized boolean available() { + if (baseUrl != null) { + return true; + } + String env = System.getenv("SCHEDULING_IT_SQL_URL"); + if (env != null && !env.isBlank()) { + baseUrl = env.endsWith(";") ? env.substring(0, env.length() - 1) : env; + user = envOr("SCHEDULING_IT_SQL_USER", "sa"); + password = envOr("SCHEDULING_IT_SQL_PASSWORD", "Str0ng!Passw0rd"); + return true; + } + if (DockerClientFactory.instance().isDockerAvailable()) { + MSSQLServerContainer c = new MSSQLServerContainer<>("mcr.microsoft.com/mssql/server:2022-latest").acceptLicense(); + c.start(); + baseUrl = c.getJdbcUrl(); + user = c.getUsername(); + password = c.getPassword(); + return true; + } + return false; + } + + public static String url(String database) { + return baseUrl + ";databaseName=" + database; + } + + public static String adminUser() { + return user; + } + + public static String adminPassword() { + return password; + } + + public static Connection admin(String database) throws SQLException { + return DriverManager.getConnection(url(database), user, password); + } + + public static Connection login(String login, String pwd) throws SQLException { + return DriverManager.getConnection(url(DB), login, pwd); + } + + /** Drops and rebuilds {@value #DB} (and drops {@value #SCHEDULING_DB}) once per JVM. */ + public static synchronized void ensureApplied() { + if (applied) { + return; + } + try (Connection c = admin("master"); Statement st = c.createStatement()) { + for (String db : List.of(DB, SCHEDULING_DB)) { + st.execute("IF DB_ID('" + db + "') IS NOT NULL BEGIN ALTER DATABASE [" + db + "] SET SINGLE_USER WITH " + + "ROLLBACK IMMEDIATE; DROP DATABASE [" + db + "]; END"); + } + Path dbDir = repoRoot().resolve("db"); + for (String folder : FOLDERS) { + List files; + try (Stream s = Files.list(dbDir.resolve(folder))) { + files = s.filter(p -> p.toString().endsWith(".sql")).sorted().toList(); + } + for (Path f : files) { + for (String batch : batches(Files.readString(f, StandardCharsets.UTF_8))) { + try { + st.execute(batch); + } catch (SQLException e) { + throw new IllegalStateException(f.getFileName() + ": " + e.getMessage(), e); + } + } + } + } + applied = true; + } catch (SQLException | IOException e) { + throw new IllegalStateException("cannot build " + DB, e); + } + } + + /** GO-separated batches, with the database renamed (new logins default to master). */ + static List batches(String script) { + String renamed = script.replace("DEFAULT_DATABASE = AssessmentDelivery", "DEFAULT_DATABASE = master") + .replaceAll("\\bAssessmentDelivery\\b", DB); + List out = new ArrayList<>(); + StringBuilder cur = new StringBuilder(); + for (String line : renamed.split("\\r?\\n")) { + if (line.trim().equalsIgnoreCase("GO")) { + add(out, cur); + } else { + cur.append(line).append('\n'); + } + } + add(out, cur); + return out; + } + + private static void add(List out, StringBuilder cur) { + if (!cur.toString().isBlank()) { + out.add(cur.toString()); + } + cur.setLength(0); + } + + static Path repoRoot() { + Path p = Path.of("").toAbsolutePath(); + while (p != null && !Files.isDirectory(p.resolve("db").resolve("triggers"))) { + p = p.getParent(); + } + if (p == null) { + throw new IllegalStateException("repo root with db/ not found"); + } + return p; + } + + private static String envOr(String k, String d) { + String v = System.getenv(k); + return v == null || v.isBlank() ? d : v; + } +} diff --git a/services/scheduling-service/src/test/java/com/northwind/scheduling/relay/LegacySingleWriterIT.java b/services/scheduling-service/src/test/java/com/northwind/scheduling/relay/LegacySingleWriterIT.java new file mode 100644 index 00000000..e330ec18 --- /dev/null +++ b/services/scheduling-service/src/test/java/com/northwind/scheduling/relay/LegacySingleWriterIT.java @@ -0,0 +1,124 @@ +package com.northwind.scheduling.relay; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Map; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIf; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +/** + * db/security/036 + 040 (MBA-3823): every legacy login is refused booking/seat writes with a SQL Server permission + * error, ad_sched_relay keeps them; 032/033 skip their side effects only for SESSION_CONTEXT writer = + * scheduling-relay (HB-01, HB-04, HB-05 stay as they were for any other writer). + */ +@EnabledIf("com.northwind.scheduling.relay.LegacyEstateDb#available") +class LegacySingleWriterIT { + + static final Map LOGINS = Map.of( + "ad_portal", "P0rtal!Dev2026", + "ad_booking_api", "B00kingApi!Dev2026", + "ad_results", "Resu1ts!Dev2026", + "ad_capacity_sync", "CapSync!Dev2026", + "ad_sched_import", "Sch3dImport!Dev2026"); + static final String RELAY_PASSWORD = "Sch3dRelay!Dev2026"; + static final int PERMISSION_DENIED = 229; + static final int COLUMN_PERMISSION_DENIED = 230; + + @BeforeAll + static void build() { + LegacyEstateDb.ensureApplied(); + } + + @ParameterizedTest + @ValueSource(strings = {"ad_portal", "ad_booking_api", "ad_results", "ad_capacity_sync", "ad_sched_import"}) + void legacyLoginCannotWriteBookingsWaitlistOrSeats(String login) throws SQLException { + try (Connection c = LegacyEstateDb.login(login, LOGINS.get(login)); Statement st = c.createStatement()) { + assertDenied(st, "INSERT INTO dbo.Booking (SessionId, CandidateId, Status, Channel) VALUES (154, 901, 'BOOKED', 'PORTAL')", + PERMISSION_DENIED); + assertDenied(st, "UPDATE dbo.Booking SET Status = 'ATTENDED' WHERE BookingId = 1", PERMISSION_DENIED); + assertDenied(st, "DELETE FROM dbo.Waitlist WHERE WaitlistId = 1", PERMISSION_DENIED); + assertDenied(st, "UPDATE dbo.ExamSession SET SeatsAvailable = SeatsAvailable - 1 WHERE SessionId = 154", + login.equals("ad_sched_import") ? PERMISSION_DENIED : COLUMN_PERMISSION_DENIED); + assertDenied(st, "UPDATE dbo.ExamSession SET Capacity = Capacity + 1 WHERE SessionId = 154", + login.equals("ad_sched_import") ? PERMISSION_DENIED : COLUMN_PERMISSION_DENIED); + for (String proc : new String[] {"usp_BookSeat", "usp_CancelBooking", "usp_ChangeCapacity", "usp_RescheduleBooking"}) { + assertDenied(st, "EXEC dbo." + proc, PERMISSION_DENIED); + } + // reads keep working (legacy pages render from the legacy tables) + try (var rs = st.executeQuery("SELECT COUNT(*) FROM dbo.Booking")) { + assertThat(rs.next()).isTrue(); + } + } + } + + @Test + void relayLoginKeepsWriteAccess() throws SQLException { + try (Connection c = LegacyEstateDb.login("ad_sched_relay", RELAY_PASSWORD); Statement st = c.createStatement()) { + c.setAutoCommit(false); + assertThat(st.executeUpdate("UPDATE dbo.ExamSession SET SeatsAvailable = SeatsAvailable WHERE SessionId = 154")).isEqualTo(1); + st.execute("SET IDENTITY_INSERT dbo.Booking ON; INSERT INTO dbo.Booking (BookingId, SessionId, CandidateId, " + + "Status, Channel) VALUES (59999999, 154, 901, 'BOOKED', 'PORTAL'); SET IDENTITY_INSERT dbo.Booking OFF;"); + assertThat(st.executeUpdate("INSERT INTO dbo.RelayCheckpoint (EventId, EventSeq, EventType) VALUES (NEWID(), 1, 'X')")).isEqualTo(1); + c.rollback(); + } + } + + @Test + void bookingTriggerSkipsExtraDecrementOnlyForRelayWrites() throws SQLException { + try (Connection c = LegacyEstateDb.admin(LegacyEstateDb.DB); Statement st = c.createStatement()) { + c.setAutoCommit(false); + int before = seats(st, 154); + st.execute("INSERT INTO dbo.Booking (SessionId, CandidateId, Status, Channel) VALUES (154, 902, 'BOOKED', 'PARTNER')"); + assertThat(seats(st, 154)).as("HB-04: non-PORTAL insert still decrements for legacy writers").isEqualTo(before - 1); + st.execute("EXEC sp_set_session_context @key = N'writer', @value = N'scheduling-relay'"); + st.execute("INSERT INTO dbo.Booking (SessionId, CandidateId, Status, Channel) VALUES (154, 903, 'BOOKED', 'PARTNER')"); + assertThat(seats(st, 154)).as("relay insert: no trigger decrement").isEqualTo(before - 1); + c.rollback(); + st.execute("EXEC sp_set_session_context @key = N'writer', @value = NULL"); + } + } + + @Test + void sessionTriggerSkipsDemotionAndRecomputeOnlyForRelayWrites() throws SQLException { + try (Connection c = LegacyEstateDb.admin(LegacyEstateDb.DB); Statement st = c.createStatement()) { + c.setAutoCommit(false); + st.execute("EXEC sp_set_session_context @key = N'writer', @value = N'scheduling-relay'"); + int booked = count(st, "SELECT COUNT(*) FROM dbo.Booking WHERE SessionId = 154 AND Status = 'BOOKED'"); + st.execute("UPDATE dbo.ExamSession SET Capacity = 1, SeatsAvailable = 7 WHERE SessionId = 154"); + assertThat(seats(st, 154)).as("no SeatsAvailable recompute").isEqualTo(7); + assertThat(count(st, "SELECT COUNT(*) FROM dbo.Booking WHERE SessionId = 154 AND Status = 'BOOKED'")) + .as("no HB-05 demotion").isEqualTo(booked); + c.rollback(); + st.execute("EXEC sp_set_session_context @key = N'writer', @value = NULL"); + c.setAutoCommit(false); + st.execute("UPDATE dbo.ExamSession SET Capacity = 1 WHERE SessionId = 154"); + assertThat(count(st, "SELECT COUNT(*) FROM dbo.Booking WHERE SessionId = 154 AND Status = 'BOOKED'")) + .as("HB-05: legacy capacity decrease still demotes").isLessThanOrEqualTo(1); + c.rollback(); + } + } + + private static void assertDenied(Statement st, String sql, int errorCode) { + assertThatThrownBy(() -> st.execute(sql)).as(sql).isInstanceOfSatisfying(SQLException.class, + e -> assertThat(e.getErrorCode()).as(e.getMessage()).isEqualTo(errorCode)); + } + + private static int seats(Statement st, int sessionId) throws SQLException { + return count(st, "SELECT SeatsAvailable FROM dbo.ExamSession WHERE SessionId = " + sessionId); + } + + private static int count(Statement st, String sql) throws SQLException { + try (var rs = st.executeQuery(sql)) { + rs.next(); + return rs.getInt(1); + } + } +} diff --git a/services/scheduling-service/src/test/java/com/northwind/scheduling/relay/RelayIT.java b/services/scheduling-service/src/test/java/com/northwind/scheduling/relay/RelayIT.java new file mode 100644 index 00000000..bd9a601e --- /dev/null +++ b/services/scheduling-service/src/test/java/com/northwind/scheduling/relay/RelayIT.java @@ -0,0 +1,176 @@ +package com.northwind.scheduling.relay; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.condition.EnabledIf; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; + +import com.northwind.scheduling.outbox.OutboxEvent; +import com.northwind.scheduling.outbox.OutboxRepository; +import com.northwind.scheduling.reconciliation.Reconciliation; +import com.northwind.scheduling.reconciliation.ReconciliationService; +import com.northwind.scheduling.service.SchedulingService; + +/** + * MBA-3823 end to end on a real AssessmentDelivery ({@link LegacyEstateDb}): snapshot import -> modern mutations -> + * outbox -> relay as ad_sched_relay -> legacy rows equal Scheduling (reconcile 0 mismatches, seat-drift 0 rows), + * and every event replayed twice changes nothing. + */ +@EnabledIf("com.northwind.scheduling.relay.LegacyEstateDb#available") +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class RelayIT { + + @DynamicPropertySource + static void props(DynamicPropertyRegistry r) { + LegacyEstateDb.ensureApplied(); + r.add("spring.datasource.url", () -> LegacyEstateDb.url(LegacyEstateDb.SCHEDULING_DB)); + r.add("spring.datasource.username", LegacyEstateDb::adminUser); + r.add("spring.datasource.password", LegacyEstateDb::adminPassword); + r.add("scheduling.legacy.url", () -> LegacyEstateDb.url(LegacyEstateDb.DB)); + r.add("scheduling.legacy.username", () -> "ad_sched_import"); + r.add("scheduling.legacy.password", () -> "Sch3dImport!Dev2026"); + r.add("scheduling.import.on-startup", () -> "true"); + r.add("scheduling.relay.enabled", () -> "true"); + r.add("scheduling.relay.url", () -> LegacyEstateDb.url(LegacyEstateDb.DB)); + r.add("scheduling.relay.username", () -> "ad_sched_relay"); + r.add("scheduling.relay.password", () -> LegacySingleWriterIT.RELAY_PASSWORD); + r.add("scheduling.relay.poll-ms", () -> "3600000"); + r.add("scheduling.reconciliation.report-dir", () -> "target/reconciliation"); + } + + @Autowired SchedulingService service; + @Autowired OutboxRepository outbox; + @Autowired RelayWorker worker; + @Autowired LegacyRelay relay; + @Autowired ReconciliationService reconciliation; + @Autowired JdbcTemplate scheduling; + + @Test + @Order(1) + void importBaselineIsRelayedAndLegacyMatchesScheduling() { + assertThat(outbox.pending(10)).extracting(e -> e.type().name()).containsExactly("InventoryBaselined"); + assertThat(worker.relayPending()).isEqualTo(1); + assertZeroDrift(); + } + + @Test + @Order(2) + void bookCancelCapacityUpDownAndAttendanceLandExactlyAsSchedulingDecided() throws SQLException { + var a = service.book(new SchedulingService.BookCommand(154, "NW00000901", null, null, "PARTNER")); + var b = service.book(new SchedulingService.BookCommand(154, "NW00000902", null, null, "PORTAL")); + assertRelayed(); + assertThat(legacyBooking(a.bookingId())).containsEntry("Status", "BOOKED").containsEntry("Channel", "PARTNER"); + assertThat(scheduling.queryForObject("SELECT legacy_booking_id FROM booking WHERE booking_id = ?", Integer.class, + a.bookingId())).isEqualTo(a.bookingId()); + + service.cancel(a.bookingId(), "test cancel"); + assertRelayed(); + assertThat(legacyBooking(a.bookingId())).containsEntry("Status", "CANCELLED"); + + int cap = legacyInt("SELECT Capacity FROM dbo.ExamSession WHERE SessionId = 154"); + service.changeCapacity(154, cap + 2, "up"); + assertRelayed(); + int occupied = legacyInt("SELECT COUNT(*) FROM dbo.Booking WHERE SessionId = 154 AND Status IN ('BOOKED','ATTENDED')"); + service.changeCapacity(154, Math.max(2, occupied - 1), "down"); + assertRelayed(); + assertThat(legacyInt("SELECT COUNT(*) FROM dbo.Waitlist w JOIN dbo.Booking b ON b.BookingId = w.BookingId " + + "WHERE b.SessionId = 154 AND b.Status = 'WAITLISTED'")) + .isEqualTo(legacyInt("SELECT COUNT(*) FROM dbo.Booking WHERE SessionId = 154 AND Status = 'WAITLISTED'")); + + var c = service.book(new SchedulingService.BookCommand(155, "NW00000903", null, null, "PORTAL")); + service.recordAttendance(c.bookingId()); + service.recordAttendance(c.bookingId()); + assertRelayed(); + assertThat(legacyBooking(c.bookingId())).containsEntry("Status", "ATTENDED"); + assertThat(b.bookingId()).isPositive(); + } + + @Test + @Order(3) + void everyEventReplayedTwiceIsANoOp() throws SQLException { + Map before = legacyCounts(); + List ids = relay.checkpointedEventIds(); + assertThat(ids).isNotEmpty(); + List all = scheduling.query("SELECT event_seq FROM outbox_event ORDER BY event_seq", + (rs, i) -> rs.getLong(1)).isEmpty() ? List.of() : allEvents(); + for (OutboxEvent e : all) { + assertThat(relay.apply(e)).as("replay of seq " + e.seq()).isFalse(); + assertThat(relay.apply(e)).isFalse(); + } + assertThat(legacyCounts()).isEqualTo(before); + assertZeroDrift(); + } + + @Test + @Order(4) + void crashAfterLegacyCommitBeforeMarkRelayedReprocessesWithoutDuplicates() throws SQLException { + service.book(new SchedulingService.BookCommand(154, "NW00000904", null, null, "PORTAL")); + List pending = outbox.pending(10); + assertThat(pending).hasSize(1); + assertThat(relay.apply(pending.get(0))).isTrue(); // legacy committed, "killed" before markRelayed + Map before = legacyCounts(); + assertThat(worker.relayPending()).isEqualTo(1); // restart: reprocessed as checkpointed no-op + assertThat(legacyCounts()).isEqualTo(before); + assertZeroDrift(); + } + + private List allEvents() { + scheduling.update("UPDATE outbox_event SET relayed_at = NULL"); + try { + return outbox.pending(100_000); + } finally { + scheduling.update("UPDATE outbox_event SET relayed_at = SYSUTCDATETIME()"); + } + } + + private void assertRelayed() { + worker.relayPending(); + assertThat(outbox.pending(10)).isEmpty(); + assertZeroDrift(); + } + + private void assertZeroDrift() { + Reconciliation r = reconciliation.reconcile(false); + assertThat(r.sessionMismatches()).isEmpty(); + assertThat(r.bookingMismatches()).isEmpty(); + assertThat(r.seatDrift()).isEmpty(); + } + + private static Map legacyCounts() throws SQLException { + return Map.of("Booking", legacyInt("SELECT COUNT(*) FROM dbo.Booking"), + "Waitlist", legacyInt("SELECT COUNT(*) FROM dbo.Waitlist"), + "AuditLog", legacyInt("SELECT COUNT(*) FROM dbo.AuditLog"), + "RelayCheckpoint", legacyInt("SELECT COUNT(*) FROM dbo.RelayCheckpoint")); + } + + private static Map legacyBooking(int id) throws SQLException { + try (Connection c = LegacyEstateDb.admin(LegacyEstateDb.DB); Statement st = c.createStatement(); + var rs = st.executeQuery("SELECT Status, Channel, SessionId FROM dbo.Booking WHERE BookingId = " + id)) { + assertThat(rs.next()).as("legacy booking " + id).isTrue(); + return Map.of("Status", rs.getString(1), "Channel", rs.getString(2), "SessionId", rs.getInt(3)); + } + } + + private static int legacyInt(String sql) throws SQLException { + try (Connection c = LegacyEstateDb.admin(LegacyEstateDb.DB); Statement st = c.createStatement(); + var rs = st.executeQuery(sql)) { + rs.next(); + return rs.getInt(1); + } + } +} diff --git a/services/scheduling-service/src/test/java/com/northwind/scheduling/web/AttendanceTokenGuardTest.java b/services/scheduling-service/src/test/java/com/northwind/scheduling/web/AttendanceTokenGuardTest.java new file mode 100644 index 00000000..5b96ed7d --- /dev/null +++ b/services/scheduling-service/src/test/java/com/northwind/scheduling/web/AttendanceTokenGuardTest.java @@ -0,0 +1,34 @@ +package com.northwind.scheduling.web; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Test; + +import com.northwind.scheduling.domain.ErrorCode; +import com.northwind.scheduling.domain.SchedulingException; + +class AttendanceTokenGuardTest { + + @Test + void acceptsOnlyTheConfiguredToken() { + AttendanceTokenGuard g = new AttendanceTokenGuard("s3cret"); + assertThatCode(() -> g.check("s3cret")).doesNotThrowAnyException(); + assertUnauthorized(g, null); + assertUnauthorized(g, ""); + assertUnauthorized(g, "s3cre"); + assertUnauthorized(g, "s3cret "); + } + + @Test + void failsClosedWhenNoTokenIsConfigured() { + assertUnauthorized(new AttendanceTokenGuard(""), ""); + assertUnauthorized(new AttendanceTokenGuard(null), "anything"); + assertUnauthorized(new AttendanceTokenGuard(" "), " "); + } + + private static void assertUnauthorized(AttendanceTokenGuard g, String token) { + assertThatThrownBy(() -> g.check(token)).isInstanceOf(SchedulingException.class) + .extracting(e -> ((SchedulingException) e).code()).isEqualTo(ErrorCode.UNAUTHORIZED); + } +} diff --git a/services/scheduling-service/src/test/java/com/northwind/scheduling/web/OpenApiSpecIT.java b/services/scheduling-service/src/test/java/com/northwind/scheduling/web/OpenApiSpecIT.java index 3a71e0a2..ce8a5a4b 100644 --- a/services/scheduling-service/src/test/java/com/northwind/scheduling/web/OpenApiSpecIT.java +++ b/services/scheduling-service/src/test/java/com/northwind/scheduling/web/OpenApiSpecIT.java @@ -137,6 +137,13 @@ void errorModelAndLegacyMappingAreDocumentedWithExamples() throws Exception { JsonNode book409 = spec.at("/paths/~1api~1v2~1bookings/post/responses/409/content/application~1json"); assertThat(book409.at("/schema/$ref").asText()).isEqualTo("#/components/schemas/ApiError"); assertThat(book409.at("/examples/NO_ACCOMMODATION_SEAT/value/legacyCode").asInt()).isEqualTo(50003); + assertThat(err.at("/properties/error/enum").toString()).doesNotContain("UNAUTHORIZED"); + JsonNode attendance = spec.at("/paths/~1api~1v2~1bookings~1{bookingId}~1attendance/post"); + assertThat(attendance.path("security").toString()).isEqualTo("[{\"serviceToken\":[]}]"); + assertThat(attendance.at("/responses/401/content/application~1json/schema/$ref").asText()) + .isEqualTo("#/components/schemas/ServiceTokenError"); + assertThat(spec.at("/components/securitySchemes/serviceToken/name").asText()).isEqualTo("X-Service-Token"); + assertThat(spec.at("/paths/~1api~1v2~1bookings/post/security").isMissingNode()).isTrue(); assertThat(spec.at("/paths/~1api~1v2~1bookings~1{bookingId}~1reschedule/post/responses/409/content/application~1json/examples") .fieldNames()).toIterable().contains("DUPLICATE_BOOKING", "NO_ACCOMMODATION_SEAT", "SAME_SESSION"); assertThat(spec.at("/paths/~1api~1v2~1bookings/post/responses/201/content/application~1json/schema/$ref").asText()) diff --git a/services/scheduling-service/src/test/java/com/northwind/scheduling/web/SchedulingApiIT.java b/services/scheduling-service/src/test/java/com/northwind/scheduling/web/SchedulingApiIT.java index 22d43ef1..802c28a1 100644 --- a/services/scheduling-service/src/test/java/com/northwind/scheduling/web/SchedulingApiIT.java +++ b/services/scheduling-service/src/test/java/com/northwind/scheduling/web/SchedulingApiIT.java @@ -2,6 +2,10 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; import java.util.List; import java.util.Map; @@ -56,10 +60,19 @@ ResponseEntity> book(int session, String cand, String acc) { MAP); } + HttpResponse attendance(String path, String token) throws Exception { + HttpRequest.Builder b = HttpRequest.newBuilder(URI.create("http://localhost:" + port + path)) + .POST(HttpRequest.BodyPublishers.noBody()); + if (token != null) { + b.header("X-Service-Token", token); + } + return HttpClient.newHttpClient().send(b.build(), HttpResponse.BodyHandlers.ofString()); + } + @Test void flywayCreatesSchedulingDbAndImporterLoadsSnapshots() { assertThat(jdbc.queryForObject("SELECT DB_NAME()", String.class)).isEqualTo("Scheduling"); - assertThat(jdbc.queryForObject("SELECT COUNT(*) FROM flyway_schema_history WHERE success = 1", Integer.class)).isEqualTo(1); + assertThat(jdbc.queryForObject("SELECT COUNT(*) FROM flyway_schema_history WHERE success = 1", Integer.class)).isEqualTo(2); List> centers = http.exchange("/api/v2/centers", HttpMethod.GET, null, LIST).getBody(); assertThat(centers).extracting(c -> c.get("centerCode")).containsExactly("AMS01", "SFO01"); List> sessions = http.exchange("/api/v2/centers/SFO01/sessions?examCode=MHL-RN1&from=2026-11-12&to=2026-11-20", @@ -86,6 +99,28 @@ void bookingOpenSessionDropsSeatsByExactlyOne() { assertThat(book(154, "NW09999999", null).getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); } + @Test + void attendanceNeedsTheServiceTokenAndWritesNoOutboxEventWithoutIt() throws Exception { + int id = ((Number) book(154, "NW00000920", null).getBody().get("bookingId")).intValue(); + String url = "/api/v2/bookings/" + id + "/attendance"; + String countSql = "SELECT COUNT(*) FROM outbox_event WHERE event_type = 'AttendanceRecorded'"; + int before = jdbc.queryForObject(countSql, Integer.class); + + // java.net.http: HttpURLConnection cannot read a 401 answer to a POST ("cannot retry due to server authentication") + HttpResponse none = attendance(url, null); + assertThat(none.statusCode()).isEqualTo(401); + assertThat(none.body()).contains("\"error\":\"UNAUTHORIZED\"", "\"legacyCode\":null"); + assertThat(attendance(url, "wrong-token").statusCode()).isEqualTo(401); + assertThat(attendance(url, "").statusCode()).isEqualTo(401); + assertThat(get("/api/v2/bookings/" + id)).containsEntry("status", "BOOKED"); + assertThat(jdbc.queryForObject(countSql, Integer.class)).isEqualTo(before); + + HttpResponse ok = attendance(url, "test-attendance-token"); + assertThat(ok.statusCode()).isEqualTo(200); + assertThat(ok.body()).contains("\"status\":\"ATTENDED\""); + assertThat(jdbc.queryForObject(countSql, Integer.class)).isEqualTo(before + 1); + } + @Test void fullSessionWaitlistsWithNextPosition() { ResponseEntity> r = book(163, "NW00000920", null); diff --git a/services/scheduling-service/src/test/resources/application.properties b/services/scheduling-service/src/test/resources/application.properties new file mode 100644 index 00000000..8872a87e --- /dev/null +++ b/services/scheduling-service/src/test/resources/application.properties @@ -0,0 +1,4 @@ +# Test contexts must never relay into a real AssessmentDelivery (application.yml defaults the relay to +# localhost:1433); RelayIT turns it on explicitly against its own isolated database. +scheduling.relay.enabled=false +scheduling.attendance.service-token=test-attendance-token