# (DD11) Business Logic — OwnerControllerTests.processFindFormByLastName() [8 LOC]

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

## 1. Role

### OwnerControllerTests.processFindFormByLastName()

This test method validates the owner search-by-last-name flow in the Owner web controller. Business-wise, it verifies that when a user submits the owner search form with a last name prefix, the controller consults the owner repository and returns the expected redirect behavior when the search identifies a matching owner. In this scenario, the repository is stubbed to return a single owner record for the last name `Franklin`, and the test confirms that the web layer responds by redirecting the browser to that owner's detail page rather than rendering a list view.

The method represents a controller contract test for the search-and-redirect branch of the owner lookup use case. It follows a routing/dispatch testing pattern: the test prepares mock repository data, invokes the HTTP endpoint through MockMvc, and asserts the response state. Its role in the system is to protect the owner search feature from regressions by ensuring the controller interprets a single-result search as a direct navigation outcome. Because it is a test method, it does not implement business processing itself; instead, it verifies the controller's integration with the repository and the resulting HTTP behavior.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["processFindFormByLastName()"]
    SETUP["Create PageImpl with george()"]
    STUB["Stub owners.findByLastNameStartingWith('Franklin', Pageable)"]
    PERFORM["Perform GET /owners?page=1 with lastName=Franklin"]
    REDIRECT_CHECK["Assert HTTP 3xx redirect"]
    VIEW_CHECK["Assert view redirect:/owners/TEST_OWNER_ID"]
    END_NODE["Return / Next"]
    START --> SETUP
    SETUP --> STUB
    STUB --> PERFORM
    PERFORM --> REDIRECT_CHECK
    REDIRECT_CHECK --> VIEW_CHECK
    VIEW_CHECK --> END_NODE
```

**Processing summary:** The test first constructs a one-element page containing the fixture owner returned by `george()`. It then stubs the repository read operation so that any search beginning with `Franklin` returns that page. Next, the test performs an HTTP GET request against the owner search endpoint with `page=1` and `lastName=Franklin`. Finally, it verifies that the controller responds with a redirect status and that the redirect target is the owner detail page for `TEST_OWNER_ID`.

## 3. Parameter Analysis

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

Instance fields and external state read by the method:
- `this.owners` — mocked owner repository used to supply search results for the controller test.
- `TEST_OWNER_ID` — test fixture identifier used to validate the expected redirect target.
- `george()` — test fixture factory that supplies the owner record placed into the mock page.
- `mockMvc` — Spring MockMvc test harness used to execute the HTTP request against the controller.

## 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 | Reads owners whose last name starts with the supplied prefix, returning the matching page to the controller test |
| - | `OwnerControllerTests.george` | OwnerControllerTests | - | Creates the owner fixture used as mock search data |
| - | `PageImpl` constructor | Spring Data | Page | Builds an in-memory page wrapper for the mocked repository result |
| - | `MockMvc.perform` | Spring Test | HTTP request/response | Executes the controller endpoint under test |
| - | `ResultActions.andExpect` | Spring Test | Assertion | Verifies HTTP status and redirect target |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Test: `OwnerControllerTests` | `OwnerControllerTests.processFindFormByLastName` | `OwnerRepository.findByLastNameStartingWith [R] Owner` |

This method is not invoked by a production screen or batch process. It is a JUnit test entry point that exercises the owner search controller behavior through MockMvc, and the main external dependency reached from the test path is the owner repository read operation.

## 6. Per-Branch Detail Blocks

**Block 1** — SEQUENCE (test setup) (L150)

> Prepares a single-owner search result page for the repository stub.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Page<Owner> tasks = new PageImpl<>(List.of(george()));` |

**Block 2** — CALL (repository stub) (L151)

> Defines the mocked read behavior for the owner last-name search.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `when(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class))).thenReturn(tasks);` |

**Block 3** — EXEC (HTTP request execution) (L152)

> Sends the search request through the controller under test.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `mockMvc.perform(get("/owners?page=1").param("lastName", "Franklin"))` |

**Block 4** — EXEC (response assertion chain) (L153-L154)

> Verifies that the controller redirects after a single match is found.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `.andExpect(status().is3xxRedirection())` |
| 2 | CALL | `.andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID));` |

**Block 5** — RETURN (test completion) (L155-L156)

> Completes the JUnit method after the assertions pass.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `}` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `Owner` | Domain entity | Customer or pet owner record maintained by the Petclinic application |
| `OwnerRepository` | Repository | Data access interface used to retrieve owner records from persistent storage |
| `findByLastNameStartingWith` | Repository query method | Search operation that returns owners whose last name begins with the supplied text |
| `lastName` | Field / request parameter | Search key entered by the user to look up owners by surname prefix |
| `Page` | Technical term | Paginated result container representing a subset of matching owner records |
| `Pageable` | Technical term | Paging request object used to control result size and page number |
| `MockMvc` | Test framework component | Spring test harness used to simulate HTTP requests to the controller |
| `3xx Redirection` | HTTP status category | Response indicating the browser should navigate to another URL |
| `redirect:/owners/{id}` | Navigation target | Redirect route to the selected owner's detail page |
| `george()` | Test fixture method | Helper that builds a sample owner record for the test scenario |
| `TEST_OWNER_ID` | Test constant | Expected owner identifier used to validate the redirect destination |
| `Franklin` | Test input value | Example last-name prefix used to exercise the owner search path |
