forked from aws-samples/friend-microservices
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinteg.friend-microservices.ts
More file actions
165 lines (150 loc) · 4.94 KB
/
integ.friend-microservices.ts
File metadata and controls
165 lines (150 loc) · 4.94 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
/// !cdk-integ FriendMicroservicesIntegStack
import "source-map-support/register";
import { IntegTest, ExpectedResult } from "@aws-cdk/integ-tests-alpha";
import { App, Duration } from "aws-cdk-lib";
import { FriendMicroservicesStack } from "../lib/friend-microservices-stack";
const app = new App();
// Stack under test
// NOTE: Do NOT set explicit `env` - integ-runner provides CDK_DEFAULT_ACCOUNT
// and CDK_DEFAULT_REGION, and the assertion stack must share the same environment.
const stackUnderTest = new FriendMicroservicesStack(
app,
"FriendMicroservicesIntegStack"
);
// Create integration test
const integ = new IntegTest(app, "FriendMicroservicesIntegTest", {
testCases: [stackUnderTest],
cdkCommandOptions: {
destroy: {
args: {
force: true,
},
},
},
regions: ["us-east-1"],
});
// ============================================================================
// Test 1: Send a friend request via SQS and verify the DynamoDB record
// ============================================================================
// Send a "Request" action to the SQS queue
const sendFriendRequest = integ.assertions.awsApiCall("SQS", "sendMessage", {
QueueUrl: stackUnderTest.queueUrl,
MessageBody: JSON.stringify({
player_id: "player-integ-1",
friend_id: "player-integ-2",
friend_action: "Request",
}),
});
// Wait for the Lambda to process and write to DynamoDB
// The frontHandler reads from SQS and writes a "Requested" record
const verifyDynamoDbRecord = integ.assertions
.awsApiCall("DynamoDB", "getItem", {
TableName: stackUnderTest.tableName,
Key: {
player_id: { S: "player-integ-1" },
friend_id: { S: "player-integ-2" },
},
})
.expect(
ExpectedResult.objectLike({
Item: {
player_id: { S: "player-integ-1" },
friend_id: { S: "player-integ-2" },
state: { S: "Requested" },
},
})
)
.waitForAssertions({
totalTimeout: Duration.minutes(2),
interval: Duration.seconds(10),
});
// Chain: send request → verify DynamoDB
sendFriendRequest.next(verifyDynamoDbRecord);
// ============================================================================
// Test 2: Verify the requestStateHandler created the reverse "Pending" record
// (DynamoDB Stream → requestStateHandler creates player-integ-2 → player-integ-1 as Pending)
// ============================================================================
const verifyPendingRecord = integ.assertions
.awsApiCall("DynamoDB", "getItem", {
TableName: stackUnderTest.tableName,
Key: {
player_id: { S: "player-integ-2" },
friend_id: { S: "player-integ-1" },
},
})
.expect(
ExpectedResult.objectLike({
Item: {
player_id: { S: "player-integ-2" },
friend_id: { S: "player-integ-1" },
state: { S: "Pending" },
},
})
)
.waitForAssertions({
totalTimeout: Duration.minutes(2),
interval: Duration.seconds(10),
});
// Chain: verify DynamoDB requested record → verify pending record
verifyDynamoDbRecord.next(verifyPendingRecord);
// ============================================================================
// Test 3: Accept the friend request and verify both records become "Friends"
// ============================================================================
// Send an "Accept" action for player-integ-2 accepting player-integ-1
const sendAccept = integ.assertions.awsApiCall("SQS", "sendMessage", {
QueueUrl: stackUnderTest.queueUrl,
MessageBody: JSON.stringify({
player_id: "player-integ-2",
friend_id: "player-integ-1",
friend_action: "Accept",
}),
});
// Verify player-integ-2's record is now "Friends"
const verifyAcceptedRecord = integ.assertions
.awsApiCall("DynamoDB", "getItem", {
TableName: stackUnderTest.tableName,
Key: {
player_id: { S: "player-integ-2" },
friend_id: { S: "player-integ-1" },
},
})
.expect(
ExpectedResult.objectLike({
Item: {
player_id: { S: "player-integ-2" },
friend_id: { S: "player-integ-1" },
state: { S: "Friends" },
},
})
)
.waitForAssertions({
totalTimeout: Duration.minutes(2),
interval: Duration.seconds(10),
});
// Verify the acceptStateHandler also updated player-integ-1's record to "Friends"
const verifyReverseAccepted = integ.assertions
.awsApiCall("DynamoDB", "getItem", {
TableName: stackUnderTest.tableName,
Key: {
player_id: { S: "player-integ-1" },
friend_id: { S: "player-integ-2" },
},
})
.expect(
ExpectedResult.objectLike({
Item: {
player_id: { S: "player-integ-1" },
friend_id: { S: "player-integ-2" },
state: { S: "Friends" },
},
})
)
.waitForAssertions({
totalTimeout: Duration.minutes(2),
interval: Duration.seconds(10),
});
// Chain: verify pending → send accept → verify accepted → verify reverse accepted
verifyPendingRecord
.next(sendAccept)
.next(verifyAcceptedRecord)
.next(verifyReverseAccepted);