# (DD16) 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 verifies the owner search-by-last-name flow exposed by the owner controller. Business-wise, it validates the case where a user submits a last name that matches exactly one owner and the application should not show a results list, but instead route directly to the matched owner's detail page. The method sets up the repository to return a single owner record for the last name `Franklin`, executes the HTTP GET request for the owner search endpoint, and asserts that the controller responds with a redirect to the matched owner's profile.

The method represents a controller-level verification of routing and search resolution behavior. It uses a standard test pattern of arrange-stub-act-assert, where the repository behavior is stubbed first, then the web request is executed, and finally the HTTP status and resolved view name are checked. In the larger system, it acts as a regression safeguard for the owner search experience, ensuring the UI behavior remains consistent when the search returns one unique owner rather than zero or many owners.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
START["processFindFormByLastName()"]
STEP1["Create PageImpl containing george() owner result"]
STEP2["Stub owners.findByLastNameStartingWith('Franklin', Pageable)"]
STEP3["Perform GET /owners?page=1 with lastName=Franklin"]
STEP4["Verify HTTP status is 3xx redirection"]
STEP5["Verify redirect view name is redirect:/owners/ownerId"]
END_NODE["Test completes"]
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 read by this method:
- `owners`: mocked owner repository used to simulate search results for last-name lookup.
- `mockMvc`: Spring MVC test harness used to execute the HTTP request and validate the response.
- `TEST_OWNER_ID`: expected owner identifier used in the redirect assertion.

## 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 |
|------|----------|---------|-------------|----------------------|
| C | `george` | OwnerControllerTests | Owner | Creates a test owner fixture used to simulate a matching search result |
| R | `findByLastNameStartingWith` | OwnerRepository | Owner | Reads owners whose last name begins with the requested text |
| R | `mockMvc.perform` | MockMvc | HTTP Request / MVC | Executes the controller request for the owner search endpoint |
| R | `andExpect` | MockMvc ResultActions | HTTP Response / View | Reads and verifies the HTTP status and resolved view name |

## 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: `OwnerControllerTests` | `OwnerControllerTests.processFindFormByLastName` | `findByLastNameStartingWith [R] Owner` |

## 6. Per-Branch Detail Blocks

**Block 1** — **SEQUENCE** `(test setup and execution)` (L149-L155)

> This block covers the full test scenario from fixture creation through response validation.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Page<Owner> tasks = new PageImpl<>(List.of(george()));` // create a single matching owner result |
| 2 | CALL | `george();` // build the owner fixture |
| 3 | CALL | `when(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class))).thenReturn(tasks);` // stub repository search result |
| 4 | CALL | `mockMvc.perform(get("/owners?page=1").param("lastName", "Franklin"))` // execute the owner search request |
| 5 | CALL | `.andExpect(status().is3xxRedirection())` // verify redirect status |
| 6 | CALL | `.andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID));` // verify direct redirect to the matched owner page |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `owners` | Field | Mocked owner repository used to simulate owner search behavior in the controller test |
| `mockMvc` | Field | Spring MVC test client used to execute the controller endpoint and inspect the HTTP response |
| `lastName` | Field | Search criterion submitted by the user to find owners by surname |
| `Page` | Technical type | Paginated result wrapper used to represent a search result set |
| `PageImpl` | Technical type | Concrete page implementation used in tests to provide mocked search results |
| `Owner` | Domain entity | PetClinic owner record containing customer information for pet ownership |
| `george()` | Test fixture | Helper method that builds a sample owner named George for search result simulation |
| `findByLastNameStartingWith` | Repository method | Search operation that returns owners whose last name starts with the given input |
| `Franklin` | Test data | Sample search input used to validate the redirect-on-single-match behavior |
| `Pageable` | Technical type | Pagination request object used by the repository search method |
| `redirect:/owners/` | Routing term | MVC redirect target that sends the user to the matched owner detail page |
| `TEST_OWNER_ID` | Test constant | Expected owner identifier appended to the redirect URL |
