# (DD46) Business Logic — PetTypeFormatterTests.testPrint() [7 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `org.springframework.samples.petclinic.owner.PetTypeFormatterTests` |
| Layer | Test / Utility Support |
| Module | `owner` (Package: `org.springframework.samples.petclinic.owner`) |

## 1. Role

### PetTypeFormatterTests.testPrint()

This test method verifies the print-side behavior of the `PetTypeFormatter` contract for a single `PetType` business object. It prepares a minimal domain fixture, assigns the pet type name `Hamster`, and then confirms that the formatter returns the same business label when asked to render the object for display in the UI. The method does not perform persistence, routing, or repository access itself; instead, it validates that the presentation-layer transformer preserves the domain name exactly when the `PetType` already has a non-null name.

From a business perspective, this is a quality gate for user-facing dropdown and form rendering. In the larger system, `PetTypeFormatter` is the shared conversion component that allows Spring MVC to display and bind pet type values consistently across screens. This test protects the display contract by ensuring that the formatter does not alter a valid pet type name during string conversion. The method has no branching logic of its own; its only business outcome is the assertion that the formatted output equals the source name.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["testPrint()"])
    CREATE_PETTYPE["Create PetType test fixture"]
    SET_NAME["Set petType.name = \"Hamster\""]
    CALL_PRINT["Call petTypeFormatter.print(petType, Locale.ENGLISH)"]
    GET_NAME["Read petType.getName()"]
    NULL_CHECK{"Is name null?"}
    RETURN_NAME["Return \"Hamster\""]
    RETURN_NULL["Return \"<null>\""]
    ASSERT_EQUAL["Assert printed value equals \"Hamster\""]
    END_NODE(["End"])
    START --> CREATE_PETTYPE
    CREATE_PETTYPE --> SET_NAME
    SET_NAME --> CALL_PRINT
    CALL_PRINT --> GET_NAME
    GET_NAME --> NULL_CHECK
    NULL_CHECK -->|No| RETURN_NAME
    NULL_CHECK -->|Yes| RETURN_NULL
    RETURN_NAME --> ASSERT_EQUAL
    RETURN_NULL --> ASSERT_EQUAL
    ASSERT_EQUAL --> END_NODE
```

## 3. Parameter Analysis

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

This test method accepts no parameters. It relies on instance fields from the test class, especially the injected `petTypeFormatter` and the mock `types` repository used for constructor setup in the surrounding test fixture.

## 4. CRUD Operations / Called Services

Analyze all method calls within this method and classify each as a CRUD operation.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `petTypeFormatter.print` | - | `PetType` | Reads the pet type name and converts it into a display string for UI presentation. |

### Notes
- The test method itself performs no database CRUD.
- `print(PetType, Locale)` is a pure read-style transformation: it accesses `petType.getName()` and returns either the existing name or the fallback string `"<null>"`.
- The related repository method `findPetTypes()` belongs to the formatter's `parse()` path, not to `testPrint()`.

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Test: `PetTypeFormatterTests` | `PetTypeFormatterTests.testPrint` | `petTypeFormatter.print [R] PetType` |

### Downstream dependency observed from the tested method
- `PetTypeFormatter.print(PetType, Locale)` -> `petType.getName()` -> returns the current pet type name or `"<null>"` when the name is absent.

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENTIAL] (L56-L60)

> Creates a test `PetType`, assigns a known name, invokes the formatter, and validates the output.

| # | Type | Code |
|---|------|------|
| 1 | SET | `PetType petType = new PetType();` // create test domain object |
| 2 | EXEC | `petType.setName("Hamster");` // prepare business label for display |
| 3 | CALL | `this.petTypeFormatter.print(petType, Locale.ENGLISH);` // render the pet type name |
| 4 | SET | `String petTypeName = ...;` // capture formatted output |
| 5 | CALL | `assertThat(petTypeName).isEqualTo("Hamster");` // verify display contract |

**Block 1.1** — [FORMATTER PRINT PATH] (L59)

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `petType.getName();` // read domain name from the PetType instance |
| 2 | RETURN | `return name != null ? name : "<null>";` // return business label or fallback |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `PetType` | Domain object | Master data record representing a category of pet such as hamster, bird, or cat. |
| `PetTypeFormatter` | Formatter | Spring MVC converter that renders and parses `PetType` values for forms and screens. |
| `print` | Method | Conversion operation that turns a domain object into a user-facing string. |
| `parse` | Method | Conversion operation that resolves a submitted string back into a matching domain object. |
| `Locale.ENGLISH` | Locale setting | English language formatting context used during the test invocation. |
| `Hamster` | Business term | Sample pet type name used to verify string rendering. |
| `<null>` | Fallback value | Default display string returned by the formatter when a pet type name is missing. |
| `PetTypeRepository` | Repository | Data access interface that retrieves pet type master records from the database. |
| `findPetTypes` | Repository query | Ordered retrieval of all pet type records for formatter parsing. |
| `Formatter` | Technical interface | Spring abstraction for converting between text and typed domain values. |
