|
| 1 | +/// !cdk-integ FriendMicroservicesIntegStack |
| 2 | +import "source-map-support/register"; |
| 3 | +import { IntegTest, ExpectedResult } from "@aws-cdk/integ-tests-alpha"; |
| 4 | +import { App, Duration } from "aws-cdk-lib"; |
| 5 | +import { FriendMicroservicesStack } from "../lib/friend-microservices-stack"; |
| 6 | + |
| 7 | +const app = new App(); |
| 8 | + |
| 9 | +// Stack under test |
| 10 | +// NOTE: Do NOT set explicit `env` - integ-runner provides CDK_DEFAULT_ACCOUNT |
| 11 | +// and CDK_DEFAULT_REGION, and the assertion stack must share the same environment. |
| 12 | +const stackUnderTest = new FriendMicroservicesStack( |
| 13 | + app, |
| 14 | + "FriendMicroservicesIntegStack" |
| 15 | +); |
| 16 | + |
| 17 | +// Create integration test |
| 18 | +const integ = new IntegTest(app, "FriendMicroservicesIntegTest", { |
| 19 | + testCases: [stackUnderTest], |
| 20 | + cdkCommandOptions: { |
| 21 | + destroy: { |
| 22 | + args: { |
| 23 | + force: true, |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + regions: ["us-east-1"], |
| 28 | +}); |
| 29 | + |
| 30 | +// ============================================================================ |
| 31 | +// Test 1: Send a friend request via SQS and verify the DynamoDB record |
| 32 | +// ============================================================================ |
| 33 | + |
| 34 | +// Send a "Request" action to the SQS queue |
| 35 | +const sendFriendRequest = integ.assertions.awsApiCall("SQS", "sendMessage", { |
| 36 | + QueueUrl: stackUnderTest.queueUrl, |
| 37 | + MessageBody: JSON.stringify({ |
| 38 | + player_id: "player-integ-1", |
| 39 | + friend_id: "player-integ-2", |
| 40 | + friend_action: "Request", |
| 41 | + }), |
| 42 | +}); |
| 43 | + |
| 44 | +// Wait for the Lambda to process and write to DynamoDB |
| 45 | +// The frontHandler reads from SQS and writes a "Requested" record |
| 46 | +const verifyDynamoDbRecord = integ.assertions |
| 47 | + .awsApiCall("DynamoDB", "getItem", { |
| 48 | + TableName: stackUnderTest.tableName, |
| 49 | + Key: { |
| 50 | + player_id: { S: "player-integ-1" }, |
| 51 | + friend_id: { S: "player-integ-2" }, |
| 52 | + }, |
| 53 | + }) |
| 54 | + .expect( |
| 55 | + ExpectedResult.objectLike({ |
| 56 | + Item: { |
| 57 | + player_id: { S: "player-integ-1" }, |
| 58 | + friend_id: { S: "player-integ-2" }, |
| 59 | + state: { S: "Requested" }, |
| 60 | + }, |
| 61 | + }) |
| 62 | + ) |
| 63 | + .waitForAssertions({ |
| 64 | + totalTimeout: Duration.minutes(2), |
| 65 | + interval: Duration.seconds(10), |
| 66 | + }); |
| 67 | + |
| 68 | +// Chain: send request → verify DynamoDB |
| 69 | +sendFriendRequest.next(verifyDynamoDbRecord); |
| 70 | + |
| 71 | +// ============================================================================ |
| 72 | +// Test 2: Verify the requestStateHandler created the reverse "Pending" record |
| 73 | +// (DynamoDB Stream → requestStateHandler creates player-integ-2 → player-integ-1 as Pending) |
| 74 | +// ============================================================================ |
| 75 | + |
| 76 | +const verifyPendingRecord = integ.assertions |
| 77 | + .awsApiCall("DynamoDB", "getItem", { |
| 78 | + TableName: stackUnderTest.tableName, |
| 79 | + Key: { |
| 80 | + player_id: { S: "player-integ-2" }, |
| 81 | + friend_id: { S: "player-integ-1" }, |
| 82 | + }, |
| 83 | + }) |
| 84 | + .expect( |
| 85 | + ExpectedResult.objectLike({ |
| 86 | + Item: { |
| 87 | + player_id: { S: "player-integ-2" }, |
| 88 | + friend_id: { S: "player-integ-1" }, |
| 89 | + state: { S: "Pending" }, |
| 90 | + }, |
| 91 | + }) |
| 92 | + ) |
| 93 | + .waitForAssertions({ |
| 94 | + totalTimeout: Duration.minutes(2), |
| 95 | + interval: Duration.seconds(10), |
| 96 | + }); |
| 97 | + |
| 98 | +// Chain: verify DynamoDB requested record → verify pending record |
| 99 | +verifyDynamoDbRecord.next(verifyPendingRecord); |
| 100 | + |
| 101 | +// ============================================================================ |
| 102 | +// Test 3: Accept the friend request and verify both records become "Friends" |
| 103 | +// ============================================================================ |
| 104 | + |
| 105 | +// Send an "Accept" action for player-integ-2 accepting player-integ-1 |
| 106 | +const sendAccept = integ.assertions.awsApiCall("SQS", "sendMessage", { |
| 107 | + QueueUrl: stackUnderTest.queueUrl, |
| 108 | + MessageBody: JSON.stringify({ |
| 109 | + player_id: "player-integ-2", |
| 110 | + friend_id: "player-integ-1", |
| 111 | + friend_action: "Accept", |
| 112 | + }), |
| 113 | +}); |
| 114 | + |
| 115 | +// Verify player-integ-2's record is now "Friends" |
| 116 | +const verifyAcceptedRecord = integ.assertions |
| 117 | + .awsApiCall("DynamoDB", "getItem", { |
| 118 | + TableName: stackUnderTest.tableName, |
| 119 | + Key: { |
| 120 | + player_id: { S: "player-integ-2" }, |
| 121 | + friend_id: { S: "player-integ-1" }, |
| 122 | + }, |
| 123 | + }) |
| 124 | + .expect( |
| 125 | + ExpectedResult.objectLike({ |
| 126 | + Item: { |
| 127 | + player_id: { S: "player-integ-2" }, |
| 128 | + friend_id: { S: "player-integ-1" }, |
| 129 | + state: { S: "Friends" }, |
| 130 | + }, |
| 131 | + }) |
| 132 | + ) |
| 133 | + .waitForAssertions({ |
| 134 | + totalTimeout: Duration.minutes(2), |
| 135 | + interval: Duration.seconds(10), |
| 136 | + }); |
| 137 | + |
| 138 | +// Verify the acceptStateHandler also updated player-integ-1's record to "Friends" |
| 139 | +const verifyReverseAccepted = integ.assertions |
| 140 | + .awsApiCall("DynamoDB", "getItem", { |
| 141 | + TableName: stackUnderTest.tableName, |
| 142 | + Key: { |
| 143 | + player_id: { S: "player-integ-1" }, |
| 144 | + friend_id: { S: "player-integ-2" }, |
| 145 | + }, |
| 146 | + }) |
| 147 | + .expect( |
| 148 | + ExpectedResult.objectLike({ |
| 149 | + Item: { |
| 150 | + player_id: { S: "player-integ-1" }, |
| 151 | + friend_id: { S: "player-integ-2" }, |
| 152 | + state: { S: "Friends" }, |
| 153 | + }, |
| 154 | + }) |
| 155 | + ) |
| 156 | + .waitForAssertions({ |
| 157 | + totalTimeout: Duration.minutes(2), |
| 158 | + interval: Duration.seconds(10), |
| 159 | + }); |
| 160 | + |
| 161 | +// Chain: verify pending → send accept → verify accepted → verify reverse accepted |
| 162 | +verifyPendingRecord |
| 163 | + .next(sendAccept) |
| 164 | + .next(verifyAcceptedRecord) |
| 165 | + .next(verifyReverseAccepted); |
0 commit comments