Skip to content

Commit

Permalink
fix(code): Code cleaning in kamelet-visual-entity.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivam Gupta committed Mar 29, 2024
1 parent 0890c55 commit 5c0ff30
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
10 changes: 5 additions & 5 deletions packages/ui/src/models/camel/kamelet-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export class KameletResource extends CamelKResource implements RouteTemplateBean
}

this.flow = new KameletVisualEntity(this.resource as IKameletDefinition);
if (this.flow.spec.template.beans) {
this.beans = new RouteTemplateBeansEntity(this.flow.spec.template as RouteTemplateBeansParentType);
if (this.flow.kamelet.spec.template.beans) {
this.beans = new RouteTemplateBeansEntity(this.flow.kamelet.spec.template as RouteTemplateBeansParentType);
}
}

Expand Down Expand Up @@ -82,13 +82,13 @@ export class KameletResource extends CamelKResource implements RouteTemplateBean
}

createRouteTemplateBeansEntity(): RouteTemplateBeansEntity {
this.flow.spec.template.beans = [];
this.beans = new RouteTemplateBeansEntity(this.flow.spec.template as RouteTemplateBeansParentType);
this.flow.kamelet.spec.template.beans = [];
this.beans = new RouteTemplateBeansEntity(this.flow.kamelet.spec.template as RouteTemplateBeansParentType);
return this.beans;
}

deleteRouteTemplateBeansEntity(): void {
this.flow.spec.template.beans = undefined;
this.flow.kamelet.spec.template.beans = undefined;
this.beans = undefined;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,23 @@ describe('KameletVisualEntity', () => {
});

it('should set the id to the name if provided', () => {
const kamelet = new KameletVisualEntity(kameletDef);
expect(kamelet.id).toEqual('My Kamelet');
expect(kamelet.metadata.name).toEqual('My Kamelet');
const kameletVisualEntity = new KameletVisualEntity(kameletDef);
expect(kameletVisualEntity.id).toEqual('My Kamelet');
expect(kameletVisualEntity.kamelet.metadata.name).toEqual('My Kamelet');
});

it('should set a random id if the kamelet name is not provided', () => {
kameletDef.metadata.name = undefined as unknown as IKameletMetadata['name'];
const kamelet = new KameletVisualEntity(kameletDef);
expect(kamelet.id).toEqual('kamelet-1234');
expect(kamelet.metadata.name).toEqual('kamelet-1234');
const kameletVisualEntity = new KameletVisualEntity(kameletDef);
expect(kameletVisualEntity.id).toEqual('kamelet-1234');
expect(kameletVisualEntity.kamelet.metadata.name).toEqual('kamelet-1234');
});

it('should set the id', () => {
const kamelet = new KameletVisualEntity(kameletDef);
kamelet.setId('new-id');
expect(kamelet.id).toEqual('new-id');
expect(kamelet.metadata.name).toEqual('new-id');
const kameletVisualEntity = new KameletVisualEntity(kameletDef);
kameletVisualEntity.setId('new-id');
expect(kameletVisualEntity.id).toEqual('new-id');
expect(kameletVisualEntity.kamelet.metadata.name).toEqual('new-id');
});

describe('getComponentSchema when querying the ROOT_PATH', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getCamelRandomId } from '../../../camel-utils/camel-random-id';
import { ROOT_PATH, getCustomSchemaFromKamelet, updateKameletFromCustomSchema } from '../../../utils';
import { EntityType } from '../../camel/entities';
import { CatalogKind } from '../../catalog-kind';
import { IKameletDefinition, IKameletMetadata, IKameletSpec } from '../../kamelets-catalog';
import { IKameletDefinition } from '../../kamelets-catalog';
import { KaotoSchemaDefinition } from '../../kaoto-schema';
import { VisualComponentSchema } from '../base-visual-entity';
import { AbstractCamelVisualEntity } from './abstract-camel-visual-entity';
Expand All @@ -11,25 +11,22 @@ import { CamelCatalogService } from './camel-catalog.service';
export class KameletVisualEntity extends AbstractCamelVisualEntity {
id: string;
readonly type = EntityType.Kamelet;
spec: IKameletSpec;
metadata: IKameletMetadata;

constructor(public kamelet: IKameletDefinition) {
super({ id: kamelet.metadata?.name, from: kamelet?.spec.template.from });
this.id = (kamelet?.metadata?.name as string) ?? getCamelRandomId('kamelet');
this.metadata = kamelet?.metadata ?? { name: this.id };
this.metadata.name = kamelet?.metadata.name ?? this.id;
this.spec = kamelet.spec;
this.kamelet.metadata = kamelet?.metadata ?? { name: this.id };
this.kamelet.metadata.name = kamelet?.metadata.name ?? this.id;
}

/** Internal API methods */
setId(routeId: string): void {
this.id = routeId;
this.metadata.name = this.id;
this.kamelet.metadata.name = this.id;
}

getId(): string {
return this.metadata.name;
return this.kamelet.metadata.name;
}

getComponentSchema(path?: string | undefined): VisualComponentSchema | undefined {
Expand Down Expand Up @@ -67,6 +64,6 @@ export class KameletVisualEntity extends AbstractCamelVisualEntity {
}

protected getRootUri(): string | undefined {
return this.spec.template.from?.uri;
return this.kamelet.spec.template.from?.uri;
}
}

0 comments on commit 5c0ff30

Please sign in to comment.