# (DD03) Business Logic — OwnerControllerTests.showOwner() [14 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `org.springframework.samples.petclinic.owner.OwnerControllerTests` |
| Layer | Test / Controller Test |
| Module | `owner` (Package: `org.springframework.samples.petclinic.owner`) |

## 1. Role

### OwnerControllerTests.showOwner()

This test method verifies the business-facing behavior of the owner detail display endpoint exposed by `OwnerController`. In domain terms, it validates that when a user requests an owner profile by owner ID, the web layer returns the correct owner detail page and populates the model with a fully assembled owner aggregate, including the owner’s personal contact information and related pet/visit data. The method is a controller test, so its role is not to implement business logic directly but to confirm that the controller correctly routes the request, retrieves the owner from the repository, and renders the expected view. It covers the success path only: the owner exists, the model contains the expected attributes, and the detail view is selected. As a result, it acts as a regression guard for the customer-facing “view owner profile” use case in the PetClinic owner management flow.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["showOwner(TEST_OWNER_ID)"])
    CALL1["mockMvc.perform(get('/owners/{ownerId}', TEST_OWNER_ID))"]
    CALL2["OwnerController.showOwner(ownerId)"]
    CALL3["owners.findById(ownerId)"]
    DEC1{"Owner found?"}
    THROW["Throw IllegalArgumentException if not found"]
    ADD["mav.addObject(owner)"]
    END_NODE(["Return ModelAndView for owners/ownerDetails"])
    START --> CALL1
    CALL1 --> CALL2
    CALL2 --> CALL3
    CALL3 --> DEC1
    DEC1 -->|Yes| ADD
    DEC1 -->|No| THROW
    ADD --> END_NODE
    THROW --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | - |

Instance fields and external state used by the test method:
- `mockMvc`: Spring MVC test harness used to execute the HTTP GET request against the controller endpoint.
- `owners`: Mocked `OwnerRepository` that supplies the owner aggregate returned by the controller.
- `TEST_OWNER_ID`: Test fixture constant representing the owner identifier used by the request and repository stub.
- `george()` / `setup()`: Test fixture state that preloads an owner with a pet and a visit so the assertions can validate nested relationships.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `owners.findById(ownerId)` | N/A | `Owner` aggregate | Reads a single owner record by primary key so the controller can display the owner detail page. |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Test: `OwnerControllerTests.showOwner` | `MockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID))` -> `OwnerController.showOwner(ownerId)` -> `owners.findById(ownerId)` | `owners.findById(ownerId) [R] Owner` |

## 6. Per-Branch Detail Blocks

**Block 1** — [TEST EXECUTION] `(GET /owners/{ownerId})` (L217)

> Executes the web request against the owner detail endpoint using the prepared owner ID fixture.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `mockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID))`; sends a simulated HTTP GET request to the owner detail URL |

**Block 2** — [ASSERTION CHAIN] `(response validation)` (L218-L229)

> Verifies that the controller returns the expected HTTP status, model content, and view name for the owner detail business use case.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `.andExpect(status().isOk())`; confirms the request is handled successfully |
| 2 | CALL | `.andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin"))))`; validates owner last name in the model |
| 3 | CALL | `.andExpect(model().attribute("owner", hasProperty("firstName", is("George"))))`; validates owner first name in the model |
| 4 | CALL | `.andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St."))))`; validates owner address in the model |
| 5 | CALL | `.andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))`; validates owner city in the model |
| 6 | CALL | `.andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))`; validates owner telephone in the model |
| 7 | CALL | `.andExpect(model().attribute("owner", hasProperty("pets", not(empty()))))`; validates that the owner has at least one pet |
| 8 | CALL | `.andExpect(model().attribute("owner", hasProperty("pets", hasItem(hasProperty("visits", hasSize(greaterThan(0)))))))`; validates that at least one pet has visit history |
| 9 | CALL | `.andExpect(view().name("owners/ownerDetails"))`; confirms the owner detail view is selected |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `owner` | Domain object | Pet owner profile containing identity, contact information, and related pets. |
| `ownerId` | Field | Unique identifier of the owner record used to retrieve the profile. |
| `MockMvc` | Technical term | Spring MVC test harness for simulating HTTP requests and verifying controller responses. |
| `ModelAndView` | Technical term | MVC response object combining the view name and the model data to render. |
| `owners/ownerDetails` | View name | Owner detail page shown to the user after the owner record is loaded. |
| `pets` | Domain collection | List of pets associated with the owner profile. |
| `visits` | Domain collection | Medical or service visit history associated with a pet. |
| `Franklin` | Test data | Expected last name of the owner fixture used in the success-path validation. |
| `George` | Test data | Expected first name of the owner fixture used in the success-path validation. |
| `Madison` | Test data | Expected city of the owner fixture used in the success-path validation. |
| `110 W. Liberty St.` | Test data | Expected street address of the owner fixture used in the success-path validation. |
| `6085551023` | Test data | Expected telephone number of the owner fixture used in the success-path validation. |
| `PetClinic` | Application name | Sample veterinary clinic domain used to demonstrate owner, pet, and visit management workflows. |
| `GET` | HTTP method | Read-only web request used to display existing owner information. |
| `Repository` | Technical term | Persistence abstraction used to retrieve domain objects from storage. |
