Skip to content

Commit

Permalink
initial commit - tests cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aramovic79 committed Nov 19, 2024
1 parent 1b8531c commit 4345d76
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 116 deletions.
10 changes: 5 additions & 5 deletions __tests__/noOrdInCdsrc.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const cds = require("@sap/cds");
const ord = require("../lib/ord");
const path = require("path");
const csn = require("./__mocks__/publicResourcesCsn.json");

jest.mock("../lib/date", () => ({
getRFC3339Date: jest.fn(() => "2024-11-04T14:33:25+01:00")
}));

describe("Tests for ORD document when .cdsrc.json has no `ord` property", () => {
let ord;

beforeAll(() => {
jest.spyOn(require("../lib/date"), "getRFC3339Date").mockReturnValue("2024-11-04T14:33:25+01:00");
ord = require("../lib/ord");
cds.root = path.join(__dirname, "bookshop");
cds.env = {};
});

afterAll(() => {
jest.clearAllMocks();
jest.resetAllMocks();
});

test("Successfully create ORD Documents with no `ord` in .cdsrc.json", () => {
Expand Down
25 changes: 9 additions & 16 deletions __tests__/ordCdsrc.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
const cds = require("@sap/cds");
const ord = require("../lib/ord");
const path = require("path");

// Mock the @sap/cds module
jest.mock("@sap/cds", () => {
const path = require("path");
let cds = jest.requireActual("@sap/cds");
cds.root = path.join(__dirname, "bookshop");

return cds;
});

jest.mock("../lib/date", () => ({
getRFC3339Date: jest.fn(() => "2024-11-04T14:33:25+01:00")
}));


describe("Tests for default ORD document when .cdsrc.json is present", () => {
let csn;
let csn, ord;

beforeAll(async () => {
cds.root = path.join(__dirname, "bookshop");
csn = await cds.load(path.join(cds.root, "srv"));
jest.spyOn(require("../lib/date"), "getRFC3339Date").mockReturnValue("2024-11-04T14:33:25+01:00");
ord = require("../lib/ord");
});

afterAll(() => {
jest.clearAllMocks();
jest.resetAllMocks();
});

test("Successfully create ORD Documents with defaults", () => {
Expand Down
17 changes: 8 additions & 9 deletions __tests__/ordPackageJson.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
const cds = require("@sap/cds");
const ord = require("../lib/ord");
const path = require("path");

jest.mock("../lib/date", () => ({
getRFC3339Date: jest.fn(() => "2024-11-04T14:33:25+01:00")
}));

describe("Tests for default ORD document when .cdsrc.json is not present", () => {
let csn;
let csn, ord;

beforeAll(async () => {
csn = await cds.load(path.join(__dirname, "bookshop", "srv"));
jest.spyOn(require("../lib/date"), "getRFC3339Date").mockReturnValue("2024-11-04T14:33:25+01:00");
ord = require("../lib/ord");
cds.root = path.join(__dirname, "bookshop");
csn = await cds.load(path.join(cds.root, "srv"));
});

beforeEach(() => {
cds.root = path.join(__dirname, "bookshop");
afterAll(() => {
jest.clearAllMocks();
jest.resetAllMocks();
});

test("Successfully create ORD Documents with defaults", () => {
Expand Down
18 changes: 8 additions & 10 deletions __tests__/protectedServices.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const cds = require("@sap/cds");
const internal_csn = require("./__mocks__/internalResourcesCsn.json");
const ord = require("../lib/ord");
const csnInternal = require("./__mocks__/internalResourcesCsn.json");
const csnPrivate = require("./__mocks__/privateResourcesCsn.json");
const path = require("path");
const private_csn = require("./__mocks__/privateResourcesCsn.json");

let ord;
function checkOrdDocument(csn) {
const document = ord(csn);

Expand All @@ -13,11 +13,6 @@ function checkOrdDocument(csn) {
expect(document.eventResources).toHaveLength(0);
}


jest.mock("../lib/date", () => ({
getRFC3339Date: jest.fn(() => "2024-11-04T14:33:25+01:00")
}));

describe("Tests for ORD document when there is no public service", () => {
beforeAll(() => {
cds.root = path.join(__dirname, "bookshop");
Expand All @@ -27,17 +22,20 @@ describe("Tests for ORD document when there is no public service", () => {
description: "this is my custom description",
policyLevel: "sap:core:v1"
};
jest.spyOn(require("../lib/date"), "getRFC3339Date").mockReturnValue("2024-11-04T14:33:25+01:00");
ord = require("../lib/ord");
});

afterAll(() => {
jest.clearAllMocks();
jest.resetAllMocks();
});

test("All services are private: Successfully create ORD Documents without packages, empty apiResources and eventResources lists", () => {
checkOrdDocument(private_csn);
checkOrdDocument(csnPrivate);
});

test("All services are internal: Successfully create ORD Documents without packages, empty apiResources and eventResources lists", () => {
checkOrdDocument(internal_csn);
checkOrdDocument(csnInternal);
});
});
189 changes: 113 additions & 76 deletions __tests__/unittest/extendOrdWithCustom.test.js
Original file line number Diff line number Diff line change
@@ -1,116 +1,151 @@
const cds = require("@sap/cds");
const path = require('path');
const { extendCustomORDContentIfExists } = require('../../lib/extendOrdWithCustom');
const path = require("path");
const { extendCustomORDContentIfExists } = require("../../lib/extendOrdWithCustom");

jest.mock("@sap/cds", () => {
const actualCds = jest.requireActual('@sap/cds');
return {
...actualCds,
env: {},
log: jest.fn(() => ({
levels: { DEBUG: 0, WARN: 1 },
warn: jest.fn(() => console.warn('Mocked warning')),
error: jest.fn(() => console.error('Mocked error')),
})),
};
});

describe('extendOrdWithCustom', () => {
describe("extendOrdWithCustom", () => {
let appConfig = {};
let warningSpy;

beforeAll(() => {
cds.env = {};
warningSpy = jest.spyOn(console, "warn");
});

beforeEach(() => {
appConfig = {
env: {
customOrdContentFile: 'customOrdContentFile.json',
customOrdContentFile: "customOrdContentFile.json",
},
};
});

describe('extendCustomORDContentIfExists', () => {
it('should skip if there is no customOrdContentFile property in the .cdsrc.json', () => {
afterAll(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});

describe("extendCustomORDContentIfExists", () => {
it("should skip if there is no customOrdContentFile property in the .cdsrc.json", () => {
const ordContent = {};
appConfig.env.customOrdContentFile = undefined;
const result = extendCustomORDContentIfExists(appConfig, ordContent);
expect(result).toEqual(ordContent);
});

it('should skip if customOrdContentFile property in the .cdsrc.json points to NON-EXISTING custom ord file', () => {
it("should skip if customOrdContentFile property in the .cdsrc.json points to NON-EXISTING custom ord file", () => {
const ordContent = {};
appConfig.env.customOrdContentFile = "./ord/NotExistingCustom.ord.json";
const result = extendCustomORDContentIfExists(appConfig, ordContent);
const result = extendCustomORDContentIfExists(
appConfig,
ordContent
);
expect(result).toEqual(ordContent);
});

it('should ignore and log warn if found ord top-level primitive property in customOrdFile', () => {
it("should ignore and log warn if found ord top-level primitive property in customOrdFile", () => {
const ordContent = {};
const warningSpy = jest.spyOn(console, 'warn');
prepareTestEnvironment({ namespace: "sap.sample" }, appConfig, 'testCustomORDContentFileThrowErrors.json');
const result = extendCustomORDContentIfExists(appConfig, ordContent);
prepareTestEnvironment({ namespace: "sap.sample" }, appConfig, "testCustomORDContentFileThrowErrors.json");
const result = extendCustomORDContentIfExists(appConfig,ordContent);

expect(warningSpy).toHaveBeenCalledTimes(3);
expect(warningSpy).toHaveBeenCalledWith('Mocked warning');

expect(warningSpy).toHaveBeenCalledWith(
"[ord-plugin] -",
expect.stringContaining("Found ord top level primitive ord property in customOrdFile:"),
expect.anything(),
expect.stringContaining("Please define it in .cdsrc.json."));
expect(result).toMatchSnapshot();
});

it('should add new ord resources that are not supported by cap framework', () => {
it("should add new ord resources that are not supported by cap framework", () => {
const ordContent = {};
prepareTestEnvironment({}, appConfig, 'testCustomORDContentFileWithNewResources.json');
const result = extendCustomORDContentIfExists(appConfig, ordContent);
prepareTestEnvironment(
{},
appConfig,
"testCustomORDContentFileWithNewResources.json"
);
const result = extendCustomORDContentIfExists(
appConfig,
ordContent
);
expect(result).toMatchSnapshot();
});

it('should enhance the list of generated ord resources', () => {
const ordContent = { packages: [{ ordId: "sap.sm:package:smDataProducts:v1", localId: "smDataProductsV1" }] };
prepareTestEnvironment({}, appConfig, 'testCustomORDContentFileWithEnhanced.json');
const result = extendCustomORDContentIfExists(appConfig, ordContent);
it("should enhance the list of generated ord resources", () => {
const ordContent = {
packages: [
{
ordId: "sap.sm:package:smDataProducts:v1",
localId: "smDataProductsV1",
},
],
};
prepareTestEnvironment(
{},
appConfig,
"testCustomORDContentFileWithEnhanced.json"
);
const result = extendCustomORDContentIfExists(
appConfig,
ordContent
);
expect(result).toMatchSnapshot();
});

it('should should patch the existing generated ord resources', () => {
it("should should patch the existing generated ord resources", () => {
const ordContent = {
packages: [{
ordId: "sap.sm:package:smDataProducts:v1",
localId: "smDataProductsV1"
}],
apiResources: [{
ordId: "sap.sm:apiResource:SupplierService:v1",
title: "should be removed",
partOfGroups: [
"sap.cds:service:sap.test.cdsrc.sample:originalService"
],
partOfPackage: "sap.sm:package:smDataProducts:v2",
extensible: {
"supported": "no"
packages: [
{
ordId: "sap.sm:package:smDataProducts:v1",
localId: "smDataProductsV1",
},
],
apiResources: [
{
ordId: "sap.sm:apiResource:SupplierService:v1",
title: "should be removed",
partOfGroups: [
"sap.cds:service:sap.test.cdsrc.sample:originalService",
],
partOfPackage: "sap.sm:package:smDataProducts:v2",
extensible: {
supported: "no",
},
entityTypeMappings: [
{
entityTypeTargets: [
{
ordId: "sap.odm:entityType:BusinessPartner:v2",
},
{
ordId: "sap.odm:entityType:BusinessPartner:v3",
},
],
},
],
},
entityTypeMappings: [
{
entityTypeTargets: [
{
ordId: "sap.odm:entityType:BusinessPartner:v2"
},
{
ordId: "sap.odm:entityType:BusinessPartner:v3"
}
]
}
]
},
{
ordId: "sap.sm:apiResource:orginalService:v2",
partOfGroups: [
"sap.cds:service:sap.test.cdsrc.sample:originalService"
],
partOfPackage: "sap.sm:package:smDataProducts:v2",
entityTypeMappings: [
{
entityTypeTargets: []
}
]
}]
{
ordId: "sap.sm:apiResource:orginalService:v2",
partOfGroups: [
"sap.cds:service:sap.test.cdsrc.sample:originalService",
],
partOfPackage: "sap.sm:package:smDataProducts:v2",
entityTypeMappings: [
{
entityTypeTargets: [],
},
],
},
],
};
prepareTestEnvironment({}, appConfig, 'testCustomORDContentFileWithPatch.json');
const result = extendCustomORDContentIfExists(appConfig, ordContent);
prepareTestEnvironment(
{},
appConfig,
"testCustomORDContentFileWithPatch.json"
);
const result = extendCustomORDContentIfExists(
appConfig,
ordContent
);
expect(result).toMatchSnapshot();
});
});
Expand All @@ -119,5 +154,7 @@ describe('extendOrdWithCustom', () => {
function prepareTestEnvironment(ordEnvVariables, appConfig, testFileName) {
cds.env["ord"] = ordEnvVariables;
appConfig.env.customOrdContentFile = testFileName;
jest.spyOn(path, 'join').mockReturnValueOnce(`${__dirname}/utils/${testFileName}`);
jest.spyOn(path, "join").mockReturnValueOnce(
`${__dirname}/utils/${testFileName}`
);
}

0 comments on commit 4345d76

Please sign in to comment.