dotnet-cqrs/test-grpc-endpoints.sh

137 lines
4.6 KiB
Bash
Executable File

#!/bin/bash
# gRPC Endpoint Testing Script for Phase 1
# Requires grpcurl: brew install grpcurl (macOS) or see https://github.com/fullstorydev/grpcurl
set -e
GRPC_HOST="localhost:6000"
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${BLUE}================================${NC}"
echo -e "${BLUE}Phase 1 gRPC Endpoint Tests${NC}"
echo -e "${BLUE}================================${NC}"
echo ""
# Check if grpcurl is installed
if ! command -v grpcurl &> /dev/null; then
echo "❌ grpcurl is not installed"
echo "Install with: brew install grpcurl (macOS)"
echo "Or see: https://github.com/fullstorydev/grpcurl"
exit 1
fi
echo -e "${GREEN}✓ grpcurl is installed${NC}"
echo ""
# Check if server is running
echo -e "${YELLOW}Checking if gRPC server is running...${NC}"
if ! grpcurl -plaintext ${GRPC_HOST} list > /dev/null 2>&1; then
echo "❌ gRPC server is not running on ${GRPC_HOST}"
echo "Please start the application first: cd Svrnty.Sample && dotnet run"
exit 1
fi
echo -e "${GREEN}✓ gRPC server is running${NC}"
echo ""
# Test 1: List Services
echo -e "${YELLOW}Test 1: List Available Services${NC}"
echo "grpcurl -plaintext ${GRPC_HOST} list"
SERVICES=$(grpcurl -plaintext ${GRPC_HOST} list 2>&1)
echo "$SERVICES"
if echo "$SERVICES" | grep -q "svrnty.cqrs.events.EventService"; then
echo -e "${GREEN}✓ Test 1 passed: EventService found${NC}"
else
echo -e "❌ Test 1 failed: EventService not found"
fi
echo ""
# Test 2: Describe EventService
echo -e "${YELLOW}Test 2: Describe EventService${NC}"
echo "grpcurl -plaintext ${GRPC_HOST} describe svrnty.cqrs.events.EventService"
DESCRIBE=$(grpcurl -plaintext ${GRPC_HOST} describe svrnty.cqrs.events.EventService 2>&1)
echo "$DESCRIBE"
if echo "$DESCRIBE" | grep -q "Subscribe"; then
echo -e "${GREEN}✓ Test 2 passed: Subscribe method found${NC}"
else
echo -e "❌ Test 2 failed: Subscribe method not found"
fi
echo ""
# Test 3: Execute gRPC Command
echo -e "${YELLOW}Test 3: Execute AddUser via gRPC CommandService${NC}"
echo "grpcurl -plaintext -d '{...}' ${GRPC_HOST} cqrs.CommandService.AddUser"
COMMAND_RESPONSE=$(grpcurl -plaintext -d '{
"name": "gRPC User",
"email": "grpc-user@example.com",
"age": 25
}' ${GRPC_HOST} cqrs.CommandService.AddUser 2>&1)
echo "$COMMAND_RESPONSE"
if echo "$COMMAND_RESPONSE" | grep -q "result"; then
echo -e "${GREEN}✓ Test 3 passed: Command executed successfully${NC}"
else
echo -e "❌ Test 3 failed: Command execution failed"
fi
echo ""
# Test 4: gRPC Query
echo -e "${YELLOW}Test 4: Execute Query via gRPC QueryService${NC}"
echo "grpcurl -plaintext -d '{...}' ${GRPC_HOST} cqrs.QueryService.FetchUser"
QUERY_RESPONSE=$(grpcurl -plaintext -d '{
"userId": 1234
}' ${GRPC_HOST} cqrs.QueryService.FetchUser 2>&1)
echo "$QUERY_RESPONSE"
if echo "$QUERY_RESPONSE" | grep -q "userId"; then
echo -e "${GREEN}✓ Test 4 passed: Query executed successfully${NC}"
else
echo -e "❌ Test 4 failed: Query execution failed"
fi
echo ""
# Test 5: EventService Subscription (Manual Test)
echo -e "${YELLOW}Test 5: EventService Subscription${NC}"
echo -e "${BLUE}This test requires manual verification:${NC}"
echo ""
echo "1. Open a new terminal window"
echo "2. Run the following command to start a gRPC event subscription:"
echo ""
echo -e "${GREEN}grpcurl -plaintext -d @ ${GRPC_HOST} svrnty.cqrs.events.EventService.Subscribe <<EOF"
echo '{
"subscribe": {
"subscription_id": "test-grpc-sub",
"correlation_id": "test-correlation",
"delivery_mode": "DELIVERY_MODE_IMMEDIATE"
}
}'
echo -e "EOF${NC}"
echo ""
echo "3. In another terminal, send a command to generate events:"
echo ""
echo -e "${GREEN}curl -X POST http://localhost:6001/api/command/addUser -H 'Content-Type: application/json' -d '{\"name\": \"gRPC Test\", \"email\": \"grpc@test.com\"}'${NC}"
echo ""
echo "4. Verify that the gRPC subscription receives the event"
echo ""
echo -e "${YELLOW}Press Enter when you have completed the manual test (or Ctrl+C to skip)${NC}"
read -r
# Summary
echo ""
echo -e "${BLUE}================================${NC}"
echo -e "${BLUE}Test Summary${NC}"
echo -e "${BLUE}================================${NC}"
echo -e "gRPC endpoint tests completed!"
echo -e ""
echo -e "${YELLOW}✓ Automated Tests:${NC}"
echo -e " - Service discovery (list/describe)"
echo -e " - Command execution"
echo -e " - Query execution"
echo -e ""
echo -e "${YELLOW}⚠ Manual Verification Required:${NC}"
echo -e " - EventService bidirectional streaming"
echo -e " - Real-time event delivery"
echo -e ""
echo -e "${BLUE}For complete testing instructions, see: PHASE1-TESTING-GUIDE.md${NC}"
echo ""