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(),