|
| 1 | +# Sentinel: Chain of Responsibility Risk Engine 🛡️ |
| 2 | + |
| 3 | +> A modular banking transaction validation system demonstrating the **Chain of Responsibility** design pattern to achieve loose coupling and fail-fast processing. |
| 4 | +
|
| 5 | +## 📖 Project Overview |
| 6 | +**Sentinel** is a financial middleware service designed to validate transactions through a dynamic pipeline of security checks. |
| 7 | + |
| 8 | +Instead of a monolithic `validateTransaction()` method with complex nested `if-else` logic, this project utilizes the **Chain of Responsibility** pattern. Each validation step (Sanity, Blacklist, Fraud) is an independent component that can be added, removed, or reordered without modifying the core service logic. |
| 9 | + |
| 10 | +## 🏗️ Design Pattern: Chain of Responsibility |
| 11 | +The application routes a request through a chain of handlers. If any handler fails the request, the process stops immediately (Fail-Fast). |
| 12 | + |
| 13 | +```mermaid |
| 14 | +graph LR |
| 15 | + A[Request] --> B(Sanity Check) |
| 16 | + B -- Pass --> C(Blacklist Check) |
| 17 | + C -- Pass --> D(Fraud Check) |
| 18 | + D -- Pass --> E[Approved ✅] |
| 19 | + B -- Fail --> F[Rejected ❌] |
| 20 | + C -- Fail --> F |
| 21 | + D -- Fail --> F |
| 22 | +``` |
| 23 | +## 🗝️ Key Components |
| 24 | +1. **`RiskHandler` (Abstract Handler):** Defines the contract and manages the link to the `next` handler in the chain. |
| 25 | +2. **`SanityCheckHandler`:** The first line of defense; validates basic constraints (e.g., Amount must be > 0). |
| 26 | +3. **`BlacklistHandler`:** Simulates a security check against a database of banned users. |
| 27 | +4. **`LargeTransactionHandler`:** Applies business logic to flag or reject transactions exceeding specific financial thresholds. |
| 28 | +5. **`TransactionService`:** Orchestrates the chain creation using Spring's Dependency Injection (`@PostConstruct`). |
| 29 | + |
| 30 | +## 🛠️ Tech Stack |
| 31 | +* **Core:** Java 21, Spring Boot 3.x |
| 32 | +* **Architecture:** Chain of Responsibility Design Pattern |
| 33 | +* **Build Tool:** Maven |
| 34 | +* **Testing:** Postman / cURL |
| 35 | + |
| 36 | +## 🏃 How to Run |
| 37 | + |
| 38 | +1. **Clone the repository:** |
| 39 | + ```bash |
| 40 | + git clone [https://github.com/yourusername/sentinel.git](https://github.com/yourusername/sentinel.git) |
| 41 | + ``` |
| 42 | +2. **Navigate to the project directory:** |
| 43 | + ```bash |
| 44 | + cd sentinel |
| 45 | + ``` |
| 46 | +3. **Run the application:** |
| 47 | + ```bash |
| 48 | + mvn spring-boot:run |
| 49 | + ``` |
| 50 | +4. The server will start on `http://localhost:8080`. |
| 51 | + |
| 52 | +## 🔌 API Endpoints (Testing Guide) |
| 53 | + |
| 54 | +### 1. Successful Transaction (Happy Path) |
| 55 | +* **Request:** `POST /api/transactions/pay?user=John&amount=500` |
| 56 | +* **Result:** `Transaction Approved!` |
| 57 | +* **Console Logs:** |
| 58 | + ```text |
| 59 | + ✅ Sanity Check Passed |
| 60 | + ✅ Blacklist Check Passed |
| 61 | + ✅ Limit Check Passed |
| 62 | + ``` |
| 63 | + |
| 64 | +### 2. Validation Failure (Fail-Fast Demo) |
| 65 | +* **Request:** `POST /api/transactions/pay?user=John&amount=-100` |
| 66 | +* **Result:** `Transaction Rejected.` |
| 67 | +* **Console Logs:** |
| 68 | + ```text |
| 69 | + ❌ Validation Failed: Amount must be positive. |
| 70 | + (Processing stopped here. Blacklist/Limit checks were NEVER executed) |
| 71 | + ``` |
| 72 | + |
| 73 | +### 3. Security Failure (Blacklist Demo) |
| 74 | +* **Request:** `POST /api/transactions/pay?user=BANNED_USER&amount=500` |
| 75 | +* **Result:** `Transaction Rejected.` |
| 76 | +* **Console Logs:** |
| 77 | + ```text |
| 78 | + ✅ Sanity Check Passed |
| 79 | + ❌ Validation Failed: Account is blacklisted. |
| 80 | + ``` |
| 81 | +--- |
| 82 | +Created by Gautam Jain to demonstrate Design Pattern implementation in Spring Boot. |
0 commit comments