All three requested features have been successfully implemented:
- ✅ Test Cases Created - Unit and integration tests
- ✅ Train Service Integration - Fare calculation from Train Service
- ✅ Payment Processing - Automatic payment/refund for fare differences
Feign Client: TrainServiceClient
- Location:
irctc-booking-service/src/main/java/com/irctc/booking/client/TrainServiceClient.java - Fallback:
TrainServiceClientFallback.java
Features:
- Get train information by ID or train number
- Calculate fare for routes
- Check seat availability
- Automatic fallback when Train Service is unavailable
Usage in Modification Service:
// Fetch train information and base fare
TrainServiceClient.TrainResponse train = trainServiceClient.getTrainById(trainId);
BigDecimal newFare = BigDecimal.valueOf(train.getBaseFare());Benefits:
- Real-time fare calculation
- Accurate pricing for modifications
- Graceful degradation with fallback
Feign Client: PaymentServiceClient
- Location:
irctc-booking-service/src/main/java/com/irctc/booking/client/PaymentServiceClient.java - Fallback:
PaymentServiceClientFallback.java
Features:
- Process payment for fare differences
- Process refunds for downgrades
- Get payment history by booking ID
- Circuit breaker protection
Usage in Modification Service:
// Process payment for positive amount
PaymentServiceClient.PaymentRequest paymentRequest = new PaymentServiceClient.PaymentRequest();
paymentRequest.setBookingId(bookingId);
paymentRequest.setAmount(totalAmount);
PaymentServiceClient.PaymentResponse response = paymentServiceClient.processPayment(paymentRequest);
// Process refund for negative amount
PaymentServiceClient.RefundRequest refundRequest = new PaymentServiceClient.RefundRequest();
refundRequest.setPaymentId(paymentId);
refundRequest.setRefundAmount(refundAmount);
PaymentServiceClient.PaymentResponse refundResponse = paymentServiceClient.processRefund(refundRequest);Benefits:
- Automatic payment processing
- Automatic refund processing
- Transaction tracking
- Circuit breaker for resilience
File: BookingModificationServiceTest.java
- ✅ Test modification options retrieval
- ✅ Test date modification with Train Service integration
- ✅ Test seat upgrade with payment processing
- ✅ Test passenger modification (add/remove)
- ✅ Test route change
- ✅ Test validation errors
- ✅ Test business rule violations
- ✅ Test payment/refund processing
Coverage:
- All modification types tested
- Error scenarios covered
- Service integration mocked
- Business logic validated
File: BookingModificationControllerTest.java
- ✅ Test GET
/api/bookings/{id}/modification-options - ✅ Test PUT
/api/bookings/{id}/modify-date - ✅ Test PUT
/api/bookings/{id}/upgrade-seat - ✅ Test PUT
/api/bookings/{id}/modify-passengers - ✅ Test PUT
/api/bookings/{id}/change-route - ✅ Test validation error handling
Coverage:
- All endpoints tested
- Request/response validation
- HTTP status codes verified
- JSON serialization/deserialization
1. User requests modification
↓
2. Calculate fare difference + modification charge
↓
3. If totalAmount > 0:
→ Process payment via Payment Service
→ Update booking with new fare
→ Return modification response with payment status
↓
4. If totalAmount < 0:
→ Get existing payment for booking
→ Process refund via Payment Service
→ Update booking with new fare
→ Return modification response with refund status
↓
5. If totalAmount = 0:
→ No payment required
→ Update booking
→ Return modification response
1. Modification results in fare reduction
↓
2. Calculate refund amount (absolute value of negative totalAmount)
↓
3. Get last payment for booking
↓
4. Process refund via Payment Service
↓
5. Update payment status to REFUNDED
↓
6. Return refund transaction ID
- ✅ Feign client with fallback
- ✅ Train information retrieval
- ✅ Fare calculation integration
- ✅ Seat availability checking
- ✅ Feign client with fallback
- ✅ Payment processing
- ✅ Refund processing
- ✅ Payment history retrieval
- ✅ Train Service integration for fare calculation
- ✅ Payment Service integration for payment/refund
- ✅ Circuit breaker protection
- ✅ Comprehensive error handling
- ✅ Fallback mechanisms
- ✅ Unit tests for service layer
- ✅ Integration tests for controller layer
- ✅ Mock service clients
- ✅ Validation testing
- ✅ Error scenario testing
- Total Tests: 12
- Coverage: Service layer business logic
- Status: ✅ All tests passing
- Total Tests: 6
- Coverage: API endpoints
- Status: ✅ All tests passing
Add to application.yml:
feign:
client:
config:
irctc-train-service:
connectTimeout: 5000
readTimeout: 10000
irctc-payment-service:
connectTimeout: 5000
readTimeout: 10000
circuitbreaker:
enabled: trueAdd to application.yml:
resilience4j:
circuitbreaker:
instances:
payment-service:
registerHealthIndicator: true
slidingWindowSize: 10
minimumNumberOfCalls: 5
permittedNumberOfCallsInHalfOpenState: 3
automaticTransitionFromOpenToHalfOpenEnabled: true
waitDurationInOpenState: 10s
failureRateThreshold: 50
eventConsumerBufferSize: 10GET /api/bookings/1/modification-options
Response:
{
"bookingId": 1,
"currentStatus": "CONFIRMED",
"canModifyDate": true,
"canUpgradeSeat": true,
"canChangeRoute": true,
"canModifyPassengers": true,
"modificationCharges": {
"dateChange": 200.00,
"seatUpgrade": 100.00,
"routeChange": 300.00,
"passengerModification": 150.00
}
}PUT /api/bookings/1/modify-date
{
"newJourneyDate": "2025-12-25T10:00:00",
"newTrainId": 300
}
Response:
{
"bookingId": 1,
"modificationType": "DATE_CHANGE",
"status": "SUCCESS",
"originalFare": 2000.00,
"newFare": 1500.00,
"fareDifference": -500.00,
"modificationCharge": 200.00,
"totalAmount": -300.00,
"refundStatus": "REFUNDED",
"refundAmount": 300.00
}PUT /api/bookings/1/upgrade-seat
{
"newSeatClass": "2AC",
"newFare": 2500.00
}
Response:
{
"bookingId": 1,
"modificationType": "SEAT_UPGRADE",
"status": "SUCCESS",
"originalFare": 2000.00,
"newFare": 2500.00,
"fareDifference": 500.00,
"modificationCharge": 100.00,
"totalAmount": 600.00,
"refundStatus": "COMPLETED"
}| Feature | Status | Notes |
|---|---|---|
| Train Service Integration | ✅ Complete | Feign client with fallback |
| Payment Service Integration | ✅ Complete | Payment & refund processing |
| Unit Tests | ✅ Complete | 12 test cases |
| Integration Tests | ✅ Complete | 6 test cases |
| Circuit Breaker | ✅ Complete | Resilience4j integration |
| Error Handling | ✅ Complete | Comprehensive error handling |
| Documentation | ✅ Complete | This document + code comments |
- End-to-End Testing: Test with actual Train and Payment services running
- Performance Testing: Load testing for modification endpoints
- Monitoring: Add metrics for modification success/failure rates
- Notification Integration: Send notifications on successful modifications
- Modification History: Track all modifications in database
All requested features have been successfully implemented and tested! 🎉