---

# (DD01) Business Logic — OwnerControllerTests.processUpdateOwnerFormWithIdMismatch() [19 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.processUpdateOwnerFormWithIdMismatch()

This test verifies the owner update flow when the identifier embedded in the request path does not match the identifier carried by the submitted owner form object. In business terms, it protects the owner maintenance process from accepting an inconsistent update request that could otherwise overwrite the wrong customer record. The method prepares an existing owner record, submits an edit request against a different owner id, and confirms that the controller rejects the mismatch by redirecting back to the edit screen instead of completing the update. The test also confirms that an error message is exposed through the flash scope, which is the user-facing signal that validation failed and the user must correct the form before trying again. This method belongs to the controller test layer and acts as a guardrail for request integrity in the owner administration workflow.

The method does not implement branching business logic itself; instead, it validates one specific negative-path scenario in the larger update-owner use case. Its role in the system is to ensure that the controller enforces identity consistency between URL state and form state, which is a common protection pattern in CRUD maintenance screens. The test uses MockMvc and a mocked repository dependency to emulate the web request lifecycle and observe the controller’s response contract.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
START["processUpdateOwnerFormWithIdMismatch()"]
SET1["Create owner with mismatched id and populated profile fields"]
SET2["Stub owners.findById(pathOwnerId) to return owner"]
CALL1["mockMvc.perform POST /owners/{ownerId}/edit with flashAttr owner"]
COND1{"Controller compares path owner id with submitted owner id"}
REDIR["Redirect to /owners/{ownerId}/edit"]
FLASH["Add flash error attribute"]
END_NODE["Return / Next"]
START --> SET1
SET1 --> SET2
SET2 --> CALL1
CALL1 --> COND1
COND1 --> REDIR
COND1 --> FLASH
REDIR --> END_NODE
FLASH --> END_NODE
```

## 3. Parameter Analysis

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

Instance fields and external state read by the method: `owners` mock repository, `mockMvc` test client, Spring MVC flash/session state, and the request path variable `pathOwnerId` defined locally in the test.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|-----------------------|
| R | `owners.findById` | N/A | `Owner` | Reads the owner record associated with the path identifier so the controller can process an update request against an existing profile. |
| R | `mockMvc.perform` | N/A | HTTP request / MVC state | Issues a simulated POST request to the owner edit endpoint and observes the controller response contract. |

This method is a test-only caller, so it does not execute business CRUD directly against a database. The only persistent-domain interaction represented here is the mocked read of the owner repository, which establishes the existing owner state required for the mismatch scenario.

## 5. Dependency Trace

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

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] (L232-L248)

> Sets up the negative-path owner update scenario and validates the controller response for an id mismatch.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int pathOwnerId = 1;` |
| 2 | SET | `Owner owner = new Owner();` |
| 3 | EXEC | `owner.setId(2);` |
| 4 | EXEC | `owner.setFirstName("John");` |
| 5 | EXEC | `owner.setLastName("Doe");` |
| 6 | EXEC | `owner.setAddress("Center Street");` |
| 7 | EXEC | `owner.setCity("New York");` |
| 8 | EXEC | `owner.setTelephone("0123456789");` |
| 9 | CALL | `when(owners.findById(pathOwnerId)).thenReturn(Optional.of(owner));` |
| 10 | CALL | `mockMvc.perform(MockMvcRequestBuilders.post("/owners/{ownerId}/edit", pathOwnerId).flashAttr("owner", owner))` |
| 11 | CALL | `.andExpect(status().is3xxRedirection())` |
| 12 | CALL | `.andExpect(redirectedUrl("/owners/" + pathOwnerId + "/edit"))` |
| 13 | CALL | `.andExpect(flash().attributeExists("error"));` |
| 14 | RETURN | `return;` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `Owner` | Domain entity | Pet owner profile maintained in the application’s customer management workflow. |
| `ownerId` | Field / path variable | The identifier embedded in the update URL that selects which owner record is being edited. |
| `flash` | Web MVC concept | Temporary redirect-scoped storage used to pass validation feedback to the next page load. |
| `MockMvc` | Test framework | Spring test client used to simulate HTTP requests without running a full server. |
| `redirectedUrl` | Assertion | Verifies that the controller sends the user back to the edit screen after validation failure. |
| `status().is3xxRedirection()` | Assertion | Confirms the response is a redirect rather than a successful update response. |
| `error` | UI message key | Flash attribute key indicating the update request failed validation. |
| id mismatch | Business rule | A safety check that rejects updates when the path owner id and submitted owner id differ. |
| CRUD | Acronym | Create, Read, Update, Delete — standard data operation categories. |
| MVC | Acronym | Model-View-Controller — web application pattern used by the controller layer. |
| repository | Technical term | Data access abstraction used to retrieve persisted owner records. |
| `pathOwnerId` | Local variable | Test variable representing the owner id embedded in the request URL. |
