---

# (DD49) Business Logic — ProcessCreationFormHasErrors.processCreationFormWithInvalidBirthDate() [15 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `org.springframework.samples.petclinic.owner.ProcessCreationFormHasErrors` |
| Layer | Controller |
| Module | `owner` (Package: `org.springframework.samples.petclinic.owner`) |

## 1. Role

### ProcessCreationFormHasErrors.processCreationFormWithInvalidBirthDate()

This test verifies the Pet creation workflow when the user submits a birth date that cannot be accepted by the data binding layer. In business terms, it validates that the pet registration screen rejects an invalid or future-dated birth date and returns the user to the same maintenance form with field-level feedback, rather than advancing the transaction. The method acts as a guardrail for the owner/pet onboarding flow by confirming that the controller preserves the owner context while isolating the invalid pet input.

The scenario is a negative-path validation check for the pet creation use case. It exercises one service category only: form submission handling for pet registration, specifically the birth-date validation branch. The test confirms that the owner model remains free of errors, the pet model carries the validation failure, and the rendered view remains the create/update form so the user can correct the entry. In the larger system, this method supports controller quality assurance for the `owner` module and ensures the UI contract for invalid date input stays stable.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["processCreationFormWithInvalidBirthDate()"])
    P1["Set currentDate = LocalDate.now()"]
    P2["Set futureBirthDate = currentDate.plusMonths(1).toString()"]
    P3["Build POST /owners/{ownerId}/pets/new request"]
    P4["Add name = Betty"]
    P5["Add birthDate = futureBirthDate"]
    P6["Execute mockMvc.perform(request)"]
    P7["Expect model has no errors for owner"]
    P8["Expect model has errors for pet"]
    P9["Expect model has field errors for pet.birthDate"]
    P10["Expect field error code typeMismatch.birthDate"]
    P11["Expect status OK"]
    P12["Expect view pets/createOrUpdatePetForm"]
    END(["Return"])
    START --> P1 --> P2 --> P3 --> P4 --> P5 --> P6 --> P7 --> P8 --> P9 --> P10 --> P11 --> P12 --> END
```

This method follows a linear test-assertion pattern with no branches, loops, or conditional dispatch. It first computes a future date relative to the current system date, then sends that value through the pet creation endpoint as the birth-date form field. The remainder of the flow validates that the controller rejects the invalid input, exposes the expected field error code, and keeps the request on the same form page.

## 3. Parameter Analysis

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

External state read by the method:
- `LocalDate.now()` for the current business date used to construct an invalid future birth date.
- `TEST_OWNER_ID` for the target owner context in the creation request.
- `mockMvc` for executing the web request against the controller under test.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|-----------------------|
| R | `LocalDate.now()` | N/A | System clock / current date | Reads the current date to derive an invalid future birth date for validation testing. |
| C | `mockMvc.perform(...)` | N/A | HTTP request to `/owners/{ownerId}/pets/new` | Submits a pet creation form with a future birth date to exercise the controller’s validation path. |
| R | `model().attributeHasNoErrors("owner")` | N/A | Owner model | Verifies that owner state remains valid while the pet form fails validation. |
| R | `model().attributeHasErrors("pet")` | N/A | Pet model | Verifies that the submitted pet form contains validation errors. |
| R | `model().attributeHasFieldErrors("pet", "birthDate")` | N/A | Pet.birthDate field | Verifies that the birth date field is the specific validation target. |
| R | `model().attributeHasFieldErrorCode("pet", "birthDate", "typeMismatch.birthDate")` | N/A | Binding/validation message codes | Verifies that the binding layer returns the expected type mismatch error code. |
| R | `status().isOk()` | N/A | HTTP response | Verifies the controller returns a successful HTTP 200 response for the re-rendered form. |
| R | `view().name("pets/createOrUpdatePetForm")` | N/A | Thymeleaf view | Verifies the form is re-displayed for correction rather than redirecting away. |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Test: `PetControllerTests` | `PetControllerTests.processCreationFormWithInvalidBirthDate` -> `mockMvc.perform(post("/owners/{ownerId}/pets/new"))` -> controller validation path -> `ProcessCreationFormHasErrors.processCreationFormWithInvalidBirthDate` | `mockMvc.perform(...) [C] HTTP /owners/{ownerId}/pets/new` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] `(test setup)` (L147-L149)

> Prepares an invalid birth date value by moving one month ahead of the current date.

| # | Type | Code |
|---|------|------|
| 1 | SET | `LocalDate currentDate = LocalDate.now();` // capture the current date |
| 2 | SET | `String futureBirthDate = currentDate.plusMonths(1).toString();` // derive a future birth date string |

**Block 2** — [SEQUENCE] `(submit creation request)` (L151-L154)

> Sends a pet creation request containing a valid name and an invalid birth date value.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param("name", "Betty").param("birthDate", futureBirthDate))` // execute the HTTP POST request |

**Block 3** — [SEQUENCE] `(assert validation response)` (L154-L160)

> Confirms that the controller rejects the birth date, preserves the owner context, and re-renders the form.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `model().attributeHasNoErrors("owner")` // owner model remains valid |
| 2 | CALL | `model().attributeHasErrors("pet")` // pet model contains validation errors |
| 3 | CALL | `model().attributeHasFieldErrors("pet", "birthDate")` // birthDate is the failing field |
| 4 | CALL | `model().attributeHasFieldErrorCode("pet", "birthDate", "typeMismatch.birthDate")` // expected binding error code |
| 5 | CALL | `status().isOk()` // HTTP response remains 200 OK |
| 6 | CALL | `view().name("pets/createOrUpdatePetForm")` // same form view is displayed again |
| 7 | RETURN | `return;` // test completes |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `owner` | Domain object | Pet owner record used as the parent context for pet registration. |
| `pet` | Domain object | Animal being registered under an owner’s account. |
| `birthDate` | Field | Pet birth date submitted on the registration form. |
| `typeMismatch.birthDate` | Validation code | Binding error code indicating the birth date value could not be accepted in the required date type. |
| `createOrUpdatePetForm` | View | Thymeleaf form used to create or edit a pet. |
| `mockMvc` | Test utility | Spring MVC test client used to exercise controller behavior without starting a full server. |
| `LocalDate` | Technical type | Java date type used to generate the current date and a future date. |
| `TEST_OWNER_ID` | Test constant | Owner identifier used to target the form submission in the test fixture. |
| `name` | Field | Pet name submitted with the request to represent a normal valid attribute. |
| `POST /owners/{ownerId}/pets/new` | Endpoint | Pet creation request path for adding a new pet to an owner. |
