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

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

## 1. Role

### OwnerControllerTests.setup()

This method prepares the in-memory test fixture that the `OwnerController` web-layer tests depend on. It constructs a representative owner domain object, configures repository stubs so the controller can find that owner by last name or by identifier, and adds a visit to the owner's pet so downstream controller scenarios have realistic nested data to render and validate. In business terms, it simulates an existing customer record with an associated pet history, allowing controller tests to behave as though the application already contains a known owner named George Franklin and a pet named Max. The method does not execute user-facing business workflows directly; instead, it acts as a test harness initializer that makes the controller's read-only and form-processing paths deterministic. Its pattern is fixture assembly plus repository mocking: the method centralizes common setup data so each test can assume the same customer, pet, and visit state. The branches are not conditional in this method; however, it pre-establishes two lookup paths that the controller tests will later exercise: search by last-name prefix and fetch by owner ID.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["setup()"])
    OWNER(["Call george() to build Owner fixture"])
    STUB1(["Stub OwnerRepository.findByLastNameStartingWith(\"Franklin\", Pageable)"])
    STUB2(["Stub OwnerRepository.findById(TEST_OWNER_ID)"])
    VISIT(["Create Visit and set date to LocalDate.now()"])
    PET(["Call george.getPet(\"Max\")"])
    ADD(["Call getVisits().add(visit)"])
    END_NODE(["End setup / test fixture ready"])

    START --> OWNER
    OWNER --> STUB1
    STUB1 --> STUB2
    STUB2 --> VISIT
    VISIT --> PET
    PET --> ADD
    ADD --> END_NODE
```

**CRITICAL — Constant Resolution:**
The method references one local test constant:
- `TEST_OWNER_ID = 1` — the fixed owner identifier used by repository stubs and fixture lookup.

## 3. Parameter Analysis

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

External state read by the method:
- `TEST_OWNER_ID` — fixed owner identifier for the test fixture.
- `this.owners` — mocked `OwnerRepository` used to stub owner lookup responses.
- `LocalDate.now()` — current date used to create a sample visit record.
- `george()` — private fixture builder defined in the same test class.

## 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.
Use the pre-computed evidence above. If SC Code or Entity/DB is missing, try to infer from the class name and source usage.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `OwnerControllerTests.george` | OwnerControllerTests | - | Builds a reusable owner fixture for controller tests |
| R | `OwnerRepository.findByLastNameStartingWith` | OwnerRepository | Owner | Stubs owner search by last-name prefix to return the sample owner page |
| R | `OwnerRepository.findById` | OwnerRepository | Owner | Stubs owner lookup by ID to return the sample owner |
| C | `Visit` construction and `visit.setDate` | Visit | Visit | Creates a sample visit record and assigns the current date |
| R | `Owner.getPet` | Owner | Pet | Locates the pet named Max inside the owner fixture |
| U | `Pet.getVisits().add` | Pet | Visit collection | Adds the sample visit to the pet's visit history |

## 5. Dependency Trace

Trace who calls this method and what this method ultimately calls.
Use the caller search results from Step 2 above.

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

## 6. Per-Branch Detail Blocks

There is no conditional branching in this method. The method is a straight-line test setup routine, so the blocks below document each executable statement in order.

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

> Builds the reusable owner test fixture that all controller scenarios share.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `Owner george = george();` |

**Block 2** — [CALL] `(stub search-by-last-name lookup)` (L92-L93)

> Configures the repository mock to return the sample owner when the controller searches for Franklin owners.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `given(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class)))` |
| 2 | CALL | `.willReturn(new PageImpl<>(List.of(george)));` |

**Block 3** — [CALL] `(stub lookup-by-id path)` (L95)

> Configures the repository mock to resolve the known owner by the fixed test identifier.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `given(this.owners.findById(TEST_OWNER_ID)).willReturn(Optional.of(george));` |

**Block 4** — [SET] `(create visit history data)` (L96-L97)

> Creates a sample visit record and assigns the visit date so the pet history is non-empty.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Visit visit = new Visit();` |
| 2 | EXEC | `visit.setDate(LocalDate.now());` |

**Block 5** — [EXEC] `(attach visit to pet Max)` (L98)

> Locates the pet named Max in the fixture and appends the visit to its history collection.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `george.getPet("Max").getVisits().add(visit);` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `Owner` | Entity | A pet owner/customer in the Petclinic domain |
| `Pet` | Entity | A pet owned by an owner; used for veterinary records |
| `Visit` | Entity | A medical or consultation appointment recorded for a pet |
| `OwnerRepository` | Repository | Data-access abstraction for owner lookup operations |
| `findByLastNameStartingWith` | Repository method | Searches owners by the beginning of their last name |
| `findById` | Repository method | Retrieves a single owner by unique identifier |
| `Pageable` | Technical term | Pagination request used when querying owner search results |
| `PageImpl` | Technical term | In-memory page implementation used in tests to simulate paged query results |
| `Optional` | Technical term | Wrapper indicating that a lookup may return a value or be empty |
| `TEST_OWNER_ID` | Test constant | Fixed owner identifier used to make repository stubbing deterministic |
| `george()` | Test fixture method | Builds a complete sample owner record for test scenarios |
| `Franklin` | Sample data | Last name used to emulate a searchable owner record |
| `Max` | Sample data | Pet name used to populate the owner's nested pet history |
| `LocalDate.now()` | Technical term | Current calendar date used to timestamp the sample visit |
| `MockitoBean` | Test framework term | Spring Boot test annotation that injects a Mockito mock into the test context |
| `WebMvcTest` | Test framework term | Spring Boot slice annotation that loads only web-layer components for controller testing |
| `@BeforeEach` | Test lifecycle | JUnit callback that runs before every test method to prepare shared test data |
| `BDDMockito.given` | Test framework term | Mockito BDD syntax used to define repository mock behavior |
| `Page` | Technical term | A paginated query result used by the owner search flow |
| `OwnerController` | Controller | Web controller under test that handles owner-related UI flows |
