diff --git a/packages/ui/src/services/documentation.service.test.ts b/packages/ui/src/services/documentation.service.test.ts index fcf09c9b2..a4881c5ec 100644 --- a/packages/ui/src/services/documentation.service.test.ts +++ b/packages/ui/src/services/documentation.service.test.ts @@ -150,7 +150,7 @@ describe('DocumentationService', () => { '| | from | timer | timerName | tutorial |', ); expect(markdown).toContain('# Beans'); - expect(markdown).toContain('| myBean | io.kaoto.MyBean | p1 | p1v |'); + expect(markdown).toContain('| myBean | io.kaoto.MyBean | p1 | | p1v |'); }); it('should generate kamelet markdown', () => { diff --git a/packages/ui/src/services/parsers/beans-parser.test.ts b/packages/ui/src/services/parsers/beans-parser.test.ts index 078070a6b..49ca0a81f 100644 --- a/packages/ui/src/services/parsers/beans-parser.test.ts +++ b/packages/ui/src/services/parsers/beans-parser.test.ts @@ -1,5 +1,5 @@ import { BeansParser } from './beans-parser'; -import { beansYaml } from '../../stubs/beans'; +import { beansWithParamsYaml, beansYaml } from '../../stubs/beans'; import { BeansEntity } from '../../models/visualization/metadata'; import { CamelResourceFactory } from '../../models/camel/camel-resource-factory'; @@ -10,15 +10,17 @@ describe('BeansParser', () => { const parsedTable = BeansParser.parseBeansEntity(beansEntity, 'Beans'); expect(parsedTable.title).toEqual('Beans'); expect(parsedTable.data.length).toEqual(3); - expect(parsedTable.headers.length).toEqual(4); + expect(parsedTable.headers.length).toEqual(5); expect(parsedTable.headers[0]).toEqual('Name'); expect(parsedTable.headers[1]).toEqual('Type'); expect(parsedTable.headers[2]).toEqual('Property Name'); - expect(parsedTable.headers[3]).toEqual('Property Value'); + expect(parsedTable.headers[3]).toEqual('Parameter Name'); + expect(parsedTable.headers[4]).toEqual('Value'); expect(parsedTable.data[0][0]).toEqual('myBean'); expect(parsedTable.data[0][1]).toEqual('io.kaoto.MyBean'); expect(parsedTable.data[0][2]).toEqual('p1'); - expect(parsedTable.data[0][3]).toEqual('p1v'); + expect(parsedTable.data[0][3]).toEqual(''); + expect(parsedTable.data[0][4]).toEqual('p1v'); }); it('should parse BeansEntity without properties', () => { @@ -31,10 +33,53 @@ describe('BeansParser', () => { expect(parsedTable.data[0][1]).toEqual('io.kaoto.MyBean'); expect(parsedTable.data[0][2]).toEqual(''); expect(parsedTable.data[0][3]).toEqual(''); + expect(parsedTable.data[0][4]).toEqual(''); expect(parsedTable.data[1][0]).toEqual('myBean2'); expect(parsedTable.data[1][1]).toEqual('io.kaoto.MyBean'); expect(parsedTable.data[1][2]).toEqual(''); expect(parsedTable.data[1][3]).toEqual(''); + expect(parsedTable.data[1][4]).toEqual(''); + }); + + it('should parse BeanEntity with parameters', () => { + const beansEntity = CamelResourceFactory.createCamelResource(beansWithParamsYaml).getEntities()[0] as BeansEntity; + const parsedTable = BeansParser.parseBeansEntity(beansEntity, 'Beans'); + expect(parsedTable.data.length).toEqual(7); + expect(parsedTable.data[0][0]).toEqual('test'); + expect(parsedTable.data[0][1]).toEqual('java.util.Timer'); + expect(parsedTable.data[0][2]).toEqual('foo'); + expect(parsedTable.data[0][3]).toEqual(''); + expect(parsedTable.data[0][4]).toEqual('bar'); + expect(parsedTable.data[1][0]).toEqual(''); + expect(parsedTable.data[1][1]).toEqual(''); + expect(parsedTable.data[1][2]).toEqual(''); + expect(parsedTable.data[1][3]).toEqual('builderClass'); + expect(parsedTable.data[1][4]).toEqual('test'); + expect(parsedTable.data[2][0]).toEqual(''); + expect(parsedTable.data[2][1]).toEqual(''); + expect(parsedTable.data[2][2]).toEqual(''); + expect(parsedTable.data[2][3]).toEqual('builderMethod'); + expect(parsedTable.data[3][4]).toEqual('test'); + expect(parsedTable.data[3][0]).toEqual(''); + expect(parsedTable.data[3][1]).toEqual(''); + expect(parsedTable.data[3][2]).toEqual(''); + expect(parsedTable.data[3][3]).toEqual('destroyMethod'); + expect(parsedTable.data[3][4]).toEqual('test'); + expect(parsedTable.data[4][0]).toEqual(''); + expect(parsedTable.data[4][1]).toEqual(''); + expect(parsedTable.data[4][2]).toEqual(''); + expect(parsedTable.data[4][3]).toEqual('factoryBean'); + expect(parsedTable.data[4][4]).toEqual('test'); + expect(parsedTable.data[5][0]).toEqual(''); + expect(parsedTable.data[5][1]).toEqual(''); + expect(parsedTable.data[5][2]).toEqual(''); + expect(parsedTable.data[5][3]).toEqual('factoryMethod'); + expect(parsedTable.data[5][4]).toEqual('test'); + expect(parsedTable.data[6][0]).toEqual(''); + expect(parsedTable.data[6][1]).toEqual(''); + expect(parsedTable.data[6][2]).toEqual(''); + expect(parsedTable.data[6][3]).toEqual('initMethod'); + expect(parsedTable.data[6][4]).toEqual('test'); }); }); }); diff --git a/packages/ui/src/services/parsers/beans-parser.ts b/packages/ui/src/services/parsers/beans-parser.ts index b8eea0d64..8629af0c3 100644 --- a/packages/ui/src/services/parsers/beans-parser.ts +++ b/packages/ui/src/services/parsers/beans-parser.ts @@ -5,26 +5,44 @@ import { BeanFactory, BeansDeserializer } from '@kaoto/camel-catalog/types'; import { CommonParser } from './common-parser'; export class BeansParser { - static readonly HEADERS_BEANS = ['Name', 'Type', 'Property Name', 'Property Value']; + static readonly HEADERS_BEANS = ['Name', 'Type', 'Property Name', 'Parameter Name', 'Value']; static parseBeansEntity(entity: BeansEntity | RouteTemplateBeansEntity, label: string): ParsedTable { const beansModel: BeansDeserializer | Partial[] = entity.parent.beans; const parsedTable = new ParsedTable({ title: label, headers: BeansParser.HEADERS_BEANS }); beansModel.forEach((bean) => { - if (!bean.properties || Object.keys(bean.properties).length === 0) { - parsedTable.data.push([bean.name || '', bean.type || '', '', '']); + const beanPropertiesCount = bean.properties ? Object.keys(bean.properties).length : 0; + const beanParametersCount = Object.keys(bean).filter( + (key) => !['name', 'type', 'properties'].includes(key), + ).length; + if (beanPropertiesCount === 0 && beanParametersCount === 0) { + parsedTable.data.push([bean.name || '', bean.type || '', '', '', '']); return; } + const parsedProperties = CommonParser.parseParameters(bean.properties); Object.entries(parsedProperties).forEach(([propKey, propValue], index) => { parsedTable.data.push([ index === 0 && bean.name ? bean.name : '', index === 0 && bean.type ? bean.type : '', propKey, + '', propValue, ]); }); + + const parsedParameters = CommonParser.parseParameters(bean, ['name', 'type', 'properties']); + Object.entries(parsedParameters).forEach(([paramKey, paramValue], index) => { + parsedTable.data.push([ + index === 0 && beanPropertiesCount === 0 && bean.name ? bean.name : '', + index === 0 && beanPropertiesCount === 0 && bean.type ? bean.type : '', + '', + paramKey, + paramValue, + ]); + }); }); + return parsedTable; } } diff --git a/packages/ui/src/services/parsers/common-parser.ts b/packages/ui/src/services/parsers/common-parser.ts index 448c930ca..b47290b67 100644 --- a/packages/ui/src/services/parsers/common-parser.ts +++ b/packages/ui/src/services/parsers/common-parser.ts @@ -160,11 +160,12 @@ export class CommonParser { /* eslint-disable @typescript-eslint/no-explicit-any */ static parseParameters( - model: Record, + model?: Record, excluded: ReadonlyArray = [], prefix?: string, ): Record { const answer: Record = {}; + if (!model) return answer; Object.entries(model) .filter(([key]) => !excluded.includes(key)) .forEach(([key, value]) => { diff --git a/packages/ui/src/stubs/beans.ts b/packages/ui/src/stubs/beans.ts index 141974c9a..8f44d33a6 100644 --- a/packages/ui/src/stubs/beans.ts +++ b/packages/ui/src/stubs/beans.ts @@ -12,6 +12,20 @@ export const beansYaml = ` p1s1: p1s1v `; +export const beansWithParamsYaml = ` +- beans: + - builderClass: test + builderMethod: test + destroyMethod: test + factoryBean: test + factoryMethod: test + initMethod: test + name: test + properties: + foo: bar + type: java.util.Timer +`; + export const beansJson = { beans: [ {