From 4f4150c16b8039379cc5bb55838ad297e25c66c6 Mon Sep 17 00:00:00 2001 From: Albin Ramovic Date: Fri, 8 Nov 2024 10:45:49 +0100 Subject: [PATCH] add unit tests for private and internal resources --- .../__snapshots__/templates.test.js.snap | 95 +++++++++- __tests__/unittest/templates.test.js | 168 ++++++++++++++++-- 2 files changed, 246 insertions(+), 17 deletions(-) diff --git a/__tests__/unittest/__snapshots__/templates.test.js.snap b/__tests__/unittest/__snapshots__/templates.test.js.snap index 13e0a4e..499a01c 100644 --- a/__tests__/unittest/__snapshots__/templates.test.js.snap +++ b/__tests__/unittest/__snapshots__/templates.test.js.snap @@ -121,6 +121,97 @@ exports[`templates createEventResourceTemplate should create event resource temp ] `; -exports[`templates ordExtension should add apiResources with ord extensions correctly 1`] = `[]`; +exports[`templates ordExtension should add apiResources with ORD Extension "visibility=public" 1`] = ` +[ + { + "apiProtocol": "odata-v4", + "description": "Description for MyService", + "entityTypeMappings": [ + { + "entityTypeTargets": "sap.odm:entityType:test:v1", + }, + ], + "entryPoints": [ + "/odata/v4/my", + ], + "extensible": { + "supported": "yes", + }, + "lastUpdate": "2022-12-19T15:47:04+00:00", + "ordId": "customer.testNamespace:apiResource:MyService:v1", + "partOfGroups": [ + "sap.cds:service:customer.testNamespace:MyService", + ], + "partOfPackage": "sap.test.cdsrc.sample:package:test-other:v1", + "releaseStatus": "active", + "resourceDefinitions": [ + { + "accessStrategies": [ + { + "type": "open", + }, + ], + "mediaType": "application/json", + "type": "openapi-v3", + "url": "/.well-known/open-resource-discovery/v1/api-metadata/MyService.oas3.json", + }, + { + "accessStrategies": [ + { + "type": "open", + }, + ], + "mediaType": "application/xml", + "type": "edmx", + "url": "/.well-known/open-resource-discovery/v1/api-metadata/MyService.edmx", + }, + ], + "shortDescription": "short description for test MyService apiResource", + "title": "This is test MyService apiResource title", + "version": "2.0.0", + "visibility": "public", + }, +] +`; + +exports[`templates ordExtension should add events with ORD Extension "visibility=public" 1`] = ` +[ + { + "description": "CAP Event resource describing events / messages.", + "extensible": { + "supported": "yes", + }, + "lastUpdate": "2022-12-19T15:47:04+00:00", + "ordId": "customer.testNamespace:eventResource:MyService:v1", + "partOfGroups": [ + "sap.cds:service:customer.testNamespace:MyService", + ], + "partOfPackage": "sap.test.cdsrc.sample:package:test-event:v1", + "releaseStatus": "active", + "resourceDefinitions": [ + { + "accessStrategies": [ + { + "type": "open", + }, + ], + "mediaType": "application/json", + "type": "asyncapi-v2", + "url": "/.well-known/open-resource-discovery/v1/api-metadata/MyService.asyncapi2.json", + }, + ], + "shortDescription": "short description for test MyService event", + "title": "This is test MyService event title", + "version": "2.0.0", + "visibility": "public", + }, +] +`; + +exports[`templates ordExtension should not add apiResources with ORD Extension "visibility=internal" 1`] = `[]`; + +exports[`templates ordExtension should not add apiResources with ORD Extension "visibility=private" 1`] = `[]`; + +exports[`templates ordExtension should not add events with ORD Extension "visibility=internal" 1`] = `[]`; -exports[`templates ordExtension should add events with ord extensions correctly 1`] = `[]`; +exports[`templates ordExtension should not add events with ORD Extension "visibility=private" 1`] = `[]`; diff --git a/__tests__/unittest/templates.test.js b/__tests__/unittest/templates.test.js index 8d973a2..3190f81 100644 --- a/__tests__/unittest/templates.test.js +++ b/__tests__/unittest/templates.test.js @@ -1,15 +1,21 @@ const cds = require('@sap/cds'); -const templates = require('../../lib/templates'); +const { + createEntityTypeTemplate, + createGroupsTemplateForService, + createAPIResourceTemplate, + createEventResourceTemplate +} = require('../../lib/templates'); describe('templates', () => { let linkedModel; + const appConfig = { ordNamespace: 'customer.testNamespace', appName: 'testAppName', lastUpdate: '2022-12-19T15:47:04+00:00' }; - beforeEach(() => { + beforeAll(() => { linkedModel = cds.linked(` namespace customer.testNamespace123; entity Books { @@ -21,7 +27,7 @@ describe('templates', () => { describe('createEntityTypeTemplate', () => { it('should return default value', () => { - expect(templates.createEntityTypeTemplate(linkedModel)).toEqual({ + expect(createEntityTypeTemplate(linkedModel)).toEqual({ ordId: 'sap.odm:entityType:undefined:v1' }); }); @@ -35,7 +41,7 @@ describe('templates', () => { groupTypeId: 'sap.cds:service', title: 'test Service' }; - expect(templates.createGroupsTemplateForService(testServiceName, linkedModel, appConfig)).toEqual(testResult); + expect(createGroupsTemplateForService(testServiceName, linkedModel, appConfig)).toEqual(testResult); }); }); @@ -44,7 +50,7 @@ describe('templates', () => { const serviceName = 'MyService'; const srvDefinition = linkedModel const packageIds = ['sap.test.cdsrc.sample:package:test-event:v1', 'sap.test.cdsrc.sample:package:test-api:v1']; - expect(templates.createAPIResourceTemplate(serviceName, srvDefinition, appConfig, packageIds)).toMatchSnapshot(); + expect(createAPIResourceTemplate(serviceName, srvDefinition, appConfig, packageIds)).toMatchSnapshot(); }); }); @@ -53,19 +59,19 @@ describe('templates', () => { const serviceName = 'MyService'; const srvDefinition = linkedModel const packageIds = ['sap.test.cdsrc.sample:package:test-event:v1', 'sap.test.cdsrc.sample:package:test-api:v1']; - expect(templates.createEventResourceTemplate(serviceName, srvDefinition, appConfig, packageIds)).toMatchSnapshot(); + expect(createEventResourceTemplate(serviceName, srvDefinition, appConfig, packageIds)).toMatchSnapshot(); }); it('should create event resource template correctly with packageIds including namespace', () => { const serviceName = 'MyService'; const srvDefinition = linkedModel const packageIds = ['customer.testNamespace:package:test:v1']; - expect(templates.createEventResourceTemplate(serviceName, srvDefinition, appConfig, packageIds)).toMatchSnapshot(); + expect(createEventResourceTemplate(serviceName, srvDefinition, appConfig, packageIds)).toMatchSnapshot(); }); }); describe('ordExtension', () => { - it('should add events with ord extensions correctly', () => { + it('should add apiResources with ORD Extension "visibility=public"', () => { const serviceName = 'MyService'; linkedModel = cds.linked(` service MyService { @@ -74,22 +80,67 @@ describe('templates', () => { title: String; } } + @ODM.entityName: 'testOdmEntity' + entity Books { + key ID: UUID; + title: String; + } annotate MyService with @ORD.Extensions : { - title : 'This is test MyService event title', - shortDescription: 'short description for test MyService event', - visibility : 'private', + title : 'This is test MyService apiResource title', + shortDescription: 'short description for test MyService apiResource', + visibility : 'public', version : '2.0.0', + partOfPackage : 'sap.test.cdsrc.sample:package:test-other:v1', extensible : { supported : 'yes' } }; `); const srvDefinition = linkedModel.definitions[serviceName]; - const packageIds = ['sap.test.cdsrc.sample:package:test-event:v1', 'sap.test.cdsrc.sample:package:test-api:v1']; - expect(templates.createEventResourceTemplate(serviceName, srvDefinition, appConfig, packageIds)).toMatchSnapshot(); + appConfig['odmEntity'] = 'sap.odm:entityType:test:v1' + const packageIds = ['customer.testNamespace:package:test:v1']; + const apiResourceTemplate = createAPIResourceTemplate(serviceName, srvDefinition, appConfig, packageIds); + + expect(apiResourceTemplate).toBeInstanceOf(Array); + expect(apiResourceTemplate).toMatchSnapshot(); }); - it('should add apiResources with ord extensions correctly', () => { + it('should not add apiResources with ORD Extension "visibility=internal"', () => { + const serviceName = 'MyService'; + linkedModel = cds.linked(` + service MyService { + entity Books { + key ID: UUID; + title: String; + } + } + @ODM.entityName: 'testOdmEntity' + entity Books { + key ID: UUID; + title: String; + } + annotate MyService with @ORD.Extensions : { + title : 'This is test MyService apiResource title', + shortDescription: 'short description for test MyService apiResource', + visibility : 'internal', + version : '2.0.0', + partOfPackage : 'sap.test.cdsrc.sample:package:test-other:v1', + extensible : { + supported : 'yes' + } + }; + `); + const srvDefinition = linkedModel.definitions[serviceName]; + appConfig['odmEntity'] = 'sap.odm:entityType:test:v1' + const packageIds = ['customer.testNamespace:package:test:v1']; + const apiResourceTemplate = createAPIResourceTemplate(serviceName, srvDefinition, appConfig, packageIds); + + expect(apiResourceTemplate).toBeInstanceOf(Array); + expect(apiResourceTemplate).toMatchSnapshot(); + expect(apiResourceTemplate).toEqual([]); + }); + + it('should not add apiResources with ORD Extension "visibility=private"', () => { const serviceName = 'MyService'; linkedModel = cds.linked(` service MyService { @@ -117,7 +168,94 @@ describe('templates', () => { const srvDefinition = linkedModel.definitions[serviceName]; appConfig['odmEntity'] = 'sap.odm:entityType:test:v1' const packageIds = ['customer.testNamespace:package:test:v1']; - expect(templates.createAPIResourceTemplate(serviceName, srvDefinition, appConfig, packageIds)).toMatchSnapshot(); + const apiResourceTemplate = createAPIResourceTemplate(serviceName, srvDefinition, appConfig, packageIds); + + expect(apiResourceTemplate).toBeInstanceOf(Array); + expect(apiResourceTemplate).toMatchSnapshot(); + expect(apiResourceTemplate).toEqual([]); + }); + + it('should add events with ORD Extension "visibility=public"', () => { + const serviceName = 'MyService'; + linkedModel = cds.linked(` + service MyService { + entity Books { + key ID: UUID; + title: String; + } + } + annotate MyService with @ORD.Extensions : { + title : 'This is test MyService event title', + shortDescription: 'short description for test MyService event', + visibility : 'public', + version : '2.0.0', + extensible : { + supported : 'yes' + } + }; + `); + const srvDefinition = linkedModel.definitions[serviceName]; + const packageIds = ['sap.test.cdsrc.sample:package:test-event:v1', 'sap.test.cdsrc.sample:package:test-api:v1']; + const eventResourceTemplate = createEventResourceTemplate(serviceName, srvDefinition, appConfig, packageIds); + + expect(eventResourceTemplate).toBeInstanceOf(Array); + expect(eventResourceTemplate).toMatchSnapshot(); + }); + + it('should not add events with ORD Extension "visibility=internal"', () => { + const serviceName = 'MyService'; + linkedModel = cds.linked(` + service MyService { + entity Books { + key ID: UUID; + title: String; + } + } + annotate MyService with @ORD.Extensions : { + title : 'This is test MyService event title', + shortDescription: 'short description for test MyService event', + visibility : 'internal', + version : '2.0.0', + extensible : { + supported : 'yes' + } + }; + `); + const srvDefinition = linkedModel.definitions[serviceName]; + const packageIds = ['sap.test.cdsrc.sample:package:test-event:v1', 'sap.test.cdsrc.sample:package:test-api:v1']; + const eventResourceTemplate = createEventResourceTemplate(serviceName, srvDefinition, appConfig, packageIds); + + expect(eventResourceTemplate).toBeInstanceOf(Array); + expect(eventResourceTemplate).toMatchSnapshot(); + expect(eventResourceTemplate).toEqual([]); + }); + + it('should not add events with ORD Extension "visibility=private"', () => { + const serviceName = 'MyService'; + linkedModel = cds.linked(` + service MyService { + entity Books { + key ID: UUID; + title: String; + } + } + annotate MyService with @ORD.Extensions : { + title : 'This is test MyService event title', + shortDescription: 'short description for test MyService event', + visibility : 'private', + version : '2.0.0', + extensible : { + supported : 'yes' + } + }; + `); + const srvDefinition = linkedModel.definitions[serviceName]; + const packageIds = ['sap.test.cdsrc.sample:package:test-event:v1', 'sap.test.cdsrc.sample:package:test-api:v1']; + const eventResourceTemplate = createEventResourceTemplate(serviceName, srvDefinition, appConfig, packageIds); + + expect(eventResourceTemplate).toBeInstanceOf(Array); + expect(eventResourceTemplate).toMatchSnapshot(); + expect(eventResourceTemplate).toEqual([]); }); }); });