Skip to content

Commit

Permalink
reflect PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mmelko committed Dec 2, 2024
1 parent c412db0 commit 3a2e5de
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 109 deletions.
69 changes: 3 additions & 66 deletions packages/ui/src/hooks/entities.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ describe('useEntities', () => {
expect(notifierSpy).toHaveBeenCalledWith('entities:updated', camelRouteYaml_1_1_updated);
});

it('should notifiy subscribers when the entities are updated', () => {
it('should notify subscribers when the entities are updated', () => {
const notifierSpy = jest.spyOn(eventNotifier, 'next');
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 @@ -162,69 +164,4 @@ describe('useEntities', () => {
`,
);
});

describe('comments', () => {
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.getComments()).toEqual([
'# This is a comment',
' # An indented comment',
'',
]);
});

it('should add comments to the source code', () => {
const notifierSpy = jest.spyOn(eventNotifier, 'next');
const { result } = renderHook(() => useEntities());

act(() => {
result.current.camelResource.setComments(['# This is a comment', ' # An indented comment', '']);
result.current.camelResource.addNewEntity();
result.current.updateSourceCodeFromEntities();
});

expect(notifierSpy).toHaveBeenCalledWith(
'entities:updated',
`# This is a comment
# An indented comment
- route:
id: route-1234
from:
id: from-1234
uri: timer
parameters:
period: "1000"
timerName: template
steps:
- log:
id: log-1234
message: \${body}
`,
);
});
});
});
11 changes: 0 additions & 11 deletions packages/ui/src/hooks/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@ import { BaseVisualCamelEntity } from '../models/visualization/base-visual-entit
import { EventNotifier } from '../utils';
import { CamelResourceFactory } from '../models/camel/camel-resource-factory';

/**
* Regular expression to match commented lines, regardless of indentation
* Given the following examples, the regular expression should match the comments:
* ```
* # This is a comment
* # This is an indented comment
*# This is an indented comment
* ```
* The regular expression should match the first three lines
*/

export interface EntitiesContextResult {
entities: BaseCamelEntity[];
currentSchemaType: SourceSchemaType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CamelResourceFactory.createCamelResource should create an empty KameletResource if no args is specified 1`] = `
[
{
"from": {
"id": "from-1234",
"parameters": {
"period": "{{period}}",
"timerName": "user",
},
"steps": [
{
"to": {
"parameters": {
"httpUri": "random-data-api.com/api/v2/users",
},
"uri": "https",
},
},
{
"to": "kamelet:sink",
},
],
"uri": "timer",
},
},
]
`;

exports[`createCamelResource should create an empty KameletResource if no args is specified 1`] = `
[
{
Expand Down
5 changes: 2 additions & 3 deletions packages/ui/src/models/camel/camel-k-resource-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {

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

if ((jsonRecord && typeof json === 'object' && 'kind' in jsonRecord) || type) {
switch (jsonRecord['kind'] || type) {
case SourceSchemaType.Integration:
Expand All @@ -24,8 +25,6 @@ export class CamelKResourceFactory {
return new KameletBindingResource(json as KameletBindingType);
case SourceSchemaType.Pipe:
return new PipeResource(json as PipeType);
default:
return undefined;
}
}
return undefined;
Expand Down
2 changes: 0 additions & 2 deletions packages/ui/src/models/camel/camel-k-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ export abstract class CamelKResource implements CamelResource {
parsedResource: unknown,
private readonly serializer: CamelResourceSerializer = new YamlCamelResourceSerializer(),
) {
this.serializer = serializer;

if (parsedResource) {
this.resource = parsedResource as CamelKType;
} else {
Expand Down
14 changes: 7 additions & 7 deletions packages/ui/src/models/camel/camel-resource-factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SourceSchemaType } from './source-schema-type';
import { CamelResource } from './camel-resource';
import { XmlCamelResourceSerializer, YamlCamelResourceSerializer } from '../../serializers';
import { CamelResourceSerializer, XmlCamelResourceSerializer, YamlCamelResourceSerializer } from '../../serializers';
import { CamelRouteResource } from './camel-route-resource';
import { CamelKResourceFactory } from './camel-k-resource-factory';

Expand All @@ -14,14 +14,14 @@ export class CamelResourceFactory {
* @param source
*/
static createCamelResource(source?: string, type?: SourceSchemaType): CamelResource {
if (XmlCamelResourceSerializer.isApplicable(source)) {
return new CamelRouteResource(source, new XmlCamelResourceSerializer());
}
const serializer: CamelResourceSerializer = XmlCamelResourceSerializer.isApplicable(source)
? new XmlCamelResourceSerializer()
: new YamlCamelResourceSerializer();

const serializer = new YamlCamelResourceSerializer();
const resource = CamelKResourceFactory.getCamelKResource(serializer.parse(source ?? ''), type);
const parsedCode = source ? serializer.parse(source) : source;
const resource = CamelKResourceFactory.getCamelKResource(parsedCode, type);

if (resource) return resource;
return new CamelRouteResource(source, serializer);
return new CamelRouteResource(parsedCode, serializer);
}
}
2 changes: 1 addition & 1 deletion packages/ui/src/models/camel/camel-resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('CamelResourceFactory.createCamelResource', () => {
});

it('should create a Kamelet', () => {
const resource = CamelResourceFactory.createCamelResource(kameletJson);
const resource = CamelResourceFactory.createCamelResource(JSON.stringify(kameletJson));
expect(resource.getType()).toEqual(SourceSchemaType.Kamelet);
expect(resource.getVisualEntities().length).toEqual(1);
});
Expand Down
20 changes: 10 additions & 10 deletions packages/ui/src/models/camel/camel-route-resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { CamelResourceFactory } from './camel-resource-factory';

describe('CamelRouteResource', () => {
it('should create CamelRouteResource', () => {
const resource = new CamelRouteResource(camelRouteYaml);
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 +39,7 @@ describe('CamelRouteResource', () => {
[null, undefined],
[[], undefined],
])('should return the appropriate entity for: %s', (json, expected) => {
const resource = new CamelRouteResource(JSON.stringify(json));
const resource = new CamelRouteResource(json);
const firstEntity = resource.getVisualEntities()[0] ?? resource.getEntities()[0];

if (typeof expected === 'function') {
Expand Down Expand Up @@ -98,22 +98,22 @@ describe('CamelRouteResource', () => {
});

it('should return visual entities', () => {
const resource = new CamelRouteResource(camelRouteYaml);
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(JSON.stringify(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(camelRouteYaml);
const resource = new CamelRouteResource(camelRouteJson);
expect(resource.toJSON()).toMatchSnapshot();
});

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

describe('removeEntity', () => {
it('should not do anything if the ID is not provided', () => {
const resource = new CamelRouteResource(camelRouteYaml);
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(camelRouteYaml);
const resource = new CamelRouteResource(camelRouteJson);

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

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

it('should allow to remove an entity', () => {
const resource = new CamelRouteResource(JSON.stringify([camelRouteJson, camelFromJson]));
const resource = new CamelRouteResource([camelRouteJson, camelFromJson]);
const camelRouteEntity = resource.getVisualEntities()[0];

resource.removeEntity(camelRouteEntity.id);
Expand All @@ -166,7 +166,7 @@ describe('CamelRouteResource', () => {
});

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

resource.removeEntity(camelRouteEntity.id);
Expand Down Expand Up @@ -209,7 +209,7 @@ describe('CamelRouteResource', () => {
[{ anotherUnknownContent: {} }],
[{}],
])('should not throw error when calling: %s', (json) => {
const resource = new CamelRouteResource(JSON.stringify(json));
const resource = new CamelRouteResource(json);
const firstEntity = resource.getVisualEntities()[0] ?? resource.getEntities()[0];
expect(firstEntity.toJSON()).not.toBeUndefined();
});
Expand Down
5 changes: 2 additions & 3 deletions packages/ui/src/models/camel/camel-route-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ export class CamelRouteResource implements CamelResource, BeansAwareResource {
private resolvedEntities: BaseVisualCamelEntityDefinition | undefined;
private serializer: CamelResourceSerializer;

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

const parsedCode = this.serializer.parse(code);
const entities = Array.isArray(parsedCode) ? parsedCode : [parsedCode];
const entities = Array.isArray(code) ? code : [code];
this.entities = entities.reduce((acc, rawItem) => {
const entity = this.getEntity(rawItem);
if (isDefined(entity) && typeof entity === 'object') {
Expand Down
6 changes: 3 additions & 3 deletions packages/ui/src/models/camel/kamelet-resource.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { kameletJson } from '../../stubs/kamelet-route';
import { AddStepMode } from '../visualization/base-visual-entity';
import { CamelComponentFilterService } from '../visualization/flows/support/camel-component-filter.service';
import { createCamelResource } from './camel-resource';
import { KameletResource } from './kamelet-resource';
import { SourceSchemaType } from './source-schema-type';
import { cloneDeep } from 'lodash';
import { CamelKResourceFactory } from './camel-k-resource-factory';
import cloneDeep from 'lodash/cloneDeep';

describe('KameletResource', () => {
it('should create a new KameletResource', () => {
Expand Down Expand Up @@ -78,7 +78,7 @@ describe('KameletResource', () => {
it('should delegate to the CamelComponentFilterService', () => {
const filterSpy = jest.spyOn(CamelComponentFilterService, 'getKameletCompatibleComponents');

const resource = createCamelResource(kameletJson);
const resource = CamelKResourceFactory.getCamelKResource(kameletJson)!;
resource.getCompatibleComponents(AddStepMode.ReplaceStep, { path: 'from', label: 'timer' });

expect(filterSpy).toHaveBeenCalledWith(AddStepMode.ReplaceStep, { path: 'from', label: 'timer' }, undefined);
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/models/camel/kamelet-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class KameletResource extends CamelKResource implements RouteTemplateBean
* the CamelRouteVisualEntity.
*/
set(this.resource, 'metadata.name', this.flow.getId());
set(this.resource, 'spec.template.from', this.flow.route.from);
set(this.resource, 'spec.template.from', this.flow.entityDef.template.from);
set(this.resource, 'spec.template.beans', this.beans?.parent.beans);
return this.resource as IKameletDefinition;
}
Expand Down
32 changes: 32 additions & 0 deletions packages/ui/src/serializers/yaml-camel-resource-serializer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { YamlCamelResourceSerializer } from './yaml-camel-resource-serializer';
import { camelRouteJson, camelRouteYaml } from '../stubs';
import { CamelRouteResource } from '../models/camel';

describe('YamlCamelResourceSerializer', () => {
let serializer: YamlCamelResourceSerializer;

beforeEach(() => {
serializer = new YamlCamelResourceSerializer();
});

it('parses YAML code into JSON object', () => {
const result = serializer.parse(camelRouteYaml);
expect(result).toEqual([camelRouteJson]);
});

it('returns empty array for empty or non-string input in parse', () => {
expect(serializer.parse('')).toEqual([]);
expect(serializer.parse(null as unknown as string)).toEqual([]);
expect(serializer.parse(123 as unknown as string)).toEqual([]);
});

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));
expect(result).toContain('# Comment2');
});
});
Loading

0 comments on commit 3a2e5de

Please sign in to comment.