# (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 method is a controller-level test that verifies the Pet creation form rejects an invalid birth date submitted through the owner/pet registration flow. In business terms, it exercises the user experience for adding a new pet to an owner record and confirms that a future birth date is treated as invalid input rather than being accepted into the pet profile. The method does not execute business persistence logic itself; instead, it validates that the web layer returns the creation form view with field-level validation feedback when the birth date cannot be converted into the expected date type.

The test implements a straightforward request/response validation pattern: it prepares a future date value, submits the form via HTTP POST, and then asserts the model, validation errors, HTTP status, and view name. This makes the method a guardrail for form binding and data validation rules in the larger PetController flow, ensuring that bad date input is handled consistently before any downstream owner/pet state would be updated. The only business branch in scope is the invalid birth-date path, which should leave the owner model error-free, flag the pet model with a birthDate field error, and re-display the pet form for correction.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["processCreationFormWithInvalidBirthDate() test"])
    SET_DATE["Set currentDate = LocalDate.now()"]
    SET_FUTURE["Set futureBirthDate = currentDate.plusMonths(1).toString()"]
    SUBMIT["Submit POST /owners/{ownerId}/pets/new with name=Betty and birthDate=futureBirthDate"]
    ASSERT_OWNER["Assert owner model has no errors"]
    ASSERT_PET_ERRORS["Assert pet model has errors"]
    ASSERT_BIRTHDATE["Assert pet.birthDate has field errors"]
    ASSERT_CODE["Assert error code = typeMismatch.birthDate"]
    ASSERT_STATUS["Assert response status is OK"]
    ASSERT_VIEW["Assert view = pets/createOrUpdatePetForm"]
    END_NODE(["Return / Next test"])

    START --> SET_DATE --> SET_FUTURE --> SUBMIT --> ASSERT_OWNER --> ASSERT_PET_ERRORS --> ASSERT_BIRTHDATE --> ASSERT_CODE --> ASSERT_STATUS --> ASSERT_VIEW --> END_NODE
```

## 3. Parameter Analysis

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

External state and instance fields read by the method:
- `mockMvc`: Spring MVC test client used to submit the owner/pet creation request.
- `TEST_OWNER_ID`: Owner identifier used to target the pet creation endpoint.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `LocalDate.now()` | N/A | N/A | Reads the current system date to generate a future birth date for validation testing |
| R | `plusMonths(1)` | N/A | N/A | Computes a future calendar value to simulate invalid user input |
| C | `mockMvc.perform(post(...))` | N/A | `PetController` request handling | Creates a test HTTP submission to the pet creation endpoint and triggers server-side form binding |
| R | `attributeHasNoErrors("owner")` | N/A | `owner` model | Verifies the owner model remains valid during failed pet form submission |
| R | `attributeHasErrors("pet")` | N/A | `pet` model | Verifies validation errors are present on the pet form model |
| R | `attributeHasFieldErrors("pet", "birthDate")` | N/A | `pet.birthDate` | Verifies the birthDate field is rejected as invalid |
| R | `attributeHasFieldErrorCode("pet", "birthDate", "typeMismatch.birthDate")` | N/A | `pet.birthDate` | Verifies the binding error code associated with the invalid date conversion |
| R | `status().isOk()` | N/A | HTTP response | Verifies the request is handled successfully and returns a validation response |
| R | `view().name("pets/createOrUpdatePetForm")` | N/A | View template | Verifies the user is returned to the pet creation/edit form for correction |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Controller test: `PetControllerTests` | `PetControllerTests.processCreationFormWithInvalidBirthDate` -> `mockMvc.perform(post("/owners/{ownerId}/pets/new"))` -> `PetController.processCreationForm` -> `ProcessCreationFormHasErrors.processCreationFormWithInvalidBirthDate` | `PetController form binding [R] pet.birthDate validation` |

## 6. Per-Branch Detail Blocks

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

> Prepares a future birth date value that should violate the pet birth-date binding rule.

| # | Type | Code |
|---|------|------|
| 1 | SET | `LocalDate currentDate = LocalDate.now();` |
| 2 | SET | `String futureBirthDate = currentDate.plusMonths(1).toString();` |

**Block 2** — [SEQUENCE] `(submit invalid creation request)` (L150)

> Sends the pet creation request with a valid name and an intentionally invalid future birth date.

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

**Block 3** — [SEQUENCE] `(validate response and model)` (L153-L159)

> Confirms the controller rejects the birth date, preserves the owner model, exposes pet validation errors, and re-renders the form.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `.andExpect(model().attributeHasNoErrors("owner"))` |
| 2 | CALL | `.andExpect(model().attributeHasErrors("pet"))` |
| 3 | CALL | `.andExpect(model().attributeHasFieldErrors("pet", "birthDate"))` |
| 4 | CALL | `.andExpect(model().attributeHasFieldErrorCode("pet", "birthDate", "typeMismatch.birthDate"))` |
| 5 | CALL | `.andExpect(status().isOk())` |
| 6 | CALL | `.andExpect(view().name("pets/createOrUpdatePetForm"))` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `owner` | Model attribute | The pet owner record currently being maintained in the user session or request context |
| `pet` | Model attribute | The pet being created for the selected owner |
| `birthDate` | Field | The pet's date of birth, used to validate the pet profile form |
| `typeMismatch.birthDate` | Validation code | Binding error indicating the submitted value could not be converted into a valid date |
| `MockMvc` | Technical term | Spring test client used to simulate HTTP requests against the MVC layer |
| `PetController` | Controller | Web controller that serves and processes pet-related owner workflows |
| `/owners/{ownerId}/pets/new` | Endpoint | Pet creation form submission URL for a specific owner |
| `createOrUpdatePetForm` | View | Form page used to create or edit a pet record |
| `LocalDate` | Technical term | Java date type used to generate a future date for invalid-input testing |
| `ownerId` | Path variable | Identifier for the owner whose pet is being created |
| `TEST_OWNER_ID` | Test constant | Fixed owner identifier used by the test fixture |
| `TEST_PET_ID` | Test constant | Fixed pet identifier used elsewhere in the same test class |
| `plusMonths` | Date operation | Adds one month to the current date to create an intentionally future birth date |
| `attributeHasNoErrors` | Assertion | Verifies a model attribute remains free of validation errors |
| `attributeHasErrors` | Assertion | Verifies a model attribute contains validation errors |
| `attributeHasFieldErrors` | Assertion | Verifies a specific field on a model attribute has validation errors |
| `attributeHasFieldErrorCode` | Assertion | Verifies the exact validation error code attached to a field |
