# (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 workflow when the submitted form data is inconsistent with the owner identifier embedded in the request path. In business terms, it protects the owner maintenance process from accidental or malicious record substitution by ensuring that an update request cannot silently persist a different owner than the one the user intended to edit. The test prepares an owner entity whose internal `id` does not match the `ownerId` path variable, then drives the controller through the HTTP POST update endpoint to confirm that the system rejects the mismatch by redirecting back to the edit page and exposing an error message. The method acts as a regression guard for the controller’s identity-consistency validation rule and supports the larger owner management module by confirming that record integrity checks are enforced before any update is accepted. It does not branch into multiple business service types itself; rather, it validates the controller branch that handles identifier mismatch conditions during an update operation.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["processUpdateOwnerFormWithIdMismatch()"]
    INIT["Create test owner data
pathOwnerId = 1
owner.id = 2
Populate name, address, city, telephone"]
    STUB["Stub owners.findById(1) -> Optional.of(owner)"]
    PERFORM["Execute POST /owners/{ownerId}/edit with flashAttr owner"]
    ASSERT_REDIRECT["Assert response status is 3xx redirection"]
    ASSERT_URL["Assert redirected URL is /owners/1/edit"]
    ASSERT_FLASH["Assert flash attribute error exists"]
    END_NODE["Return / Next"]

    START --> INIT
    INIT --> STUB
    STUB --> PERFORM
    PERFORM --> ASSERT_REDIRECT
    ASSERT_REDIRECT --> ASSERT_URL
    ASSERT_URL --> ASSERT_FLASH
    ASSERT_FLASH --> END_NODE
```

## 3. Parameter Analysis

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

External state read by this method: test instance fields such as `owners` and `mockMvc`, plus Spring MVC test infrastructure configured for the `OwnerController` web layer.

## 4. CRUD Operations / Called Services

This method is a test harness and does not directly perform business CRUD on the owner repository. Its only runtime interactions are stubbing and HTTP test execution.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `owners.findById(pathOwnerId)` | N/A | `Owner` | Test stub read that simulates retrieval of an existing owner record for the controller path lookup |
| C | `mockMvc.perform(...)` | N/A | N/A | Executes the controller request path as a test action; no persistent database create occurs |
| R | `status().is3xxRedirection()` | N/A | N/A | Verifies the HTTP redirect outcome after mismatch validation |
| R | `redirectedUrl("/owners/" + pathOwnerId + "/edit")` | N/A | N/A | Verifies the controller routes the user back to the edit form |
| R | `flash().attributeExists("error")` | N/A | N/A | Verifies an error flash message is produced for the validation failure |

## 5. Dependency Trace

The method is not invoked by production screens or batches; it is a JUnit test method executed by the test runner. It indirectly exercises the controller update endpoint and validates the mismatch branch in `OwnerController.processUpdateOwnerForm`.

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

## 6. Per-Branch Detail Blocks

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

> Prepares the mismatch scenario by defining the path owner identifier and a different owner entity identifier.

| # | 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** — [SEQUENCE] `(repository stubbing)` (L242)

> Simulates the controller being able to load the owner referenced by the URL path.

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

**Block 3** — [SEQUENCE] `(HTTP request execution and assertions)` (L244-L248)

> Sends the update form submission and verifies the controller rejects the identifier mismatch.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `mockMvc.perform(MockMvcRequestBuilders.post("/owners/{ownerId}/edit", pathOwnerId).flashAttr("owner", owner))` |
| 2 | CALL | `.andExpect(status().is3xxRedirection())` |
| 3 | CALL | `.andExpect(redirectedUrl("/owners/" + pathOwnerId + "/edit"))` |
| 4 | CALL | `.andExpect(flash().attributeExists("error"));` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `owner` | Domain entity | Customer/animal owner record managed by the Pet Clinic application |
| `ownerId` | Field / path variable | Identifier of the owner record targeted by the web request |
| `pathOwnerId` | Test variable | Simulated identifier extracted from the URL path for the update request |
| `id` | Field | Persistent identifier of the owner entity |
| `flash attribute` | Web MVC concept | Temporary message carried across a redirect, used here to expose validation errors |
| `3xx redirection` | HTTP status family | Browser redirect response indicating the controller did not complete the update in place |
| `mismatch` | Validation code | Logical condition indicating that the form-submitted owner ID and URL owner ID do not agree |
| `MockMvc` | Test framework component | Spring MVC test harness used to execute the controller endpoint without a running server |
| `owners` | Repository | Data access abstraction for owner records |
| `findById` | Repository method | Lookup operation used to locate an owner by its identifier |
| `POST /owners/{ownerId}/edit` | Web endpoint | Owner update submission route |
| `redirectedUrl` | Test assertion | Verifies the controller sends the browser back to the expected URL after validation failure |
| `error` | Flash message key | Error indicator shown to the user when the submitted owner identity is inconsistent |
