A high-performance, production-ready checkout service for supermarket pricing with support for special offers, bulk discounts, and flexible pricing strategies.
- Flexible Pricing Strategies: Supports standard pricing and special offers (e.g., "3 for $1.30")
- RESTful API: Clean, well-documented REST API with OpenAPI/Swagger documentation
- Production Ready: Health checks, metrics, structured logging, and comprehensive error handling
- Containerized: Docker and Docker Compose support for easy deployment
- Fully Tested: Comprehensive unit and integration tests with TestContainers
- Modern Stack: Spring Boot 3.4, Java 21, MongoDB
| Component | Technology |
|---|---|
| Language | Java 21 (LTS) |
| Framework | Spring Boot 3.4 |
| Database | MongoDB 7.0 |
| API Docs | OpenAPI 3.0 (SpringDoc) |
| Testing | JUnit 5, TestContainers, Mockito |
| Build | Maven 3.9+ |
| Container | Docker |
| CI/CD | GitHub Actions |
src/main/java/com/supermarket/checkout/
├── api/ # REST Controllers & DTOs
│ ├── dto/ # Request/Response objects
│ ├── CheckoutController # Checkout endpoint
│ └── ItemController # Item catalog CRUD
├── config/ # Configuration classes
├── domain/ # Domain models
│ ├── entity/ # MongoDB entities
│ └── model/ # Value objects
├── exception/ # Exception handling
├── pricing/ # Pricing strategies
│ ├── PricingEngine # Strategy selector
│ ├── PricingStrategy # Strategy interface
│ ├── StandardPricing # Default pricing
│ └── SpecialOffer # Bulk discount pricing
├── repository/ # Data access layer
└── service/ # Business logic
- Java 21+
- Maven 3.9+
- Docker & Docker Compose (for containerized deployment)
-
Clone the repository
git clone <repository-url> cd checkout
-
Run with Docker Compose (recommended)
docker-compose up -d
-
Or run with Maven (requires local MongoDB)
./mvnw spring-boot:run
The service will be available at http://localhost:8080
Once running, access the Swagger UI at:
- Swagger UI: http://localhost:8080/swagger-ui.html
- OpenAPI Spec: http://localhost:8080/api-docs
Calculate the total price for a basket of items.
POST /api/v1/checkout
Content-Type: application/json
{
"items": ["A", "A", "A", "B", "B", "C", "D"]
}Response:
{
"items": [
{
"sku": "A",
"name": "Apple",
"quantity": 3,
"unitPrice": 0.50,
"subtotal": 1.30,
"specialOfferApplied": true
}
],
"total": 2.10,
"totalItems": 7
}| Method | Endpoint | Description |
|---|---|---|
| GET | /api/v1/items |
List all items |
| GET | /api/v1/items/{sku} |
Get item by SKU |
| POST | /api/v1/items |
Create new item |
| PUT | /api/v1/items/{sku} |
Update item |
| DELETE | /api/v1/items/{sku} |
Delete item |
| Endpoint | Description |
|---|---|
/actuator/health |
Health check |
/actuator/info |
Application info |
/actuator/metrics |
Application metrics |
/actuator/prometheus |
Prometheus metrics |
Default pricing configuration:
| SKU | Name | Unit Price | Special Offer |
|---|---|---|---|
| A | Apple | $0.50 | 3 for $1.30 |
| B | Banana | $0.30 | 2 for $0.45 |
| C | Cherry | $0.20 | - |
| D | Date | $0.15 | - |
For items with special offers:
total = (quantity / offerQty) * offerPrice + (quantity % offerQty) * unitPrice
Example: 5 Apples (3 for $1.30, normally $0.50 each)
= (5 / 3) * $1.30 + (5 % 3) * $0.50
= 1 * $1.30 + 2 * $0.50
= $2.30
| Variable | Description | Default |
|---|---|---|
SERVER_PORT |
Server port | 8080 |
MONGODB_URI |
MongoDB connection string | mongodb://localhost:27017/checkout |
SPRING_PROFILES_ACTIVE |
Active profile | default |
default- Development with local MongoDBprod- Production configurationtest- Test configuration
# Compile
./mvnw clean compile
# Run tests
./mvnw test
# Run integration tests
./mvnw verify
# Package
./mvnw package
# Build Docker image
./mvnw spring-boot:build-image# Unit tests only
./mvnw test
# Integration tests only
./mvnw verify -DskipUnitTests
# All tests with coverage
./mvnw verify
# View coverage report
open target/site/jacoco/index.htmlThe project includes:
- JaCoCo for code coverage (80% line coverage target)
- ArchUnit for architecture tests
- OWASP Dependency Check for vulnerability scanning
# Build image
docker build -t supermarket/checkout-service:latest .
# Run container
docker run -p 8080:8080 \
-e MONGODB_URI=mongodb://host.docker.internal:27017/checkout \
supermarket/checkout-service:latest# Start all services
docker-compose up -d
# Start with dev tools (includes Mongo Express)
docker-compose --profile dev up -d
# View logs
docker-compose logs -f app
# Stop all services
docker-compose downThe application is container-ready with:
- Health check endpoints (
/actuator/health/liveness,/actuator/health/readiness) - Prometheus metrics endpoint (
/actuator/prometheus) - Graceful shutdown support
- Resource-efficient JVM settings
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.