From 478d87e6129cef8f00c4af838fc4a25b11eeffc6 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Tue, 16 Feb 2021 13:28:31 +0100 Subject: [PATCH] Pass CatalogApi instead of CatalogClient Co-authored-by: Patrik Oldsberg --- .../src/lib/catalog/CatalogEntityClient.ts | 4 +-- .../src/service/router.test.ts | 25 +++++++------------ .../scaffolder-backend/src/service/router.ts | 4 +-- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/plugins/scaffolder-backend/src/lib/catalog/CatalogEntityClient.ts b/plugins/scaffolder-backend/src/lib/catalog/CatalogEntityClient.ts index 13cfd32f6a..cea0e85148 100644 --- a/plugins/scaffolder-backend/src/lib/catalog/CatalogEntityClient.ts +++ b/plugins/scaffolder-backend/src/lib/catalog/CatalogEntityClient.ts @@ -15,14 +15,14 @@ */ import { TemplateEntityV1alpha1 } from '@backstage/catalog-model'; -import { CatalogClient } from '@backstage/catalog-client'; +import { CatalogApi } from '@backstage/catalog-client'; import { ConflictError, NotFoundError } from '@backstage/backend-common'; /** * A catalog client tailored for reading out entity data from the catalog. */ export class CatalogEntityClient { - constructor(private readonly catalogClient: CatalogClient) {} + constructor(private readonly catalogClient: CatalogApi) {} /** * Looks up a single template using a template name. diff --git a/plugins/scaffolder-backend/src/service/router.test.ts b/plugins/scaffolder-backend/src/service/router.test.ts index b5de493680..d8a236a213 100644 --- a/plugins/scaffolder-backend/src/service/router.test.ts +++ b/plugins/scaffolder-backend/src/service/router.test.ts @@ -39,16 +39,14 @@ import request from 'supertest'; import { createRouter } from './router'; import { Templaters, Preparers, Publishers } from '../scaffolder'; import Docker from 'dockerode'; -import { CatalogClient } from '@backstage/catalog-client'; - -jest.mock('@backstage/catalog-client'); -const MockedCatalogClient = CatalogClient as jest.Mock; +import { CatalogApi } from '@backstage/catalog-client'; jest.mock('dockerode'); -const generateEntityClient: any = (template: any) => ({ - findTemplate: () => Promise.resolve(template), -}); +const createCatalogClient = (templates: any[] = []) => + ({ + getEntities: async () => ({ items: templates }), + } as CatalogApi); function createDatabase(): PluginDatabaseManager { return SingleConnectionDatabaseManager.fromConfig( @@ -99,7 +97,6 @@ describe('createRouter - working directory', () => { }, }; - const mockedEntityClient = generateEntityClient(template); it('should throw an error when working directory does not exist or is not writable', async () => { mockAccess.mockImplementation(() => { throw new Error('access error'); @@ -113,9 +110,8 @@ describe('createRouter - working directory', () => { publishers: new Publishers(), config: new ConfigReader(workDirConfig('/path')), dockerClient: new Docker(), - entityClient: mockedEntityClient, database: createDatabase(), - catalogClient: new MockedCatalogClient(), + catalogClient: createCatalogClient([template]), }), ).rejects.toThrow('access error'); }); @@ -128,9 +124,8 @@ describe('createRouter - working directory', () => { publishers: new Publishers(), config: new ConfigReader(workDirConfig('/path')), dockerClient: new Docker(), - entityClient: mockedEntityClient, database: createDatabase(), - catalogClient: new MockedCatalogClient(), + catalogClient: createCatalogClient([template]), }); const app = express().use(router); @@ -158,9 +153,8 @@ describe('createRouter - working directory', () => { publishers: new Publishers(), config: new ConfigReader({}), dockerClient: new Docker(), - entityClient: mockedEntityClient, database: createDatabase(), - catalogClient: new MockedCatalogClient(), + catalogClient: createCatalogClient([template]), }); const app = express().use(router); @@ -229,9 +223,8 @@ describe('createRouter', () => { publishers: new Publishers(), config: new ConfigReader({}), dockerClient: new Docker(), - entityClient: generateEntityClient(template), database: createDatabase(), - catalogClient: new MockedCatalogClient(), + catalogClient: createCatalogClient([template]), }); app = express().use(router); }); diff --git a/plugins/scaffolder-backend/src/service/router.ts b/plugins/scaffolder-backend/src/service/router.ts index bb5fec306a..5adfee275e 100644 --- a/plugins/scaffolder-backend/src/service/router.ts +++ b/plugins/scaffolder-backend/src/service/router.ts @@ -48,7 +48,7 @@ import { NotFoundError, PluginDatabaseManager, } from '@backstage/backend-common'; -import { CatalogClient } from '@backstage/catalog-client'; +import { CatalogApi } from '@backstage/catalog-client'; export interface RouterOptions { preparers: PreparerBuilder; @@ -59,7 +59,7 @@ export interface RouterOptions { config: Config; dockerClient: Docker; database: PluginDatabaseManager; - catalogClient: CatalogClient; + catalogClient: CatalogApi; } export async function createRouter(