Lab 5 — Model the benefits table
Purpose: Back the plan-card with a Sling Model: typed fields, derived values, child-resource benefits, and a JSON export.
Who this page is for
| Audience | Why it matters to you |
|---|---|
| All engineers | Pairs with Session 5 |
Goal
Replace Lab 4's direct properties.* access with a proper model, add derived logic, and export JSON.
Steps
- In the
coremodule, createPlanCardModelas in Session 5 (adaptable: request; OPTIONAL injection),
plus a nested/companion Benefit model class (label, covered) adapted from child resources of benefits.
- Add derived getters:
getFormattedPremium()andgetCoveredCount()(count of benefits with covered=true). - Switch
plancard.htmltodata-sly-use.model="com.academy.phi.models.PlanCardModel"and render only getters —
no properties.* left in the script.
- Deploy; confirm the page renders identically, plus show "X of Y benefits covered" from the derived getter.
- Add
@Exporter(name = "jackson", extensions = "json")and request
<component-path>.model.json — your model is now a headless API.
- Unit test: with wcm.io AEM Mocks, load a JSON fixture of the component content and assert
getCoveredCount() and empty-content behaviour.
Checkpoint
| Verify | Expectation |
|---|---|
| Empty component instance | Renders placeholder, no NPE — OPTIONAL + defaults doing their job |
| error.log during render | Clean — injection failures would log here |
.model.json output | Title, premium, benefits array present; internal fields absent |
| Unit test | Green without a running AEM — models are just classes |
Stretch
Inject an @OSGiService (write a trivial PremiumFormatter OSGi service) to experience service injection.