---

# (DD05) 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 controller test fixture for owner-related web flows by constructing a canonical owner record and wiring the repository mocks to return that record during controller execution. Business-wise, it simulates a known customer profile — George Franklin with pet Max — so that owner search, owner lookup, and downstream pet/visit rendering paths can be executed deterministically. The method acts as a shared test bootstrap routine for all test cases in `OwnerControllerTests`, ensuring that each MVC scenario starts with the same owner, pet, and visit state. It implements a fixture-seeding pattern rather than a business operation in the production sense, but it is critical for validating the owner management entry points exposed by the web layer. The branches are not conditional in the classical sense; instead, the method sets up two repository read responses and then enriches the owner graph with a visit so that controller views can render complete pet history. In broader system terms, it is a prerequisite for controller-level verification of owner search and owner detail screens.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["setup()"])
    CREATE_OWNER["Call george() to create owner fixture"]
    STUB_SEARCH["Stub owners.findByLastNameStartingWith(\"Franklin\", Pageable) -> PageImpl(List.of(george))"]
    STUB_FIND["Stub owners.findById(1) -> Optional.of(george)"]
    CREATE_VISIT["Create Visit and setDate(LocalDate.now())"]
    ADD_VISIT["Call george.getPet(\"Max\").getVisits().add(visit)"]
    END_NODE(["Return / next test"])

    START --> CREATE_OWNER
    CREATE_OWNER --> STUB_SEARCH
    STUB_SEARCH --> STUB_FIND
    STUB_FIND --> CREATE_VISIT
    CREATE_VISIT --> ADD_VISIT
    ADD_VISIT --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | This lifecycle method accepts no parameters. Its behavior is driven entirely by the test class field `owners`, the constant `TEST_OWNER_ID = 1`, and the locally created owner fixture returned by `george()`. |

## 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 | Owner / Pet / PetType | Builds a canonical owner fixture used by the test setup |
| R | `OwnerRepository.findByLastNameStartingWith` | OwnerRepository | Owner | Prepares the mock search result for owners whose last name starts with Franklin |
| R | `OwnerRepository.findById` | OwnerRepository | Owner | Prepares the mock owner lookup response for owner ID 1 |
| R | `Pet.getVisits` | Pet | Visit | Exposes the visit collection so the test can seed one visit record |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Controller test lifecycle (`@BeforeEach`) | `OwnerControllerTests.setup` | `OwnerControllerTests.george [R] Owner / Pet / PetType` |
| 2 | Controller test lifecycle (`@BeforeEach`) | `OwnerControllerTests.setup` | `OwnerRepository.findByLastNameStartingWith [R] Owner` |
| 3 | Controller test lifecycle (`@BeforeEach`) | `OwnerControllerTests.setup` | `OwnerRepository.findById [R] Owner` |
| 4 | Controller test lifecycle (`@BeforeEach`) | `OwnerControllerTests.setup` | `Pet.getVisits [R] Visit` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] (L91-L102)

> Sets up the owner search fixture, the owner lookup fixture, and the pet visit history used by subsequent MVC tests.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `Owner george = george();` |
| 2 | CALL | `given(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class)))` |
| 3 | CALL | `.willReturn(new PageImpl<>(List.of(george)));` |
| 4 | CALL | `given(this.owners.findById(TEST_OWNER_ID)).willReturn(Optional.of(george));` |
| 5 | SET | `Visit visit = new Visit();` |
| 6 | EXEC | `visit.setDate(LocalDate.now());` |
| 7 | EXEC | `george.getPet("Max").getVisits().add(visit);` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `setup` | Test lifecycle method | Prepares the controller test fixture before each test case runs. |
| `OwnerControllerTests` | Test class | MVC test suite covering owner-related web flows. |
| `owners` | Mock repository | Stubbed data access dependency used to simulate owner lookups. |
| `george` | Test fixture method / local variable | Canonical owner record for George Franklin, used as the standard owner test dataset. |
| `TEST_OWNER_ID` | Constant | Stable owner identifier used to simulate lookup of a specific owner. |
| `findByLastNameStartingWith` | Repository query | Owner search by last-name prefix for the owner listing screen. |
| `findById` | Repository query | Owner lookup by unique identifier for detail-oriented controller flows. |
| `PageImpl` | Technical type | In-memory Spring Data page wrapper for mock search results. |
| `Pageable` | Technical type | Pagination request used by owner search queries. |
| `Visit` | Domain entity | A pet visit record representing a veterinary appointment or checkup. |
| `getVisits` | Domain accessor | Returns the collection of visits associated with a pet. |
| `LocalDate.now()` | Date source | Current test date used to timestamp the seeded visit. |
| `Franklin` | Domain value | Owner last name used to match the mocked search query. |
| `Max` | Domain value | Pet name used to attach the seeded visit to the correct pet. |
| `Owner` | Domain entity | Pet clinic customer record containing contact details and associated pets. |
| `Pet` | Domain entity | An animal owned by a customer, used here to hold visit history. |
| `PetType` | Domain entity | Classification of the pet, such as dog. |
| `AOT` | Acronym | Ahead-of-Time compilation mode in Spring test/runtime execution. |
| `MVC` | Acronym | Model-View-Controller web architecture used by the controller test layer. |
| `BDDMockito` | Technical library | Mockito BDD style used to define repository return values. |
| `MockMvc` | Technical component | Spring test harness for simulating HTTP requests against controllers. |
