Skip to content

Commit

Permalink
Merge pull request #47 from cap-js/add-scripts-test
Browse files Browse the repository at this point in the history
Add scripts for test pipeline
  • Loading branch information
DanSchlachter authored Feb 27, 2024
2 parents 1db02bc + 6f13d18 commit e01826d
Show file tree
Hide file tree
Showing 14 changed files with 436 additions and 237 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@
"watch": "cds watch",
"start": "cds-serve",
"test": "npx jest --silent",
"add-change-tracking": "npm add @cap-js/change-tracking && cp xmpls/change-tracking.cds ./srv",
"add-change-tracking": "npm add @cap-js/change-tracking && cp xmpls/change-tracking.cds ./srv && cp xmpls/change-tracking.test.js ./test",
"add-telemetry": "npm add @cap-js/telemetry",
"add-attachments": "npm add @cap-js/attachments && cp xmpls/attachments.cds ./srv && cp -r xmpls/content ./db/data/content",
"clone-add-attachments": "git clone https://github.com/cap-js/attachments.git && cp -r attachments/xmpl/db . && cp -r attachments/xmpl/app . && npm add https://github.com/cap-js/attachments.git",
"add-notifications": "npm add @cap-js/notifications && cp xmpls/alert-notifications.js ./srv && cp xmpls/notification-types.json ./srv",
"add-audit-log": "npm add @cap-js/audit-logging && cp xmpls/data-privacy.cds ./srv",
"add-audit-log": "npm add @cap-js/audit-logging && cp xmpls/data-privacy.cds ./srv && cp xmpls/audit-log.test.js ./test",
"add-remote-service": "cp -r xmpls/remote-service/ .",
"add-messaging": "cp -r xmpls/messaging/ .",
"add-all-xmpls": "npm run add-remote-service && npm run add-messaging && npm run add-change-tracking && npm run add-audit-log",
"reset": "read -p 'This will irreversibly reset your working directory including ALL files in this git repo. Continue?' -n 1 -r && echo && if [[ $REPLY =~ ^[Yy]$ ]]; then git clean -fd && git reset --hard && npm i; fi"
},
"jest": {
Expand Down
2 changes: 1 addition & 1 deletion test/basics.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const cds = require('@sap/cds/lib')
const { GET, expect, axios } = cds.test(__dirname + '/..')
const { GET, expect, axios } = cds.test(__dirname + '/..', '--with-mocks')

axios.defaults.auth = { username: 'alice' }

Expand Down
2 changes: 1 addition & 1 deletion test/drafts.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const cds = require('@sap/cds/lib')
const { GET, POST, DELETE, PATCH, expect, axios } = cds.test(__dirname + '/..')
const { GET, POST, DELETE, PATCH, expect, axios } = cds.test(__dirname + '/..', '--with-mocks')

axios.defaults.auth = { username: 'alice' }

Expand Down
62 changes: 62 additions & 0 deletions xmpls/audit-log.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const cds = require("@sap/cds");
const { GET, POST, PATCH, DELETE, expect, axios, assert } = cds.test(__dirname + '/..', '--with-mocks')

axios.defaults.auth = { username: "alice" };
describe("Integration Test for AuditLog", () => {
let customerID,audit;
beforeAll(async () => {
audit = await cds.connect.to('audit-log')
});

it("Should return list of Customers", async () => {
const response = await GET("/odata/v4/processor/Customers");

audit.on('SensitiveDataRead', function (req) {
const { event, data } = req
assert.ok(event.includes("SensitiveDataRead"))
})
expect(response.status).to.eql(200);
});


it("Should return list of Customers data by explicitly selecting the fields", async () => {
const response = await GET("/odata/v4/processor/Customers?$select=name");
expect(response.status).to.eql(200);
});


it('Creating a customer with personal data', async () => {
const response = await POST(`/odata/v4/admin/Customers`, {
ID: "{{$guid}}",
firstName: "Bob",
lastName: "Builder",
email: "[email protected]"
});
audit.on('PersonalDataModified', function (req) {
const { event, data } = req
assert.ok(event.includes("PersonalDataModified"))
})
customerID = response.data.ID;
expect(response.status).to.equal(201);
});


it('Updating a customer with personal data details', async () => {
const audit = await cds.connect.to('audit-log')
audit.on('PersonalDataModified', function (req) {
const { event, data } = req
assert.ok(event.includes("PersonalDataModified"))
})
const response = await PATCH(`/odata/v4/admin/Customers('${customerID}')`, {
"addresses": [
{
"city": "Walldorf",
"postCode": "69190",
"streetAddress": "Dietmar-Hopp-Allee 16"
}
]
});
expect(response.status).to.equal(200);
});

});
133 changes: 133 additions & 0 deletions xmpls/change-tracking.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
const cds = require("@sap/cds");
const { GET, POST, PATCH, DELETE , expect, axios} = cds.test(__dirname + '/..', '--with-mocks')

describe("Integration Test for ChangeTracking", () => {
let draftId,incidentId;
axios.defaults.auth = { username: "alice" };
let processorService = null;
let ChangeView = null;
beforeAll(async () => {
processorService = await cds.connect.to('ProcessorService');
ChangeView = processorService.entities.ChangeView;
});
it('Create an incident ', async () => {
const { status, statusText, data } = await POST(`/odata/v4/processor/Incidents`, {
title: 'Urgent attention required !',
status_code: 'N',
"customer": {ID:"1004100"}
});
draftId = data.ID;
expect(status).to.equal(201);
expect(statusText).to.equal('Created');
});

it('+ Activate the draft & check Urgency code as H using custom logic', async () => {
const response = await POST(
`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=false)/ProcessorService.draftActivate`
);
expect(response.status).to.eql(201);
expect(response.data.urgency_code).to.eql('H');
});

it('+ Test the incident status', async () => {
const { status, data: { status_code, ID } } = await GET(`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=true)`);
incidentId = ID;
expect(status).to.eql(200);
expect(status_code).to.eql('N');
});

it('+ Test the title detail in ChangeView', async () => {
const response = await GET(`/odata/v4/processor/Incidents?$filter=ID eq ${draftId}`);
const incidentChanges = await SELECT.from(ChangeView).where({
entity: "sap.capire.incidents.Incidents",
attribute: "title",
})
expect(incidentChanges.length).to.equal(1);
const incidentChange = incidentChanges[0];
expect(incidentChange.entityKey).to.equal(draftId);
expect(incidentChange.attribute).to.equal("title");
expect(incidentChange.modification).to.equal("create");
expect(incidentChange.valueChangedFrom).to.equal("");
expect(incidentChange.valueChangedTo).to.equal("Urgent attention required !");
});

it('+ Test the status detail in ChangeView', async () => {
const response = await GET(`/odata/v4/processor/Incidents?$filter=ID eq ${draftId}`);
const incidentChanges = await SELECT.from(ChangeView).where({
entity: "sap.capire.incidents.Incidents",
attribute: "status",
})
expect(incidentChanges.length).to.equal(1);
const incidentChange = incidentChanges[0];
expect(incidentChange.entityKey).to.equal(draftId);
expect(incidentChange.attribute).to.equal("status");
expect(incidentChange.modification).to.equal("create");
expect(incidentChange.valueChangedFrom).to.equal("");
expect(incidentChange.valueChangedTo).to.equal("N");
});

it('+ Test the customer detail in ChangeView', async () => {
const response = await GET(`/odata/v4/processor/Incidents?$filter=ID eq ${draftId}`);
const incidentChanges = await SELECT.from(ChangeView).where({
entity: "sap.capire.incidents.Incidents",
attribute: "customer",
})
expect(incidentChanges.length).to.equal(1);
const incidentChange = incidentChanges[0];
expect(incidentChange.entityKey).to.equal(draftId);
expect(incidentChange.attribute).to.equal("customer");
expect(incidentChange.modification).to.equal("create");
expect(incidentChange.valueChangedFrom).to.equal("");
expect(incidentChange.valueChangedTo).to.equal("Sunny Sunshine");
});

describe("Test Changes for Update Incident", () => {
it(`Should Close the Incident-${draftId}`, async ()=>{
const {status} = await POST(`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=true)/ProcessorService.draftEdit`,
{
"PreserveChanges": true
});
expect(status).to.equal(201);
});
it(`Should Close the Incident-${draftId}`, async ()=>{
const {status } = await PATCH(`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=false)`,{status_code: 'C'});
expect(status).to.equal(200);
});
it('+ Activate the draft & check Status code as C using custom logic', async () => {
const response = await POST(
`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=false)/ProcessorService.draftActivate`
);
expect(response.status).to.eql(200);
});
it('+ Test the status detail in ChangeView', async () => {
const response = await GET(`/odata/v4/processor/Incidents?$filter=ID eq ${draftId}`);
const incidentChanges = await SELECT.from(ChangeView).where({
entity: "sap.capire.incidents.Incidents",
attribute: "status",
modification: 'update',
})
expect(incidentChanges.length).to.equal(1);
const incidentChange = incidentChanges[0];
expect(incidentChange.entityKey).to.equal(draftId);
expect(incidentChange.attribute).to.equal("status");
expect(incidentChange.modification).to.equal("update");
expect(incidentChange.valueChangedFrom).to.equal("N");
expect(incidentChange.valueChangedTo).to.equal("C");
});
});

describe("Test Changes for Delete Incident", () => {
it('- Delete the Incident', async () => {
const response = await DELETE(`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=true)`);
expect(response.status).to.eql(204);
});

it('+ Test the status detail in ChangeView', async () => {
const incidentChanges = await SELECT.from(ChangeView).where({
entity: "sap.capire.incidents.Incidents",
attribute: "status",
})
expect(incidentChanges.length).to.equal(0);
});
});
});
2 changes: 1 addition & 1 deletion xmpls/messaging/app/incidents/field.cds
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ProcessorService as service from '../../srv/processor-service';
using ProcessorService as service from '../../srv/services.cds';
using from './annotations.cds';
annotate service.Incidents with @(
/*adding email to the object page enables users to view the
Expand Down
20 changes: 18 additions & 2 deletions xmpls/messaging/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,27 @@
"scripts": {
"watch": "cds watch",
"start": "cds-serve",
"test": "npx jest --silent"
"test": "npx jest --silent",
"add-change-tracking": "npm add @cap-js/change-tracking && cp xmpls/change-tracking.cds ./srv && cp xmpls/change-tracking.test.js ./test",
"add-telemetry": "npm add @cap-js/telemetry",
"add-attachments": "npm add @cap-js/attachments && cp xmpls/attachments.cds ./srv && cp -r xmpls/content ./db/data/content",
"clone-add-attachments": "git clone https://github.com/cap-js/attachments.git && cp -r attachments/xmpl/db . && cp -r attachments/xmpl/app . && npm add https://github.com/cap-js/attachments.git",
"add-notifications": "npm add @cap-js/notifications && cp xmpls/alert-notifications.js ./srv && cp xmpls/notification-types.json ./srv",
"add-audit-log": "npm add @cap-js/audit-logging && cp xmpls/data-privacy.cds ./srv && cp xmpls/audit-log.test.js ./test",
"add-remote-service": "cp -r xmpls/remote-service/ .",
"add-messaging": "cp -r xmpls/messaging/ .",
"add-all-xmpls": "npm run add-remote-service && npm run add-messaging && npm run add-change-tracking && npm run add-audit-log",
"reset": "read -p 'This will irreversibly reset your working directory including ALL files in this git repo. Continue?' -n 1 -r && echo && if [[ $REPLY =~ ^[Yy]$ ]]; then git clean -fd && git reset --hard && npm i; fi"
},
"private": true,
"sapux": [
"app/incidents"
],
"jest": {
"modulePathIgnorePatterns": [
"<rootDir>/xmpls/"
]
},
"cds": {
"requires": {
"incidents-app": {
Expand All @@ -55,7 +70,8 @@
"users": {
"alice": {
"roles": [
"support"
"support",
"admin"
]
},
"bob": {
Expand Down
2 changes: 1 addition & 1 deletion xmpls/messaging/srv/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class ProcessorService extends cds.ApplicationService {
address.email(emails => {
emails('*')})
}).where({BusinessPartner: Id}));
customer.email = customer.email[0]?.email
if(customer){
customer.email = customer.email[0]?.email
const result= await cds.run(UPDATE(Customers).where({ID: customer.ID}).set({email:customer.email}));
console.log("result",result);
}
Expand Down
106 changes: 106 additions & 0 deletions xmpls/messaging/test/messaging.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const cds = require("@sap/cds");
const { GET, POST, PATCH, DELETE, expect, axios } = cds.test(__dirname + '/..', '--with-mocks')

describe("Integration Test for Eventing", () => {
let draftId,incidentId;
axios.defaults.auth = { username: "alice" };
describe("GET should return 200", () => {

it("Should return list of Business Partners", async () => {
const response = await GET("/odata/v4/api-business-partner/A_BusinessPartner");
expect(response.status).to.eql(200);
});

it("Should return list of Business Partners Address", async () => {
const response = await GET("/odata/v4/api-business-partner/A_BusinessPartnerAddress");
expect(response.status).to.eql(200);
});
it("Should return list of Business Partners Email Address", async () => {
const response = await GET("/odata/v4/api-business-partner/A_AddressEmailAddress");
expect(response.status).to.eql(200);
});
it("Should return list of Business Partners Address PhoneNumber", async () => {
const response = await GET("/odata/v4/api-business-partner/A_AddressPhoneNumber");
expect(response.status).to.eql(200);
});
});

describe('Draft Choreography APIs', () => {
it('Create an incident ', async () => {
const { status, statusText, data } = await POST(`/odata/v4/processor/Incidents`, {
title: 'Urgent attention required !',
status_code: 'N',
"customer": {ID:"1004100"}
});
draftId = data.ID;
expect(status).to.equal(201);
expect(statusText).to.equal('Created');
});
it('+ Activate the draft & check Urgency code as H using custom logic', async () => {
const response = await POST(
`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=false)/ProcessorService.draftActivate`
);
expect(response.status).to.eql(201);
expect(response.data.urgency_code).to.eql('H');
});
it('+ Test the customer detail', async () => {
const response = await GET(`/odata/v4/processor/Incidents?$filter=ID eq ${draftId}`);
expect(response.status).to.eql(200);
expect(response.data.value).to.exist;
expect(response.data.value[0]).to.contains({
"customer_ID": "1004100"
});
incidentId = response.data.ID;
});

describe("Create annd Update Business Partner", () => {
it("Update Business Partner", async () => {
const response = await PATCH(
`/odata/v4/api-business-partner/A_BusinessPartner('1004100')`,
{
to_BusinessPartnerAddress: [{
AddressID: "457",
to_EmailAddress:[{
AddressID: "457",
Person: "johnson",
OrdinalNumber: "334",
EmailAddress: "[email protected]"
}]
}]
}
);
expect(response.status).to.eql(200);
});
describe("Verify the updated Business Partner", () => {
it("Verify the Address of Business Partner", async () => {
const response = await GET(`/odata/v4/api-business-partner/A_BusinessPartnerAddress?$filter=BusinessPartner eq '1004100'`);
expect(response.status).to.eql(200);
expect(response.data.value).to.exist;
expect(response.data.value[0]).to.contains({
AddressID: "457",
});
});

it("Verify the Email address of Business Partner", async () => {
const response = await GET(`/odata/v4/api-business-partner/A_AddressEmailAddress?$filter=AddressID eq '457'`);
expect(response.status).to.eql(200);
expect(response.data.value).to.exist;
expect(response.data.value[0]).to.contains({
AddressID: "457",
Person: "johnson",
OrdinalNumber: "334",
EmailAddress: "[email protected]"
});
});
});
});

it(`Should Close the Incident-${draftId}`, async ()=>{
const {status} = await POST(`/odata/v4/processor/Incidents(ID=${draftId},IsActiveEntity=true)/ProcessorService.draftEdit`,
{
"PreserveChanges": true
});
expect(status).to.equal(201);
});
});
});
Loading

0 comments on commit e01826d

Please sign in to comment.