Skip to content

Commit

Permalink
feat(kamelets): Load Kamelet boundaries catalog
Browse files Browse the repository at this point in the history
This commit loads the Kamelet boundaries catalog, which include
kamelet:source and kamelet:sink.

Relates to: #110
  • Loading branch information
lordrip committed Nov 29, 2023
1 parent 8dcd1c5 commit 87d3ce2
Show file tree
Hide file tree
Showing 12 changed files with 465 additions and 11 deletions.
11 changes: 11 additions & 0 deletions packages/ui/jest-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { FilterDOMPropsKeys, filterDOMProps } from 'uniforms';

filterDOMProps.register('inputRef' as FilterDOMPropsKeys, 'placeholder' as FilterDOMPropsKeys);

Object.defineProperty(window, 'fetch', {
writable: true,
value: jest.fn(),
});

jest
.spyOn(global, 'crypto', 'get')
.mockImplementation(() => ({ getRandomValues: () => [12345678] }) as unknown as Crypto);
Expand All @@ -18,3 +23,9 @@ jest.spyOn(console, 'warn').mockImplementation((...args) => {

console.log(...args);
});

const fetchMock = jest.spyOn(window, 'fetch');

beforeEach(() => {
fetchMock.mockResolvedValue(null as unknown as Response);
});
2 changes: 2 additions & 0 deletions packages/ui/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export default {
'!src/**/*.models.ts',
// Ignore stub files
'!src/**/stubs/*.ts',
// Ignore icon resolver
'!src/**/node-icon-resolver.ts',
],

// The directory where Jest should output its coverage files
Expand Down
42 changes: 42 additions & 0 deletions packages/ui/src/camel-utils/camel-to-tile.adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,48 @@ describe('kameletToTile', () => {
expect(tile.tags).toEqual(['source']);
});

it('should use the selected CatalogKind.Kamelet by default', () => {
const kameletDef = {
metadata: {
labels: {
'camel.apache.org/kamelet.type': 'source',
},
annotations: {},
},
spec: {
definition: {
title: 'My Kamelet',
description: 'My Kamelet Description',
},
},
} as IKameletDefinition;

const tile = kameletToTile(kameletDef);

expect(tile.type).toEqual(CatalogKind.Kamelet);
});

it('should use the selected CatalogKind', () => {
const kameletDef = {
metadata: {
labels: {
'camel.apache.org/kamelet.type': 'source',
},
annotations: {},
},
spec: {
definition: {
title: 'My Kamelet',
description: 'My Kamelet Description',
},
},
} as IKameletDefinition;

const tile = kameletToTile(kameletDef, CatalogKind.KameletBoundary);

expect(tile.type).toEqual(CatalogKind.KameletBoundary);
});

it('should populate the version for annotations', () => {
const kameletDef = {
metadata: {
Expand Down
7 changes: 5 additions & 2 deletions packages/ui/src/camel-utils/camel-to-tile.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export const camelProcessorToTile = (processorDef: ICamelProcessorDefinition): I
};
};

export const kameletToTile = (kameletDef: IKameletDefinition): ITile => {
export const kameletToTile = (
kameletDef: IKameletDefinition,
type: CatalogKind.Kamelet | CatalogKind.KameletBoundary = CatalogKind.Kamelet,
): ITile => {
const headerTags: string[] = [];
if (kameletDef.metadata.annotations['camel.apache.org/kamelet.support.level']) {
headerTags.push(kameletDef.metadata.annotations['camel.apache.org/kamelet.support.level']);
Expand All @@ -64,7 +67,7 @@ export const kameletToTile = (kameletDef: IKameletDefinition): ITile => {
}

return {
type: CatalogKind.Kamelet,
type,
name: kameletDef.metadata.name,
title: kameletDef.spec.definition.title,
description: kameletDef.spec.definition.description,
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/src/models/camel-catalog-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Catalogs {
languages: CatalogEntry;
dataformats: CatalogEntry;
kamelets: CatalogEntry;
'kamelets-boundaries': CatalogEntry;
patterns: CatalogEntry;
}

Expand Down Expand Up @@ -49,4 +50,5 @@ export interface ComponentsCatalog {
[CatalogKind.Language]?: Record<string, ICamelLanguageDefinition>;
[CatalogKind.Dataformat]?: Record<string, ICamelDataformatDefinition>;
[CatalogKind.Kamelet]?: Record<string, IKameletDefinition>;
[CatalogKind.KameletBoundary]?: Record<string, IKameletDefinition>;
}
1 change: 1 addition & 0 deletions packages/ui/src/models/catalog-kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export const enum CatalogKind {
Language = 'language',
Dataformat = 'dataformat',
Kamelet = 'kamelet',
KameletBoundary = 'kamelet-boundary',
}
102 changes: 102 additions & 0 deletions packages/ui/src/providers/catalog-tiles.provider.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import componentsCatalog from '@kaoto-next/camel-catalog/camel-catalog-aggregate-components.json';
import patternsCatalog from '@kaoto-next/camel-catalog/camel-catalog-aggregate-patterns.json';
import kameletsCatalog from '@kaoto-next/camel-catalog/kamelets-aggregate.json';
import kameletsBoundariesCatalog from '@kaoto-next/camel-catalog/kamelets-boundaries.json';
import { act, render, screen } from '@testing-library/react';
import { camelComponentToTile, camelProcessorToTile, kameletToTile } from '../camel-utils';
import { CatalogKind, ICamelComponentDefinition, ICamelProcessorDefinition, IKameletDefinition } from '../models';
import { CamelCatalogService } from '../models/visualization/flows/camel-catalog.service';
import { CatalogTilesProvider } from './catalog-tiles.provider';

jest.mock('../camel-utils', () => {
const actual = jest.requireActual('../camel-utils');

return {
...actual,
camelComponentToTile: jest.fn(),
camelProcessorToTile: jest.fn(),
kameletToTile: jest.fn(),
};
});

describe('CatalogTilesProvider', () => {
let getCatalogByKeySpy: jest.SpyInstance;

beforeEach(() => {
getCatalogByKeySpy = jest.spyOn(CamelCatalogService, 'getCatalogByKey');
CamelCatalogService.setCatalogKey(
CatalogKind.Component,
componentsCatalog as unknown as Record<string, ICamelComponentDefinition>,
);
CamelCatalogService.setCatalogKey(
CatalogKind.Pattern,
patternsCatalog as unknown as Record<string, ICamelProcessorDefinition>,
);
CamelCatalogService.setCatalogKey(
CatalogKind.Kamelet,
kameletsCatalog as unknown as Record<string, IKameletDefinition>,
);
CamelCatalogService.setCatalogKey(
CatalogKind.KameletBoundary,
kameletsBoundariesCatalog as unknown as Record<string, IKameletDefinition>,
);
});

it('should render children', async () => {
await act(async () => {
render(
<CatalogTilesProvider>
<span data-testid="tiles-loaded">Loaded</span>
</CatalogTilesProvider>,
);
});

expect(screen.getByTestId('tiles-loaded')).toBeInTheDocument();
});

it('should query the catalog to build the tiles', async () => {
await act(async () => {
render(
<CatalogTilesProvider>
<span data-testid="tiles-loaded">Loaded</span>
</CatalogTilesProvider>,
);
});

expect(getCatalogByKeySpy).toHaveBeenCalledTimes(4);
expect(getCatalogByKeySpy).toHaveBeenCalledWith(CatalogKind.Component);
expect(getCatalogByKeySpy).toHaveBeenCalledWith(CatalogKind.Pattern);
expect(getCatalogByKeySpy).toHaveBeenCalledWith(CatalogKind.Kamelet);
expect(getCatalogByKeySpy).toHaveBeenCalledWith(CatalogKind.KameletBoundary);
});

it('should build the tiles', async () => {
await act(async () => {
render(
<CatalogTilesProvider>
<span data-testid="tiles-loaded">Loaded</span>
</CatalogTilesProvider>,
);
});

expect(camelComponentToTile).toHaveBeenCalled();
expect(camelProcessorToTile).toHaveBeenCalled();
expect(kameletToTile).toHaveBeenCalled();
});

it('should avoid building the tiles if the catalog is empty', async () => {
CamelCatalogService.clearCatalogs();

await act(async () => {
render(
<CatalogTilesProvider>
<span data-testid="tiles-loaded">Loaded</span>
</CatalogTilesProvider>,
);
});

expect(camelComponentToTile).not.toHaveBeenCalled();
expect(camelProcessorToTile).not.toHaveBeenCalled();
expect(kameletToTile).not.toHaveBeenCalled();
});
});
3 changes: 3 additions & 0 deletions packages/ui/src/providers/catalog-tiles.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export const CatalogTilesProvider: FunctionComponent<PropsWithChildren> = (props
Object.values(catalogService.getCatalogByKey(CatalogKind.Kamelet) ?? {}).forEach((kamelet) => {
combinedTiles.push(kameletToTile(kamelet));
});
Object.values(catalogService.getCatalogByKey(CatalogKind.KameletBoundary) ?? {}).forEach((kamelet) => {
combinedTiles.push(kameletToTile(kamelet, CatalogKind.KameletBoundary));
});

return combinedTiles;
}, [catalogService]);
Expand Down
Loading

0 comments on commit 87d3ce2

Please sign in to comment.