---
# (DD19) Business Logic — OwnerControllerTests.setup() [13 LOC]

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

## 1. Role

### OwnerControllerTests.setup()

This method prepares the owner-controller test fixture so that every test method starts from a consistent, domain-realistic owner record. It builds a canonical `Owner` instance named George Franklin, then wires the mocked `OwnerRepository` to return that owner both when the controller searches by last-name prefix and when it loads an owner by identifier. The method also seeds the embedded pet history by creating a `Visit` dated with the current day and attaching it to George's pet named Max, which allows downstream controller tests to exercise owner, pet, and visit-related views with non-empty data.

From a business perspective, the method supports read-path verification for owner lookup and owner detail scenarios. It does not execute production CRUD logic itself; instead, it configures test doubles for the controller's read dependencies so the controller can be tested in isolation. The setup pattern is classic test-fixture initialization and data stubbing: construct a representative domain object, stub repository calls, then enrich nested child entities to support rendering and relationship traversal in later assertions. There are no conditional branches in this method.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["setup()"])
    STEP1(["Create canonical Owner fixture via george()"])
    STEP2(["Stub owners.findByLastNameStartingWith(\"Franklin\", Pageable) to return PageImpl containing George"])
    STEP3(["Stub owners.findById(1) to return Optional.of(George)"])
    STEP4(["Create Visit and set date to LocalDate.now()"])
    STEP5(["Add Visit to George's pet Max visits collection"])
    END_NODE(["Return to test runner"])

    START --> STEP1
    STEP1 --> STEP2
    STEP2 --> STEP3
    STEP3 --> STEP4
    STEP4 --> STEP5
    STEP5 --> END_NODE
```

**CRITICAL — Constant Resolution:**
- `TEST_OWNER_ID = 1` is the only test constant referenced in this method.
- `Franklin` is a literal surname value used as the repository search prefix.
- `LocalDate.now()` injects the current test execution date into the visit fixture.

## 3. Parameter Analysis

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

External state and instance fields read by the method:
- `this.owners` — mocked `OwnerRepository` used to define repository behavior for owner search and owner lookup.
- `TEST_OWNER_ID` — test fixture identifier used for owner-by-id stubbing.
- `george()` — helper method that creates the canonical owner aggregate used by the test suite.

## 4. CRUD Operations / Called Services

### Pre-computed evidence from code analysis graph:

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|-----------------------|
| R | `OwnerRepository.findById` | OwnerRepository | Owner | Calls `findById` in `OwnerRepository` |
| R | `OwnerRepository.findByLastNameStartingWith` | OwnerRepository | Owner | Calls `findByLastNameStartingWith` in `OwnerRepository` |
| R | `Pet.getVisits` | Pet | - | Calls `getVisits` in `Pet` |
| - | `OwnerControllerTests.george` | OwnerControllerTests | - | Calls `george` in `OwnerControllerTests` |

Analyze all method calls within this method and classify each as a CRUD operation.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|-----------------------|
| R | `OwnerControllerTests.george` | OwnerControllerTests | - | Creates the canonical owner aggregate used to seed repository stubs |
| R | `OwnerRepository.findByLastNameStartingWith` | OwnerRepository | Owner | Stubs owner search by last-name prefix for controller tests |
| R | `OwnerRepository.findById` | OwnerRepository | Owner | Stubs owner lookup by identifier for controller tests |
| C | `Visit` | - | Visit | Instantiates a visit test object for the owner's pet history |
| R | `Pet.getVisits` | Pet | - | Retrieves the pet's visit collection so the test can append a visit |
| U | `Visit.setDate` | - | Visit | Assigns the visit date to the current day in the test fixture |

## 5. Dependency Trace

Trace who calls this method and what this method ultimately calls.

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | ControllerTest lifecycle | JUnit 5 `@BeforeEach` lifecycle -> `OwnerControllerTests.setup()` | `OwnerRepository.findByLastNameStartingWith [R] Owner` |
| 2 | ControllerTest lifecycle | JUnit 5 `@BeforeEach` lifecycle -> `OwnerControllerTests.setup()` | `OwnerRepository.findById [R] Owner` |
| 3 | ControllerTest lifecycle | JUnit 5 `@BeforeEach` lifecycle -> `OwnerControllerTests.setup()` | `Pet.getVisits [R] -` |
| 4 | ControllerTest lifecycle | JUnit 5 `@BeforeEach` lifecycle -> `OwnerControllerTests.setup()` | `Visit [C] Visit` |
| 5 | ControllerTest lifecycle | JUnit 5 `@BeforeEach` lifecycle -> `OwnerControllerTests.setup()` | `Visit.setDate [U] Visit` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] `(test fixture initialization)` (L91)

> Initializes the reusable owner test data before each test method runs.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `Owner george = george();` // Build canonical owner fixture |

**Block 2** — [SEQUENCE] `(stub owner search by last name prefix)` (L92-L94)

> Prepares the repository mock to support owner lookup screens that search by surname.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `given(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class)))` // Stub surname-prefix search |
| 2 | SET | `.willReturn(new PageImpl<>(List.of(george)));` // Return one-page result containing George |

**Block 3** — [SEQUENCE] `(stub owner lookup by identifier)` (L96)

> Prepares the repository mock so controller tests can resolve the owner detail view by ID.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `given(this.owners.findById(TEST_OWNER_ID))` // Stub lookup by test owner id |
| 2 | SET | `.willReturn(Optional.of(george));` // Return the same canonical owner |

**Block 4** — [SEQUENCE] `(create visit fixture)` (L97-L98)

> Creates a visit record to populate the pet history used by downstream controller assertions.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Visit visit = new Visit();` // Instantiate visit fixture |
| 2 | EXEC | `visit.setDate(LocalDate.now());` // Assign current date to the visit |

**Block 5** — [SEQUENCE] `(attach visit to Max)` (L99)

> Adds the visit to George's pet named Max so nested pet history rendering has data.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `george.getPet("Max").getVisits().add(visit);` // Append visit to Max's visit collection |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `Owner` | Domain entity | Pet owner record containing identity and contact details used by the owner management flow |
| `OwnerRepository` | Repository | Data access contract for finding owners by surname prefix or identifier |
| `Pet` | Domain entity | Animal owned by a customer; used to model owner-to-pet relationships |
| `PetType` | Domain entity | Classification of a pet, such as dog or cat |
| `Visit` | Domain entity | Veterinary visit record associated with a pet |
| `findByLastNameStartingWith` | Repository operation | Search operation that returns owners whose last name begins with the supplied text |
| `findById` | Repository operation | Lookup operation that returns a single owner by unique identifier |
| `Pageable` | Technical term | Pagination request object used to control search result slicing |
| `PageImpl` | Technical term | Concrete page container used in tests to simulate a paged repository response |
| `Optional` | Technical term | Container that represents the possible absence of a repository result |
| `JUnit 5 @BeforeEach` | Test lifecycle | Executes the setup method before every test to keep the fixture consistent |
| `george()` | Test helper | Helper that builds the canonical owner aggregate for controller tests |
| `TEST_OWNER_ID` | Test constant | Fixed identifier used to simulate a known owner record in repository stubbing |
| `Franklin` | Business term | Sample last name used as the owner search key in the test fixture |
| `Max` | Business term | Sample pet name attached to the owner fixture |
| `LocalDate.now()` | Technical term | Current system date used to time-stamp the visit fixture |
| `AOT` | Acronym | Ahead-Of-Time compilation mode; disabled for this test class |
| `Native Image` | Runtime term | GraalVM native executable mode; disabled for this test class |
| `MockMvc` | Test utility | Spring MVC test harness used by controller tests |
| `BDDMockito.given` | Test utility | Mockito BDD-style stubbing helper used to configure mock repository behavior |
