Tracks
/
FHIR
FHIR
/
Exercises
/
Build a FHIR Patient (Java)
Build a FHIR Patient (Java)

Build a FHIR Patient (Java)

Easy

Instructions

A FHIR Patient resource is a JSON document. Before a server can store or return one, your code has to construct it and serialize it to canonical JSON β€” the same bytes every time, so downstream systems and tests can rely on it.

This kata is the offline, deterministic version of Batch-1 Day 5, Week 1, Assignment 3/4 (stand up a FHIR server and create Patient resources). Instead of posting to a live server, you build the resource in memory and emit its JSON.

Implement FhirPatient with a fluent builder:

  • static Builder builder() β€” start building a patient.
  • On Builder: id(String), active(boolean), family(String), addGiven(String), gender(String), birthDate(String), and build() returning a FhirPatient.
  • String toJson() β€” return compact JSON (no whitespace) with keys in exactly this order, omitting optional keys when unset:
{"resourceType":"Patient"[,"id":"<id>"][,"active":<true|false>],"name":[{"use":"official","family":"<family>"[,"given":["<g1>","<g2>",...]]}],"gender":"<gender>","birthDate":"<birthDate>"}

Rules

  • resourceType is always "Patient" and always first.
  • id is included only when set.
  • active is included only when it has been set explicitly (an unset active is omitted β€” it is not defaulted to false).
  • given is an array, included only when at least one given name has been added, in the order they were added.
  • family, gender, and birthDate are required.
  • Validation at build() β€” throw IllegalArgumentException when:
    • family is null or empty,
    • gender is not one of male, female, other, unknown,
    • birthDate does not match ^\d{4}-\d{2}-\d{2}$.
  • JSON escaping: escape backslash (\) and double-quote (") in the string values of id, family, and given.

Source

Batch-1 Day 5 Week 1 Assignment 3/4 (build a FHIR server and create Patient resources), refactored to an offline build-and-serialize kata.
Edit via GitHub The link opens in a new window or tab
FHIR Exercism

Ready to start Build a FHIR Patient (Java)?

Sign up to Exercism to learn and master FHIR with 9 exercises, and real human mentoring, all for free.