# (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 verifies the owner search flow when a user searches by last name and exactly one matching owner is returned. In business terms, it validates the controller behavior for a “find owner” inquiry where the search result should immediately resolve to a single owner profile instead of presenting a results list. The test prepares a repository response containing one owner with last name `Franklin`, submits a search request with the same last name, and asserts that the web layer responds with a redirect to that owner’s detail page.

The method represents a focused routing test for the owner lookup use case. It confirms the controller’s dispatch pattern for the single-match branch of the search workflow: when the search criteria resolves to one record, the user should be taken directly to the owner record rather than staying on the search screen. The method also indirectly verifies that the search form request is accepted with pagination parameters and that the controller can use the repository-provided result to produce the correct redirect target. Because this is a test method, its role in the larger system is to lock down controller behavior and prevent regressions in the owner search experience.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["processFindFormByLastName()"])
    PREPARE(["Prepare repository mock result with one Owner"])
    STUB(["when owners.findByLastNameStartingWith(Franklin, pageable) thenReturn PageImpl"])
    REQUEST(["Submit GET /owners?page=1 with lastName = Franklin"])
    ASSERT_STATUS(["Assert response status is 3xx redirection"])
    ASSERT_VIEW(["Assert view name is redirect:/owners/1"])
    END_NODE(["Test passes / scenario validated"])

    START --> PREPARE
    PREPARE --> 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 method has no parameters. Its behavior is driven by the mocked repository state, the HTTP request built inside the test, and the constant `TEST_OWNER_ID` field used to verify the redirect target.

## 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 | Stubs a repository read for owners whose last name starts with `Franklin` |
| - | `george` | OwnerControllerTests | - | Builds a test Owner aggregate with a matching last name and pet data |
| - | `mockMvc.perform` | MockMvc | - | Issues an HTTP GET request to the owner search endpoint |
| - | `status().is3xxRedirection` | MockMvcResultMatchers | - | Verifies the controller returned a redirect response |
| - | `view().name` | MockMvcResultMatchers | - | Verifies the redirect target view is the owner detail URL |

## 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 | Controller test suite | `OwnerControllerTests.processFindFormByLastName` | `OwnerRepository.findByLastNameStartingWith [R] Owner` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SET] test data preparation (L149-L152)

> Prepares the repository mock with a single matching owner so the single-result branch can be validated.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Page<Owner> tasks = new PageImpl<>(List.of(george()));` |
| 2 | CALL | `george();` |
| 3 | CALL | `when(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class))).thenReturn(tasks);` |

**Block 2** — [CALL] execute search request (L153)

> Executes the controller endpoint with a last name that should match exactly one owner.

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

**Block 3** — [ASSERT] single-result redirect validation (L154-L155)

> Verifies that one matching owner causes an immediate redirect to the owner detail page.

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

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `Owner` | Domain entity | Pet owner record containing identity and contact information |
| `OwnerController` | Controller | Web controller that manages owner search, creation, update, and detail pages |
| `OwnerRepository` | Repository | Data access component used to search and load owners |
| `findByLastNameStartingWith` | Repository method | Owner lookup that returns owners whose last name begins with the supplied text |
| `lastName` | Field | Search criterion entered by the user to locate an owner |
| `Pageable` | Technical type | Pagination request used to limit and navigate query results |
| `PageImpl` | Technical type | In-memory page wrapper used in the test to simulate repository paging results |
| `MockMvc` | Test framework | Spring test utility used to execute controller requests without a running server |
| `redirect:/owners/` | Web routing term | Redirect route to the owner detail page |
| `TEST_OWNER_ID` | Constant | Test identifier used to verify the redirect points to the expected owner |
| `Franklin` | Test data value | Matching last name used to trigger the single-owner redirect path |
| `george()` | Test helper | Builds a fully populated owner fixture for controller testing |
