Transactional ecommerce backend for handling product catalog, shopping cart, order creation, inventory reservation, and mock payment confirmation.
- User registration and JWT authentication
- Role-based access control for customers and admins
- Product catalog with admin-only product management
- Shopping cart with quantity management
- Transactional order creation from cart
- Inventory reservation and stock restoration on cancellation
- Mock payment confirmation with idempotency support
- Global error responses with validation details
- OpenAPI documentation with Swagger UI
- Integration tests with PostgreSQL Testcontainers
- Java 21 + Spring Boot 4
- PostgreSQL + Flyway
- Docker / Docker Compose
- JUnit 5 + Testcontainers
- OpenAPI / Swagger UI
- Docker Desktop
- Java 21
- Git
src/main/java/com/ktrubilo9/orderflow
├── auth # registration, login, JWT generation and verification
├── cart # customer cart and cart items
├── common # shared exceptions and API error handling
├── config # security, password encoder and OpenAPI configuration
├── inventory # stock validation, reservation and release logic
├── order # orders, order items and order state transitions
├── payment # mock payment flow and idempotency handling
├── product # product catalog and admin product management
└── user # user entity, roles and repository
Create an environment file from the example:
cp .env.example .envStart the application and database:
docker compose up --buildThe API should be available at:
http://localhost:8080
Swagger UI should be available at:
http://localhost:8080/swagger-ui/index.html
Health check endpoint:
http://localhost:8080/actuator/health
Example variables used by the application:
POSTGRES_DB=orderflow
POSTGRES_USER=orderflow
POSTGRES_PASSWORD=orderflow
POSTGRES_PORT=5432
SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/orderflow
SPRING_DATASOURCE_USERNAME=orderflow
SPRING_DATASOURCE_PASSWORD=orderflow
JWT_SECRET=replace-with-a-strong-random-secret
JWT_EXPIRATION_SECONDS=3600
SERVER_PORT=8080Run the full test suite:
./mvnw testThe tests use Testcontainers, so Docker must be running.
The test suite covers:
- authentication flow
- JWT verification
- request validation
- product management permissions
- cart operations
- order creation from cart
- stock reservation
- insufficient stock handling
- order cancellation and stock restoration
- mock payment flow
- payment idempotency
- repository behavior
Authentication:
POST /api/auth/registerPOST /api/auth/loginGET /api/auth/me
Products:
GET /api/productsGET /api/products/{id}POST /api/products- admin onlyPUT /api/products/{id}- admin onlyPATCH /api/products/{id}/deactivate- admin only
Cart:
GET /api/cartPOST /api/cart/itemsPATCH /api/cart/items/{itemId}DELETE /api/cart/items/{itemId}DELETE /api/cart/items
Orders:
GET /api/ordersPOST /api/ordersGET /api/orders/{orderId}GET /api/orders/{orderId}/paymentsPATCH /api/orders/{orderId}/cancel
Payments:
GET /api/paymentsPOST /api/payments
Flyway seeds several demo products:
KEYBOARD-001MOUSE-001MONITOR-001
This allows the customer flow to be tested immediately after startup.
- Payments are mocked and always create a successful payment if the order is payable.
- Public registration creates customer accounts. Admin-only product endpoints require a user with the
ADMINrole. - Product stock is reserved when an order is created.
- Cancelling a pending order restores stock.
- Reusing the same payment idempotency key for the same order returns the existing payment.
- Reusing an idempotency key for another order returns a conflict.