Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions packages/ui/src/dynamic-catalog/dynamic-catalog-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,89 @@ describe('DynamicCatalogRegistry', () => {
});
});

describe('resolveCatalogLookup', () => {
const timerComponent = {
component: { name: 'timer', syntax: 'timer:name' },
properties: {},
propertiesSchema: {},
} as ICamelComponentDefinition;

const kameletComponent = {
component: { name: 'kamelet' },
properties: {},
propertiesSchema: {},
} as ICamelComponentDefinition;

const chuckNorrisKamelet = {
kind: 'Kamelet',
metadata: { name: 'chuck-norris-source' },
spec: { definition: {} },
} as IKameletDefinition;

beforeEach(() => {
const componentCatalog = new DynamicCatalog<ICamelComponentDefinition>({
id: 'component-provider',
fetch: (key) =>
Promise.resolve(
({ timer: timerComponent, kamelet: kameletComponent } as Record<string, ICamelComponentDefinition>)[key],
),
fetchAll: () => Promise.resolve({ timer: timerComponent, kamelet: kameletComponent }),
});

const kameletCatalog = new DynamicCatalog<IKameletDefinition>({
id: 'kamelet-provider',
fetch: (key) =>
Promise.resolve(({ 'chuck-norris-source': chuckNorrisKamelet } as Record<string, IKameletDefinition>)[key]),
fetchAll: () => Promise.resolve({ 'chuck-norris-source': chuckNorrisKamelet }),
});

registry.setCatalog(CatalogKind.Component, componentCatalog);
registry.setCatalog(CatalogKind.Kamelet, kameletCatalog);
});

it('should return undefined for an empty component name', async () => {
const lookup = await registry.resolveCatalogLookup('');

expect(lookup).toBeUndefined();
});

it('should return a component from the catalog lookup', async () => {
const lookup = await registry.resolveCatalogLookup('timer');

expect(lookup).toEqual({
catalogKind: CatalogKind.Component,
definition: timerComponent,
});
});

it('should return a kamelet from the catalog lookup', async () => {
const lookup = await registry.resolveCatalogLookup('kamelet:chuck-norris-source');

expect(lookup).toEqual({
catalogKind: CatalogKind.Kamelet,
definition: chuckNorrisKamelet,
});
});

it('should return the kamelet component for unknown kamelets', async () => {
const lookup = await registry.resolveCatalogLookup('kamelet:non-existing-kamelet');

expect(lookup).toEqual({
catalogKind: CatalogKind.Component,
definition: kameletComponent,
});
});

it('should pass forceFresh to underlying catalog lookups', async () => {
const componentCatalog = registry.getCatalog(CatalogKind.Component)!;
const getSpy = jest.spyOn(componentCatalog, 'get');

await registry.resolveCatalogLookup('timer', { forceFresh: true });

expect(getSpy).toHaveBeenCalledWith('timer', { forceFresh: true });
});
});

it('should maintain a singleton registry', () => {
const registry1 = DynamicCatalogRegistry.get();
const registry2 = DynamicCatalogRegistry.get();
Expand Down
33 changes: 32 additions & 1 deletion packages/ui/src/dynamic-catalog/dynamic-catalog-registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CatalogKind } from '../models/catalog-kind';
import { DynamicCatalogTypeMap, IDynamicCatalog, IDynamicCatalogRegistry } from './models';
import { CatalogLookupResult, DynamicCatalogTypeMap, IDynamicCatalog, IDynamicCatalogRegistry } from './models';

export class DynamicCatalogRegistry {
private static instance: CatalogsRegistry;
Expand Down Expand Up @@ -33,6 +33,37 @@ class CatalogsRegistry implements IDynamicCatalogRegistry {
return catalog?.get(key, options) as Promise<DynamicCatalogTypeMap[K] | undefined>;
}

async resolveCatalogLookup(
componentName: string,
options: { forceFresh?: boolean } = {},
): Promise<CatalogLookupResult | undefined> {
if (!componentName) {
return undefined;
}

if (componentName.startsWith('kamelet:')) {
const kameletName = componentName.replace('kamelet:', '');
const definition = await this.getEntity(CatalogKind.Kamelet, kameletName, options);

if (definition) {
return {
catalogKind: CatalogKind.Kamelet,
definition,
};
}

return {
catalogKind: CatalogKind.Component,
definition: await this.getEntity(CatalogKind.Component, 'kamelet', options),
};
}

return {
catalogKind: CatalogKind.Component,
definition: await this.getEntity(CatalogKind.Component, componentName, options),
};
}

clearRegistry(): void {
this.catalogs.clear();
}
Expand Down
16 changes: 16 additions & 0 deletions packages/ui/src/dynamic-catalog/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ export interface IDynamicCatalog<T> {
clearCache(): void;
}

export type CatalogLookupResult =
| {
catalogKind: CatalogKind.Component;
definition?: ICamelComponentDefinition;
}
| {
catalogKind: CatalogKind.Kamelet;
definition?: IKameletDefinition;
};

export interface IDynamicCatalogRegistry {
setCatalog<K extends CatalogKind>(kind: K, catalog: IDynamicCatalog<DynamicCatalogTypeMap[K]>): void;
getCatalog<K extends CatalogKind>(kind: K): IDynamicCatalog<DynamicCatalogTypeMap[K]> | undefined;
Expand All @@ -52,5 +62,11 @@ export interface IDynamicCatalogRegistry {
key: string,
options?: { forceFresh?: boolean },
): Promise<DynamicCatalogTypeMap[K] | undefined>;

resolveCatalogLookup(
componentName: string,
options?: { forceFresh?: boolean },
): Promise<CatalogLookupResult | undefined>;

clearRegistry(): void;
}
Loading