Skip to content

Commit 9b51302

Browse files
Use update letter sqs queue for duplicate checking
1 parent 2a6c9b6 commit 9b51302

6 files changed

Lines changed: 85 additions & 47 deletions

File tree

tests/component-tests/allocation-tests/supplier-allocation.spec.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { sendSnsBatchEvent } from "tests/helpers/send-sns-event";
1414
test.describe("Supplier Allocation Tests", () => {
1515
test("Verify that supplier allocations are correctly updated for a volume group", async () => {
1616
test.setTimeout(180_000);
17-
const volumeGroupId = "volumeGroup-test3";
17+
const volumeGroupId = "volumeGroup-test1";
1818
const originalTotalAllocation =
1919
await getTotalAllocationForVolumeGroup(volumeGroupId);
2020
logger.info(
@@ -34,8 +34,14 @@ test.describe("Supplier Allocation Tests", () => {
3434
// Create 2 messages with same domain id
3535
const domainId = randomUUID();
3636

37-
const message1 = createPreparedV1Event({ domainId });
38-
const message2 = createPreparedV1Event({ domainId });
37+
const message1 = createPreparedV1Event({
38+
domainId,
39+
letterVariantId: "notify-standard-test1",
40+
});
41+
const message2 = createPreparedV1Event({
42+
domainId,
43+
letterVariantId: "notify-standard-test1",
44+
});
3945

4046
const eventBatch = [message1, message2];
4147
const response = await sendSnsBatchEvent(

tests/component-tests/events-tests/event-subscription.spec.ts

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { expect, test } from "@playwright/test";
22
import { sendSnsEvent } from "tests/helpers/send-sns-event";
3-
import { createPreparedV1Event } from "tests/helpers/event-fixtures";
3+
import { sendSqsEvent } from "tests/helpers/send-sqs-event";
4+
import {
5+
createPendingAllocatedEvent,
6+
createPreparedV1Event,
7+
} from "tests/helpers/event-fixtures";
48
import { randomUUID } from "node:crypto";
59
import { logger } from "tests/helpers/pino-logger";
610
import { createValidRequestHeaders } from "tests/constants/request-headers";
@@ -91,35 +95,14 @@ test.describe("Event Subscription SNS Tests", () => {
9195
);
9296
expect(getLetterResponse.status()).toBe(404);
9397
});
94-
95-
test("Verify that an error is logged for a duplicate letter id", async () => {
98+
test("Verify that an error is logged for duplicates sent on the sqs queue", async () => {
9699
const domainId = randomUUID();
97100
logger.info(`Testing event subscription with domainId: ${domainId}`);
98-
const preparedEvent1 = createPreparedV1Event({ domainId });
99-
const response1 = await sendSnsEvent(preparedEvent1);
100-
101-
expect(response1.MessageId).toBeTruthy();
102-
103-
// poll supplier allocator to check if supplier has been allocated
104-
const message = await pollSupplierAllocatorLogForResolvedSpec(domainId);
105-
const supplierAllocatorLog = JSON.parse(message) as {
106-
msg?: { allocationDetails?: { supplierSpec?: { supplierId?: string } } };
107-
};
108-
const supplierId =
109-
supplierAllocatorLog.msg?.allocationDetails?.supplierSpec?.supplierId;
110-
111-
logger.info(
112-
`Supplier ${supplierId} allocated for domainId ${domainId} in supplier allocator lambda`,
113-
);
114-
if (!supplierId) {
115-
throw new Error("supplierId was not found in supplier allocator log");
116-
}
117-
118-
const preparedEvent2 = createPreparedV1Event({ domainId });
119-
const response2 = await sendSnsEvent(preparedEvent2);
120-
expect(response2.MessageId).toBeTruthy();
101+
const pendingEvent = createPendingAllocatedEvent({ domainId });
102+
await sendSqsEvent(JSON.stringify(pendingEvent));
103+
const pendingEventDuplicate = createPendingAllocatedEvent({ domainId });
104+
await sendSqsEvent(JSON.stringify(pendingEventDuplicate));
121105

122-
// poll supplier upsert to check if duplicate letter id was processed
123106
await pollUpsertLetterLogForWarning("Letter already exists", domainId);
124107
});
125108
});

tests/component-tests/letterQueue-tests/queue-operations.spec.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { expect, test } from "@playwright/test";
22
import { randomUUID } from "node:crypto";
33
import {
4-
createPreparedEventBatchWithSameDomainId,
4+
createPendingEventBatchWithSameDomainId,
55
createPreparedV1Event,
66
} from "tests/helpers/event-fixtures";
77
import { logger } from "tests/helpers/pino-logger";
8-
import { sendSnsBatchEvent, sendSnsEvent } from "tests/helpers/send-sns-event";
8+
import { sendSnsEvent } from "tests/helpers/send-sns-event";
9+
import { sendSqsEventBatch } from "tests/helpers/send-sqs-event";
910
import {
1011
pollUpsertLetterLogForWarning,
1112
supplierIdFromSupplierAllocatorLog,
@@ -80,27 +81,19 @@ test.describe("Letter Queue Tests", () => {
8081

8182
test("Verify if the only one entry is inserted in the letter queue table for a batch of events with the same letterId", async () => {
8283
const letterId = randomUUID();
83-
const eventBatch = createPreparedEventBatchWithSameDomainId({
84+
const eventBatch = createPendingEventBatchWithSameDomainId({
8485
domainId: letterId,
8586
});
8687
logger.info(
8788
`Sending batch event with ${eventBatch.length} events ${letterId}`,
8889
);
89-
const response = await sendSnsBatchEvent(
90-
eventBatch.map((event) => ({
91-
id: event.id,
92-
message: event,
93-
})),
94-
);
95-
expect(response.Successful).toHaveLength(eventBatch.length);
96-
97-
const supplierId = await supplierIdFromSupplierAllocatorLog(letterId);
90+
await sendSqsEventBatch(eventBatch.map((event) => JSON.stringify(event)));
9891

9992
logger.info(
10093
`Verifying duplicate queue inserts are ignored for the batch of events with same letterId ${letterId}`,
10194
);
10295
const [letterExists, itemCount] = await checkLetterQueueTable(
103-
supplierId,
96+
"supplier1",
10497
letterId,
10598
);
10699
expect(letterExists).toBe(true);

tests/constants/api-constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ export const EVENT_SUBSCRIPTION_TOPIC_ARN =
1717
`arn:aws:sns:${AWS_REGION}:${AWS_ACCOUNT_ID}:${EVENT_SUBSCRIPTION_TOPIC_NAME}`;
1818
export const LETTERQUEUE_TABLENAME = `nhs-${envName}-supapi-letter-queue`;
1919
export const SUPPLIER_QUOTAS_TABLENAME = `nhs-${envName}-supapi-supplier-quotas`;
20+
export const UPSERT_LETTERS_QUEUE_URL = `https://sqs.${AWS_REGION}.amazonaws.com/${AWS_ACCOUNT_ID}/nhs-${envName}-supapi-letter-updates-queue`;

tests/helpers/event-fixtures.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,34 @@ export function createPreparedV1Event(overrides: Record<string, unknown> = {}) {
3939
};
4040
}
4141

42-
export function createPreparedEventBatchWithSameDomainId(
42+
export function createPendingEventBatchWithSameDomainId(
4343
overrides: Record<string, unknown> = {},
4444
) {
4545
return [
46-
createPreparedV1Event(overrides),
47-
createPreparedV1Event(overrides),
48-
createPreparedV1Event(overrides),
46+
createPendingAllocatedEvent(overrides),
47+
createPendingAllocatedEvent(overrides),
48+
createPendingAllocatedEvent(overrides),
4949
];
5050
}
51+
52+
export function createPendingAllocatedEvent(
53+
overrides: Record<string, unknown> = {},
54+
) {
55+
const letterEvent = createPreparedV1Event(overrides);
56+
const allocationDetails = {
57+
supplierSpec: {
58+
supplierId: "supplier1",
59+
specId: "spec1",
60+
priority: 1,
61+
billingId: "billing1",
62+
},
63+
allocationStatus: {
64+
status: "PENDING",
65+
},
66+
};
67+
68+
return {
69+
letterEvent,
70+
allocationDetails,
71+
};
72+
}

tests/helpers/send-sqs-event.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {
2+
SQSClient,
3+
SendMessageBatchCommand,
4+
SendMessageCommand,
5+
} from "@aws-sdk/client-sqs";
6+
import { UPSERT_LETTERS_QUEUE_URL } from "tests/constants/api-constants";
7+
import { logger } from "tests/helpers/pino-logger";
8+
9+
const sqsClient = new SQSClient({});
10+
11+
export async function sendSqsEvent(messageBody: string): Promise<void> {
12+
logger.info(
13+
`Sending message to SQS queue ${UPSERT_LETTERS_QUEUE_URL}: ${messageBody}`,
14+
);
15+
await sqsClient.send(
16+
new SendMessageCommand({
17+
QueueUrl: UPSERT_LETTERS_QUEUE_URL,
18+
MessageBody: messageBody,
19+
}),
20+
);
21+
}
22+
23+
export async function sendSqsEventBatch(messages: string[]): Promise<void> {
24+
await sqsClient.send(
25+
new SendMessageBatchCommand({
26+
QueueUrl: UPSERT_LETTERS_QUEUE_URL,
27+
Entries: messages.map((message, index) => ({
28+
Id: `message-${index}`,
29+
MessageBody: message,
30+
})),
31+
}),
32+
);
33+
}

0 commit comments

Comments
 (0)