Improved generic handler remote services

This commit is contained in:
Uwe Klinger
2021-05-14 08:55:17 +02:00
parent 7ae992c5bb
commit fb5d00bbe0
3 changed files with 289 additions and 109 deletions

View File

@@ -4,12 +4,21 @@ if (cds.User.default) cds.User.default = cds.User.Privileged;
// hard core monkey patch
else cds.User = cds.User.Privileged; // hard core monkey patch for older cds releases
// TODO: remove hack
process.env.S4_APIKEY = "-";
const envelope = (entity, value) => {
const result = {"@odata.context": entity.match(/\$metadata/) ? entity : `$metadata#${entity}`};
const BPs = {
"@odata.context": "$metadata#Suppliers",
value: [
if (Array.isArray(value)) {
result.value = value;
} else {
Object.assign(result, value);
}
return result;
}
const BPs = [
{
BusinessPartner: "11",
BusinessPartnerFullName: "Alice Wonder",
@@ -20,12 +29,9 @@ const BPs = {
BusinessPartnerFullName: "Hugo Hollandaise",
BusinessPartnerType: "CUSTOMER",
},
],
};
];
const Suppliers = {
"@odata.context": "$metadata#Suppliers",
value: [
const Suppliers = [
{
ID: "11",
fullName: "Alice Wonder",
@@ -36,12 +42,9 @@ const Suppliers = {
fullName: "Hugo Hollandaise",
customerType: "CUSTOMER",
},
],
};
];
const SuppliersExpandNotes = {
"@odata.context": "$metadata#Suppliers",
value: [
const SuppliersExpandNotes = [
{
ID: "11",
fullName: "Alice Wonder",
@@ -71,8 +74,40 @@ const SuppliersExpandNotes = {
},
],
},
],
};
];
const NotesExpandSuppliers = [
{
"ID": "24B58115-E394-423B-BEAB-53419A32B927",
"note": "note3",
"supplier_ID": "9980000082",
"supplier": {
"ID": "9980000082",
"fullName": "Hugo Hollandaise",
"customerType": "CUSTOMER"
}
},
{
"ID": "545A3CF9-84CF-46C8-93DC-E29F0F2BC6BE",
"note": "note2 for 11",
"supplier_ID": "11",
"supplier": {
"ID": "11",
"fullName": "Alice Wonder",
"customerType": "CUSTOMER"
}
},
{
"ID": "D632D4EE-E772-454A-913E-26A7B8DAA7FB",
"note": "note1 for 11",
"supplier_ID": "11",
"supplier": {
"ID": "11",
"fullName": "Alice Wonder",
"customerType": "CUSTOMER"
}
}
];
class MockServer {
async start() {
@@ -97,12 +132,15 @@ class MockServer {
}
}
if (!global.beforeAll) global.beforeAll = global.before;
describe("Notes", () => {
const mockServer = new MockServer();
before( async () => {
beforeAll( async () => {
mockServer.start();
// TODO: Need better solution. Does it conflict with other tests?
cds.env.add({
requires: {
API_BUSINESS_PARTNER: {
@@ -159,7 +197,7 @@ describe("Notes", () => {
expect({ status, data }).to.containSubset({
status: 200,
data: Suppliers,
data: envelope("Suppliers", Suppliers)
});
});
@@ -168,7 +206,7 @@ describe("Notes", () => {
expect({ status, data }).to.containSubset({
status: 200,
data: SuppliersExpandNotes,
data: envelope('Suppliers(notes())', SuppliersExpandNotes),
});
});
@@ -177,10 +215,29 @@ describe("Notes", () => {
expect({ status, data }).to.containSubset({
status: 200,
data: {value: SuppliersExpandNotes.value[0].notes },
data: envelope('../$metadata#Notes', SuppliersExpandNotes[0].notes)
});
});
it("get notes with suppliers", async () => {
const { status, data } = await GET("/notes/Notes?$expand=supplier");
expect({ status, data }).to.containSubset({
status: 200,
data: envelope('Notes(supplier())', NotesExpandSuppliers)
});
});
// TODO: Seems not to respect filter for targetkeyFieldName
/*
it.only("get supplier via navigation", async () => {
const { status, data } = await GET("/notes/Notes(545A3CF9-84CF-46C8-93DC-E29F0F2BC6BE)/supplier");
expect({ status, data }).to.containSubset({
status: 200,
data: envelope("Suppliers", NotesExpandSuppliers[0].supplier )
});
});
*/
after(() => mockServer.close());
});