diff --git a/packages/ui/src/components/InlineEdit/routeIdValidator.test.ts b/packages/ui/src/components/InlineEdit/routeIdValidator.test.ts index a7468b280..1dfad04d5 100644 --- a/packages/ui/src/components/InlineEdit/routeIdValidator.test.ts +++ b/packages/ui/src/components/InlineEdit/routeIdValidator.test.ts @@ -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'); @@ -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'); @@ -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'); @@ -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'); diff --git a/packages/ui/src/components/Visualization/Canvas/Canvas.test.tsx b/packages/ui/src/components/Visualization/Canvas/Canvas.test.tsx index 2262a8223..6801b3638 100644 --- a/packages/ui/src/components/Visualization/Canvas/Canvas.test.tsx +++ b/packages/ui/src/components/Visualization/Canvas/Canvas.test.tsx @@ -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'); diff --git a/packages/ui/src/components/Visualization/Custom/ContextMenu/ItemEnableAllSteps.test.tsx b/packages/ui/src/components/Visualization/Custom/ContextMenu/ItemEnableAllSteps.test.tsx index db58f5b07..798f18cc9 100644 --- a/packages/ui/src/components/Visualization/Custom/ContextMenu/ItemEnableAllSteps.test.tsx +++ b/packages/ui/src/components/Visualization/Custom/ContextMenu/ItemEnableAllSteps.test.tsx @@ -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()); @@ -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()); diff --git a/packages/ui/src/components/Visualization/Custom/ContextMenu/NodeContextMenu.test.tsx b/packages/ui/src/components/Visualization/Custom/ContextMenu/NodeContextMenu.test.tsx index a1a979037..43b83c620 100644 --- a/packages/ui/src/components/Visualization/Custom/ContextMenu/NodeContextMenu.test.tsx +++ b/packages/ui/src/components/Visualization/Custom/ContextMenu/NodeContextMenu.test.tsx @@ -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()); diff --git a/packages/ui/src/hooks/entities.test.tsx b/packages/ui/src/hooks/entities.test.tsx index b82ad2963..1f9c5bc7d 100644 --- a/packages/ui/src/hooks/entities.test.tsx +++ b/packages/ui/src/hooks/entities.test.tsx @@ -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(); }); @@ -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`, + ); + }); }); diff --git a/packages/ui/src/models/camel/camel-k-resource-factory.ts b/packages/ui/src/models/camel/camel-k-resource-factory.ts index f5f538d72..bd5cdd1ef 100644 --- a/packages/ui/src/models/camel/camel-k-resource-factory.ts +++ b/packages/ui/src/models/camel/camel-k-resource-factory.ts @@ -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) : {}; if ((jsonRecord && typeof json === 'object' && 'kind' in jsonRecord) || type) { diff --git a/packages/ui/src/models/camel/camel-k-resource.ts b/packages/ui/src/models/camel/camel-k-resource.ts index ce22e7e63..6b6ddb32d 100644 --- a/packages/ui/src/models/camel/camel-k-resource.ts +++ b/packages/ui/src/models/camel/camel-k-resource.ts @@ -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; diff --git a/packages/ui/src/models/camel/camel-resource-factory.ts b/packages/ui/src/models/camel/camel-resource-factory.ts index 5fd6d3494..83a4abc6c 100644 --- a/packages/ui/src/models/camel/camel-resource-factory.ts +++ b/packages/ui/src/models/camel/camel-resource-factory.ts @@ -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 { /** @@ -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); } } diff --git a/packages/ui/src/models/camel/camel-route-resource.test.ts b/packages/ui/src/models/camel/camel-route-resource.test.ts index 3573d53ed..1c29e4a34 100644 --- a/packages/ui/src/models/camel/camel-route-resource.test.ts +++ b/packages/ui/src/models/camel/camel-route-resource.test.ts @@ -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); @@ -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]; @@ -98,14 +110,14 @@ 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); @@ -113,7 +125,7 @@ describe('CamelRouteResource', () => { describe('toJSON', () => { it('should return JSON', () => { - const resource = new CamelRouteResource(camelRouteJson); + const resource = new CamelRouteResource([camelRouteJson]); expect(resource.toJSON()).toMatchSnapshot(); }); @@ -141,7 +153,7 @@ 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(); @@ -149,7 +161,7 @@ describe('CamelRouteResource', () => { }); 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'); @@ -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); @@ -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(); diff --git a/packages/ui/src/models/camel/camel-route-resource.ts b/packages/ui/src/models/camel/camel-route-resource.ts index 8b847d212..238d93865 100644 --- a/packages/ui/src/models/camel/camel-route-resource.ts +++ b/packages/ui/src/models/camel/camel-route-resource.ts @@ -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'; @@ -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') { @@ -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; } diff --git a/packages/ui/src/models/camel/kamelet-resource.test.ts b/packages/ui/src/models/camel/kamelet-resource.test.ts index 591f16609..7a31cbb45 100644 --- a/packages/ui/src/models/camel/kamelet-resource.test.ts +++ b/packages/ui/src/models/camel/kamelet-resource.test.ts @@ -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', () => { diff --git a/packages/ui/src/serializers/__snapshots__/yaml-camel-resource-serializer.test.ts.snap b/packages/ui/src/serializers/__snapshots__/yaml-camel-resource-serializer.test.ts.snap new file mode 100644 index 000000000..6b3d51d86 --- /dev/null +++ b/packages/ui/src/serializers/__snapshots__/yaml-camel-resource-serializer.test.ts.snap @@ -0,0 +1,38 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`YamlCamelResourceSerializer includes comments in YAML string 1`] = ` +"# comment1 + +# Comment2 +- route: + id: route-8888 + from: + uri: timer + parameters: + timerName: tutorial + steps: + - set-header: + name: myChoice + simple: \${random(2)} + - choice: + otherwise: + steps: + - to: + uri: "amqp:queue:" + - to: + uri: "amqp:queue:" + - log: + id: log-2 + message: We got a \${body} + when: + - steps: + - log: + id: log-1 + message: We got a one. + simple: \${header.myChoice} == 1 + - to: + uri: direct:my-route + parameters: + bridgeErrorHandler: true +" +`; diff --git a/packages/ui/src/serializers/camel-resource-serializer.ts b/packages/ui/src/serializers/camel-resource-serializer.ts index 2853bdc5d..34d22546f 100644 --- a/packages/ui/src/serializers/camel-resource-serializer.ts +++ b/packages/ui/src/serializers/camel-resource-serializer.ts @@ -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 | undefined; serialize: (resource: CamelResource) => string; + getComments: () => string[]; + setComments: (comments: string[]) => void; } diff --git a/packages/ui/src/serializers/xml-camel-resource-serializer.ts b/packages/ui/src/serializers/xml-camel-resource-serializer.ts index 70e5eae13..6d9985965 100644 --- a/packages/ui/src/serializers/xml-camel-resource-serializer.ts +++ b/packages/ui/src/serializers/xml-camel-resource-serializer.ts @@ -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 { //TODO implement return {}; } @@ -15,4 +16,12 @@ export class XmlCamelResourceSerializer implements CamelResourceSerializer { //TODO implement return ''; } + + getComments(): string[] { + return []; + } + + setComments(_comments: string[]): void { + //TODO implement + } } diff --git a/packages/ui/src/serializers/yaml-camel-resource-serializer.test.ts b/packages/ui/src/serializers/yaml-camel-resource-serializer.test.ts index ea6518f01..9991cb242 100644 --- a/packages/ui/src/serializers/yaml-camel-resource-serializer.test.ts +++ b/packages/ui/src/serializers/yaml-camel-resource-serializer.test.ts @@ -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; @@ -22,11 +23,19 @@ 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'); }); + + it('includes comments in YAML string', () => { + const entities = serializer.parse('# comment1\n' + camelRouteYaml); + expect(serializer.comments.includes('# comment1')).toBeTruthy(); + + serializer.comments.push('# Comment2'); + const result = serializer.serialize(new CamelRouteResource(entities as CamelYamlDsl)); + expect(result).toMatchSnapshot(); + }); }); diff --git a/packages/ui/src/serializers/yaml-camel-resource-serializer.ts b/packages/ui/src/serializers/yaml-camel-resource-serializer.ts index 1d5af6472..aad2b1ab1 100644 --- a/packages/ui/src/serializers/yaml-camel-resource-serializer.ts +++ b/packages/ui/src/serializers/yaml-camel-resource-serializer.ts @@ -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 { /** @@ -13,7 +14,7 @@ export class YamlCamelResourceSerializer implements CamelResourceSerializer { * ``` * The regular expression should match the first three lines */ - COMMENTED_LINES_REGEXP = /^\s*#.*$/; + static readonly COMMENTED_LINES_REGEXP = /^\s*#.*$/; comments: string[] = []; static isApplicable(_code: unknown): boolean { @@ -23,7 +24,7 @@ export class YamlCamelResourceSerializer implements CamelResourceSerializer { return true; } - parse(code: string): unknown { + parse(code: string): CamelYamlDsl | Integration | Kamelet | KameletBinding | Pipe { if (!code || typeof code !== 'string') return []; this.comments = this.parseComments(code); @@ -40,11 +41,19 @@ export class YamlCamelResourceSerializer implements CamelResourceSerializer { return code; } + getComments(): string[] { + return this.comments; + } + + setComments(comments: string[]): void { + this.comments = comments; + } + 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; diff --git a/packages/ui/src/stubs/TestProvidersWrapper.tsx b/packages/ui/src/stubs/TestProvidersWrapper.tsx index dfead3956..9963905bf 100644 --- a/packages/ui/src/stubs/TestProvidersWrapper.tsx +++ b/packages/ui/src/stubs/TestProvidersWrapper.tsx @@ -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();