---

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

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

## 1. Role

### OwnerControllerTests.setup()

This method prepares the test fixture for owner-related web controller scenarios by building a representative owner profile and wiring repository stubs that the controller test methods depend on. It creates a canonical sample owner named George Franklin, then configures the mocked `OwnerRepository` so searches by last name and by ID return that same owner record. In business terms, this ensures that every test starts from a stable customer-account state with one owned pet (`Max`) and one existing visit history entry, which mirrors the minimum realistic data needed for owner detail, search, and pet history flows.

The method acts as a test-data bootstrapper rather than production business logic. It follows a fixture-seeding pattern: construct domain objects, inject repository behavior, and enrich the owner aggregate with a visit record so downstream controller requests can render pages and evaluate state consistently. Because it is annotated with `@BeforeEach`, it is executed before every test method and therefore serves as a shared setup entry point for all owner-controller web tests in this class. There are no conditional branches in the method; its entire purpose is deterministic initialization of the mocked persistence layer and domain graph.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["setup()"])
    STEP1(["Create test Owner george via george()"])
    STEP2(["Stub OwnerRepository.findByLastNameStartingWith('Franklin', Pageable)"])
    STEP3(["Stub OwnerRepository.findById(TEST_OWNER_ID)"])
    STEP4(["Create Visit and set visit date to LocalDate.now()"])
    STEP5(["Add Visit to George's pet Max visits collection"])
    END_NODE(["Return / Next"])
    START --> STEP1
    STEP1 --> STEP2
    STEP2 --> STEP3
    STEP3 --> STEP4
    STEP4 --> STEP5
    STEP5 --> END_NODE
```

## 3. Parameter Analysis

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

Instance fields and external state read by this method:
- `owners` - mocked `OwnerRepository` used to define repository responses for controller tests.
- `TEST_OWNER_ID` - fixed test identifier used to bind the sample owner to a stable ID.
- `LocalDate.now()` - runtime date source used to seed the sample visit date.
- `george()` - helper method that constructs the canonical owner aggregate for the test 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 | Owner / Pet | Builds the canonical owner aggregate used as test fixture input |
| R | `OwnerRepository.findByLastNameStartingWith` | OwnerRepository | Owner | Stubs owner lookup by last name for search-driven controller flows |
| R | `OwnerRepository.findById` | OwnerRepository | Owner | Stubs owner lookup by identifier for detail and edit controller flows |
| C | `Visit` constructor plus `Visit.setDate` and `Pet.getVisits().add` | Visit / Pet | Visit | Creates and attaches a new visit record to the sample pet's visit history |

## 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 | `OwnerControllerTests` | `OwnerControllerTests.initCreationForm` / `processCreationFormSuccess` / other test methods -> `setup()` | `OwnerControllerTests.george [R] Owner / Pet` |
| 2 | `OwnerControllerTests` | `OwnerControllerTests.* test methods` -> `setup()` -> `OwnerRepository.findByLastNameStartingWith` | `OwnerRepository.findByLastNameStartingWith [R] Owner` |
| 3 | `OwnerControllerTests` | `OwnerControllerTests.* test methods` -> `setup()` -> `OwnerRepository.findById` | `OwnerRepository.findById [R] Owner` |
| 4 | `OwnerControllerTests` | `OwnerControllerTests.* test methods` -> `setup()` -> `Pet.getVisits()` -> `Visit` creation | `Visit [C] Visit` |

## 6. Per-Branch Detail Blocks

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

> Initializes the owner-controller test fixture before each test method.

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

**Block 2** — [SEQUENCE] `(repository stub for last-name lookup)` (L93-L94)

> Defines the search result returned when controller code queries owners by last name.

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

**Block 3** — [SEQUENCE] `(repository stub for owner ID lookup)` (L96)

> Ensures controller code can resolve the sample owner by ID during detail and edit flows.

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

**Block 4** — [SEQUENCE] `(visit history seed)` (L97-L99)

> Creates a single visit record so the sample pet has non-empty medical history.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Visit visit = new Visit();` |
| 2 | EXEC | `visit.setDate(LocalDate.now());` |
| 3 | EXEC | `george.getPet("Max").getVisits().add(visit);` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `owners` | Field | Mocked owner repository used to simulate owner persistence lookups during controller tests |
| `TEST_OWNER_ID` | Constant / Field | Fixed owner identifier used to make test data deterministic |
| `Owner` | Domain entity | Pet clinic customer record that stores personal and contact information |
| `Pet` | Domain entity | Animal owned by a customer; used for visit-history and pet-management scenarios |
| `Visit` | Domain entity | A recorded appointment or medical visit for a pet |
| `Pageable` | Technical term | Pagination request object used to model pageable search results |
| `PageImpl` | Technical term | Spring Data page implementation used to return a pageable result set in tests |
| `Optional` | Technical term | Container indicating that an owner lookup may or may not find a matching record |
| `LocalDate` | Technical term | Date-only value used to seed the visit date |
| `Franklin` | Business value | Last name of the canonical sample owner used in the test fixture |
| `Max` | Business value | Name of the canonical sample pet attached to the owner fixture |
| `dog` | Business value | Pet type assigned to the sample pet |
| `@BeforeEach` | JUnit annotation | Indicates the method runs before every test case to reset shared state |
| `@WebMvcTest` | Spring test annotation | Configures a controller-focused web test slice |
| `MockMvc` | Technical term | Spring test harness for executing MVC requests without a running server |
| `OwnerRepository` | Repository interface | Data access contract for retrieving owner records |
| `findByLastNameStartingWith` | Repository method | Search operation that returns owners matching a last-name prefix |
| `findById` | Repository method | Lookup operation that returns a single owner by identifier |
| `george()` | Helper method | Test-data factory that builds the canonical owner aggregate used in controller tests |
