|
| 1 | +# Greek Gods API (US-001) — Micronaut |
| 2 | + |
| 3 | +Micronaut 4.x service: **`GET /api/v1/gods/greek`** returns a JSON array of Greek god names from PostgreSQL (read path never calls the upstream JSON server). Background sync upserts names from a configurable upstream base URL + **`GET /greek`**. |
| 4 | + |
| 5 | +## Run |
| 6 | + |
| 7 | +Prerequisites: JDK 21+, Maven (wrapper included), local PostgreSQL (or override URL via env). |
| 8 | + |
| 9 | +```bash |
| 10 | +./mvnw mn:run |
| 11 | +``` |
| 12 | + |
| 13 | +Default datasource in `src/main/resources/application.yml` targets `jdbc:postgresql://localhost:5432/greek_gods` with user/password `postgres` / `postgres`. Override using Micronaut environment variables, for example: |
| 14 | + |
| 15 | +- `DATASOURCES_DEFAULT_URL` |
| 16 | +- `DATASOURCES_DEFAULT_USERNAME` |
| 17 | +- `DATASOURCES_DEFAULT_PASSWORD` |
| 18 | + |
| 19 | +Upstream sync (no hard-coded production URL in code): |
| 20 | + |
| 21 | +- `GREEK_GODS_UPSTREAM_BASE_URL` — overrides `greek-gods.upstream.base-url` (JDK `HttpClient` calls `{base-url}/greek`). |
| 22 | + |
| 23 | +## Build and tests |
| 24 | + |
| 25 | +```bash |
| 26 | +./mvnw clean verify |
| 27 | +``` |
| 28 | + |
| 29 | +- **Unit tests** (`*Test`): Surefire — no Docker. |
| 30 | +- **Integration tests** (`*IT`): Failsafe — Testcontainers PostgreSQL, WireMock for sync; require Docker. |
| 31 | + |
| 32 | +Skip integration tests (faster local iteration): |
| 33 | + |
| 34 | +```bash |
| 35 | +./mvnw clean verify -DskipITs=true |
| 36 | +``` |
| 37 | + |
| 38 | +or use the Maven profile: |
| 39 | + |
| 40 | +```bash |
| 41 | +./mvnw clean verify -Pfast |
| 42 | +``` |
| 43 | + |
| 44 | +Run a subset of scenarios by JUnit tag (Failsafe / Surefire): |
| 45 | + |
| 46 | +```bash |
| 47 | +./mvnw -q verify -Dgroups=smoke |
| 48 | +./mvnw -q verify -Dgroups=error-handling |
| 49 | +``` |
| 50 | + |
| 51 | +## JUnit tags (Gherkin mapping) |
| 52 | + |
| 53 | +| Tag | Meaning | |
| 54 | +|-----|---------| |
| 55 | +| `smoke`, `happy-path` | 20 canonical names, ordered, set equality | |
| 56 | +| `performance` | Single GET under 1 second | |
| 57 | +| `load-testing` | 100 concurrent GETs, wall clock under 2 seconds | |
| 58 | +| `error-handling` | 500 `application/problem+json` (normative fields); empty DB → `[]` | |
| 59 | +| `data-quality` | After WireMock stubbed sync, API matches stub payload | |
| 60 | +| `api-specification` | `Content-Type` includes `application/json`; JSON array of strings | |
| 61 | + |
| 62 | +**`@availability`** from the feature file is **not** automated (SLO/monitoring only). |
| 63 | + |
| 64 | +### CI flake note (`load-testing`) |
| 65 | + |
| 66 | +The **`load-testing`** scenario enforces **100 concurrent** requests within **2 seconds** total wall time. Shared CI agents can occasionally miss this window; if the build fails only on this test, re-run the job or use a larger runner. Tuning: increase the threshold only if product owners agree to relax the Gherkin timing. |
| 67 | + |
| 68 | +### Error-handling IT |
| 69 | + |
| 70 | +`GreekGodsDatabaseFailureIT` uses **`@MockBean` on `GreekGodRepository`** to simulate a read-path persistence failure while Flyway still runs against a real Testcontainers database. The HTTP response still satisfies ADR-001 (500 + `type`, `title`, `status`). |
| 71 | + |
| 72 | +## OpenAPI |
| 73 | + |
| 74 | +Runtime annotations align with `requirements3/design/greekController-oas.yaml`. After `./mvnw compile`, the generated spec is at `target/classes/META-INF/swagger/greek-gods-api-1.0.0.yml`. |
| 75 | + |
| 76 | +## Scheduling |
| 77 | + |
| 78 | +`@Scheduled` is provided by **`micronaut-context`** (transitive). There is no separate `micronaut-scheduling` artifact on the 4.10 BOM. Sync timing is configured with: |
| 79 | + |
| 80 | +- `greek-gods.sync.fixed-delay` (default `5m`) |
| 81 | +- `greek-gods.sync.initial-delay` (default `30s`) |
| 82 | +- `greek-gods.sync.scheduler-enabled` (`false` disables the job bean) |
| 83 | +- `greek-gods.sync.enabled` (skips work inside the job when `false`) |
| 84 | + |
| 85 | +## HTTP client note |
| 86 | + |
| 87 | +Upstream sync uses **`java.net.http.HttpClient`** (blocking `send`) with **`greek-gods.upstream.connect-timeout`** and **`read-timeout`** — no Micronaut **`micronaut-http-client`** / Netty on the outbound path. |
| 88 | + |
| 89 | +### Why `micronaut-core-reactive` stays on the classpath |
| 90 | + |
| 91 | +There is **no supported way** to run this app with **Micronaut’s HTTP server** (embedded Tomcat here) and **omit** `micronaut-core-reactive`: |
| 92 | + |
| 93 | +- Server-agnostic response encoding lives in **`io.micronaut.http.server.ResponseLifecycle`** (Micronaut 4.8+), used by both Netty and servlet runtimes. |
| 94 | +- That class imports **`io.micronaut.core.async.publisher.Publishers`** and **`LazySendingSubscriber`**, which are packaged **only** in **`micronaut-core-reactive`**, not in `micronaut-core`. |
| 95 | +- **`micronaut-http-server`**’s POM does not list `micronaut-core-reactive`, so many apps only got it **transitively** (e.g. via `micronaut-http-client`). This module declares it **explicitly** so the app still starts after removing the declarative client. |
| 96 | + |
| 97 | +So: **your controllers and upstream sync stay imperative/blocking**, but the framework still loads small reactive helper types for **response encoding**. Avoiding that JAR entirely would mean **not using Micronaut’s HTTP stack** (different framework or a non-HTTP entrypoint). Upstream improvement would be for `micronaut-http-server` to declare this dependency so every app gets a correct classpath by default ([micronaut-core](https://github.com/micronaut-projects/micronaut-core) `ResponseLifecycle`). |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +Micronaut reference: [User Guide](https://docs.micronaut.io/4.10.10/guide/index.html) |
0 commit comments