Skip to content

Commit

Permalink
Fix(1429): Supporting parameter notation and ceased URI parameters se…
Browse files Browse the repository at this point in the history
…rialization
  • Loading branch information
shivamG640 committed Sep 30, 2024
1 parent b0291b5 commit afb1ae3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('AbstractCamelVisualEntity', () => {

it('should delegate the serialization to the `CamelComponentSchemaService`', () => {
const newUri = 'timer:MyTimer';
const spy = jest.spyOn(CamelComponentSchemaService, 'getUriSerializedDefinition');
const spy = jest.spyOn(CamelComponentSchemaService, 'getMultiValueSerializedDefinition');
abstractVisualEntity.updateModel('from', { uri: newUri });

expect(spy).toHaveBeenCalledWith('from', { uri: newUri });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ export abstract class AbstractCamelVisualEntity<T extends object> implements Bas

updateModel(path: string | undefined, value: unknown): void {
if (!path) return;
let updatedValue = CamelComponentSchemaService.getUriSerializedDefinition(path, value);
updatedValue = CamelComponentSchemaService.getMultiValueSerializedDefinition(path, updatedValue);
let updatedValue = CamelComponentSchemaService.getMultiValueSerializedDefinition(path, value);

setValue(this.route, path, updatedValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,52 +595,6 @@ describe('CamelComponentSchemaService', () => {
});
});

describe('getUriSerializedDefinition', () => {
it('should return the same parameters if the definition is not a component', () => {
const definition = { log: { message: 'Hello World' } };
const result = CamelComponentSchemaService.getUriSerializedDefinition('from', definition);

expect(result).toEqual(definition);
});

it('should return the same parameters if the component is not found', () => {
const definition = { uri: 'unknown-component' };
const result = CamelComponentSchemaService.getUriSerializedDefinition('from', definition);

expect(result).toEqual(definition);
});

it('should query the catalog service and generate the required parameters array', () => {
const definition = { uri: 'log', parameters: { message: 'Hello World' } };
const catalogServiceSpy = jest.spyOn(CamelCatalogService, 'getCatalogLookup');
const camelUriHelperSpy = jest.spyOn(CamelUriHelper, 'getUriStringFromParameters');

CamelComponentSchemaService.getUriSerializedDefinition('from', definition);

expect(catalogServiceSpy).toHaveBeenCalledWith('log');
expect(camelUriHelperSpy).toHaveBeenCalledWith(definition.uri, 'log:loggerName', definition.parameters, {
requiredParameters: ['loggerName'],
defaultValues: {
groupActiveOnly: 'true',
level: 'INFO',
maxChars: 10000,
showBody: true,
showBodyType: true,
showCachedStreams: true,
skipBodyLineSeparator: true,
style: 'Default',
},
});
});

it('should return the serialized definition', () => {
const definition = { uri: 'timer', parameters: { timerName: 'MyTimer', delay: '1000', repeatCount: 10 } };
const result = CamelComponentSchemaService.getUriSerializedDefinition('from', definition);

expect(result).toEqual({ uri: 'timer:MyTimer', parameters: { delay: '1000', repeatCount: 10 } });
});
});

describe('getComponentNameFromUri', () => {
it('should return undefined if the uri is empty', () => {
const componentName = CamelComponentSchemaService.getComponentNameFromUri('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,43 +219,6 @@ export class CamelComponentSchemaService {
return '';
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
static getUriSerializedDefinition(path: string, definition: any): ParsedParameters | undefined {
const camelElementLookup = this.getCamelComponentLookup(path, definition);
if (camelElementLookup.componentName === undefined) {
return definition;
}

const catalogLookup = CamelCatalogService.getCatalogLookup(camelElementLookup.componentName);
if (
catalogLookup.catalogKind === CatalogKind.Component &&
catalogLookup.definition?.component.syntax !== undefined
) {
const requiredParameters: string[] = [];
const defaultValues: ParsedParameters = {};
if (catalogLookup.definition?.properties !== undefined) {
Object.entries(catalogLookup.definition.properties).forEach(([key, value]) => {
if (value.required) requiredParameters.push(key);
if (value.defaultValue) defaultValues[key] = value.defaultValue;
});
}

const result = CamelUriHelper.getUriStringFromParameters(
definition.uri,
catalogLookup.definition.component.syntax,
definition.parameters,
{
requiredParameters,
defaultValues,
},
);

return Object.assign({}, definition, { uri: result.uri, parameters: result.parameters });
}

return definition;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
static getMultiValueSerializedDefinition(path: string, definition: any): ParsedParameters | undefined {
const camelElementLookup = this.getCamelComponentLookup(path, definition);
Expand Down Expand Up @@ -464,7 +427,21 @@ export class CamelComponentSchemaService {
const [pathUri, queryUri] = definition.uri?.split('?') ?? [undefined, undefined];
if (queryUri) {
definition.uri = pathUri;
Object.assign(definition.parameters, CamelUriHelper.getParametersFromQueryString(queryUri));

const validParametersFromQueryString = Object.entries(
CamelUriHelper.getParametersFromQueryString(queryUri),
).reduce((parameters, parameter) => {
if (parameter) {
const [key, stringValue] = parameter;
if (catalogLookup.definition?.properties[key]) {
parameters[key] = stringValue;
}
}

return parameters;
}, {} as ParsedParameters);

Object.assign(definition.parameters, validParametersFromQueryString);
}

if (pathUri && catalogLookup.catalogKind === CatalogKind.Component) {
Expand Down

0 comments on commit afb1ae3

Please sign in to comment.