---
# (DD04) Business Logic — OwnerControllerTests.setup() [13 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.setup()

This method prepares the test fixture for the `OwnerController` test suite by constructing a representative owner domain object and wiring Mockito stubs for repository lookups that the controller tests depend on. It establishes a realistic baseline customer record, including a pet and at least one visit, so downstream controller scenarios can validate both owner summary views and nested pet/visit rendering paths. The method acts as a reusable test-data initializer rather than a business transaction handler, and its primary role is to make the controller tests deterministic and self-contained. It follows a fixture-seeding pattern: create domain data, stub repository responses, and enrich the created aggregate with relational child objects. The method has no conditional branches; it executes the same setup sequence before each test method.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["setup()"])
    CALL_GEO(["Call george()"])
    MOCK_LASTNAME(["Stub owners.findByLastNameStartingWith(\"Franklin\", Pageable)"])
    MOCK_ID(["Stub owners.findById(1)"])
    CREATE_VISIT(["Create Visit and set date to LocalDate.now()"])
    ADD_VISIT(["Add Visit to Max.getVisits()"])
    END_NODE(["Return / Test fixture ready"])

    START --> CALL_GEO
    CALL_GEO --> MOCK_LASTNAME
    MOCK_LASTNAME --> MOCK_ID
    MOCK_ID --> CREATE_VISIT
    CREATE_VISIT --> ADD_VISIT
    ADD_VISIT --> END_NODE
```

**CRITICAL — Constant Resolution:**
- `TEST_OWNER_ID = 1` is the fixed owner identifier used throughout the test fixture.
- `"Franklin"` is the last-name search key used to simulate a successful owner lookup.
- `"Max"` is the pet name referenced when attaching the visit to the prepared owner aggregate.

## 3. Parameter Analysis

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

External state read by the method:
- `this.owners` — mocked `OwnerRepository` used to seed repository responses.
- `TEST_OWNER_ID` — class constant used as the canonical owner identifier in the fixture.

## 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 | - | Builds the reusable owner test fixture for later repository stubbing |
| R | `OwnerRepository.findByLastNameStartingWith` | OwnerRepository | Owner | Stubs repository search by last name to return the prepared owner page |
| R | `OwnerRepository.findById` | OwnerRepository | Owner | Stubs repository lookup by owner ID to return the prepared owner |
| R | `Pet.getVisits` | Pet | - | Accesses the pet's visit collection so a visit can be attached |
| C | `Visit.setDate` | Visit | Visit | Initializes a new visit with the current business date for test coverage |

## 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 | JUnit lifecycle | `OwnerControllerTests` test runner -> `setup()` | `OwnerRepository.findByLastNameStartingWith [R] Owner` |
| 2 | JUnit lifecycle | `OwnerControllerTests` test runner -> `setup()` | `OwnerRepository.findById [R] Owner` |
| 3 | JUnit lifecycle | `OwnerControllerTests` test runner -> `setup()` | `Pet.getVisits [R] -` |
| 4 | JUnit lifecycle | `OwnerControllerTests` test runner -> `setup()` | `Visit.setDate [C] Visit` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENTIAL] `(setup initialization)` (L91)

> Prepares the owner test fixture before each controller test execution.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `Owner george = george();` // Build a representative owner aggregate |

**Block 2** — [SEQUENTIAL] `(repository stubbing for last-name search)` (L93)

> Seeds the mocked repository so owner search scenarios return a deterministic result.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `given(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class)))` // Stub a last-name prefix lookup |
| 2 | EXEC | `.willReturn(new PageImpl<>(List.of(george)));` // Return a page containing the prepared owner |

**Block 3** — [SEQUENTIAL] `(repository stubbing for ID lookup)` (L96)

> Seeds the mocked repository so owner detail scenarios can resolve the same owner by identifier.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `given(this.owners.findById(TEST_OWNER_ID))` // Stub lookup by fixed owner identifier [-> TEST_OWNER_ID="1"] |
| 2 | EXEC | `.willReturn(Optional.of(george));` // Return the prepared owner as the repository result |

**Block 4** — [SEQUENTIAL] `(visit enrichment)` (L97-L100)

> Adds one visit to the prepared owner so nested pet/visit assertions can verify populated relationships.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Visit visit = new Visit();` // Create a new visit record |
| 2 | EXEC | `visit.setDate(LocalDate.now());` // Set the visit date to the current day |
| 3 | CALL | `george.getPet("Max")` // Retrieve the pet named Max ["Max"] |
| 4 | EXEC | `.getVisits().add(visit);` // Attach the visit to the pet's visit collection |

**Block 5** — [RETURN] `(test fixture ready)` (L102)

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return;` // End of setup execution |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `Owner` | Domain entity | Pet owner record used by the controller under test |
| `OwnerRepository` | Repository | Data access abstraction for retrieving owners |
| `Pet` | Domain entity | Animal belonging to an owner |
| `Visit` | Domain entity | Veterinary visit record associated with a pet |
| `TEST_OWNER_ID` | Constant | Fixed owner identifier used to make repository stubs deterministic |
| `Franklin` | Business term | Owner last name used as the search key in the fixture |
| `Max` | Business term | Pet name in the fixture used to anchor visit-related assertions |
| `Pageable` | Technical type | Paging request used by repository search methods |
| `PageImpl` | Technical type | Concrete page wrapper used to return stubbed search results |
| `Optional` | Technical type | Wrapper indicating a lookup may or may not return an owner |
| `LocalDate.now()` | Technical API | Current business date used to timestamp the visit fixture |
| `BDDMockito.given` | Test API | Mockito BDD syntax for defining repository stubs |
| `WebMvcTest` | Test annotation | Spring MVC test slice that loads the controller layer |
| `BeforeEach` | Test lifecycle | JUnit callback that runs the fixture setup before every test method |
