---
# (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 method validates the controller's security and integrity behavior when an update request is submitted with a mismatched identifier. In business terms, it simulates an owner maintenance scenario where the record selected from the URL path represents one owner, but the form payload contains a different owner ID, which should not be allowed to overwrite the target record. The test confirms that the application rejects the conflicting submission, returns the user to the edit screen, and surfaces an error message through flash attributes instead of proceeding with an update. This is a negative-path regression test that protects the owner master data maintenance flow from accidental or malicious cross-record updates. The method does not execute a business update itself; it orchestrates mock data, performs the HTTP request, and asserts the expected controller response contract.

The test uses the standard Arrange-Act-Assert pattern. It sets up an owner returned from the repository, posts the owner edit form, and checks that the framework redirects back to the edit page with an error indicator. Its role in the larger system is to verify the controller's input validation behavior for owner edit operations, ensuring the web layer preserves data consistency before any persistence operation can occur.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
START(["processUpdateOwnerFormWithIdMismatch()"])
A["Create owner with path ID = 1 and form owner ID = 2"]
B["Stub owners.findById(1) to return the owner"]
C["Perform POST /owners/1/edit with owner in flash attribute"]
D{"Path owner ID matches flash owner ID?"}
E["Continue normal update flow"]
F["Redirect back to edit page with error flash attribute"]
END_NODE(["Return / Next"])
START --> A
A --> B
B --> C
C --> D
D -->|No| F
D -->|Yes| E
E --> END_NODE
F --> END_NODE
```

## 3. Parameter Analysis

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

This method defines no explicit parameters. Its behavior is driven by test fixtures, the mocked repository response, and the HTTP request built inside the method body. The external state it reads includes the mocked `owners` repository and the `mockMvc` test harness instance.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|-----------------------|
| R | `owners.findById` | N/A | `Owner` | Reads the owner record for the path identifier so the controller can evaluate the edit request against an existing persisted owner. |

The method itself does not invoke application service components or database operations directly. It prepares a repository stub for a read operation and then validates the MVC response triggered by the controller under test.

## 5. Dependency Trace

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

This method is a test entry point rather than a production business endpoint, so it is not called by a screen, batch, or CBS flow in runtime. The only meaningful downstream dependency is the mocked repository read configured within the test.

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] `(test setup)` (L232-L240)

> Prepares an owner object where the path identifier and submitted form identifier are intentionally different.

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

**Block 2** — [EXECUTION PREPARATION] `(stub repository response)` (L242)

| # | Type | Code |
|---|------|------|
| 1 | CALL | `when(owners.findById(pathOwnerId)).thenReturn(Optional.of(owner));` |

**Block 3** — [EXECUTION] `(perform POST request to edit endpoint)` (L244)

> Submits the edit form for the path owner while carrying the mismatched owner object in the flash scope.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `mockMvc.perform(MockMvcRequestBuilders.post("/owners/{ownerId}/edit", pathOwnerId).flashAttr("owner", owner))` |

**Block 4** — [ASSERTION] `(verify controller rejection path)` (L245-L248)

| # | Type | Code |
|---|------|------|
| 1 | CALL | `.andExpect(status().is3xxRedirection())` |
| 2 | CALL | `.andExpect(redirectedUrl("/owners/" + pathOwnerId + "/edit"))` |
| 3 | CALL | `.andExpect(flash().attributeExists("error"));` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `Owner` | Domain entity | Pet owner master record maintained through the owner management screen. |
| `ownerId` | Field / Path variable | Identifier of the owner record selected from the URL. |
| `flash` | Web mechanism | Temporary redirect-scoped storage used to pass validation feedback to the next page view. |
| `error` | UI message key | Indicator that the submitted owner update was rejected because of invalid input or inconsistent state. |
| `MockMvc` | Test framework component | Spring MVC test harness used to simulate an HTTP request to the controller. |
| `owners` | Repository mock | Mocked data access dependency used to supply the existing owner record for the controller test. |
| `edit` | UI action | Owner edit form used to maintain owner master data. |
| `POST` | HTTP method | Submission method used to send the edited owner data to the server. |
| `ID mismatch` | Validation condition | Condition where the owner ID in the request path and the owner ID in the submitted form do not match. |
