Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PRDP-311] feat: Updated integration tests for cart implementation #115

Merged
merged 19 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion helm/values-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ microservice-chart:
OTEL_TRACES_SAMPLER: "always_on"
AZURE_FUNCTIONS_MESH_JAVA_OPTS: "-javaagent:/home/site/wwwroot/jmx_prometheus_javaagent-0.19.0.jar=12345:/home/site/wwwroot/config.yaml -javaagent:/home/site/wwwroot/opentelemetry-javaagent.jar -Xmx768m -XX:+UseG1GC"
ECOMMERCE_FILTER_ENABLED: "true"
ENABLE_CART: "false"
ENABLE_CART: "true"
envFieldRef:
APP_NAME: "metadata.labels['app.kubernetes.io/instance']"
APP_VERSION: "metadata.labels['app.kubernetes.io/version']"
Expand Down
2 changes: 1 addition & 1 deletion helm/values-uat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ microservice-chart:
OTEL_TRACES_SAMPLER: "always_on"
AZURE_FUNCTIONS_MESH_JAVA_OPTS: "-javaagent:/home/site/wwwroot/jmx_prometheus_javaagent-0.19.0.jar=12345:/home/site/wwwroot/config.yaml -Xmx768m -XX:+UseG1GC"
ECOMMERCE_FILTER_ENABLED: "true"
ENABLE_CART: "false"
ENABLE_CART: "true"
envFieldRef:
APP_NAME: "metadata.labels['app.kubernetes.io/instance']"
APP_VERSION: "metadata.labels['app.kubernetes.io/version']"
Expand Down
1 change: 1 addition & 0 deletions integration-test/src/config/.env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ BIZ_EVENT_COSMOS_DB_CONTAINER_NAME=biz-events
RECEIPTS_COSMOS_CONN_STRING=<cosmos-connection-string>
RECEIPT_COSMOS_DB_NAME=db
RECEIPT_COSMOS_DB_CONTAINER_NAME=receipts
RECEIPTS_COSMOS_CART_CONTAINER_NAME=cart-for-receipts

DATASTORE_URL=https://api.dev.platform.pagopa.it/receipts/datastore/v1/recoverFailed
3 changes: 2 additions & 1 deletion integration-test/src/config/.env.local
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ BIZ_EVENT_COSMOS_DB_CONTAINER_NAME=biz-events

RECEIPTS_COSMOS_CONN_STRING=<cosmos-connection-string>
RECEIPT_COSMOS_DB_NAME=db
RECEIPT_COSMOS_DB_CONTAINER_NAME=receipts
RECEIPT_COSMOS_DB_CONTAINER_NAME=receipts
RECEIPTS_COSMOS_CART_CONTAINER_NAME=cart-for-receipts
1 change: 1 addition & 0 deletions integration-test/src/config/.env.uat
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ BIZ_EVENT_COSMOS_DB_CONTAINER_NAME=biz-events
RECEIPTS_COSMOS_CONN_STRING=<cosmos-connection-string>
RECEIPT_COSMOS_DB_NAME=db
RECEIPT_COSMOS_DB_CONTAINER_NAME=receipts
RECEIPTS_COSMOS_CART_CONTAINER_NAME=cart-for-receipts

DATASTORE_URL=https://api.uat.platform.pagopa.it/receipts/datastore/v1/recoverFailed
15 changes: 14 additions & 1 deletion integration-test/src/features/receipt_pdf_datastore.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,17 @@ Feature: All about payment events consumed by Azure functions receipt-pdf-datast
When biz event has been properly stored into receipt datastore after 10000 ms with eventId "receipt-datastore-int-test-id-1"
Then the receipts datastore returns the receipt
And the receipt has eventId "receipt-datastore-int-test-id-1"
And the receipt has not the status "NOT_QUEUE_SENT"
And the receipt has not the status "NOT_QUEUE_SENT"
And the receipt has not a datastore reason error message

Scenario: a list of biz event from the same cart stored on biz-events datastore is stored into receipts datastore
Given a list of 5 bizEvents starting with id "receipt-datastore-int-test-id-2" and transactionId "receipt-datastore-int-test-transactionId-2" stored on biz-events datastore with status DONE
When cart event has been properly stored into receipt datastore after 20000 ms with id "receipt-datastore-int-test-transactionId-2"
Then the receipts datastore return the cart event
And the cart event has status "SENT"
And receipt has been properly stored into receipt datastore after 0 ms with eventId "receipt-datastore-int-test-transactionId-2"
And the receipts datastore returns the receipt
And the receipt has eventId "receipt-datastore-int-test-transactionId-2"
And the receipt has not the status "NOT_QUEUE_SENT"
And the receipt has not a datastore reason error message

Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ async function getDocumentByIdFromBizEventsDatastore(id) {
.fetchAll();
}

async function createDocumentInBizEventsDatastore(id) {
let event = createEvent(id);
async function createDocumentInBizEventsDatastore(id, transactionId, totalNotice) {
let event = createEvent(id, transactionId, totalNotice);
try {
return await container.items.create(event);
} catch (err) {
Expand Down
17 changes: 13 additions & 4 deletions integration-test/src/step_definitions/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

function createEvent(id) {
function createEvent(id, transactionId, totalNotice) {
let json_event = {
"id": id,
"version": "2",
Expand Down Expand Up @@ -76,7 +76,7 @@ function createEvent(id) {
"paymentToken": "9851395f09544a04b288202299193ca6",
"amount": "10.0",
"fee": "2.0",
"totalNotice": "1",
"totalNotice": totalNotice ? totalNotice : "1",
"paymentMethod": "creditCard",
"touchpoint": "app",
"remittanceInformation": "TARI 2021",
Expand Down Expand Up @@ -110,7 +110,7 @@ function createEvent(id) {
},
"transaction": {
"idTransaction": "123456",
"transactionId": "123456",
"transactionId": transactionId ? transactionId : "123456",
"grandTotal": 0,
"amount": 0,
"fee": 0
Expand Down Expand Up @@ -145,6 +145,15 @@ function createReceipt(id, fiscalCode, pdfName) {
return receipt
}

function createCartEvent(id, listOfBizEventsIds) {
return {
"id":id,
"cartPaymentId": listOfBizEventsIds,
"totalNotice": listOfBizEventsIds.length,
"status": "INSERTED"
}
}

module.exports = {
createEvent, sleep
createEvent, sleep, createCartEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const assert = require('assert');
const { After, Given, When, Then, setDefaultTimeout } = require('@cucumber/cucumber');
const { sleep, recoverFailedEvent } = require("./common");
const { createDocumentInBizEventsDatastore, deleteDocumentFromBizEventsDatastore } = require("./biz_events_datastore_client");
const { getDocumentByIdFromReceiptsDatastore, deleteDocumentFromReceiptsDatastoreByEventId, deleteDocumentFromReceiptsDatastore, updateReceiptToFailed } = require("./receipts_datastore_client");
const { getDocumentByIdFromReceiptsDatastore, deleteDocumentFromReceiptsDatastoreByEventId, deleteDocumentFromReceiptsDatastore, deleteDocumentFromCartDatastore, createDocumentInCartDatastore, getCartDocumentByIdFromReceiptsDatastore } = require("./receipts_datastore_client");

// set timeout for Hooks function, it allows to wait for long task
setDefaultTimeout(360 * 1000);
Expand All @@ -13,6 +13,8 @@ this.responseToCheck = null;
this.response = null;
this.receiptId = null;
this.event = null;
this.cartId = null;
this.listOfBizEventsIds = null;

// After each Scenario
After(async function () {
Expand All @@ -23,10 +25,20 @@ After(async function () {
if (this.receiptId != null) {
await deleteDocumentFromReceiptsDatastore(this.receiptId);
}
if(this.cartId != null) {
await deleteDocumentFromCartDatastore(this.cartId);
}
if(this.listOfBizEventsIds?.length > 0) {
for(bizEvent of this.listOfBizEventsIds){
await deleteDocumentFromBizEventsDatastore(bizEvent);
}
}
this.eventId = null;
this.responseToCheck = null;
this.receiptId = null;
this.event = null;
this.listOfBizEventsIds = null;
this.cartId = null;
});

Given('a random biz event with id {string} stored on biz-events datastore with status DONE', async function (id) {
Expand All @@ -39,18 +51,66 @@ Given('a random biz event with id {string} stored on biz-events datastore with s
assert.strictEqual(bizEventStoreResponse.statusCode, 201);
});

Given('a list of {int} bizEvents starting with id {string} and transactionId {string} stored on biz-events datastore with status DONE', async function (numberOfEvents, id, transactionId) {
this.eventId = transactionId;
this.listOfBizEventsIds = [];

for(let i = 0; i < numberOfEvents; i++) {
let finalId = id+i;

await deleteDocumentFromBizEventsDatastore(finalId);
await deleteDocumentFromReceiptsDatastoreByEventId(finalId);

let bizEventStoreResponse = await createDocumentInBizEventsDatastore(finalId, transactionId, `${numberOfEvents}`);
assert.strictEqual(bizEventStoreResponse.statusCode, 201);

this.listOfBizEventsIds.push(finalId);
}
});

Given('a cart event with id {string} containing the ids the bizEvents', async function (id) {
this.cartId = id;
// prior cancellation to avoid dirty cases
await deleteDocumentFromCartDatastore(id);

let cartResponse = await createDocumentInCartDatastore(id, this.listOfBizEventsIds);
assert.strictEqual(cartResponse.statusCode, 201);
});

When('biz event has been properly stored into receipt datastore after {int} ms with eventId {string}', async function (time, eventId) {
// boundary time spent by azure function to process event
await sleep(time);
this.responseToCheck = await getDocumentByIdFromReceiptsDatastore(eventId);
});

When('receipt has been properly stored into receipt datastore after {int} ms with eventId {string}', async function (time, id) {
// boundary time spent by azure function to process event
await sleep(time);
this.responseToCheck = await getDocumentByIdFromReceiptsDatastore(id);
});

When('cart event has been properly stored into receipt datastore after {int} ms with id {string}', async function (time, id) {
// boundary time spent by azure function to process event
await sleep(time);
this.responseToCheck = await getCartDocumentByIdFromReceiptsDatastore(id);
});

Then('the receipts datastore returns the receipt', async function () {
assert.notStrictEqual(this.responseToCheck.resources.length, 0);
this.receiptId = this.responseToCheck.resources[0].id;
assert.strictEqual(this.responseToCheck.resources.length, 1);
});

Then('the receipts datastore return the cart event', async function () {
assert.notStrictEqual(this.responseToCheck.resources.length, 0);
this.cartId = this.responseToCheck.resources[0].id;
assert.strictEqual(this.responseToCheck.resources.length, 1);
});

Then("the cart event has status {string}", function(status) {
assert.strictEqual(this.responseToCheck.resources[0].status, status);
});

Then('the receipt has eventId {string}', function (targetId) {
assert.strictEqual(this.responseToCheck.resources[0].eventId, targetId);
});
Expand All @@ -59,6 +119,14 @@ Then('the receipt has not the status {string}', function (targetStatus) {
assert.notStrictEqual(this.responseToCheck.resources[0].status, targetStatus);
});

Then("the receipt has not a datastore reason error message", function(){
let receiptResponse = this.responseToCheck.resources[0];
if(receiptResponse?.reasonErr.message){
let booleanResponseErr = receiptResponse.reasonErr.message.includes("BizEventToReceiptService") || !receiptResponse?.event?.mount;

assert.strictEqual(booleanResponseErr, false);
}
});



42 changes: 36 additions & 6 deletions integration-test/src/step_definitions/receipts_datastore_client.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const { CosmosClient } = require("@azure/cosmos");
const { createReceipt } = require("./common");
const { createCartEvent } = require("./common");

const cosmos_db_conn_string = process.env.RECEIPTS_COSMOS_CONN_STRING || "";
const databaseId = process.env.RECEIPT_COSMOS_DB_NAME;
const receiptContainerId = process.env.RECEIPT_COSMOS_DB_CONTAINER_NAME;
const cosmos_db_conn_string = process.env.RECEIPTS_COSMOS_CONN_STRING || "";
const databaseId = process.env.RECEIPT_COSMOS_DB_NAME;
const receiptContainerId = process.env.RECEIPT_COSMOS_DB_CONTAINER_NAME;
const cartContainerId = process.env.RECEIPTS_COSMOS_CART_CONTAINER_NAME;

const client = new CosmosClient(cosmos_db_conn_string);
const receiptContainer = client.database(databaseId).container(receiptContainerId);
const cartContainer = client.database(databaseId).container(cartContainerId);

async function getDocumentByIdFromReceiptsDatastore(id) {
return await receiptContainer.items
Expand All @@ -17,7 +19,16 @@ async function getDocumentByIdFromReceiptsDatastore(id) {
.fetchNext();
}

async function deleteDocumentFromReceiptsDatastoreByEventId(eventId){
async function getCartDocumentByIdFromReceiptsDatastore(id) {
return await cartContainer.items
.query({
query: "SELECT * from c WHERE c.id=@id",
parameters: [{ name: "@id", value: id }]
})
.fetchNext();
}

async function deleteDocumentFromReceiptsDatastoreByEventId(eventId) {
let documents = await getDocumentByIdFromReceiptsDatastore(eventId);

documents?.resources?.forEach(el => {
Expand All @@ -35,6 +46,25 @@ async function deleteDocumentFromReceiptsDatastore(id) {
}
}

async function createDocumentInCartDatastore(id, listOfBizEventsIds) {
let event = createCartEvent(id, listOfBizEventsIds);
try {
return await cartContainer.items.create(event);
} catch (err) {
console.log(err);
}
}

async function deleteDocumentFromCartDatastore(id) {
try {
return await cartContainer.item(id, id).delete();
} catch (error) {
if (error.code !== 404) {
console.log(error)
}
}
}

module.exports = {
getDocumentByIdFromReceiptsDatastore, deleteDocumentFromReceiptsDatastoreByEventId, deleteDocumentFromReceiptsDatastore
getDocumentByIdFromReceiptsDatastore, deleteDocumentFromReceiptsDatastoreByEventId, deleteDocumentFromReceiptsDatastore, createDocumentInCartDatastore, deleteDocumentFromCartDatastore, getCartDocumentByIdFromReceiptsDatastore
}
Loading
Loading