# (DD11) Business Logic — OwnerControllerTests.processFindFormByLastName() [8 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.processFindFormByLastName()

This test method verifies the owner search-by-last-name flow exposed by the owner controller. From a business perspective, it validates the scenario where a user enters a last name and the application responds by locating matching owners, then redirecting to the single-owner detail page when exactly one matching owner is found. The method exercises the controller's search routing behavior rather than the repository implementation itself, using a mocked repository response to simulate the underlying owner lookup service. It represents a user-facing find operation in the owner management domain, ensuring that a successful last-name search is interpreted as a direct navigation to the matching owner record. The method also confirms the controller integrates correctly with pagination-aware repository access, because the search is executed through a `Pageable` query. In the broader system, this test acts as a regression guard for the owner search entry point and the redirect behavior that short-circuits the list view when the search returns a unique owner.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["processFindFormByLastName() test"])
    INIT(["Create Page<Owner> with one owner: george()"])
    STUB(["Stub owners.findByLastNameStartingWith('Franklin', Pageable) to return the page"])
    REQUEST(["Perform GET /owners?page=1 with lastName=Franklin"])
    ASSERT_STATUS(["Assert HTTP status is 3xx Redirection"])
    ASSERT_VIEW(["Assert view is redirect:/owners/1"])
    END_NODE(["Test completes"])

    START --> INIT
    INIT --> STUB
    STUB --> REQUEST
    REQUEST --> ASSERT_STATUS
    ASSERT_STATUS --> ASSERT_VIEW
    ASSERT_VIEW --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | This JUnit test method takes no parameters. The business input values are provided indirectly through mocked repository behavior and the MVC request parameters `lastName=Franklin` and `page=1`, which simulate the user entering a last name in the owner search form. |

**Instance fields / external state used:** `mockMvc`, `owners`, `TEST_OWNER_ID`, and the helper method `george()`.

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `OwnerRepository.findByLastNameStartingWith` | OwnerRepository | Owner | Calls `findByLastNameStartingWith` in `OwnerRepository` |
| - | `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 | `OwnerRepository.findByLastNameStartingWith` | `OwnerRepository` | `Owner` | Repository lookup for owners whose last name begins with the submitted search text |
| - | `OwnerControllerTests.george` | `OwnerControllerTests` | - | Test helper that constructs the owner fixture used as the mocked search result |
| - | `MockMvc.perform` | `MockMvc` | - | Executes the simulated HTTP GET request for the owner search screen |
| - | `status().is3xxRedirection` | `MockMvcResultMatchers` | - | Verifies the controller responds with a redirect after a successful unique match |
| - | `view().name` | `MockMvcResultMatchers` | - | Verifies the redirect target is the owner detail route for the matched owner |

## 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 | Test runner / JUnit | `OwnerControllerTests.init test execution -> OwnerControllerTests.processFindFormByLastName` | `OwnerRepository.findByLastNameStartingWith [R] Owner` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] (L150-L155)

> This block sets up the mock search result, performs the controller request, and verifies the redirect outcome for a successful unique owner match.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Page<Owner> tasks = new PageImpl<>(List.of(george()));` // Create a single-row page containing the test owner |
| 2 | CALL | `george()` // Build the owner fixture used in the mocked search result |
| 3 | CALL | `when(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class))).thenReturn(tasks);` // Stub the repository lookup for last name Franklin |
| 4 | CALL | `mockMvc.perform(get("/owners?page=1").param("lastName", "Franklin"))` // Simulate the user submitting the owner find form |
| 5 | CALL | `.andExpect(status().is3xxRedirection())` // Verify the controller redirects after finding one owner |
| 6 | CALL | `.andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID))` // Verify redirect target points to the matched owner detail page |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `Owner` | Domain object | A pet owner record in the PetClinic system |
| `OwnerRepository` | Repository | Data access component used to search and retrieve owner records |
| `findByLastNameStartingWith` | Repository method | Search operation that returns owners whose last name begins with the supplied text |
| `lastName` | Form field | User-entered surname used to search for matching owners |
| `page` | Request parameter | Pagination index used when calling the owner search endpoint |
| `MockMvc` | Test framework | Spring MVC test harness used to simulate HTTP requests and assert controller responses |
| `Page` | Pagination type | Container for a paged repository result set |
| `PageImpl` | Pagination implementation | Concrete test fixture that wraps a list of owners as a page |
| `Pageable` | Pagination interface | Describes page and sorting information passed to repository queries |
| `redirect:/owners/{id}` | Route pattern | Controller redirect to the owner detail page for the matched owner |
| `Franklin` | Test data value | Surname used in the success-path search scenario |
| `TEST_OWNER_ID` | Test constant | Fixed owner identifier used to verify the redirect target |
| `george()` | Test helper method | Factory method that builds the owner fixture named George Franklin |
