Skip to content

Commit

Permalink
fix(CamelResourceFactory): use types for the CamelResources instead o…
Browse files Browse the repository at this point in the history
…f unknown

update tests to reflect types
  • Loading branch information
mmelko committed Dec 13, 2024
1 parent 3a34678 commit 83b70f2
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('routeIdValidator', () => {
});

it('should return sucess if the name is unique', () => {
const resource = new CamelRouteResource(camelRouteJson);
const resource = new CamelRouteResource([camelRouteJson]);
const visualEntities = resource.getVisualEntities();
jest.spyOn(visualEntities[0], 'getId').mockReturnValue('flow-1234');

Expand All @@ -28,7 +28,7 @@ describe('routeIdValidator', () => {
});

it('should return an error if the name is not unique', () => {
const resource = new CamelRouteResource(camelRouteJson);
const resource = new CamelRouteResource([camelRouteJson]);
const visualEntities = resource.getVisualEntities();
jest.spyOn(visualEntities[0], 'getId').mockReturnValue('flow-1234');

Expand All @@ -39,7 +39,7 @@ describe('routeIdValidator', () => {
});

it('should return an error if the name is not a valid URI', () => {
const resource = new CamelRouteResource(camelRouteJson);
const resource = new CamelRouteResource([camelRouteJson]);
const visualEntities = resource.getVisualEntities();
jest.spyOn(visualEntities[0], 'getId').mockReturnValue('flow-1234');

Expand All @@ -50,7 +50,7 @@ describe('routeIdValidator', () => {
});

it('should return an error if the name is not unique neither a valid URI', () => {
const resource = new CamelRouteResource(camelRouteJson);
const resource = new CamelRouteResource([camelRouteJson]);
const visualEntities = resource.getVisualEntities();
jest.spyOn(visualEntities[0], 'getId').mockReturnValue('The amazing Route');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('Canvas', () => {
});

it('should be able to delete the routes', async () => {
const camelResource = new CamelRouteResource(camelRouteJson);
const camelResource = new CamelRouteResource([camelRouteJson]);
const routeEntities = camelResource.getVisualEntities();
const removeSpy = jest.spyOn(camelResource, 'removeEntity');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('ItemEnableAllSteps', () => {
});

it('should NOT render an ItemEnableAllSteps if there are not at least 2 or more disabled steps', () => {
const camelResource = new CamelRouteResource(camelRouteJson);
const camelResource = new CamelRouteResource([camelRouteJson]);
const visualEntity = camelResource.getVisualEntities()[0];
const { nodes, edges } = FlowService.getFlowDiagram(visualEntity.toVizNode());

Expand Down Expand Up @@ -50,7 +50,7 @@ describe('ItemEnableAllSteps', () => {
});

it('should call updateModel and updateEntitiesFromCamelResource on click', async () => {
const camelResource = new CamelRouteResource(camelRouteWithDisabledSteps);
const camelResource = new CamelRouteResource([camelRouteWithDisabledSteps]);
const visualEntity = camelResource.getVisualEntities()[0];
const { nodes, edges } = FlowService.getFlowDiagram(visualEntity.toVizNode());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('NodeContextMenu', () => {
});

it('should render an ItemEnableAllSteps', () => {
const camelResource = new CamelRouteResource(camelRouteWithDisabledSteps);
const camelResource = new CamelRouteResource([camelRouteWithDisabledSteps]);
const visualEntity = camelResource.getVisualEntities()[0];
const { nodes, edges } = FlowService.getFlowDiagram(visualEntity.toVizNode());

Expand Down
32 changes: 30 additions & 2 deletions packages/ui/src/hooks/entities.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ describe('useEntities', () => {
const { result } = renderHook(() => useEntities());

act(() => {
console.log('result.current.camelResource:', result.current.camelResource);
result.current.camelResource.addNewEntity();
console.log('result.current.camelResource:', result.current.camelResource);
result.current.updateSourceCodeFromEntities();
});

Expand Down Expand Up @@ -164,4 +162,34 @@ describe('useEntities', () => {
`,
);
});
it(`should store code's comments`, () => {
const code = `# This is a comment
# An indented comment
- route:
id: route-1234
from:
id: from-1234
uri: timer
parameters:
period: "1000"
timerName: template
# This comment won't be stored
steps:
- log:
id: log-1234
message: \${body}
`;

const { result } = renderHook(() => useEntities());

act(() => {
eventNotifier.next('code:updated', code);
});

expect(result.current.camelResource.toString()).toContain(
`# This is a comment
# An indented comment`,
);
});
});
5 changes: 4 additions & 1 deletion packages/ui/src/models/camel/camel-k-resource-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import {
} from '@kaoto/camel-catalog/types';

export class CamelKResourceFactory {
static getCamelKResource(json?: unknown, type?: SourceSchemaType): CamelResource | undefined {
static getCamelKResource(
json?: IntegrationType | IKameletDefinition | KameletBindingType | PipeType,
type?: SourceSchemaType,
): CamelResource | undefined {
const jsonRecord = json ? (json as Record<string, unknown>) : {};

if ((jsonRecord && typeof json === 'object' && 'kind' in jsonRecord) || type) {
Expand Down
1 change: 0 additions & 1 deletion packages/ui/src/models/camel/camel-k-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const CAMEL_K_K8S_API_VERSION_V1 = 'camel.apache.org/v1';

export abstract class CamelKResource implements CamelResource {
static readonly PARAMETERS_ORDER = ['apiVersion', 'kind', 'metadata', 'spec', 'source', 'steps', 'sink'];
// static serializer = new YamlResourceSerializer();
readonly sortFn = createCamelPropertiesSorter(CamelKResource.PARAMETERS_ORDER) as (a: unknown, b: unknown) => number;
protected resource: CamelKType;
private metadata?: MetadataEntity;
Expand Down
11 changes: 8 additions & 3 deletions packages/ui/src/models/camel/camel-resource-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { CamelResource } from './camel-resource';
import { CamelResourceSerializer, XmlCamelResourceSerializer, YamlCamelResourceSerializer } from '../../serializers';
import { CamelRouteResource } from './camel-route-resource';
import { CamelKResourceFactory } from './camel-k-resource-factory';
import { CamelYamlDsl, Integration, KameletBinding, Pipe } from '@kaoto/camel-catalog/types';
import { IKameletDefinition } from '../kamelets-catalog';

export class CamelResourceFactory {
/**
Expand All @@ -18,10 +20,13 @@ export class CamelResourceFactory {
? new XmlCamelResourceSerializer()
: new YamlCamelResourceSerializer();

const parsedCode = source ? serializer.parse(source) : source;
const resource = CamelKResourceFactory.getCamelKResource(parsedCode, type);
const parsedCode = typeof source === 'string' ? serializer.parse(source) : source;
const resource = CamelKResourceFactory.getCamelKResource(
parsedCode as Integration | KameletBinding | Pipe | IKameletDefinition,
type,
);

if (resource) return resource;
return new CamelRouteResource(parsedCode, serializer);
return new CamelRouteResource(parsedCode as CamelYamlDsl, serializer);
}
}
93 changes: 53 additions & 40 deletions packages/ui/src/models/camel/camel-route-resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { CamelRouteResource } from './camel-route-resource';
import { EntityType } from './entities';
import { SourceSchemaType } from './source-schema-type';
import { CamelResourceFactory } from './camel-resource-factory';
import { CamelYamlDsl } from '@kaoto/camel-catalog/types';

describe('CamelRouteResource', () => {
it('should create CamelRouteResource', () => {
const resource = new CamelRouteResource(camelRouteJson);
const resource = new CamelRouteResource([camelRouteJson]);
expect(resource.getType()).toEqual(SourceSchemaType.Route);
expect(resource.getVisualEntities().length).toEqual(1);
expect(resource.getEntities().length).toEqual(0);
Expand All @@ -27,18 +28,29 @@ describe('CamelRouteResource', () => {
});

describe('constructor', () => {
it.each([
[camelRouteJson, CamelRouteVisualEntity],
[camelFromJson, CamelRouteVisualEntity],
[{ from: { uri: 'direct:foo', steps: [] } }, CamelRouteVisualEntity],
[{ from: 'direct:foo' }, NonVisualEntity],
[{ from: { uri: 'direct:foo' } }, CamelRouteVisualEntity],
[{ beans: [] }, BeansEntity],
[{}, NonVisualEntity],
[undefined, undefined],
[null, undefined],
const testCases: [CamelYamlDsl, unknown][] = [
// Good cases
[[camelRouteJson], CamelRouteVisualEntity],
[[camelFromJson], CamelRouteVisualEntity],
[[{ from: { uri: 'direct:foo', steps: [] } }], CamelRouteVisualEntity],
[[{ from: { uri: 'direct:foo' } }] as CamelYamlDsl, CamelRouteVisualEntity],
[[{ beans: [] }], BeansEntity],
[[], undefined],
])('should return the appropriate entity for: %s', (json, expected) => {

// Temporary good cases
[camelRouteJson as unknown as CamelYamlDsl, CamelRouteVisualEntity],
[camelFromJson as unknown as CamelYamlDsl, CamelRouteVisualEntity],
[{ from: { uri: 'direct:foo', steps: [] } } as unknown as CamelYamlDsl, CamelRouteVisualEntity],
[{ from: { uri: 'direct:foo' } } as unknown as CamelYamlDsl, CamelRouteVisualEntity],
[{ beans: [] } as unknown as CamelYamlDsl, BeansEntity],

// Bad cases
[{ from: 'direct:foo' } as unknown as CamelYamlDsl, NonVisualEntity],
[{} as CamelYamlDsl, NonVisualEntity],
[undefined as unknown as CamelYamlDsl, undefined],
[null as unknown as CamelYamlDsl, undefined],
];
it.each(testCases)('should return the appropriate entity for: %s', (json, expected) => {
const resource = new CamelRouteResource(json);
const firstEntity = resource.getVisualEntities()[0] ?? resource.getEntities()[0];

Expand Down Expand Up @@ -98,22 +110,22 @@ describe('CamelRouteResource', () => {
});

it('should return visual entities', () => {
const resource = new CamelRouteResource(camelRouteJson);
const resource = new CamelRouteResource([camelRouteJson]);
expect(resource.getVisualEntities()).toHaveLength(1);
expect(resource.getVisualEntities()[0]).toBeInstanceOf(CamelRouteVisualEntity);
expect(resource.getEntities()).toHaveLength(0);
});

it('should return entities', () => {
const resource = new CamelRouteResource(beansJson);
const resource = new CamelRouteResource([beansJson]);
expect(resource.getEntities()).toHaveLength(1);
expect(resource.getEntities()[0]).toBeInstanceOf(BeansEntity);
expect(resource.getVisualEntities()).toHaveLength(0);
});

describe('toJSON', () => {
it('should return JSON', () => {
const resource = new CamelRouteResource(camelRouteJson);
const resource = new CamelRouteResource([camelRouteJson]);
expect(resource.toJSON()).toMatchSnapshot();
});

Expand Down Expand Up @@ -141,15 +153,15 @@ describe('CamelRouteResource', () => {

describe('removeEntity', () => {
it('should not do anything if the ID is not provided', () => {
const resource = new CamelRouteResource(camelRouteJson);
const resource = new CamelRouteResource([camelRouteJson]);

resource.removeEntity();

expect(resource.getVisualEntities()).toHaveLength(1);
});

it('should not do anything when providing a non existing ID', () => {
const resource = new CamelRouteResource(camelRouteJson);
const resource = new CamelRouteResource([camelRouteJson]);

resource.removeEntity('non-existing-id');

Expand All @@ -166,7 +178,7 @@ describe('CamelRouteResource', () => {
});

it('should NOT create a new entity after deleting them all', () => {
const resource = new CamelRouteResource(camelRouteJson);
const resource = new CamelRouteResource([camelRouteJson]);
const camelRouteEntity = resource.getVisualEntities()[0];

resource.removeEntity(camelRouteEntity.id);
Expand All @@ -187,28 +199,29 @@ describe('CamelRouteResource', () => {
});

describe('toJson', () => {
it.each([
[camelRouteJson],
[camelFromJson],
[{ from: { uri: 'direct:foo', steps: [] } }],
[{ from: 'direct:foo' }],
[{ from: { uri: 'direct:foo' } }],
[{ beans: [] }],
[{ errorHandler: [] }],
[{ intercept: {} }],
[{ interceptFrom: {} }],
[{ interceptSendToEndpoint: {} }],
[{ onCompletion: {} }],
[{ onException: {} }],
[{ rest: {} }],
[{ restConfiguration: {} }],
[{ route: {} }],
[{ routeConfiguration: {} }],
[{ routeTemplate: {} }],
[{ templatedRoute: {} }],
[{ anotherUnknownContent: {} }],
[{}],
])('should not throw error when calling: %s', (json) => {
const testCases: [CamelYamlDsl][] = [
[[camelRouteJson]],
[[camelFromJson]],
[[{ from: { uri: 'direct:foo', steps: [] } }]],
[{ from: 'direct:foo' } as unknown as CamelYamlDsl],
[{ from: { uri: 'direct:foo' } } as unknown as CamelYamlDsl],
[[{ beans: [] }]],
[[{ errorHandler: {} }]],
[[{ intercept: {} }]],
[[{ interceptFrom: {} }]],
[{ interceptSendToEndpoint: {} } as unknown as CamelYamlDsl],
[{ onCompletion: {} } as unknown as CamelYamlDsl],
[{ onException: {} } as unknown as CamelYamlDsl],
[{ rest: {} } as unknown as CamelYamlDsl],
[[{ restConfiguration: {} }]],
[{ route: {} } as unknown as CamelYamlDsl],
[[{ routeConfiguration: {} }]],
[[{ routeTemplate: {} }] as unknown as CamelYamlDsl],
[{ templatedRoute: {} } as unknown as CamelYamlDsl],
[{ anotherUnknownContent: {} } as unknown as CamelYamlDsl],
[{} as CamelYamlDsl],
];
it.each(testCases)('should not throw error when calling: %s', (json) => {
const resource = new CamelRouteResource(json);
const firstEntity = resource.getVisualEntities()[0] ?? resource.getEntities()[0];
expect(firstEntity.toJSON()).not.toBeUndefined();
Expand Down
11 changes: 7 additions & 4 deletions packages/ui/src/models/camel/camel-route-resource.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RouteDefinition } from '@kaoto/camel-catalog/types';
import { CamelYamlDsl, RouteDefinition } from '@kaoto/camel-catalog/types';
import { TileFilter } from '../../components/Catalog';
import { createCamelPropertiesSorter, isDefined } from '../../utils';
import { CatalogKind } from '../catalog-kind';
Expand Down Expand Up @@ -50,11 +50,11 @@ export class CamelRouteResource implements CamelResource, BeansAwareResource {
private resolvedEntities: BaseVisualCamelEntityDefinition | undefined;
private serializer: CamelResourceSerializer;

constructor(code?: unknown, serializer?: CamelResourceSerializer) {
constructor(rawEntities?: CamelYamlDsl, serializer?: CamelResourceSerializer) {
this.serializer = serializer ?? new YamlCamelResourceSerializer();
if (!code) return;
if (!rawEntities) return;

const entities = Array.isArray(code) ? code : [code];
const entities = Array.isArray(rawEntities) ? rawEntities : [rawEntities];
this.entities = entities.reduce((acc, rawItem) => {
const entity = this.getEntity(rawItem);
if (isDefined(entity) && typeof entity === 'object') {
Expand Down Expand Up @@ -96,7 +96,10 @@ export class CamelRouteResource implements CamelResource, BeansAwareResource {
getSerializer(): CamelResourceSerializer {
return this.serializer;
}

setSerializer(serializer: CamelResourceSerializer): void {
// Preserve comments
serializer.setComments(this.serializer.getComments());
this.serializer = serializer;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/models/camel/kamelet-resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CamelComponentFilterService } from '../visualization/flows/support/came
import { KameletResource } from './kamelet-resource';
import { SourceSchemaType } from './source-schema-type';
import { CamelKResourceFactory } from './camel-k-resource-factory';
import cloneDeep from 'lodash/cloneDeep';
import cloneDeep from 'lodash';

describe('KameletResource', () => {
it('should create a new KameletResource', () => {
Expand Down
Loading

0 comments on commit 83b70f2

Please sign in to comment.