From 197ed5b83c91bcfb72cce5f6099d4737621324e4 Mon Sep 17 00:00:00 2001 From: Kiss Miklos Date: Fri, 1 Jul 2022 17:21:47 +0200 Subject: [PATCH] add and fix tests Signed-off-by: Kiss Miklos --- .../modules/core/FileReaderProcessor.test.ts | 5 +-- .../modules/core/PlaceholderProcessor.test.ts | 31 +++++++++++++++++++ .../modules/core/UrlReaderProcessor.test.ts | 4 +++ .../src/service/AuthorizedRefreshService.ts | 4 +-- .../src/service/DefaultRefreshService.ts | 2 +- .../src/service/createRouter.test.ts | 4 +-- plugins/catalog-backend/src/service/types.ts | 2 +- 7 files changed, 44 insertions(+), 8 deletions(-) diff --git a/plugins/catalog-backend/src/modules/core/FileReaderProcessor.test.ts b/plugins/catalog-backend/src/modules/core/FileReaderProcessor.test.ts index ad47350346..0c114f49a1 100644 --- a/plugins/catalog-backend/src/modules/core/FileReaderProcessor.test.ts +++ b/plugins/catalog-backend/src/modules/core/FileReaderProcessor.test.ts @@ -89,8 +89,9 @@ describe('FileReaderProcessor', () => { type: 'file', target: expect.stringMatching(/^[^*]*$/), }); - expect(emit.mock.calls[1][0].entityRef).toEqual( - 'component:default/component-test', + expect(emit.mock.calls[1][0].key).toContain('file:'); + expect(emit.mock.calls[1][0].key).toContain( + 'fileReaderProcessor/component.yaml', ); expect(emit.mock.calls[2][0].entity).toEqual({ kind: 'API', diff --git a/plugins/catalog-backend/src/modules/core/PlaceholderProcessor.test.ts b/plugins/catalog-backend/src/modules/core/PlaceholderProcessor.test.ts index 3ec699bc04..aa448dd955 100644 --- a/plugins/catalog-backend/src/modules/core/PlaceholderProcessor.test.ts +++ b/plugins/catalog-backend/src/modules/core/PlaceholderProcessor.test.ts @@ -17,6 +17,7 @@ import { UrlReader } from '@backstage/backend-common'; import { Entity } from '@backstage/catalog-model'; import { ConfigReader } from '@backstage/config'; import { ScmIntegrations } from '@backstage/integration'; +import { CatalogProcessorResult } from '../../api'; import { jsonPlaceholderResolver, PlaceholderProcessor, @@ -357,6 +358,36 @@ describe('PlaceholderProcessor', () => { expect(read).not.toBeCalled(); }); + it('should emit the resolverValue as a refreshKey', async () => { + read.mockResolvedValue( + Buffer.from(JSON.stringify({ a: ['b', 7] }), 'utf-8'), + ); + + const processor = new PlaceholderProcessor({ + resolvers: { + json: jsonPlaceholderResolver, + }, + reader, + integrations, + }); + + const emitted = new Array(); + await processor.preProcessEntity( + { + apiVersion: 'a', + kind: 'k', + metadata: { name: 'n' }, + spec: { a: [{ b: { $json: './path-to-file.json' } }] }, + }, + { type: 'fake', target: 'http://example.com' }, + result => emitted.push(result), + ); + console.log(emitted); + expect(emitted[0]).toEqual({ + type: 'refresh', + key: 'url:./path-to-file.json', + }); + }); }); describe('yamlPlaceholderResolver', () => { diff --git a/plugins/catalog-backend/src/modules/core/UrlReaderProcessor.test.ts b/plugins/catalog-backend/src/modules/core/UrlReaderProcessor.test.ts index 631f463fd5..fb3f48c939 100644 --- a/plugins/catalog-backend/src/modules/core/UrlReaderProcessor.test.ts +++ b/plugins/catalog-backend/src/modules/core/UrlReaderProcessor.test.ts @@ -86,6 +86,10 @@ describe('UrlReaderProcessor', () => { location: spec, entity: { kind: 'component', metadata: { name: 'mock-url-entity' } }, }); + expect(emitted[1]).toEqual({ + type: 'refresh', + key: 'url:http://localhost/component.yaml', + }); expect(mockCache.set).toBeCalledWith('v1', { etag: 'my-etag', value: [ diff --git a/plugins/catalog-backend/src/service/AuthorizedRefreshService.ts b/plugins/catalog-backend/src/service/AuthorizedRefreshService.ts index 2e48b3d8b6..19f6270da6 100644 --- a/plugins/catalog-backend/src/service/AuthorizedRefreshService.ts +++ b/plugins/catalog-backend/src/service/AuthorizedRefreshService.ts @@ -49,7 +49,7 @@ export class AuthorizedRefreshService implements RefreshService { } await this.service.refresh(options); } - async refreshByRefreshKey(options: RefreshByRefreshKeysOptions) { - await this.service.refreshByRefreshKey(options); + async refreshByRefreshKeys(options: RefreshByRefreshKeysOptions) { + await this.service.refreshByRefreshKeys(options); } } diff --git a/plugins/catalog-backend/src/service/DefaultRefreshService.ts b/plugins/catalog-backend/src/service/DefaultRefreshService.ts index e5deab1790..deb69bd041 100644 --- a/plugins/catalog-backend/src/service/DefaultRefreshService.ts +++ b/plugins/catalog-backend/src/service/DefaultRefreshService.ts @@ -49,7 +49,7 @@ export class DefaultRefreshService implements RefreshService { }); }); } - async refreshByRefreshKey(options: RefreshByRefreshKeysOptions) { + async refreshByRefreshKeys(options: RefreshByRefreshKeysOptions) { await this.database.transaction(async tx => { await this.database.refreshByRefreshKeys(tx, options); }); diff --git a/plugins/catalog-backend/src/service/createRouter.test.ts b/plugins/catalog-backend/src/service/createRouter.test.ts index d652893b55..b7eeb6f1bc 100644 --- a/plugins/catalog-backend/src/service/createRouter.test.ts +++ b/plugins/catalog-backend/src/service/createRouter.test.ts @@ -57,7 +57,7 @@ describe('createRouter readonly disabled', () => { listLocations: jest.fn(), deleteLocation: jest.fn(), }; - refreshService = { refresh: jest.fn(), refreshByRefreshKey: jest.fn() }; + refreshService = { refresh: jest.fn(), refreshByRefreshKeys: jest.fn() }; orchestrator = { process: jest.fn() }; const router = await createRouter({ entitiesCatalog, @@ -712,7 +712,7 @@ describe('NextRouter permissioning', () => { listLocations: jest.fn(), deleteLocation: jest.fn(), }; - refreshService = { refresh: jest.fn(), refreshByRefreshKey: jest.fn() }; + refreshService = { refresh: jest.fn(), refreshByRefreshKeys: jest.fn() }; const router = await createRouter({ entitiesCatalog, locationService, diff --git a/plugins/catalog-backend/src/service/types.ts b/plugins/catalog-backend/src/service/types.ts index 1e2142fc3a..a073e5bb6c 100644 --- a/plugins/catalog-backend/src/service/types.ts +++ b/plugins/catalog-backend/src/service/types.ts @@ -74,7 +74,7 @@ export interface RefreshService { * Request a refresh of entities in the catalog. */ refresh(options: RefreshOptions): Promise; - refreshByRefreshKey(options: RefreshByRefreshKeysOptions): Promise; + refreshByRefreshKeys(options: RefreshByRefreshKeysOptions): Promise; } /**