Skip to content

Commit 3ce3ed2

Browse files
committed
Add Train Service and Payment Service integration with test cases
- Create Feign clients for Train Service and Payment Service - Integrate Train Service for real-time fare calculation - Add automatic payment/refund processing for fare differences - Implement circuit breaker protection for service calls - Create comprehensive unit tests (12 test cases) - Create integration tests for all modification endpoints (6 test cases) - Add fallback mechanisms for service unavailability - Complete documentation for integrations and testing
1 parent e0f9fc0 commit 3ce3ed2

8 files changed

Lines changed: 1365 additions & 9 deletions

File tree

Lines changed: 336 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,336 @@
1+
# 🎫 Booking Modifications - Integration & Testing Summary
2+
3+
## ✅ Implementation Complete
4+
5+
All three requested features have been successfully implemented:
6+
7+
1.**Test Cases Created** - Unit and integration tests
8+
2.**Train Service Integration** - Fare calculation from Train Service
9+
3.**Payment Processing** - Automatic payment/refund for fare differences
10+
11+
---
12+
13+
## 🔗 Service Integrations
14+
15+
### 1. Train Service Integration
16+
17+
**Feign Client**: `TrainServiceClient`
18+
- **Location**: `irctc-booking-service/src/main/java/com/irctc/booking/client/TrainServiceClient.java`
19+
- **Fallback**: `TrainServiceClientFallback.java`
20+
21+
**Features**:
22+
- Get train information by ID or train number
23+
- Calculate fare for routes
24+
- Check seat availability
25+
- Automatic fallback when Train Service is unavailable
26+
27+
**Usage in Modification Service**:
28+
```java
29+
// Fetch train information and base fare
30+
TrainServiceClient.TrainResponse train = trainServiceClient.getTrainById(trainId);
31+
BigDecimal newFare = BigDecimal.valueOf(train.getBaseFare());
32+
```
33+
34+
**Benefits**:
35+
- Real-time fare calculation
36+
- Accurate pricing for modifications
37+
- Graceful degradation with fallback
38+
39+
---
40+
41+
### 2. Payment Service Integration
42+
43+
**Feign Client**: `PaymentServiceClient`
44+
- **Location**: `irctc-booking-service/src/main/java/com/irctc/booking/client/PaymentServiceClient.java`
45+
- **Fallback**: `PaymentServiceClientFallback.java`
46+
47+
**Features**:
48+
- Process payment for fare differences
49+
- Process refunds for downgrades
50+
- Get payment history by booking ID
51+
- Circuit breaker protection
52+
53+
**Usage in Modification Service**:
54+
```java
55+
// Process payment for positive amount
56+
PaymentServiceClient.PaymentRequest paymentRequest = new PaymentServiceClient.PaymentRequest();
57+
paymentRequest.setBookingId(bookingId);
58+
paymentRequest.setAmount(totalAmount);
59+
PaymentServiceClient.PaymentResponse response = paymentServiceClient.processPayment(paymentRequest);
60+
61+
// Process refund for negative amount
62+
PaymentServiceClient.RefundRequest refundRequest = new PaymentServiceClient.RefundRequest();
63+
refundRequest.setPaymentId(paymentId);
64+
refundRequest.setRefundAmount(refundAmount);
65+
PaymentServiceClient.PaymentResponse refundResponse = paymentServiceClient.processRefund(refundRequest);
66+
```
67+
68+
**Benefits**:
69+
- Automatic payment processing
70+
- Automatic refund processing
71+
- Transaction tracking
72+
- Circuit breaker for resilience
73+
74+
---
75+
76+
## 🧪 Test Coverage
77+
78+
### Unit Tests
79+
80+
**File**: `BookingModificationServiceTest.java`
81+
- ✅ Test modification options retrieval
82+
- ✅ Test date modification with Train Service integration
83+
- ✅ Test seat upgrade with payment processing
84+
- ✅ Test passenger modification (add/remove)
85+
- ✅ Test route change
86+
- ✅ Test validation errors
87+
- ✅ Test business rule violations
88+
- ✅ Test payment/refund processing
89+
90+
**Coverage**:
91+
- All modification types tested
92+
- Error scenarios covered
93+
- Service integration mocked
94+
- Business logic validated
95+
96+
### Integration Tests
97+
98+
**File**: `BookingModificationControllerTest.java`
99+
- ✅ Test GET `/api/bookings/{id}/modification-options`
100+
- ✅ Test PUT `/api/bookings/{id}/modify-date`
101+
- ✅ Test PUT `/api/bookings/{id}/upgrade-seat`
102+
- ✅ Test PUT `/api/bookings/{id}/modify-passengers`
103+
- ✅ Test PUT `/api/bookings/{id}/change-route`
104+
- ✅ Test validation error handling
105+
106+
**Coverage**:
107+
- All endpoints tested
108+
- Request/response validation
109+
- HTTP status codes verified
110+
- JSON serialization/deserialization
111+
112+
---
113+
114+
## 🔄 Payment Flow
115+
116+
### Payment Processing Flow
117+
118+
```
119+
1. User requests modification
120+
121+
2. Calculate fare difference + modification charge
122+
123+
3. If totalAmount > 0:
124+
→ Process payment via Payment Service
125+
→ Update booking with new fare
126+
→ Return modification response with payment status
127+
128+
4. If totalAmount < 0:
129+
→ Get existing payment for booking
130+
→ Process refund via Payment Service
131+
→ Update booking with new fare
132+
→ Return modification response with refund status
133+
134+
5. If totalAmount = 0:
135+
→ No payment required
136+
→ Update booking
137+
→ Return modification response
138+
```
139+
140+
### Refund Processing Flow
141+
142+
```
143+
1. Modification results in fare reduction
144+
145+
2. Calculate refund amount (absolute value of negative totalAmount)
146+
147+
3. Get last payment for booking
148+
149+
4. Process refund via Payment Service
150+
151+
5. Update payment status to REFUNDED
152+
153+
6. Return refund transaction ID
154+
```
155+
156+
---
157+
158+
## 🚀 Features Added
159+
160+
### 1. Train Service Client
161+
- ✅ Feign client with fallback
162+
- ✅ Train information retrieval
163+
- ✅ Fare calculation integration
164+
- ✅ Seat availability checking
165+
166+
### 2. Payment Service Client
167+
- ✅ Feign client with fallback
168+
- ✅ Payment processing
169+
- ✅ Refund processing
170+
- ✅ Payment history retrieval
171+
172+
### 3. Enhanced Modification Service
173+
- ✅ Train Service integration for fare calculation
174+
- ✅ Payment Service integration for payment/refund
175+
- ✅ Circuit breaker protection
176+
- ✅ Comprehensive error handling
177+
- ✅ Fallback mechanisms
178+
179+
### 4. Test Suite
180+
- ✅ Unit tests for service layer
181+
- ✅ Integration tests for controller layer
182+
- ✅ Mock service clients
183+
- ✅ Validation testing
184+
- ✅ Error scenario testing
185+
186+
---
187+
188+
## 📊 Test Results
189+
190+
### Unit Tests
191+
- **Total Tests**: 12
192+
- **Coverage**: Service layer business logic
193+
- **Status**: ✅ All tests passing
194+
195+
### Integration Tests
196+
- **Total Tests**: 6
197+
- **Coverage**: API endpoints
198+
- **Status**: ✅ All tests passing
199+
200+
---
201+
202+
## 🔧 Configuration
203+
204+
### Feign Client Configuration
205+
206+
Add to `application.yml`:
207+
```yaml
208+
feign:
209+
client:
210+
config:
211+
irctc-train-service:
212+
connectTimeout: 5000
213+
readTimeout: 10000
214+
irctc-payment-service:
215+
connectTimeout: 5000
216+
readTimeout: 10000
217+
circuitbreaker:
218+
enabled: true
219+
```
220+
221+
### Circuit Breaker Configuration
222+
223+
Add to `application.yml`:
224+
```yaml
225+
resilience4j:
226+
circuitbreaker:
227+
instances:
228+
payment-service:
229+
registerHealthIndicator: true
230+
slidingWindowSize: 10
231+
minimumNumberOfCalls: 5
232+
permittedNumberOfCallsInHalfOpenState: 3
233+
automaticTransitionFromOpenToHalfOpenEnabled: true
234+
waitDurationInOpenState: 10s
235+
failureRateThreshold: 50
236+
eventConsumerBufferSize: 10
237+
```
238+
239+
---
240+
241+
## 📝 API Examples
242+
243+
### 1. Get Modification Options
244+
```bash
245+
GET /api/bookings/1/modification-options
246+
247+
Response:
248+
{
249+
"bookingId": 1,
250+
"currentStatus": "CONFIRMED",
251+
"canModifyDate": true,
252+
"canUpgradeSeat": true,
253+
"canChangeRoute": true,
254+
"canModifyPassengers": true,
255+
"modificationCharges": {
256+
"dateChange": 200.00,
257+
"seatUpgrade": 100.00,
258+
"routeChange": 300.00,
259+
"passengerModification": 150.00
260+
}
261+
}
262+
```
263+
264+
### 2. Modify Date with Payment
265+
```bash
266+
PUT /api/bookings/1/modify-date
267+
{
268+
"newJourneyDate": "2025-12-25T10:00:00",
269+
"newTrainId": 300
270+
}
271+
272+
Response:
273+
{
274+
"bookingId": 1,
275+
"modificationType": "DATE_CHANGE",
276+
"status": "SUCCESS",
277+
"originalFare": 2000.00,
278+
"newFare": 1500.00,
279+
"fareDifference": -500.00,
280+
"modificationCharge": 200.00,
281+
"totalAmount": -300.00,
282+
"refundStatus": "REFUNDED",
283+
"refundAmount": 300.00
284+
}
285+
```
286+
287+
### 3. Upgrade Seat with Payment
288+
```bash
289+
PUT /api/bookings/1/upgrade-seat
290+
{
291+
"newSeatClass": "2AC",
292+
"newFare": 2500.00
293+
}
294+
295+
Response:
296+
{
297+
"bookingId": 1,
298+
"modificationType": "SEAT_UPGRADE",
299+
"status": "SUCCESS",
300+
"originalFare": 2000.00,
301+
"newFare": 2500.00,
302+
"fareDifference": 500.00,
303+
"modificationCharge": 100.00,
304+
"totalAmount": 600.00,
305+
"refundStatus": "COMPLETED"
306+
}
307+
```
308+
309+
---
310+
311+
## ✅ Status Summary
312+
313+
| Feature | Status | Notes |
314+
|---------|--------|-------|
315+
| Train Service Integration | ✅ Complete | Feign client with fallback |
316+
| Payment Service Integration | ✅ Complete | Payment & refund processing |
317+
| Unit Tests | ✅ Complete | 12 test cases |
318+
| Integration Tests | ✅ Complete | 6 test cases |
319+
| Circuit Breaker | ✅ Complete | Resilience4j integration |
320+
| Error Handling | ✅ Complete | Comprehensive error handling |
321+
| Documentation | ✅ Complete | This document + code comments |
322+
323+
---
324+
325+
## 🎯 Next Steps (Optional)
326+
327+
1. **End-to-End Testing**: Test with actual Train and Payment services running
328+
2. **Performance Testing**: Load testing for modification endpoints
329+
3. **Monitoring**: Add metrics for modification success/failure rates
330+
4. **Notification Integration**: Send notifications on successful modifications
331+
5. **Modification History**: Track all modifications in database
332+
333+
---
334+
335+
**All requested features have been successfully implemented and tested!** 🎉
336+

0 commit comments

Comments
 (0)