Skip to content

Commit

Permalink
feat: etag on patch (#860)
Browse files Browse the repository at this point in the history
* feat: etag on patch

* chore: etag

* chore: fix snapshot
  • Loading branch information
nlunets authored Sep 25, 2024
1 parent 950e878 commit d848d39
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-spoons-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-ux/fe-mockserver-core': patch
---

Etag on patch
22 changes: 18 additions & 4 deletions packages/fe-mockserver-core/src/data/dataAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -944,13 +944,19 @@ export class DataAccess implements DataAccessInterface {
}
patchData = finalPatchObject;
}
return (await this.getMockEntitySet(entitySetName)).performPATCH(
const mockEntitySet = await this.getMockEntitySet(entitySetName);

const resultData = await mockEntitySet.performPATCH(
odataRequest.queryPath[0].keys,
patchData,
odataRequest.tenantId,
odataRequest,
true
);
if (this.validateETag && !Array.isArray(resultData) && mockEntitySet.isDraft()) {
odataRequest.setETag(resultData['@odata.etag']);
}
return resultData;
}

public async createData(odataRequest: ODataRequest, postData: any) {
Expand Down Expand Up @@ -1029,9 +1035,14 @@ export class DataAccess implements DataAccessInterface {
currentKeys[key.name] = postData[key.name];
}
});
postData = await (
await this.getMockEntitySet(parentEntitySet.name)
).performPOST(currentKeys, postData, odataRequest.tenantId, odataRequest, true);
const mockEntitySet = await this.getMockEntitySet(parentEntitySet.name);
postData = await mockEntitySet.performPOST(
currentKeys,
postData,
odataRequest.tenantId,
odataRequest,
true
);
// Update keys from location
parentEntitySet.entityType.keys.forEach((key) => {
if (postData[key.name] !== undefined) {
Expand All @@ -1057,6 +1068,9 @@ export class DataAccess implements DataAccessInterface {
);
}
odataRequest.setResponseData(await postData);
if (this.validateETag && !Array.isArray(postData) && mockEntitySet.isDraft()) {
odataRequest.setETag(postData['@odata.etag']);
}
return postData;
} else {
throw new Error('Unknown Entity Set' + entitySetName);
Expand Down
3 changes: 0 additions & 3 deletions packages/fe-mockserver-core/src/router/batchRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ function createBatchResponseObject(
if (partRequest.getETag()) {
batchResponse += `ETag: ${partRequest.getETag()}${NL}`;
}
if (partRequest.getETag()) {
batchResponse += `ETag: ${partRequest.getETag()}${NL}`;
}
batchResponse += NL;
const responseData = partRequest.getResponseData();
batchResponse += `HTTP/1.1 ${partRequest.statusCode} ${http.STATUS_CODES[partRequest.statusCode]}${NL}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,30 @@ exports[`V4 Requestor can update data through a call 4`] = `
}
`;
exports[`V4 Requestor can update data through a call 5`] = `
{
"code": 412001,
"message": "ETag condition not met",
}
`;
exports[`V4 Requestor can update data through a call 6`] = `
{
"@odata.context": "$metadata#RootElement(ID=556)/Prop1",
"@odata.metadataEtag": "W/"62b2-j3ePZjiQElXD9LZZ+gEAOLMD5nc"",
"HasActiveEntity": false,
"HasDraftEntity": false,
"ID": 556,
"IsActiveEntity": false,
"Prop1": "Lali",
"Prop2": "",
"Sibling_ID": 0,
"isBoundAction1Hidden": false,
"isBoundAction2Hidden": false,
"isBoundAction3Hidden": false,
}
`;
exports[`V4 Requestor get one data 1`] = `
{
"@odata.context": "$metadata#RootElement(ID=2)",
Expand Down
19 changes: 19 additions & 0 deletions packages/fe-mockserver-core/test/unit/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,31 @@ describe('V4 Requestor', function () {
})
.execute();
delete res3.body.DraftAdministrativeData;
const newEtag = res3.body['@odata.etag'];
delete res3.body['@odata.etag'];
expect(res3.body).toMatchSnapshot();
const dataRes3 = await dataRequestor.getList<any>('RootElement').executeAsBatch();
expect(dataRes3.length).toBe(5);
expect(dataRes3[4].ID).toBe(556);
expect(dataRes3[4].Prop1).toBe('Lali-hoho');
const res4 = await dataRequestor
.updateData<any>('RootElement(ID=556)/Prop1', 'Lali', true, 'PUT', {
'If-Match': dataRes2[4]['@odata.etag']
})
.execute();
expect(res4.body).toMatchSnapshot();
const dataRes4 = await dataRequestor.getList<any>('RootElement').executeAsBatch();
expect(dataRes4.length).toBe(5);
expect(dataRes4[4].ID).toBe(556);
expect(dataRes4[4].Prop1).toBe('Lali-hoho');
const res5 = await dataRequestor
.updateData<any>('RootElement(ID=556)/Prop1', 'Lali', true, 'PUT', {
'If-Match': newEtag
})
.execute();
delete res5.body['@odata.etag'];
delete res5.body.DraftAdministrativeData;
expect(res5.body).toMatchSnapshot();
});
describe('Sticky', () => {
const dataRequestor = new ODataV4Requestor('http://localhost:33331/tenant-0/sap/fe/core/mock/sticky');
Expand Down

0 comments on commit d848d39

Please sign in to comment.