Testing AI Integration Guide
Prerequisites
- All three services running:
- notification-service (port 8081)
- worker-service (no port)
- ai-service (port 8083)
-
Kafka and PostgreSQL running via docker-compose
- Postman collection imported (with new AI integration requests)
Test Scenarios
Test 1: Successful Enhancement (Happy Path)
Objective: Verify AI service enhancement works correctly
Steps:
- Ensure all services are running and healthy
- Create a notification:
curl -X POST http://localhost:8081/notifications \
-H "Content-Type: application/json" \
-d '{
"recipient": "test@example.com",
"subject": "Welcome to our platform",
"message": "Thank you for signing up!",
"channel": "EMAIL"
}'
Expected response:
{
"id": 1,
"recipient": "test@example.com",
"subject": "Welcome to our platform",
"message": "Thank you for signing up!",
"channel": "EMAIL",
"status": "PENDING",
"createdAt": "2024-01-15T10:00:00Z"
}
-
Wait 5-10 seconds for Kafka consumer to process
-
Check worker-service logs:
INFO - Requesting AI enhancement for channel: EMAIL
INFO - Using AI-enhanced content. Confidence: 0.87
INFO - Sending EMAIL notification to: test@example.com
INFO - Subject: 🚀 Exclusive: Welcome to our platform
INFO - Successfully processed notification ID: 1
- Verify database update:
SELECT subject, message, status FROM notifications WHERE id = 1;
Output:
subject: 🚀 Exclusive: Welcome to our platform
message: Thank you for signing up! ✨ This message has been AI-optimized...
status: SENT
Expected Result: ✓ Notification enhanced and marked as SENT
Test 2: AI Service Timeout Fallback
Objective: Verify graceful fallback when AI service times out
Setup:
- Add delay to AI service response (simulate slow processing):
Edit ai-service/src/main/java/com/ai/service/AiOptimizationService.java:
public OptimizationResponse optimize(OptimizationRequest request) {
Thread.sleep(6000); // Sleep longer than 5s timeout
// ... rest of logic
}
Recompile and restart AI service.
- Create a notification:
curl -X POST http://localhost:8081/notifications \
-H "Content-Type: application/json" \
-d '{
"recipient": "test@example.com",
"subject": "Order Confirmation",
"message": "Your order has been confirmed",
"channel": "EMAIL"
}'
- Check worker-service logs within 10 seconds:
INFO - Requesting AI enhancement for channel: EMAIL
WARN - AI enhancement failed, using fallback. Error: ...timeout...
INFO - Sending EMAIL notification to: test@example.com
INFO - Subject: Order Confirmation
INFO - Successfully processed notification ID: 2
- Verify database:
SELECT subject, message, status FROM notifications WHERE id = 2;
Output:
subject: Order Confirmation (original, not enhanced)
message: Your order has been confirmed (original)
status: SENT
Expected Result: ✓ Notification sent with original content despite timeout
Test 3: AI Service Unavailable (Circuit Breaker)
Objective: Verify circuit breaker prevents cascading failures
Setup:
- Stop the AI service:
# In the ai-service terminal, press Ctrl+C
- Create 5-10 notifications rapidly:
for i in {1..10}; do
curl -X POST http://localhost:8081/notifications \
-H "Content-Type: application/json" \
-d "{
\"recipient\": \"test$i@example.com\",
\"subject\": \"Test $i\",
\"message\": \"Message $i\",
\"channel\": \"EMAIL\"
}"
sleep 0.5
done
- Watch worker-service logs:
First 3 attempts:
INFO - Requesting AI enhancement for channel: EMAIL
WARN - Connection refused (AI service down)
WARN - AI enhancement failed, using fallback
After ~5 more failures:
INFO - Requesting AI enhancement for channel: EMAIL
WARN - Circuit breaker is OPEN. Falling back immediately.
- Verify notifications are still sent:
SELECT COUNT(*) FROM notifications WHERE status = 'SENT';
-- Should show 10 (all processed despite AI service down)
- Restart AI service:
cd ai-service
mvn spring-boot:run
- Create more notifications:
curl -X POST http://localhost:8081/notifications \
-H "Content-Type: application/json" \
-d '{
"recipient": "recovery@example.com",
"subject": "Service Recovery Test",
"message": "Testing recovery",
"channel": "EMAIL"
}'
- Check logs - circuit breaker should eventually recover:
Circuit breaker transitioning to HALF_OPEN state
Attempting recovery calls...
[after 3 successful calls]
Circuit breaker transitioning to CLOSED state
INFO - Requesting AI enhancement for channel: EMAIL
INFO - Using AI-enhanced content. Confidence: 0.87
Expected Result: ✓ System remains operational, notifications sent, auto-recovery works
Test 4: Invalid Request to AI Service
Objective: Verify fallback on malformed AI requests
Setup:
- Modify AI client to send invalid data:
Edit worker-service to intentionally send bad channel:
AiEnhancementRequest request = new AiEnhancementRequest(
subject,
message,
"INVALID_CHANNEL" // This channel doesn't exist
);
- Create a notification and observe fallback
Expected Result: ✓ Fallback to original content, notification still sent
Test 5: Load Test with AI Enhancement
Objective: Verify performance under load
Setup:
- Use Postman Collection Runner:
- Select “Load Test - Create Multiple Notifications”
- Set iterations: 20
- Set delay: 100ms
-
Run the collection
- Monitor logs for timing:
Notification 1: Enhancement took 120ms, Send took 2000ms, Total: 2120ms
Notification 2: Enhancement took 95ms, Send took 2000ms, Total: 2095ms
Notification 3: Enhancement took 110ms, Send took 2000ms, Total: 2110ms
...
- Verify all completed:
SELECT COUNT(*) FROM notifications WHERE status = 'SENT';
-- Should show 20+ notifications
Expected Result: ✓ All notifications processed, AI enhancement adds minimal overhead
Test 6: Channel-Specific Enhancement
Objective: Verify AI service returns different enhancements per channel
Test EMAIL:
curl -X POST http://localhost:8083/ai/optimize \
-H "Content-Type: application/json" \
-d '{
"subject": "Update your password",
"message": "We recommend updating your password for security",
"channel": "EMAIL"
}'
Expected: Full message with emoji, power words
Test SMS:
curl -X POST http://localhost:8083/ai/optimize \
-H "Content-Type: application/json" \
-d '{
"subject": "Update your password",
"message": "We recommend updating your password for security",
"channel": "SMS"
}'
Expected: Truncated to ~100 characters
Test PUSH:
curl -X POST http://localhost:8083/ai/optimize \
-H "Content-Type: application/json" \
-d '{
"subject": "Update your password",
"message": "We recommend updating your password for security",
"channel": "PUSH"
}'
Expected: Truncated to ~150 characters
Expected Result: ✓ Different optimization strategies per channel
Debugging
Check Circuit Breaker Status
Add Spring Boot Actuator to worker-service (optional):
curl http://localhost:8082/actuator/circuitbreakers/aiServiceCircuitBreaker
Enable Full Debug Logging
Add to worker-service application.yml:
logging:
level:
com.worker: DEBUG
org.springframework.cloud.openfeign: DEBUG
io.github.resilience4j: DEBUG
Monitor Kafka Messages
docker exec kafka kafka-console-consumer.sh \
--bootstrap-servers localhost:9092 \
--topic notification.requested \
--from-beginning
Check Database State
docker exec postgres psql -U postgres -d notification_db \
-c "SELECT id, subject, status, created_at FROM notifications ORDER BY id DESC LIMIT 10;"
View Service Logs
Notification Service:
docker logs notification-service
Worker Service:
docker logs worker-service
AI Service:
docker logs ai-service
Performance Benchmarks
Expected Metrics
| Metric | Expected | Notes |
|---|---|---|
| AI Enhancement Time | 100-200ms | Per notification |
| AI Timeout | 5000ms | Circuit breaker triggers |
| Fallback Time | < 1ms | Immediate return |
| Total Processing Time | 2.1-2.3s | Includes 2s simulated send |
| Throughput | 15-20 msg/sec | With 3 worker threads |
| Success Rate (Normal) | 99%+ | All enhanced and sent |
| Success Rate (AI Down) | 100% | Fallback to original |
| Circuit Breaker Open→Close | 10 sec | Recovery time |
Troubleshooting
Issue: AI enhancement not happening
Check:
- AI service is running:
curl http://localhost:8083/ai/health - Worker service logs show enhancement attempts
- Check Feign client configuration
- Verify network connectivity between services
Issue: Notifications stuck in PROCESSING
Check:
- Worker service is running
- Kafka consumer is consuming messages
- Database connection is working
- Check for exceptions in logs
Issue: Circuit breaker always open
Check:
- Verify AI service URL is correct in configuration
- Check if AI service is actually responding
- Lower
failureRateThresholdto 30% for testing - Reset by restarting worker service
Issue: Timeout happening immediately
Check:
- Increase
timeoutDurationin configuration - Check AI service performance
- Monitor network latency
- Verify AI service endpoints exist
Test Automation
Shell Script: End-to-End Test
#!/bin/bash
echo "Testing AI Integration..."
# Test 1: Send notification
RESPONSE=$(curl -s -X POST http://localhost:8081/notifications \
-H "Content-Type: application/json" \
-d '{
"recipient": "test@example.com",
"subject": "Test",
"message": "Test message",
"channel": "EMAIL"
}')
ID=$(echo $RESPONSE | grep -o '"id":[0-9]*' | grep -o '[0-9]*')
echo "Created notification ID: $ID"
# Wait for processing
sleep 5
# Test 2: Check database
RESULT=$(docker exec postgres psql -U postgres -d notification_db \
-c "SELECT status FROM notifications WHERE id = $ID;")
if [[ $RESULT == *"SENT"* ]]; then
echo "✓ Test PASSED: Notification enhanced and sent"
exit 0
else
echo "✗ Test FAILED: Notification not sent"
exit 1
fi
Summary
The AI integration should:
- ✓ Enhance messages successfully when AI service is available
- ✓ Fallback gracefully when AI service is unavailable
- ✓ Return to normal operation when AI service recovers
- ✓ Prevent cascading failures with circuit breaker
- ✓ Complete notifications regardless of AI service state
- ✓ Preserve asynchronous architecture
- ✓ Add minimal performance overhead (< 200ms)
- ✓ Provide clear logging for debugging