Skip to content

Commit

Permalink
Merge pull request #20 from cap-js/unit_tests_for_ntypes
Browse files Browse the repository at this point in the history
Unit tests for ntypes
  • Loading branch information
RamIndia authored Oct 23, 2023
2 parents 7874669 + 5f763a7 commit bbad0f9
Show file tree
Hide file tree
Showing 7 changed files with 909 additions and 37 deletions.
5 changes: 0 additions & 5 deletions .prettierrc

This file was deleted.

6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tabWidth": 2,
"useTabs": false,
"printWidth": 200,
"trailingComma" : "none"
}
6 changes: 5 additions & 1 deletion lib/content-deployment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ async function deployNotificationTypes() {
setGlobalLogLevel("error");

// read notification types
const notificationTypes = readFile(cds.env.requires?.notifications?.types);
const notificationTypes = readFile(cds.env?.requires?.notifications?.types);

if (validateNotificationTypes(notificationTypes)) {
await processNotificationTypes(notificationTypes);
}
}

deployNotificationTypes();

module.exports = {
deployNotificationTypes
}
52 changes: 23 additions & 29 deletions lib/notificationTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ const cds = require("@sap/cds");
const LOG = cds.log('notifications');

const defaultTemplate = {
"NotificationTypeKey": "Default",
"NotificationTypeVersion": "1",
"Templates": [
NotificationTypeKey: "Default",
NotificationTypeVersion: "1",
Templates: [
{
"Language": "en",
"Description": "Other Notifications",
"TemplatePublic": "{{title}}",
"TemplateSensitive": "{{title}}",
"TemplateGrouped": "Other Notifications",
"TemplateLanguage": "mustache",
"Subtitle": "{{description}}"
Language: "en",
Description: "Other Notifications",
TemplatePublic: "{{title}}",
TemplateSensitive: "{{title}}",
TemplateGrouped: "Other Notifications",
TemplateLanguage: "mustache",
Subtitle: "{{description}}"
}
]
};

function fromOdataArrayFormat(objectInArray) {
if (objectInArray === undefined || objectInArray === null || Array.isArray(objectInArray)) {
return objectInArray;
return (objectInArray === undefined || objectInArray === null) ? [] : objectInArray;
} else {
return objectInArray.results;
return (objectInArray.results === undefined || objectInArray.results === null) ? [] : objectInArray.results;
}
}

Expand Down Expand Up @@ -63,15 +63,15 @@ async function getNotificationTypes() {
const notificationDestination = await getNotificationDestination();
const response = await executeHttpRequest(notificationDestination, {
url: `${NOTIFICATION_TYPES_API_ENDPOINT}/NotificationTypes?$format=json&$expand=Templates,Actions,DeliveryChannels`,
method: "get",
method: "get"
});
return response.data.d.results;
}

async function createNotificationType(notificationType) {
const notificationDestination = await getNotificationDestination();
const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
url: NOTIFICATION_TYPES_API_ENDPOINT,
url: NOTIFICATION_TYPES_API_ENDPOINT
});

LOG._warn && LOG.warn(
Expand All @@ -82,15 +82,15 @@ async function createNotificationType(notificationType) {
url: `${NOTIFICATION_TYPES_API_ENDPOINT}/NotificationTypes`,
method: "post",
data: notificationType,
headers: csrfHeaders,
headers: csrfHeaders
});
return response.data?.d ?? response;
}

async function updateNotificationType(id, notificationType) {
const notificationDestination = await getNotificationDestination();
const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
url: NOTIFICATION_TYPES_API_ENDPOINT,
url: NOTIFICATION_TYPES_API_ENDPOINT
});

LOG._info && LOG.info(
Expand All @@ -101,15 +101,15 @@ async function updateNotificationType(id, notificationType) {
url: `${NOTIFICATION_TYPES_API_ENDPOINT}/NotificationTypes(guid'${id}')`,
method: "patch",
data: notificationType,
headers: csrfHeaders,
headers: csrfHeaders
});
return response.status;
}

async function deleteNotificationType(notificationType) {
const notificationDestination = await getNotificationDestination();
const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
url: NOTIFICATION_TYPES_API_ENDPOINT,
url: NOTIFICATION_TYPES_API_ENDPOINT
});

LOG._info && LOG.info(
Expand All @@ -119,16 +119,12 @@ async function deleteNotificationType(notificationType) {
const response = await executeHttpRequest(notificationDestination, {
url: `${NOTIFICATION_TYPES_API_ENDPOINT}/NotificationTypes(guid'${notificationType.NotificationTypeId}')`,
method: "delete",
headers: csrfHeaders,
headers: csrfHeaders
});
return response.status;
}

function _createChannelsMap(channels) {
if (channels === null || channels === undefined) {
return {};
}

const channelMap = {};

channels.forEach((channel) => {
Expand Down Expand Up @@ -240,9 +236,9 @@ function isNotificationTypeEqual(oldNotificationType, newNotificationType) {

return (
oldNotificationType.IsGroupable == newNotificationType.IsGroupable &&
areTemplatesEqual(oldNotificationType.Templates.results, fromOdataArrayFormat(newNotificationType.Templates)) &&
areActionsEqual(oldNotificationType.Actions.results, fromOdataArrayFormat(newNotificationType.Actions)) &&
areDeliveryChannelsEqual(oldNotificationType.DeliveryChannels.results, fromOdataArrayFormat(newNotificationType.DeliveryChannels))
areTemplatesEqual(fromOdataArrayFormat(oldNotificationType.Templates), fromOdataArrayFormat(newNotificationType.Templates)) &&
areActionsEqual(fromOdataArrayFormat(oldNotificationType.Actions), fromOdataArrayFormat(newNotificationType.Actions)) &&
areDeliveryChannelsEqual(fromOdataArrayFormat(oldNotificationType.DeliveryChannels), fromOdataArrayFormat(newNotificationType.DeliveryChannels))
);
}

Expand All @@ -262,9 +258,7 @@ async function processNotificationTypes(notificationTypesJSON) {
}

if (!existingType.NotificationTypeKey.startsWith(`${prefix}/`)) {
LOG._info && LOG.info(
`Skipping Notification Type of other application: ${existingType.NotificationTypeKey}.`
);
LOG._info && LOG.info(`Skipping Notification Type of other application: ${existingType.NotificationTypeKey}.`);
continue;
}

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
"lodash": "4.17.21"
},
"devDependencies": {
"jest": "^29.7.0"
"jest": "^29.7.0",
"chai": "^4.3.10"
},
"scripts": {
"lint": "npx eslint .",
"test": "npx jest --silent",
"test": "npx jest",
"test-with-coverage": "npx jest --coverage"
},
"cds": {
Expand Down
48 changes: 48 additions & 0 deletions test/lib/content-deployment.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// import required modules and functions
const cds = require("@sap/cds");
const { validateNotificationTypes, readFile } = require("../../lib/utils");
const { processNotificationTypes } = require("../../lib/notificationTypes");
const { setGlobalLogLevel } = require("@sap-cloud-sdk/util");
const assert = require("chai");

jest.mock("../../lib/utils");
jest.mock("../../lib/notificationTypes");
jest.mock("@sap-cloud-sdk/util");

const contentDeployment = require("../../lib/content-deployment");

describe("contentDeployment", () => {
beforeEach(() => {
jest.clearAllMocks();
});

test("Given valid notification types | When Deploy is called | Then process is called", async () => {
setGlobalLogLevel.mockImplementation(() => undefined);
readFile.mockImplementation(() => []);
validateNotificationTypes.mockImplementation(() => true);
processNotificationTypes.mockImplementation(() => Promise.resolve());

await contentDeployment.deployNotificationTypes();

console.log(setGlobalLogLevel.mock.calls);
assert.expect(setGlobalLogLevel.mock.calls[0][0]).to.be.equal("error");
assert.expect(readFile.mock.calls[0][0]).to.be.equal(cds.env?.requires?.notifications?.types);
assert.expect(validateNotificationTypes.mock.calls[0][0]).to.be.deep.equal([]);
assert.expect(processNotificationTypes.mock.calls[0][0]).to.be.deep.equal([]);
});

test("Given invalid notification types | When Deploy is called | Then process is called", async () => {
setGlobalLogLevel.mockImplementation(() => undefined);
readFile.mockImplementation(() => []);
validateNotificationTypes.mockImplementation(() => false);
processNotificationTypes.mockImplementation(() => Promise.resolve());

await contentDeployment.deployNotificationTypes();

console.log(setGlobalLogLevel.mock.calls);
assert.expect(setGlobalLogLevel.mock.calls[0][0]).to.be.equal("error");
assert.expect(readFile.mock.calls[0][0]).to.be.equal(cds.env?.requires?.notifications?.types);
assert.expect(validateNotificationTypes.mock.calls[0][0]).to.be.deep.equal([]);
assert.expect(processNotificationTypes.mock.calls[0]).to.be.deep.equal(undefined);
});
});
Loading

0 comments on commit bbad0f9

Please sign in to comment.