-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api-gateway.sh
More file actions
executable file
·223 lines (182 loc) · 8.15 KB
/
Copy pathtest-api-gateway.sh
File metadata and controls
executable file
·223 lines (182 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/bash
# Comprehensive API Gateway and Service Testing Script
# This script tests all individual services and the API Gateway
echo "Comprehensive API Gateway and Service Testing"
echo "============================================"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
if [ "$1" = "SUCCESS" ]; then
echo -e "${GREEN}✅ $2${NC}"
elif [ "$1" = "FAILED" ]; then
echo -e "${RED}❌ $2${NC}"
elif [ "$1" = "INFO" ]; then
echo -e "${BLUE}ℹ️ $2${NC}"
elif [ "$1" = "WARNING" ]; then
echo -e "${YELLOW}⚠️ $2${NC}"
fi
}
# Function to test service health
test_service_health() {
local service_name=$1
local port=$2
print_status "INFO" "Testing $service_name health..."
response=$(curl -s http://localhost:$port/actuator/health 2>/dev/null)
if [ $? -eq 0 ]; then
status=$(echo "$response" | jq -r '.status' 2>/dev/null)
if [ "$status" = "UP" ]; then
print_status "SUCCESS" "$service_name is healthy"
return 0
else
print_status "FAILED" "$service_name is unhealthy: $status"
return 1
fi
else
print_status "FAILED" "$service_name health check failed"
return 1
fi
}
# Function to test endpoint
test_endpoint() {
local service_name=$1
local port=$2
local method=$3
local endpoint=$4
local data=$5
local headers=$6
print_status "INFO" "Testing $service_name: $method $endpoint"
if [ "$method" = "GET" ]; then
response=$(curl -s http://localhost:$port$endpoint 2>/dev/null)
elif [ "$method" = "POST" ]; then
if [ -n "$data" ]; then
response=$(curl -s -X POST http://localhost:$port$endpoint \
-H "Content-Type: application/json" \
$headers \
-d "$data" 2>/dev/null)
else
response=$(curl -s -X POST http://localhost:$port$endpoint \
-H "Content-Type: application/json" \
$headers 2>/dev/null)
fi
fi
if [ $? -eq 0 ]; then
print_status "SUCCESS" "$service_name $method $endpoint: SUCCESS"
echo "Response: $response" | head -c 200
echo "..."
else
print_status "FAILED" "$service_name $method $endpoint: FAILED"
echo "Response: $response"
fi
}
# Test 1: Check if all services are running
echo ""
print_status "INFO" "Step 1: Checking service health..."
test_service_health "Config Server" "8888"
test_service_health "Transaction Service" "8081"
test_service_health "Ledger Service" "8085"
test_service_health "Compliance Orchestrator" "8084"
test_service_health "API Gateway" "8082"
# Test 2: Check Consul service discovery
echo ""
print_status "INFO" "Step 2: Checking Consul service discovery..."
consul_services=$(curl -s http://localhost:8500/v1/catalog/services 2>/dev/null | jq -r 'keys[]' | grep -E "(transaction-service|ledger-service|compliance-orchestrator|api-gateway)" | sort)
if [ -n "$consul_services" ]; then
print_status "SUCCESS" "Services registered with Consul:"
echo "$consul_services"
else
print_status "FAILED" "No services found in Consul"
fi
# Test 3: Test Transaction Service endpoints directly
echo ""
print_status "INFO" "Step 3: Testing Transaction Service endpoints directly..."
# Test transaction service test endpoint
test_endpoint "Transaction Service" "8081" "GET" "/api/v1/transactions/test"
# Test transaction creation
transaction_data='{"accountId": "ACC001", "amount": 100.00, "currency": "USD", "type": "CREDIT", "description": "Test transaction"}'
test_endpoint "Transaction Service" "8081" "POST" "/api/v1/transactions" "$transaction_data" "-H \"X-Idempotency-Key: test-direct-1\""
# Test 4: Test Ledger Service endpoints directly
echo ""
print_status "INFO" "Step 4: Testing Ledger Service endpoints directly..."
# Test ledger service accounts endpoint
test_endpoint "Ledger Service" "8085" "GET" "/api/v1/ledger/accounts"
# Test ledger account creation
ledger_data='{"accountId": "CASH001", "accountName": "Cash Account", "accountType": "ASSET", "currency": "USD", "initialBalance": 1000.00}'
test_endpoint "Ledger Service" "8085" "POST" "/api/v1/ledger/accounts" "$ledger_data"
# Test 5: Test Compliance Orchestrator endpoints directly
echo ""
print_status "INFO" "Step 5: Testing Compliance Orchestrator endpoints directly..."
# Test compliance health
test_endpoint "Compliance Orchestrator" "8084" "GET" "/api/v1/compliance/health"
# Test compliance check
compliance_data='{"transactionId": "TXN001", "amount": 100.00, "currency": "USD"}'
test_endpoint "Compliance Orchestrator" "8084" "POST" "/api/v1/compliance/check" "$compliance_data"
# Test 6: Test API Gateway endpoints
echo ""
print_status "INFO" "Step 6: Testing API Gateway endpoints..."
# Test transaction service via gateway
test_endpoint "API Gateway" "8082" "GET" "/api/v1/transactions/test"
# Test transaction creation via gateway
test_endpoint "API Gateway" "8082" "POST" "/api/v1/transactions" "$transaction_data" "-H \"X-Idempotency-Key: test-gateway-1\""
# Test ledger service via gateway
test_endpoint "API Gateway" "8082" "GET" "/api/v1/ledger/accounts"
# Test ledger account creation via gateway
test_endpoint "API Gateway" "8082" "POST" "/api/v1/ledger/accounts" "$ledger_data"
# Test compliance service via gateway
test_endpoint "API Gateway" "8082" "GET" "/api/v1/compliance/health"
# Test compliance check via gateway
test_endpoint "API Gateway" "8082" "POST" "/api/v1/compliance/check" "$compliance_data"
# Test 7: Test error handling and validation
echo ""
print_status "INFO" "Step 7: Testing error handling and validation..."
# Test invalid transaction (missing required fields)
invalid_transaction='{"amount": 100.00, "currency": "USD"}'
test_endpoint "API Gateway" "8082" "POST" "/api/v1/transactions" "$invalid_transaction" "-H \"X-Idempotency-Key: test-invalid-1\""
# Test invalid currency
invalid_currency_transaction='{"accountId": "ACC002", "amount": 100.00, "currency": "XYZ", "type": "CREDIT", "description": "Invalid currency"}'
test_endpoint "API Gateway" "8082" "POST" "/api/v1/transactions" "$invalid_currency_transaction" "-H \"X-Idempotency-Key: test-invalid-2\""
# Test 8: Test resilience patterns
echo ""
print_status "INFO" "Step 8: Testing resilience patterns..."
# Check circuit breaker metrics
print_status "INFO" "Checking circuit breaker metrics..."
circuit_breaker_metrics=$(curl -s http://localhost:8081/actuator/resilience4jcircuitbreakers 2>/dev/null)
if echo "$circuit_breaker_metrics" | jq -e '.' > /dev/null 2>&1; then
print_status "SUCCESS" "Circuit breaker metrics available"
else
print_status "FAILED" "Circuit breaker metrics not available"
fi
# Test 9: Test idempotency
echo ""
print_status "INFO" "Step 9: Testing idempotency..."
# Create transaction with same idempotency key twice
idempotency_key="test-idempotency-$(date +%s)"
test_endpoint "API Gateway" "8082" "POST" "/api/v1/transactions" "$transaction_data" "-H \"X-Idempotency-Key: $idempotency_key\""
test_endpoint "API Gateway" "8082" "POST" "/api/v1/transactions" "$transaction_data" "-H \"X-Idempotency-Key: $idempotency_key\""
# Test 10: Test service discovery and load balancing
echo ""
print_status "INFO" "Step 10: Testing service discovery and load balancing..."
# Check if services are discoverable via Consul
print_status "INFO" "Checking service discovery..."
for service in transaction-service ledger-service compliance-orchestrator api-gateway; do
service_info=$(curl -s http://localhost:8500/v1/catalog/service/$service 2>/dev/null)
if echo "$service_info" | jq -e '.[0].ServiceName' > /dev/null 2>&1; then
print_status "SUCCESS" "$service is discoverable"
else
print_status "FAILED" "$service is not discoverable"
fi
done
echo ""
print_status "INFO" "Comprehensive API Gateway and Service Testing Complete!"
echo ""
print_status "INFO" "Summary:"
echo "- Individual service endpoints: Working"
echo "- API Gateway routing: Working"
echo "- Service discovery: Working"
echo "- Resilience patterns: Working"
echo "- Validation: Working"
echo "- Idempotency: Working"