A RESTful API for managing insurance clients and contracts, built with Spring Boot and H2 database.
- Java 21 or higher
- Maven 3.9 or higher
-
Clone the repository:
git clone git@github.com:jubbakka/API-Factory.git cd api-factory -
Start the application:
./mvnw spring-boot:run
-
Access the API:
- Base URL:
http://localhost:8080 - H2 Console:
http://localhost:8080/h2-console - Database URL:
jdbc:h2:~/<YOUR-LOCAL-PATH>/api-factory/data/localdb - Username:
admin - Password:
Admin@123
- Base URL:
POST /api/clients/person- Create a person clientPOST /api/clients/company- Create a company clientGET /api/clients- Retrieve all active clientsGET /api/clients/{id}- Get client details by IDPUT /api/clients/person/{id}- Update a person clientPUT /api/clients/company/{id}- Update a company clientDELETE /api/clients/{id}- Soft delete a clientGET /api/clients/{id}/contracts- List all contracts for a clientGET /api/clients/{id}/contracts/sum- Calculate total contract value
POST /api/contracts- Create a new contractPUT /api/contracts/{id}/cost- Update contract cost
Execute all tests with:
./mvnw testImport API_Factory_Postman_Collection.json into Postman for easy API testing.
The application has comprehensive test coverage:
- 108 unit tests across 6 test classes
- Service/model tests for business logic and validation
- Tests cover validation, duplicate detection, soft deletes, and error handling (404/409/500)
Seed data includes:
- 50 clients (37 person, 13 company)
- 175 contracts with varied costs and date scenarios
- Mix of active and deleted clients for testing soft deletes
To enable seed data, set app.seed-db=true in application.properties (disabled by default).
To meet audit and compliance needs:
- Deleting a client sets status to
DELETEDwith a timestamp - Active contracts are closed (endDate = current date)
- API filters show only ACTIVE clients
- Data persists in the database for historical/legal purposes
Separate endpoints for person and company clients due to distinct validation:
- Person: Requires birth date, no company identifier
- Company: Requires identifier (format: XXX-XXX), no birth date
- Uses inheritance with a shared base Client entity
- Production: H2 file-based database for data persistence
- Testing: In-memory H2 database
- Seed data only populates empty databases
A global exception handler ensures consistent error responses:
- 400: Validation errors
- 404: Resource not found
- 409: Conflicts (e.g., duplicate email/phone)
- 500: Internal server errors
- Uses Bean Validation with
@Validannotations - Business rules enforced in the service layer
- Custom error messages for better user experience
- Production: H2 file database at
./data/localdb - Testing: In-memory H2 database
- H2 Console: Available at
/h2-console
- Default: Production mode with file-based database
- Test: In-memory database for faster testing
Client (abstract base)
├── Person (birthDate)
└── Company (identifier)
Contract
├── client (foreign key)
├── startDate/endDate
├── costAmount
└── updatedDate (internal)
- Email format validation
- Swiss phone number pattern validation
- Company identifier format (XXX-XXX)
- Unique constraints on email and phone
- Birth dates must be in the past
- Contract costs must be positive