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 12, 2024
1 parent 3a34678 commit 020af1b
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 33 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
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
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()

Check warning on line 20 in packages/ui/src/models/camel/camel-resource-factory.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/models/camel/camel-resource-factory.ts#L20

Added line #L20 was not covered by tests
: 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);
}
}
19 changes: 10 additions & 9 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 @@ -39,7 +40,7 @@ describe('CamelRouteResource', () => {
[null, undefined],
[[], undefined],
])('should return the appropriate entity for: %s', (json, expected) => {
const resource = new CamelRouteResource(json);
const resource = new CamelRouteResource(json as CamelYamlDsl);
const firstEntity = resource.getVisualEntities()[0] ?? resource.getEntities()[0];

if (typeof expected === 'function') {
Expand Down Expand Up @@ -98,22 +99,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 +142,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 +167,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 Down Expand Up @@ -209,7 +210,7 @@ describe('CamelRouteResource', () => {
[{ anotherUnknownContent: {} }],
[{}],
])('should not throw error when calling: %s', (json) => {
const resource = new CamelRouteResource(json);
const resource = new CamelRouteResource(json as CamelYamlDsl);
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;

Check warning on line 97 in packages/ui/src/models/camel/camel-route-resource.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/models/camel/camel-route-resource.ts#L96-L97

Added lines #L96 - L97 were not covered by tests
}

setSerializer(serializer: CamelResourceSerializer): void {

Check warning on line 100 in packages/ui/src/models/camel/camel-route-resource.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/models/camel/camel-route-resource.ts#L100

Added line #L100 was not covered by tests
// Preserve comments
serializer.setComments(this.serializer.getComments());
this.serializer = serializer;

Check warning on line 103 in packages/ui/src/models/camel/camel-route-resource.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/models/camel/camel-route-resource.ts#L102-L103

Added lines #L102 - L103 were not covered by tests
}

Expand Down
5 changes: 4 additions & 1 deletion packages/ui/src/serializers/camel-resource-serializer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { CamelResource } from '../models/camel';
import { CamelYamlDsl, Integration, Kamelet, KameletBinding, Pipe } from '@kaoto/camel-catalog/types';

export interface CamelResourceSerializer {
parse: (code: string) => unknown;
parse: (code: string) => CamelYamlDsl | Integration | Kamelet | KameletBinding | Pipe;
serialize: (resource: CamelResource) => string;
getComments: () => string[];
setComments: (comments: string[]) => void;
}
11 changes: 10 additions & 1 deletion packages/ui/src/serializers/xml-camel-resource-serializer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { CamelResource } from '../models/camel';
import { CamelResourceSerializer } from './camel-resource-serializer';
import { CamelYamlDsl, Integration, Kamelet, KameletBinding, Pipe } from '@kaoto/camel-catalog/types';

export class XmlCamelResourceSerializer implements CamelResourceSerializer {
static isApplicable(_code: unknown): boolean {
return false;
}

parse(_code: unknown): unknown {
parse(_code: string): CamelYamlDsl | Integration | Kamelet | KameletBinding | Pipe {

Check warning on line 10 in packages/ui/src/serializers/xml-camel-resource-serializer.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/serializers/xml-camel-resource-serializer.ts#L10

Added line #L10 was not covered by tests
//TODO implement
return {};

Check warning on line 12 in packages/ui/src/serializers/xml-camel-resource-serializer.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/serializers/xml-camel-resource-serializer.ts#L12

Added line #L12 was not covered by tests
}
Expand All @@ -15,4 +16,12 @@ export class XmlCamelResourceSerializer implements CamelResourceSerializer {
//TODO implement
return '';

Check warning on line 17 in packages/ui/src/serializers/xml-camel-resource-serializer.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/serializers/xml-camel-resource-serializer.ts#L17

Added line #L17 was not covered by tests
}

getComments(): string[] {
return [];

Check warning on line 21 in packages/ui/src/serializers/xml-camel-resource-serializer.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/serializers/xml-camel-resource-serializer.ts#L20-L21

Added lines #L20 - L21 were not covered by tests
}

setComments(_comments: string[]): void {

Check warning on line 24 in packages/ui/src/serializers/xml-camel-resource-serializer.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/serializers/xml-camel-resource-serializer.ts#L24

Added line #L24 was not covered by tests
//TODO implement
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { YamlCamelResourceSerializer } from './yaml-camel-resource-serializer';
import { camelRouteJson, camelRouteYaml } from '../stubs';
import { CamelRouteResource } from '../models/camel';
import { CamelYamlDsl } from '@kaoto/camel-catalog/types';

describe('YamlCamelResourceSerializer', () => {
let serializer: YamlCamelResourceSerializer;
Expand All @@ -22,11 +23,10 @@ describe('YamlCamelResourceSerializer', () => {

it('includes comments in serialized YAML string', () => {
const entities = serializer.parse('# comment1\n' + camelRouteYaml);
console.log(serializer.comments);
expect(serializer.comments.includes('# comment1')).toBeTruthy();

serializer.comments.push('# Comment2');
const result = serializer.serialize(new CamelRouteResource(entities));
const result = serializer.serialize(new CamelRouteResource(entities as CamelYamlDsl));
expect(result).toContain('# Comment2');
});
});
15 changes: 12 additions & 3 deletions packages/ui/src/serializers/yaml-camel-resource-serializer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CamelResource } from '../models/camel';
import { parse, stringify } from 'yaml';
import { CamelResourceSerializer } from './camel-resource-serializer';
import { CamelYamlDsl, Integration, Kamelet, KameletBinding, Pipe } from '@kaoto/camel-catalog/types';

export class YamlCamelResourceSerializer implements CamelResourceSerializer {
/**
Expand All @@ -13,7 +14,7 @@ export class YamlCamelResourceSerializer implements CamelResourceSerializer {
* ```
* The regular expression should match the first three lines
*/
COMMENTED_LINES_REGEXP = /^\s*#.*$/;
static COMMENTED_LINES_REGEXP = /^\s*#.*$/;
comments: string[] = [];

static isApplicable(_code: unknown): boolean {

Check warning on line 20 in packages/ui/src/serializers/yaml-camel-resource-serializer.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/serializers/yaml-camel-resource-serializer.ts#L20

Added line #L20 was not covered by tests
Expand All @@ -23,7 +24,7 @@ export class YamlCamelResourceSerializer implements CamelResourceSerializer {
return true;

Check warning on line 24 in packages/ui/src/serializers/yaml-camel-resource-serializer.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/serializers/yaml-camel-resource-serializer.ts#L24

Added line #L24 was not covered by tests
}

parse(code: string): unknown {
parse(code: string): CamelYamlDsl | Integration | Kamelet | KameletBinding | Pipe {
if (!code || typeof code !== 'string') return [];

this.comments = this.parseComments(code);
Expand All @@ -40,11 +41,19 @@ export class YamlCamelResourceSerializer implements CamelResourceSerializer {
return code;
}

getComments(): string[] {
return this.comments;

Check warning on line 45 in packages/ui/src/serializers/yaml-camel-resource-serializer.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/serializers/yaml-camel-resource-serializer.ts#L44-L45

Added lines #L44 - L45 were not covered by tests
}

setComments(comments: string[]): void {
this.comments = comments;

Check warning on line 49 in packages/ui/src/serializers/yaml-camel-resource-serializer.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/serializers/yaml-camel-resource-serializer.ts#L48-L49

Added lines #L48 - L49 were not covered by tests
}

private parseComments(code: string): string[] {
const lines = code.split('\n');
const comments: string[] = [];
for (const line of lines) {
if (line.trim() === '' || this.COMMENTED_LINES_REGEXP.test(line)) {
if (line.trim() === '' || YamlCamelResourceSerializer.COMMENTED_LINES_REGEXP.test(line)) {
comments.push(line);
} else {
break;
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/stubs/TestProvidersWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface TestProvidersWrapperResult {
}

export const TestProvidersWrapper = (props: TestProviderWrapperProps = {}): TestProvidersWrapperResult => {
const camelResource = props.camelResource ?? new CamelRouteResource(camelRouteJson);
const camelResource = props.camelResource ?? new CamelRouteResource([camelRouteJson]);
const currentSchemaType = camelResource.getType();
const setCurrentSchemaTypeSpy = jest.fn();
const updateEntitiesFromCamelResourceSpy = jest.fn();
Expand Down

0 comments on commit 020af1b

Please sign in to comment.