---

# (DD04) 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 controller's defensive behavior when an owner edit request carries conflicting identifiers: the owner ID in the request path does not match the ID of the submitted form object. In business terms, it validates that the update flow rejects inconsistent ownership data and preserves the integrity of the customer record being edited. The method sets up an existing owner record, deliberately injects a mismatched form object, and confirms that the web layer responds by redirecting back to the edit screen rather than applying the update. This reflects a guardrail pattern in the larger system: the controller must not trust the submitted form alone and must keep the edit operation tied to the canonical owner identifier in the URL. The single branch covered by this test is the mismatch path, which should surface an error message and force the user to correct the form instead of changing persisted data.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
START(["processUpdateOwnerFormWithIdMismatch test"])
INIT(["Create pathOwnerId = 1 and Owner with id = 2"])
STUB(["Stub owners.findById(pathOwnerId) -> Optional.of(owner)"])
POST(["Submit POST /owners/{ownerId}/edit with flash owner"])
ASSERT_REDIRECT(["Expect 3xx redirection"])
ASSERT_URL(["Expect redirect to /owners/1/edit"])
ASSERT_ERROR(["Expect flash attribute error exists"])
END(["Test completes"])
START --> INIT
INIT --> STUB
STUB --> POST
POST --> ASSERT_REDIRECT
ASSERT_REDIRECT --> ASSERT_URL
ASSERT_URL --> ASSERT_ERROR
ASSERT_ERROR --> END
```

## 3. Parameter Analysis

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

External state read by the method: `mockMvc`, `owners` mock repository, Spring MVC request routing, and flash-scoped model attributes in the test harness.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|-----------------------|
| R | `owners.findById(pathOwnerId)` | N/A | `Owner` | Reads the existing owner record by path identifier so the controller can validate the submitted edit request against the persisted owner identity. |
| C | `MockMvcRequestBuilders.post("/owners/{ownerId}/edit", pathOwnerId).flashAttr("owner", owner)` | N/A | Web request / `Owner` form model | Creates a test POST request with the edited owner form payload to exercise the controller's update validation path. |
| R | `mockMvc.perform(...)` | N/A | Web MVC test harness | Executes the controller endpoint and reads the resulting HTTP status, redirect URL, and flash attributes for verification. |
| R | `status().is3xxRedirection()` | N/A | HTTP response | Checks that the controller rejects the mismatch with a redirect response instead of committing an update. |
| R | `redirectedUrl("/owners/" + pathOwnerId + "/edit")` | N/A | HTTP response | Confirms the user is returned to the same edit page tied to the canonical owner ID. |
| R | `flash().attributeExists("error")` | N/A | Flash scope | Verifies that the controller exposes an error message to explain why the update was rejected. |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Controller test: `OwnerControllerTests` | `OwnerControllerTests.processUpdateOwnerFormWithIdMismatch` | `owners.findById(pathOwnerId) [R] Owner` |
| 2 | Controller test: `OwnerControllerTests` | `OwnerControllerTests.processUpdateOwnerFormWithIdMismatch` | `mockMvc.perform(...) [R] HTTP response` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] (L231-L249)

> This test prepares a mismatched owner edit scenario and verifies that the web layer refuses to process an inconsistent identifier pair.

| # | 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"));` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `owner` | Domain entity | Customer/owner record managed by the Petclinic owner maintenance flow. |
| `ownerId` | Field / URL variable | Canonical owner identifier used in the edit request path. |
| `pathOwnerId` | Test variable | The owner identifier embedded in the URL that the controller uses as the source of truth. |
| `flash` | Web MVC mechanism | Temporary redirect-scoped storage used to deliver validation or error messages to the next page. |
| `MockMvc` | Test framework | Spring MVC test harness used to invoke controller endpoints without starting a full server. |
| `POST /owners/{ownerId}/edit` | Web route | Owner update submission endpoint. |
| `redirection` | HTTP behavior | Response pattern that sends the user back to an edit form instead of accepting the update. |
| `OwnerControllerTests` | Test class | Controller-level test suite covering owner edit and update behavior. |
| `id mismatch` | Business rule | Condition where the submitted owner record identity does not match the owner identifier in the URL, indicating inconsistent update data. |
