From cada3c3c090a79ea008939d2b1855bfaf46b3de8 Mon Sep 17 00:00:00 2001 From: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> Date: Wed, 11 Sep 2024 13:19:10 +0400 Subject: [PATCH] Add unit tests for entity processor and validator Signed-off-by: vahagsaribekyan <32436397+vahagsaribekyan@users.noreply.github.com> --- .../core/BuiltinKindsEntityProcessor.test.ts | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.test.ts b/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.test.ts index e0368d40a6..f640b22520 100644 --- a/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.test.ts +++ b/plugins/catalog-backend/src/modules/core/BuiltinKindsEntityProcessor.test.ts @@ -46,13 +46,14 @@ describe('BuiltinKindsEntityProcessor', () => { providesApis: ['b'], consumesApis: ['c'], dependsOn: ['Resource:r', 'Component:d'], + dependencyOf: ['Resource:f', 'Component:g'], system: 's', }, }; await processor.postProcessEntity(entity, location, emit); - expect(emit).toHaveBeenCalledTimes(14); + expect(emit).toHaveBeenCalledTimes(18); expect(emit).toHaveBeenCalledWith({ type: 'relation', relation: { @@ -132,6 +133,38 @@ describe('BuiltinKindsEntityProcessor', () => { type: 'dependencyOf', target: { kind: 'Component', namespace: 'default', name: 'n' }, }, + }); + expect(emit).toHaveBeenCalledWith({ + type: 'relation', + relation: { + source: { kind: 'Component', namespace: 'default', name: 'n' }, + type: 'dependencyOf', + target: { kind: 'Resource', namespace: 'default', name: 'f' }, + }, + }); + expect(emit).toHaveBeenCalledWith({ + type: 'relation', + relation: { + source: { kind: 'Resource', namespace: 'default', name: 'f' }, + type: 'dependsOn', + target: { kind: 'Component', namespace: 'default', name: 'n' }, + }, + }); + expect(emit).toHaveBeenCalledWith({ + type: 'relation', + relation: { + source: { kind: 'Component', namespace: 'default', name: 'n' }, + type: 'dependencyOf', + target: { kind: 'Component', namespace: 'default', name: 'g' }, + }, + }); + expect(emit).toHaveBeenCalledWith({ + type: 'relation', + relation: { + source: { kind: 'Component', namespace: 'default', name: 'g' }, + type: 'dependsOn', + target: { kind: 'Component', namespace: 'default', name: 'n' }, + }, }); expect(emit).toHaveBeenCalledWith({ type: 'relation', @@ -190,6 +223,29 @@ describe('BuiltinKindsEntityProcessor', () => { ); }); + it('generates an error for component entities with unspecified dependencyOf entity reference kinds', async () => { + const entity: ComponentEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { name: 'n' }, + spec: { + type: 'service', + owner: 'o', + subcomponentOf: 's', + lifecycle: 'l', + providesApis: ['b'], + consumesApis: ['c'], + dependencyOf: ['y'], + system: 's', + }, + }; + await expect( + processor.postProcessEntity(entity, location, emit), + ).rejects.toThrow( + 'Entity reference "y" had missing or empty kind (e.g. did not start with "component:" or similar)', + ); + }); + it('generates relations for api entities', async () => { const entity: ApiEntity = { apiVersion: 'backstage.io/v1alpha1',