const { EventHubConsumerClient, earliestEventPosition } = require("@azure/event-hubs");
const { ContainerClient } = require("@azure/storage-blob");
const { BlobCheckpointStore } = require("@azure/eventhubs-checkpointstore-blob");
const FIGmdLogger = require(process.cwd() + "/utility/logger").logger;
const secretManagerConfig = require(process.cwd() + '/utility/secretmanagerhandler');
const secretValue = secretManagerConfig.secretValue;
const { publisher } = require(process.cwd() + "/publisher/publisherbusiness");
const moment = require('moment');
/* main function sets up the Event Hub consumer, processes filtered events, publishes messages to Google Pub/Sub,
updates checkpoints, and logs events and errors.
*/
async function main() {
try {
// Create a blob container client to interact with the Azure Blob container.
const containerClient = new ContainerClient(secretValue?.AZURE_STORAGE_CONNECTION_STRING, secretValue?.AZURE_CONTAINER_NAME);
// Initialize a blob checkpoint store using the container client for event checkpointing.
const checkpointStore = new BlobCheckpointStore(containerClient);
// Create a consumer client for the event hub by specifying the checkpoint store.
const consumerClient = new EventHubConsumerClient(secretValue?.AZURE_EV_CONSUMER_GROUP, secretValue?.AZURE_EH_CONNECTION_STRING, process.env.AZURE_EVENT_HUB_NAME, checkpointStore);
// Subscribe to the events, and specify handlers for processing the events and errors.
consumerClient.subscribe({
processEvents: async (aevents, context) => {
if (events.length === 0) {
FIGmdLogger.info("ReceiveEvent processEvents:No events received within wait time. Waiting for next interval.");
return;
}
// Filter events based on subscription key and ensure EventName is 'TaskCompleted' and Status is 'completed'
const filteredEvents = events.filter((event) => (event?.body?.SubscriptionKey === secretValue?.CDXP_SUBSCRIPTION_KEY) && (event?.body?.EventName.toLowerCase() === process?.env?.CDXP_READY_FOR_DOWNLOAD_EVENT_NAME) && (event?.body?.Status.toLowerCase() === process.env.CDXP_COMPETED_EVENT_STATUS));
const lastEvent = events[events.length - 1];
await context.updateCheckpoint(lastEvent);
FIGmdLogger.info(`ReceiveEvent processEvents:Checkpoint updated for partition: '${context.partitionId}'`, { eventId: lastEvent?.body?.EventId, taskId: lastEvent?.body?.TaskId, subscriptionKey: lastEvent?.body?.SubscriptionKey, eventName: lastEvent?.body?.EventName, eventStatus: lastEvent?.body?.Status });
},
processError: async (err, context) => {
FIGmdLogger.error(`ReceiveEvent processError:Error processing events from partition: '${context.partitionId}. Error: ${err.message}`, { consumerGroup: context.consumerGroup, partitionId: context.partitionId, errorMessage: err.message, errStack: err.stack });
}
},
// Configure the consumer to start from the earliest event position and set the maximum batch size for event processing.
{ startPosition: earliestEventPosition, maxBatchSize: process.env.MAX_BATCH_SIZE }
);
} catch (error) {
throw error;
}
}
main().catch((err) => {
const { message, stack } = err;
FIGmdLogger.error(`ReceiveEvent error occurred: ${message}`, { stackTrace: stack });
});`your text`
I need to write unit tests for the provided code using Mocha, Chai, and Sinon. Could you please help me? Below are the scenarios that need to be tested:
-
Container Client Connection
Verify that the container client connects with the given credentials (AZURE_STORAGE_CONNECTION_STRING
,AZURE_CONTAINER_NAME
). -
Blob Checkpoint Store Connection
Check if the Azure Blob checkpoint store is connected with the receivedcontainerClient
. -
EventHubConsumerClient Connection
Ensure that theEventHubConsumerClient
connects with the provided credentials (AZURE_EV_CONSUMER_GROUP
,AZURE_EH_CONNECTION_STRING
,AZURE_EVENT_HUB_NAME
) and thecheckpointStore
. -
ConsumerClient Subscribe Method
Test if thesubscribe
method of the consumer client works as expected. -
processEvents Method
Verify that theprocessEvents
method functions correctly when events are received. -
processError Method
Ensure that theprocessError
method works properly when an error occurs. -
Event Filtering
Confirm that the event filtering mechanism is working as intended. -
Ignore Logs
You can ignore testing the logs.