From 948d675442ab62fdd72bd9b54d3ac0f7bb38a25d Mon Sep 17 00:00:00 2001 From: sblausten Date: Wed, 29 Jun 2022 17:19:34 +0200 Subject: [PATCH 01/14] Allow FactRetrieverRegistry to be passed in to context builder for more flexibility Signed-off-by: sblausten --- .../techInsightsContextBuilder.test.ts | 74 +++++++++++++++++++ .../src/service/techInsightsContextBuilder.ts | 18 ++++- 2 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts diff --git a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts new file mode 100644 index 0000000000..d9aee6d1d3 --- /dev/null +++ b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts @@ -0,0 +1,74 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { buildTechInsightsContext } from './techInsightsContextBuilder'; +import { + DatabaseManager, + getVoidLogger, + PluginDatabaseManager, + ServerTokenManager, +} from '@backstage/backend-common'; +import { ConfigReader } from '@backstage/config'; +import { TaskScheduler } from '@backstage/backend-tasks'; +import { FactRetrieverRegistry } from './fact/FactRetrieverRegistry'; + +jest.mock('./fact/FactRetrieverRegistry'); + +describe('buildTechInsightsContext', () => { + const pluginDatabase = {} as PluginDatabaseManager; + const manager = {} as DatabaseManager; + const discoveryMock = { + getBaseUrl: (_: string) => Promise.resolve('http://mock.url'), + getExternalBaseUrl: (_: string) => Promise.resolve('http://mock.url'), + }; + const scheduler = new TaskScheduler(manager, getVoidLogger()).forPlugin( + 'tech-insights', + ); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('constructs the default FactRetrieverRegistry if factRetrievers but no factRetrieverRegistry are passed in', () => { + buildTechInsightsContext({ + database: pluginDatabase, + logger: getVoidLogger(), + factRetrievers: [], + scheduler: scheduler, + config: ConfigReader.fromConfigs([]), + discovery: discoveryMock, + tokenManager: ServerTokenManager.noop(), + }); + + expect(FactRetrieverRegistry).toHaveBeenCalledTimes(1); + }); + + it('uses factRetrieverRegistry implementation instead of the default FactRetrieverRegistry if it is passed in', () => { + const factRetrieverRegistryMock = {} as FactRetrieverRegistry; + + buildTechInsightsContext({ + database: pluginDatabase, + logger: getVoidLogger(), + factRetrievers: [], + factRetrieverRegistry: factRetrieverRegistryMock, + scheduler: scheduler, + config: ConfigReader.fromConfigs([]), + discovery: discoveryMock, + tokenManager: ServerTokenManager.noop(), + }); + + expect(FactRetrieverRegistry).not.toHaveBeenCalled(); + }); +}); diff --git a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts index e22bfbd253..418c406784 100644 --- a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts +++ b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts @@ -52,13 +52,15 @@ export interface TechInsightsOptions< * A collection of FactRetrieverRegistrations. * Used to register FactRetrievers and their schemas and schedule an execution loop for them. */ - factRetrievers: FactRetrieverRegistration[]; + factRetrievers?: FactRetrieverRegistration[]; /** * Optional factory exposing a `construct` method to initialize a FactChecker implementation */ factCheckerFactory?: FactCheckerFactory; + factRetrieverRegistry?: FactRetrieverRegistry; + logger: Logger; config: Config; discovery: PluginEndpointDiscovery; @@ -109,7 +111,19 @@ export const buildTechInsightsContext = async < tokenManager, } = options; - const factRetrieverRegistry = new FactRetrieverRegistry(factRetrievers); + const buildFactRetrieverRegistry = () => { + if (!options.factRetrieverRegistry) { + if (!factRetrievers) { + throw new Error( + 'Failed to build FactRetrieverRegistry because no factRetrievers found', + ); + } + return new FactRetrieverRegistry(factRetrievers); + } + return options.factRetrieverRegistry; + }; + + const factRetrieverRegistry = buildFactRetrieverRegistry(); const persistenceContext = await initializePersistenceContext( await database.getClient(), From f3d530bbe6ab5328059f49326bd3471ab7a9862b Mon Sep 17 00:00:00 2001 From: sblausten Date: Wed, 29 Jun 2022 18:01:31 +0200 Subject: [PATCH 02/14] Use new interface Signed-off-by: sblausten --- .../src/service/fact/FactRetrieverRegistry.ts | 13 +++++++++++-- .../src/service/techInsightsContextBuilder.ts | 9 ++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/plugins/tech-insights-backend/src/service/fact/FactRetrieverRegistry.ts b/plugins/tech-insights-backend/src/service/fact/FactRetrieverRegistry.ts index b122f52f0a..a8317db1d2 100644 --- a/plugins/tech-insights-backend/src/service/fact/FactRetrieverRegistry.ts +++ b/plugins/tech-insights-backend/src/service/fact/FactRetrieverRegistry.ts @@ -21,8 +21,17 @@ import { } from '@backstage/plugin-tech-insights-node'; import { ConflictError, NotFoundError } from '@backstage/errors'; -export class FactRetrieverRegistry { - private readonly retrievers = new Map(); +export interface FactRetrieverRegistryInterface { + readonly retrievers: Map; + register(registration: FactRetrieverRegistration): void; + get(retrieverReference: string): FactRetrieverRegistration; + listRetrievers(): FactRetriever[]; + listRegistrations(): FactRetrieverRegistration[]; + getSchemas(): FactSchema[]; +} + +export class FactRetrieverRegistry implements FactRetrieverRegistryInterface { + readonly retrievers = new Map(); constructor(retrievers: FactRetrieverRegistration[]) { retrievers.forEach(it => { diff --git a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts index 418c406784..1314ee5dbd 100644 --- a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts +++ b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts @@ -16,7 +16,10 @@ import { FactRetrieverEngine } from './fact/FactRetrieverEngine'; import { Logger } from 'winston'; -import { FactRetrieverRegistry } from './fact/FactRetrieverRegistry'; +import { + FactRetrieverRegistry, + FactRetrieverRegistryInterface, +} from './fact/FactRetrieverRegistry'; import { Config } from '@backstage/config'; import { PluginDatabaseManager, @@ -59,7 +62,7 @@ export interface TechInsightsOptions< */ factCheckerFactory?: FactCheckerFactory; - factRetrieverRegistry?: FactRetrieverRegistry; + factRetrieverRegistry?: FactRetrieverRegistryInterface; logger: Logger; config: Config; @@ -111,7 +114,7 @@ export const buildTechInsightsContext = async < tokenManager, } = options; - const buildFactRetrieverRegistry = () => { + const buildFactRetrieverRegistry = (): FactRetrieverRegistryInterface => { if (!options.factRetrieverRegistry) { if (!factRetrievers) { throw new Error( From 9120fb3056fdd49ae0e19b2381e4f0e3542af4a0 Mon Sep 17 00:00:00 2001 From: sblausten Date: Thu, 30 Jun 2022 10:06:01 +0200 Subject: [PATCH 03/14] Refactor naming Signed-off-by: sblausten --- plugins/tech-insights-backend/src/index.ts | 1 + .../src/service/fact/FactRetrieverRegistry.ts | 4 ++-- .../src/service/techInsightsContextBuilder.test.ts | 10 +++++----- .../src/service/techInsightsContextBuilder.ts | 8 ++++---- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/plugins/tech-insights-backend/src/index.ts b/plugins/tech-insights-backend/src/index.ts index 82fda47367..66180b1006 100644 --- a/plugins/tech-insights-backend/src/index.ts +++ b/plugins/tech-insights-backend/src/index.ts @@ -25,5 +25,6 @@ export type { export type { PersistenceContext } from './service/persistence/persistenceContext'; export { createFactRetrieverRegistration } from './service/fact/createFactRetriever'; +export type { FactRetrieverRegistry } from './service/fact/FactRetrieverRegistry'; export type { FactRetrieverRegistrationOptions } from './service/fact/createFactRetriever'; export * from './service/fact/factRetrievers'; diff --git a/plugins/tech-insights-backend/src/service/fact/FactRetrieverRegistry.ts b/plugins/tech-insights-backend/src/service/fact/FactRetrieverRegistry.ts index a8317db1d2..29099201ce 100644 --- a/plugins/tech-insights-backend/src/service/fact/FactRetrieverRegistry.ts +++ b/plugins/tech-insights-backend/src/service/fact/FactRetrieverRegistry.ts @@ -21,7 +21,7 @@ import { } from '@backstage/plugin-tech-insights-node'; import { ConflictError, NotFoundError } from '@backstage/errors'; -export interface FactRetrieverRegistryInterface { +export interface FactRetrieverRegistry { readonly retrievers: Map; register(registration: FactRetrieverRegistration): void; get(retrieverReference: string): FactRetrieverRegistration; @@ -30,7 +30,7 @@ export interface FactRetrieverRegistryInterface { getSchemas(): FactSchema[]; } -export class FactRetrieverRegistry implements FactRetrieverRegistryInterface { +export class DefaultFactRetrieverRegistry implements FactRetrieverRegistry { readonly retrievers = new Map(); constructor(retrievers: FactRetrieverRegistration[]) { diff --git a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts index d9aee6d1d3..d2660880b8 100644 --- a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts +++ b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts @@ -22,7 +22,7 @@ import { } from '@backstage/backend-common'; import { ConfigReader } from '@backstage/config'; import { TaskScheduler } from '@backstage/backend-tasks'; -import { FactRetrieverRegistry } from './fact/FactRetrieverRegistry'; +import { DefaultFactRetrieverRegistry } from './fact/FactRetrieverRegistry'; jest.mock('./fact/FactRetrieverRegistry'); @@ -41,7 +41,7 @@ describe('buildTechInsightsContext', () => { jest.clearAllMocks(); }); - it('constructs the default FactRetrieverRegistry if factRetrievers but no factRetrieverRegistry are passed in', () => { + it('constructs the default FactRetrieverRegistry if factRetrievers but no factRetrieverRegistry are passed', () => { buildTechInsightsContext({ database: pluginDatabase, logger: getVoidLogger(), @@ -52,11 +52,11 @@ describe('buildTechInsightsContext', () => { tokenManager: ServerTokenManager.noop(), }); - expect(FactRetrieverRegistry).toHaveBeenCalledTimes(1); + expect(DefaultFactRetrieverRegistry).toHaveBeenCalledTimes(1); }); it('uses factRetrieverRegistry implementation instead of the default FactRetrieverRegistry if it is passed in', () => { - const factRetrieverRegistryMock = {} as FactRetrieverRegistry; + const factRetrieverRegistryMock = {} as DefaultFactRetrieverRegistry; buildTechInsightsContext({ database: pluginDatabase, @@ -69,6 +69,6 @@ describe('buildTechInsightsContext', () => { tokenManager: ServerTokenManager.noop(), }); - expect(FactRetrieverRegistry).not.toHaveBeenCalled(); + expect(DefaultFactRetrieverRegistry).not.toHaveBeenCalled(); }); }); diff --git a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts index 1314ee5dbd..b194fb6b69 100644 --- a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts +++ b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts @@ -17,8 +17,8 @@ import { FactRetrieverEngine } from './fact/FactRetrieverEngine'; import { Logger } from 'winston'; import { + DefaultFactRetrieverRegistry, FactRetrieverRegistry, - FactRetrieverRegistryInterface, } from './fact/FactRetrieverRegistry'; import { Config } from '@backstage/config'; import { @@ -62,7 +62,7 @@ export interface TechInsightsOptions< */ factCheckerFactory?: FactCheckerFactory; - factRetrieverRegistry?: FactRetrieverRegistryInterface; + factRetrieverRegistry?: FactRetrieverRegistry; logger: Logger; config: Config; @@ -114,14 +114,14 @@ export const buildTechInsightsContext = async < tokenManager, } = options; - const buildFactRetrieverRegistry = (): FactRetrieverRegistryInterface => { + const buildFactRetrieverRegistry = (): FactRetrieverRegistry => { if (!options.factRetrieverRegistry) { if (!factRetrievers) { throw new Error( 'Failed to build FactRetrieverRegistry because no factRetrievers found', ); } - return new FactRetrieverRegistry(factRetrievers); + return new DefaultFactRetrieverRegistry(factRetrievers); } return options.factRetrieverRegistry; }; From 818fa28d71e12bb5397038f54690fb3d05bff66d Mon Sep 17 00:00:00 2001 From: sblausten Date: Thu, 30 Jun 2022 10:11:09 +0200 Subject: [PATCH 04/14] Add Changeset Signed-off-by: sblausten --- .changeset/eighty-windows-brush.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/eighty-windows-brush.md diff --git a/.changeset/eighty-windows-brush.md b/.changeset/eighty-windows-brush.md new file mode 100644 index 0000000000..e66a0e1574 --- /dev/null +++ b/.changeset/eighty-windows-brush.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-tech-insights-backend': minor +--- + +Make FactRetrieverRegistry injectable to overrider default implementation From ef2196cb524bf6b356a5e8c183bff5c19d5a36e2 Mon Sep 17 00:00:00 2001 From: sblausten Date: Thu, 30 Jun 2022 10:19:19 +0200 Subject: [PATCH 05/14] Fix grammer in changeset and export default registry also Signed-off-by: sblausten --- .changeset/eighty-windows-brush.md | 2 +- plugins/tech-insights-backend/src/index.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.changeset/eighty-windows-brush.md b/.changeset/eighty-windows-brush.md index e66a0e1574..61a09fb663 100644 --- a/.changeset/eighty-windows-brush.md +++ b/.changeset/eighty-windows-brush.md @@ -2,4 +2,4 @@ '@backstage/plugin-tech-insights-backend': minor --- -Make FactRetrieverRegistry injectable to overrider default implementation +Make FactRetrieverRegistry injectable to override default implementation diff --git a/plugins/tech-insights-backend/src/index.ts b/plugins/tech-insights-backend/src/index.ts index 66180b1006..7ab9246f9a 100644 --- a/plugins/tech-insights-backend/src/index.ts +++ b/plugins/tech-insights-backend/src/index.ts @@ -26,5 +26,6 @@ export type { export type { PersistenceContext } from './service/persistence/persistenceContext'; export { createFactRetrieverRegistration } from './service/fact/createFactRetriever'; export type { FactRetrieverRegistry } from './service/fact/FactRetrieverRegistry'; +export { DefaultFactRetrieverRegistry } from './service/fact/FactRetrieverRegistry'; export type { FactRetrieverRegistrationOptions } from './service/fact/createFactRetriever'; export * from './service/fact/factRetrievers'; From 8e40830f9005b43f80271d74024c5a5d802f3cd4 Mon Sep 17 00:00:00 2001 From: sblausten Date: Thu, 30 Jun 2022 10:42:16 +0200 Subject: [PATCH 06/14] Fix Vale check Signed-off-by: sblausten --- .changeset/eighty-windows-brush.md | 2 +- plugins/tech-insights-backend/src/index.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.changeset/eighty-windows-brush.md b/.changeset/eighty-windows-brush.md index 61a09fb663..753b53fbb4 100644 --- a/.changeset/eighty-windows-brush.md +++ b/.changeset/eighty-windows-brush.md @@ -2,4 +2,4 @@ '@backstage/plugin-tech-insights-backend': minor --- -Make FactRetrieverRegistry injectable to override default implementation +Allow FactRetrieverRegistry to be injected into buildTechInsightsContext so that we can override default registry implementation. diff --git a/plugins/tech-insights-backend/src/index.ts b/plugins/tech-insights-backend/src/index.ts index 7ab9246f9a..66180b1006 100644 --- a/plugins/tech-insights-backend/src/index.ts +++ b/plugins/tech-insights-backend/src/index.ts @@ -26,6 +26,5 @@ export type { export type { PersistenceContext } from './service/persistence/persistenceContext'; export { createFactRetrieverRegistration } from './service/fact/createFactRetriever'; export type { FactRetrieverRegistry } from './service/fact/FactRetrieverRegistry'; -export { DefaultFactRetrieverRegistry } from './service/fact/FactRetrieverRegistry'; export type { FactRetrieverRegistrationOptions } from './service/fact/createFactRetriever'; export * from './service/fact/factRetrievers'; From 3c84892796c7cdacc4dffc281d131055fb1e58a8 Mon Sep 17 00:00:00 2001 From: sblausten Date: Thu, 30 Jun 2022 11:07:32 +0200 Subject: [PATCH 07/14] Add documentation Signed-off-by: sblausten --- .../src/service/techInsightsContextBuilder.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts index b194fb6b69..1e3eabfb8b 100644 --- a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts +++ b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts @@ -52,8 +52,10 @@ export interface TechInsightsOptions< CheckResultType extends CheckResult, > { /** - * A collection of FactRetrieverRegistrations. + * Optional collection of FactRetrieverRegistrations. * Used to register FactRetrievers and their schemas and schedule an execution loop for them. + * + * Not needed if passing in your own FactRetrieverRegistry implementation */ factRetrievers?: FactRetrieverRegistration[]; @@ -62,6 +64,11 @@ export interface TechInsightsOptions< */ factCheckerFactory?: FactCheckerFactory; + /** + * Optional FactRetrieverRegistry implementation that replaces the default one. + * + * If passing this in you don't need to pass in factRetrievers also. + */ factRetrieverRegistry?: FactRetrieverRegistry; logger: Logger; From 156bb8ffba1bb933d6a1681d45f4d1420ec1f309 Mon Sep 17 00:00:00 2001 From: sblausten Date: Thu, 30 Jun 2022 11:50:26 +0200 Subject: [PATCH 08/14] Add clearer documentation Signed-off-by: sblausten --- .../src/service/techInsightsContextBuilder.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts index 1e3eabfb8b..008acf192d 100644 --- a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts +++ b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts @@ -52,10 +52,10 @@ export interface TechInsightsOptions< CheckResultType extends CheckResult, > { /** - * Optional collection of FactRetrieverRegistrations. + * Optional collection of FactRetrieverRegistrations (required if no factRetrieverRegistry passed in). * Used to register FactRetrievers and their schemas and schedule an execution loop for them. * - * Not needed if passing in your own FactRetrieverRegistry implementation + * Not needed if passing in your own FactRetrieverRegistry implementation. Required otherwise. */ factRetrievers?: FactRetrieverRegistration[]; From 0b36b9cb0beef252a11f2d2bcd24fdd07b785eda Mon Sep 17 00:00:00 2001 From: sblausten Date: Thu, 30 Jun 2022 12:24:50 +0200 Subject: [PATCH 09/14] Adds api report Signed-off-by: sblausten --- plugins/tech-insights-backend/api-report.md | 20 ++++++++++++++++++- .../src/service/fact/FactRetrieverRegistry.ts | 4 ++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/plugins/tech-insights-backend/api-report.md b/plugins/tech-insights-backend/api-report.md index 9fee3be281..32d45f8afd 100644 --- a/plugins/tech-insights-backend/api-report.md +++ b/plugins/tech-insights-backend/api-report.md @@ -11,6 +11,7 @@ import { FactCheckerFactory } from '@backstage/plugin-tech-insights-node'; import { FactLifecycle } from '@backstage/plugin-tech-insights-node'; import { FactRetriever } from '@backstage/plugin-tech-insights-node'; import { FactRetrieverRegistration } from '@backstage/plugin-tech-insights-node'; +import { FactSchema } from '@backstage/plugin-tech-insights-node'; import { Logger } from 'winston'; import { PluginDatabaseManager } from '@backstage/backend-common'; import { PluginEndpointDiscovery } from '@backstage/backend-common'; @@ -51,6 +52,22 @@ export type FactRetrieverRegistrationOptions = { lifecycle?: FactLifecycle; }; +// @public (undocumented) +export interface FactRetrieverRegistry { + // (undocumented) + get(retrieverReference: string): FactRetrieverRegistration; + // (undocumented) + getSchemas(): FactSchema[]; + // (undocumented) + listRegistrations(): FactRetrieverRegistration[]; + // (undocumented) + listRetrievers(): FactRetriever[]; + // (undocumented) + register(registration: FactRetrieverRegistration): void; + // (undocumented) + readonly retrievers: Map; +} + // @public export type PersistenceContext = { techInsightsStore: TechInsightsStore; @@ -91,7 +108,8 @@ export interface TechInsightsOptions< // (undocumented) discovery: PluginEndpointDiscovery; factCheckerFactory?: FactCheckerFactory; - factRetrievers: FactRetrieverRegistration[]; + factRetrieverRegistry?: FactRetrieverRegistry; + factRetrievers?: FactRetrieverRegistration[]; // (undocumented) logger: Logger; // (undocumented) diff --git a/plugins/tech-insights-backend/src/service/fact/FactRetrieverRegistry.ts b/plugins/tech-insights-backend/src/service/fact/FactRetrieverRegistry.ts index 29099201ce..504d561f17 100644 --- a/plugins/tech-insights-backend/src/service/fact/FactRetrieverRegistry.ts +++ b/plugins/tech-insights-backend/src/service/fact/FactRetrieverRegistry.ts @@ -21,6 +21,10 @@ import { } from '@backstage/plugin-tech-insights-node'; import { ConflictError, NotFoundError } from '@backstage/errors'; +/** + * @public + * + */ export interface FactRetrieverRegistry { readonly retrievers: Map; register(registration: FactRetrieverRegistration): void; From 331b68025874aa47cf04468edd534ce39cd0233b Mon Sep 17 00:00:00 2001 From: sblausten Date: Thu, 30 Jun 2022 12:46:49 +0200 Subject: [PATCH 10/14] Fix test mock Signed-off-by: sblausten --- .../src/service/techInsightsContextBuilder.test.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts index d2660880b8..ef9484369a 100644 --- a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts +++ b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts @@ -23,11 +23,20 @@ import { import { ConfigReader } from '@backstage/config'; import { TaskScheduler } from '@backstage/backend-tasks'; import { DefaultFactRetrieverRegistry } from './fact/FactRetrieverRegistry'; +import { Knex } from 'knex'; jest.mock('./fact/FactRetrieverRegistry'); describe('buildTechInsightsContext', () => { - const pluginDatabase = {} as PluginDatabaseManager; + const pluginDatabase: PluginDatabaseManager = { + getClient: () => { + return Promise.resolve({ + migrate: { + latest: () => {}, + }, + }) as unknown as Promise; + }, + }; const manager = {} as DatabaseManager; const discoveryMock = { getBaseUrl: (_: string) => Promise.resolve('http://mock.url'), From f90371a6bb5e09ac951683e63be06592a150a8c9 Mon Sep 17 00:00:00 2001 From: sblausten Date: Thu, 30 Jun 2022 12:58:36 +0200 Subject: [PATCH 11/14] Fix test mock Signed-off-by: sblausten --- .../src/service/techInsightsContextBuilder.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts index ef9484369a..ef3ce98690 100644 --- a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts +++ b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts @@ -37,7 +37,10 @@ describe('buildTechInsightsContext', () => { }) as unknown as Promise; }, }; - const manager = {} as DatabaseManager; + const databaseManager: Partial = { + forPlugin: () => pluginDatabase, + }; + const manager = databaseManager as DatabaseManager; const discoveryMock = { getBaseUrl: (_: string) => Promise.resolve('http://mock.url'), getExternalBaseUrl: (_: string) => Promise.resolve('http://mock.url'), From 5dedcb541e77639ac8d9b752e22f720239a7faad Mon Sep 17 00:00:00 2001 From: sblausten Date: Thu, 30 Jun 2022 13:21:23 +0200 Subject: [PATCH 12/14] Mock out engine Signed-off-by: sblausten --- .../src/service/techInsightsContextBuilder.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts index ef3ce98690..46d1e61e22 100644 --- a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts +++ b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts @@ -26,6 +26,11 @@ import { DefaultFactRetrieverRegistry } from './fact/FactRetrieverRegistry'; import { Knex } from 'knex'; jest.mock('./fact/FactRetrieverRegistry'); +jest.mock('./fact/FactRetrieverEngine', () => ({ + create: jest.fn().mockResolvedValue({ + schedule: jest.fn(), + }), +})); describe('buildTechInsightsContext', () => { const pluginDatabase: PluginDatabaseManager = { From 8915b74167cbcad90ac45b425b42287a9f81a7ed Mon Sep 17 00:00:00 2001 From: sblausten Date: Thu, 30 Jun 2022 16:43:09 +0200 Subject: [PATCH 13/14] Fix mocking Signed-off-by: sblausten --- .../src/service/techInsightsContextBuilder.test.ts | 10 +++++----- .../src/service/techInsightsContextBuilder.ts | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts index 46d1e61e22..82f504c8a8 100644 --- a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts +++ b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.test.ts @@ -27,9 +27,11 @@ import { Knex } from 'knex'; jest.mock('./fact/FactRetrieverRegistry'); jest.mock('./fact/FactRetrieverEngine', () => ({ - create: jest.fn().mockResolvedValue({ - schedule: jest.fn(), - }), + FactRetrieverEngine: { + create: jest.fn().mockResolvedValue({ + schedule: jest.fn(), + }), + }, })); describe('buildTechInsightsContext', () => { @@ -68,7 +70,6 @@ describe('buildTechInsightsContext', () => { discovery: discoveryMock, tokenManager: ServerTokenManager.noop(), }); - expect(DefaultFactRetrieverRegistry).toHaveBeenCalledTimes(1); }); @@ -85,7 +86,6 @@ describe('buildTechInsightsContext', () => { discovery: discoveryMock, tokenManager: ServerTokenManager.noop(), }); - expect(DefaultFactRetrieverRegistry).not.toHaveBeenCalled(); }); }); diff --git a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts index 008acf192d..1ce87adb96 100644 --- a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts +++ b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts @@ -120,6 +120,7 @@ export const buildTechInsightsContext = async < scheduler, tokenManager, } = options; + logger.info('HERE1'); const buildFactRetrieverRegistry = (): FactRetrieverRegistry => { if (!options.factRetrieverRegistry) { @@ -139,6 +140,7 @@ export const buildTechInsightsContext = async < await database.getClient(), { logger }, ); + logger.info('HERE2'); const factRetrieverEngine = await FactRetrieverEngine.create({ scheduler, From d223fa15663a4e4552812b14422fded352a69c32 Mon Sep 17 00:00:00 2001 From: sblausten Date: Thu, 30 Jun 2022 16:47:32 +0200 Subject: [PATCH 14/14] Clean Signed-off-by: sblausten --- .../src/service/techInsightsContextBuilder.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts index 1ce87adb96..008acf192d 100644 --- a/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts +++ b/plugins/tech-insights-backend/src/service/techInsightsContextBuilder.ts @@ -120,7 +120,6 @@ export const buildTechInsightsContext = async < scheduler, tokenManager, } = options; - logger.info('HERE1'); const buildFactRetrieverRegistry = (): FactRetrieverRegistry => { if (!options.factRetrieverRegistry) { @@ -140,7 +139,6 @@ export const buildTechInsightsContext = async < await database.getClient(), { logger }, ); - logger.info('HERE2'); const factRetrieverEngine = await FactRetrieverEngine.create({ scheduler,