Skip to content

Commit

Permalink
feat(Rest): Add Rest visual entity
Browse files Browse the repository at this point in the history
This commit adds support for the Rest DSL from Apache Camel.

fix: #1505
fix: #54
  • Loading branch information
lordrip committed Jan 7, 2025
1 parent 0bdf733 commit d84cd72
Show file tree
Hide file tree
Showing 12 changed files with 444 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public String getDataFormatCatalog() throws Exception {
eipModelOptions = eipModel.getOptions();
}

sortPropertiesAccordingToCamelCatalog(dataFormatSchema, eipModelOptions);
sortPropertiesAccordingToCamelCatalog(dataFormatSchema, eipModelOptions);

var dataFormatCatalog = (EipModel) camelCatalog.model(Kind.eip, dataFormatName);
if (dataFormatCatalog == null) {
Expand Down Expand Up @@ -260,7 +260,7 @@ public String getLanguageCatalog() throws Exception {
eipModelOptions = eipModel.getOptions();
}

sortPropertiesAccordingToCamelCatalog(languageSchema, eipModelOptions);
sortPropertiesAccordingToCamelCatalog(languageSchema, eipModelOptions);

var languageCatalog = (EipModel) camelCatalog.model(Kind.eip, languageName);
if (languageCatalog == null) {
Expand Down Expand Up @@ -362,12 +362,17 @@ public String getPatternCatalog() throws Exception {
continue;
}

sortedSchemaProperties.set(propertyName, propertySchema);

var catalogOpOptional = eipModel.getOptions().stream()
.filter(op -> op.getName().equals(propertyName)).findFirst();

if (catalogOpOptional.isEmpty()) {
throw new Exception(
LOGGER.warning(
String.format("Option '%s' not found for processor '%s'", propertyName, processorFQCN));
continue;
}

var catalogOp = catalogOpOptional.get();
if ("object".equals(catalogOp.getType()) && !catalogOp.getJavaType().startsWith("java.util.Map")
&& !propertySchema.has("$comment")) {
Expand All @@ -383,8 +388,6 @@ public String getPatternCatalog() throws Exception {
} else if (catalogOp.getGroup() != null) {
propertySchema.put("group", catalogOp.getGroup());
}

sortedSchemaProperties.set(propertyName, propertySchema);
}

var json = JsonMapper.asJsonObject(eipModel).toJson();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class CamelYamlDslSchemaProcessor {
private static final String PROCESSOR_DEFINITION = "org.apache.camel.model.ProcessorDefinition";
private static final String TOKENIZER_DEFINITION = "org.apache.camel.model.TokenizerDefinition";
private static final String ROUTE_CONFIGURATION_DEFINITION = "org.apache.camel.model.RouteConfigurationDefinition";
private static final String REST_DEFINITION = "org.apache.camel.model.rest.RestDefinition";
private static final String LOAD_BALANCE_DEFINITION = "org.apache.camel.model.LoadBalanceDefinition";
private static final String EXPRESSION_SUB_ELEMENT_DEFINITION =
"org.apache.camel.model.ExpressionSubElementDefinition";
Expand All @@ -58,7 +59,20 @@ public class CamelYamlDslSchemaProcessor {
"org.apache.camel.model.ToDefinition",
List.of("uri", "parameters"),
"org.apache.camel.model.WireTapDefinition",
List.of("uri", "parameters"));
List.of("uri", "parameters"),
"org.apache.camel.model.rest.GetDefinition",
List.of("to"),
"org.apache.camel.model.rest.PostDefinition",
List.of("to"),
"org.apache.camel.model.rest.PutDefinition",
List.of("to"),
"org.apache.camel.model.rest.DeleteDefinition",
List.of("to"),
"org.apache.camel.model.rest.HeadDefinition",
List.of("to"),
"org.apache.camel.model.rest.PatchDefinition",
List.of("to"));

private final List<String> processorReferenceBlockList = List.of(PROCESSOR_DEFINITION);

public CamelYamlDslSchemaProcessor(ObjectMapper mapper, ObjectNode yamlDslSchema) throws Exception {
Expand Down Expand Up @@ -269,6 +283,7 @@ public Map<String, ObjectNode> getProcessors() throws Exception {
.withObject(PROCESSOR_DEFINITION)
.withObject("/properties");
addRouteConfigurationProcessors(relocatedDefinitions, processors);
addRestProcessors(relocatedDefinitions, processors);

var answer = new LinkedHashMap<String, ObjectNode>();
for (var processorEntry : processors) {
Expand Down Expand Up @@ -354,6 +369,24 @@ private void addRouteConfigurationProcessors(ObjectNode relocatedDefinitions, Ob
processors.setAll(onCompletionProcessor);
}

private void addRestProcessors(ObjectNode relocatedDefinitions, ObjectNode processors) {
var restProcessor = relocatedDefinitions
.withObject(REST_DEFINITION)
.withObject("/properties");
var restGetProcessor = restProcessor.withObject("get").withObject("items");
var restPostProcessor = restProcessor.withObject("post").withObject("items");
var restPutProcessor = restProcessor.withObject("put").withObject("items");
var restDeleteProcessor = restProcessor.withObject("delete").withObject("items");
var restHeadProcessor = restProcessor.withObject("head").withObject("items");
var restPatchProcessor = restProcessor.withObject("patch").withObject("items");
processors.set("get", restGetProcessor);
processors.set("post", restPostProcessor);
processors.set("put", restPutProcessor);
processors.set("delete", restDeleteProcessor);
processors.set("head", restHeadProcessor);
processors.set("patch", restPatchProcessor);
}

private ObjectNode extractFromOneOf(String name, ObjectNode definition) throws Exception {
if (!definition.has("oneOf")) {
return definition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,35 @@ void testGetDataFormatCatalog() throws Exception {
assertEquals(1, customPropertiesSchemaRequiredFields.size(), "Size should be 1");
}

@Test
void testRestProcessors() throws Exception {
var restGetProcessorSchema = processorCatalog
.withObject("/get")
.withObject("propertiesSchema");
var restPostProcessorSchema = processorCatalog
.withObject("/post")
.withObject("propertiesSchema");
var restPutProcessorSchema = processorCatalog
.withObject("/put")
.withObject("propertiesSchema");
var restDeleteProcessorSchema = processorCatalog
.withObject("/delete")
.withObject("propertiesSchema");
var restHeadProcessorSchema = processorCatalog
.withObject("/head")
.withObject("propertiesSchema");
var restPatchProcessorSchema = processorCatalog
.withObject("/patch")
.withObject("propertiesSchema");

assertFalse(restGetProcessorSchema.isEmpty(), "get processor schema should not be empty");
assertFalse(restPostProcessorSchema.isEmpty(), "post processor schema should not be empty");
assertFalse(restPutProcessorSchema.isEmpty(), "put processor schema should not be empty");
assertFalse(restDeleteProcessorSchema.isEmpty(), "delete processor schema should not be empty");
assertFalse(restHeadProcessorSchema.isEmpty(), "head processor schema should not be empty");
assertFalse(restPatchProcessorSchema.isEmpty(), "patch processor schema should not be empty");
}

@Test
void testDataFormatEnumParameter() throws Exception {
checkEnumParameters(dataFormatCatalog);
Expand Down
6 changes: 4 additions & 2 deletions packages/ui/src/models/camel/camel-route-resource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CamelYamlDsl, RouteDefinition } from '@kaoto/camel-catalog/types';
import { TileFilter } from '../../components/Catalog';
import { YamlCamelResourceSerializer } from '../../serializers';
import { CamelResourceSerializer } from '../../serializers/camel-resource-serializer';
import { createCamelPropertiesSorter, isDefined } from '../../utils';
import { CatalogKind } from '../catalog-kind';
import { AddStepMode, BaseVisualCamelEntityConstructor } from '../visualization/base-visual-entity';
Expand All @@ -11,6 +13,7 @@ import { CamelInterceptVisualEntity } from '../visualization/flows/camel-interce
import { CamelOnCompletionVisualEntity } from '../visualization/flows/camel-on-completion-visual-entity';
import { CamelOnExceptionVisualEntity } from '../visualization/flows/camel-on-exception-visual-entity';
import { CamelRestConfigurationVisualEntity } from '../visualization/flows/camel-rest-configuration-visual-entity';
import { CamelRestVisualEntity } from '../visualization/flows/camel-rest-visual-entity';
import { CamelRouteConfigurationVisualEntity } from '../visualization/flows/camel-route-configuration-visual-entity';
import { NonVisualEntity } from '../visualization/flows/non-visual-entity';
import { CamelComponentFilterService } from '../visualization/flows/support/camel-component-filter.service';
Expand All @@ -20,8 +23,6 @@ import { BeansEntity, isBeans } from '../visualization/metadata';
import { BaseVisualCamelEntityDefinition, BeansAwareResource, CamelResource } from './camel-resource';
import { BaseCamelEntity, EntityType } from './entities';
import { SourceSchemaType } from './source-schema-type';
import { CamelResourceSerializer } from '../../serializers/camel-resource-serializer';
import { YamlCamelResourceSerializer } from '../../serializers';

export class CamelRouteResource implements CamelResource, BeansAwareResource {
static readonly SUPPORTED_ENTITIES: { type: EntityType; group: string; Entity: BaseVisualCamelEntityConstructor }[] =
Expand All @@ -39,6 +40,7 @@ export class CamelRouteResource implements CamelResource, BeansAwareResource {
{ type: EntityType.OnException, group: 'Error Handling', Entity: CamelOnExceptionVisualEntity },
{ type: EntityType.ErrorHandler, group: 'Error Handling', Entity: CamelErrorHandlerVisualEntity },
{ type: EntityType.RestConfiguration, group: 'Rest', Entity: CamelRestConfigurationVisualEntity },
{ type: EntityType.Rest, group: 'Rest', Entity: CamelRestVisualEntity },
];
static readonly PARAMETERS_ORDER = ['id', 'description', 'uri', 'parameters', 'steps'];
private static readonly ERROR_RELATED_ENTITIES = [EntityType.OnException, EntityType.ErrorHandler];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ export class CamelRestConfigurationVisualEntity implements BaseVisualCamelEntity
return restConfigurationGroupNode;
}

toJSON(): unknown {
return this.restConfigurationDef;
toJSON(): { restConfiguration: RestConfiguration } {
return { restConfiguration: this.restConfigurationDef.restConfiguration };
}

private getValidatorFunction(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import catalogLibrary from '@kaoto/camel-catalog/index.json';
import { CatalogLibrary, Rest } from '@kaoto/camel-catalog/types';
import { restStub } from '../../../stubs/rest';
import { getFirstCatalogMap } from '../../../stubs/test-load-catalog';
import { EntityType } from '../../camel/entities';
import { CatalogKind } from '../../catalog-kind';
import { CamelCatalogService } from './camel-catalog.service';
import { CamelRestVisualEntity } from './camel-rest-visual-entity';
import { KaotoSchemaDefinition } from '../../kaoto-schema';
import { AbstractCamelVisualEntity } from './abstract-camel-visual-entity';

describe('CamelRestVisualEntity', () => {
const REST_ID_REGEXP = /^rest-[a-zA-Z0-9]{4}$/;
let restDef: { rest: Rest };
let restSchema: KaotoSchemaDefinition['schema'];

beforeAll(async () => {
const catalogsMap = await getFirstCatalogMap(catalogLibrary as CatalogLibrary);
CamelCatalogService.setCatalogKey(CatalogKind.Entity, catalogsMap.entitiesCatalog);
restSchema = catalogsMap.entitiesCatalog[EntityType.Rest].propertiesSchema as KaotoSchemaDefinition['schema'];
});

afterAll(() => {
CamelCatalogService.clearCatalogs();
});

beforeEach(() => {
restDef = {
rest: {
...restStub.rest,
},
};
});

describe('isApplicable', () => {
it.each([
[true, { rest: {} }],
[true, { rest: { bindingMode: 'off' } }],
[true, restStub],
[false, { from: { id: 'from-1234', steps: [] } }],
[false, { rest: { bindingMode: 'off' }, anotherProperty: true }],
])('should return %s for %s', (result, definition) => {
expect(CamelRestVisualEntity.isApplicable(definition)).toEqual(result);
});
});

describe('constructor', () => {
it('should set id to generated id', () => {
const entity = new CamelRestVisualEntity(restDef);

expect(entity.id).toMatch(REST_ID_REGEXP);
});
});

it('should return id', () => {
const entity = new CamelRestVisualEntity(restDef);

expect(entity.getId()).toMatch(REST_ID_REGEXP);
});

it('should set id', () => {
const entity = new CamelRestVisualEntity(restDef);
const newId = 'newId';
entity.setId(newId);

expect(entity.getId()).toEqual(newId);
});

it('should delegate to super return node label', () => {
const superGetNodeLabelSpy = jest
.spyOn(AbstractCamelVisualEntity.prototype, 'getNodeLabel')
.mockReturnValueOnce('label');
const entity = new CamelRestVisualEntity(restDef);

expect(entity.getNodeLabel()).toEqual('label');
expect(superGetNodeLabelSpy).toHaveBeenCalled();
});

it('should delegate to super return node tooltip', () => {
const superGetNodeLabelSpy = jest
.spyOn(AbstractCamelVisualEntity.prototype, 'getTooltipContent')
.mockReturnValueOnce('tooltip');
const entity = new CamelRestVisualEntity(restDef);

expect(entity.getTooltipContent()).toEqual('tooltip');
expect(superGetNodeLabelSpy).toHaveBeenCalled();
});

describe('getComponentSchema', () => {
it('should return entity current definition', () => {
const entity = new CamelRestVisualEntity(restDef);

expect(entity.getComponentSchema(CamelRestVisualEntity.ROOT_PATH)?.definition).toEqual(restDef.rest);
});

it('should return schema from store', () => {
const entity = new CamelRestVisualEntity(restDef);

expect(entity.getComponentSchema(CamelRestVisualEntity.ROOT_PATH)?.schema).toEqual(restSchema);
});
});

describe('updateModel', () => {
it('should update model', () => {
const entity = new CamelRestVisualEntity(restDef);
const path = 'rest.bindingMode';
const value = 'json';

entity.updateModel(path, value);

expect(restDef.rest.bindingMode).toEqual(value);
});

it('should not update model if path is not defined', () => {
const entity = new CamelRestVisualEntity(restDef);
const value = 'json_xml';

entity.updateModel(undefined, value);

expect(restDef.rest.bindingMode).toEqual('auto');
});

it('should reset the rest object if it is not defined', () => {
const entity = new CamelRestVisualEntity(restDef);

entity.updateModel('rest', {});

expect(restDef.rest).toEqual({});
});
});

it('return no interactions', () => {
const entity = new CamelRestVisualEntity(restDef);

expect(entity.getNodeInteraction()).toEqual({
canHavePreviousStep: false,
canHaveNextStep: false,
canHaveChildren: false,
canHaveSpecialChildren: false,
canRemoveStep: false,
canReplaceStep: false,
canRemoveFlow: true,
canBeDisabled: false,
});
});

describe('getNodeValidationText', () => {
it('should return undefined for valid definitions', () => {
const entity = new CamelRestVisualEntity({
rest: {
...restDef.rest,
bindingMode: 'json',
},
});

expect(entity.getNodeValidationText()).toBeUndefined();
});

it('should not modify the original definition when validating', () => {
const originalRestDef: Rest = { ...restDef.rest };
const entity = new CamelRestVisualEntity(restDef);

entity.getNodeValidationText();

expect(restDef.rest).toEqual(originalRestDef);
});

it('should NOT return errors when there is an invalid property', () => {
const invalidRestDef: Rest = {
...restDef.rest,
bindingMode: 'true' as unknown as Rest['bindingMode'],
openApi: 'true' as unknown as Rest['openApi'],
};
const entity = new CamelRestVisualEntity({ rest: invalidRestDef });

expect(entity.getNodeValidationText()).toBeUndefined();
});
});

describe('toVizNode', () => {
it('should return visualization node', () => {
const entity = new CamelRestVisualEntity(restDef);

const vizNode = entity.toVizNode();

expect(vizNode.data).toEqual({
componentName: undefined,
entity,
icon: '',
isGroup: true,
path: 'rest',
processorName: 'rest',
});
});
});

it('should serialize the rest definition', () => {
const entity = new CamelRestVisualEntity(restDef);

expect(entity.toJSON()).toEqual(restDef);
});
});
Loading

0 comments on commit d84cd72

Please sign in to comment.