# (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 method verifies the controller behavior when a user submits the pet creation form with a birth date that cannot be bound correctly. In business terms, it simulates a customer entering a future date value for a pet registration request and confirms that the application rejects the invalid date input without corrupting the owner record. The method focuses on validation and form re-display behavior rather than persistence, ensuring the user remains on the pet creation screen with field-level feedback. It validates that the owner aggregate remains free of errors while the pet sub-form reports a binding problem specifically on the `birthDate` field. The overall role of the method is to protect the pet enrollment workflow from invalid temporal data and to guarantee a predictable user experience when input conversion fails.

The processing pattern is linear and assertion-driven: it prepares a future date string, submits the HTTP POST request, and then checks the model, response status, and view name. There are no conditional branches inside the test itself, but the business expectation is that the controller path branches into an error-handling form-rendering path instead of a successful create path. This makes the method a regression guard for validation behavior in the owner/pet creation flow.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["processCreationFormWithInvalidBirthDate()"])
    N1["Read current date using LocalDate.now()"]
    N2["Calculate futureBirthDate by adding one month and converting to String"]
    N3["Submit POST /owners/{ownerId}/pets/new with name=Betty and birthDate=futureBirthDate"]
    N4["Assert owner model has no errors"]
    N5["Assert pet model has errors"]
    N6["Assert pet.birthDate field has an error"]
    N7["Assert field error code equals typeMismatch.birthDate"]
    N8["Assert HTTP status is 200 OK"]
    N9["Assert returned view is pets/createOrUpdatePetForm"]
    END(["Return"])
    START --> N1
    N1 --> N2
    N2 --> N3
    N3 --> N4
    N4 --> N5
    N5 --> N6
    N6 --> N7
    N7 --> N8
    N8 --> N9
    N9 --> END
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | This is a test method with no formal parameters. It relies on the test fixture, including the configured `MockMvc` instance and the shared `TEST_OWNER_ID` constant from the enclosing test class, to execute the request against the pet creation endpoint. |

Instance fields / external state read by the method:
- `mockMvc`: Spring test client used to submit the form request and inspect the MVC response.
- `TEST_OWNER_ID`: identifier of the owner whose pet creation screen is being exercised.
- Spring MVC validation and binding infrastructure behind the `/owners/{ownerId}/pets/new` endpoint.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|-----------------------|
| R | `LocalDate.now()` | N/A | System clock | Reads the current business date to construct a future date that will trigger binding or validation failure. |
| R | `plusMonths(1)` | N/A | Date value | Derives a future birth date value that is intentionally invalid for the form submission scenario. |
| R | `toString()` | N/A | Date value | Converts the derived birth date into request text for the HTTP form parameter. |
| C | `mockMvc.perform(post(...))` | N/A | `/owners/{ownerId}/pets/new` request | Creates and submits a new pet registration request with invalid input to exercise the create flow's error handling branch. |
| R | `attributeHasNoErrors("owner")` | N/A | MVC model | Reads the owner model state to confirm the owner aggregate is unaffected by the invalid pet date input. |
| R | `attributeHasErrors("pet")` | N/A | MVC model | Reads the pet model binding result to confirm validation errors were recorded. |
| R | `attributeHasFieldErrors("pet", "birthDate")` | N/A | MVC model field state | Verifies that the invalid data is isolated to the birthDate field. |
| R | `attributeHasFieldErrorCode("pet", "birthDate", "typeMismatch.birthDate")` | N/A | MVC binding error code | Confirms the specific binding error code for the rejected birth date input. |
| R | `status().isOk()` | N/A | HTTP response | Reads the response status to confirm the form is re-rendered instead of redirected. |
| R | `view().name("pets/createOrUpdatePetForm")` | N/A | View resolution | Confirms the controller returned the pet form view for user correction. |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Test: `PetControllerTests` | `PetControllerTests.processCreationFormWithInvalidBirthDate` -> `MockMvc.perform` -> MVC endpoint `/owners/{ownerId}/pets/new` -> controller validation/rendering path | `view().name("pets/createOrUpdatePetForm") [R] MVC view` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] `(test setup)` (L146-L148)

> Prepares a future date string that should fail binding or validation when posted as a pet birth date.

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

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

> Sends the form submission to the pet creation endpoint with a valid name and an invalid future birth date.

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

**Block 3** — [SEQUENCE] `(model and view assertions)` (L154-L160)

> Verifies the controller responded by re-rendering the form and surfacing field-level validation feedback on the pet object only.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `andExpect(model().attributeHasNoErrors("owner"))` // owner model must remain clean |
| 2 | CALL | `andExpect(model().attributeHasErrors("pet"))` // pet binding result must contain errors |
| 3 | CALL | `andExpect(model().attributeHasFieldErrors("pet", "birthDate"))` // birthDate must be flagged |
| 4 | CALL | `andExpect(model().attributeHasFieldErrorCode("pet", "birthDate", "typeMismatch.birthDate"))` // verify conversion error code |
| 5 | CALL | `andExpect(status().isOk())` // form is re-displayed instead of redirected |
| 6 | CALL | `andExpect(view().name("pets/createOrUpdatePetForm"))` // return to the pet form view |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `MockMvc` | Technical component | Spring test client that simulates browser requests to MVC controllers without starting a full browser. |
| `owner` | Domain object | The customer account that owns pets in the PetClinic workflow. |
| `pet` | Domain object | The animal being registered under an owner. |
| `birthDate` | Field | The pet's date of birth used for registration and validation. |
| `typeMismatch.birthDate` | Validation code | Spring binding error code used when a submitted value cannot be converted to the expected date type. |
| `createOrUpdatePetForm` | View | Thymeleaf page used to capture or correct pet details. |
| `POST /owners/{ownerId}/pets/new` | HTTP endpoint | Controller route used to create a new pet for an existing owner. |
| `TEST_OWNER_ID` | Test fixture constant | Predefined owner identifier used by the controller test to target a known owner record. |
| `LocalDate` | Java time type | Java date type representing a calendar date without time zone information. |
| `plusMonths(1)` | Date operation | Produces a future date one month ahead of the current date. |
| `binding error` | Technical/business concept | A form submission problem where user input cannot be mapped to the expected field type. |
| `validation error` | Business term | A rule violation that prevents successful processing of the submitted form. |
