Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Catalog): Add group/label information #1963

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
import org.apache.camel.catalog.CamelCatalog;
import org.apache.camel.tooling.model.EipModel;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.*;

public class CamelCatalogSchemaEnhancer {

private final CamelCatalog camelCatalog;
private final Map<String, String> JAVA_TYPE_TO_MODEL_NAME = new LinkedHashMap<>();
private final Map<String, String> JAVA_TYPE_TO_MODEL_NAME = new HashMap<>();

public CamelCatalogSchemaEnhancer(CamelCatalog camelCatalog) {
this.camelCatalog = camelCatalog;
Expand Down Expand Up @@ -101,6 +99,54 @@ void sortPropertiesAccordingToCatalog(EipModel model, ObjectNode modelNode) {
// TODO: Sort properties according to the Camel catalog
}

/**
* Fill the group/label information of the model in the schema
*
* @param modelName the name of the Camel model
* @param modelNode the JSON schema node of the model
*/
void fillGroupInformation(String modelName, ObjectNode modelNode) {
EipModel model = camelCatalog.eipModel(modelName);
if (model == null) {
return;
}

fillGroupInformation(model, modelNode);
}

/**
* Fill the group/label information of the model in the schema
*
* @param model the Camel model
* @param modelNode the JSON schema node of the model
*/
void fillGroupInformation(EipModel model, ObjectNode modelNode) {
List<EipModel.EipOptionModel> modelOptions = model.getOptions();

modelNode.withObject("properties").fields().forEachRemaining(entry -> {
String propertyName = entry.getKey();

Optional<EipModel.EipOptionModel> modelOption =
modelOptions.stream().filter(option -> option.getName().equals(propertyName)).findFirst();
if (modelOption.isEmpty()) {
return;
}

String group =
modelOption.get().getGroup() != null ? modelOption.get().getGroup() : modelOption.get().getLabel();
if (group == null) {
return;
}

ObjectNode propertyNode = (ObjectNode) entry.getValue();
if (propertyNode.has("$comment")) {
propertyNode.put("$comment", propertyNode.get("$comment").asText() + "|group:" + group);
} else {
propertyNode.put("$comment", "group:" + group);
}
});
}

/**
* Get the Camel model by its Java type
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ public Map<String, ObjectNode> generate() {

camelCatalogSchemaEnhancer.fillRequiredPropertiesIfNeeded(eipName, eipJSONSchema);
camelCatalogSchemaEnhancer.sortPropertiesAccordingToCatalog(eipName, eipJSONSchema);
camelCatalogSchemaEnhancer.fillGroupInformation(eipName, eipJSONSchema);
iterateOverDefinitions(eipJSONSchema.withObject("definitions"), (model, node) -> {
if (model == null) {
return;
}

camelCatalogSchemaEnhancer.fillRequiredPropertiesIfNeeded(model, node);
camelCatalogSchemaEnhancer.sortPropertiesAccordingToCatalog(model, node);
camelCatalogSchemaEnhancer.fillGroupInformation(model, node);
});

eipMap.put(eipName, eipJSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,43 @@ void shouldFillRequiredPropertiesIfNeededForModel() {
assertTrue(requiredProperties.contains("expression"));
}

@Test
void shouldFillGroupInformationForModelName() {
var choiceNode = camelYamlDslSchema
.withObject("items")
.withObject("definitions")
.withObject("org.apache.camel.model.ChoiceDefinition");

EipModel model = camelCatalog.eipModel("choice");
camelCatalogSchemaEnhancer.fillGroupInformation(model, choiceNode);

var idPropertyNode = choiceNode.withObject("properties").withObject("id");
var preconditionPropertyNode = choiceNode.withObject("properties").withObject("precondition");

assertTrue(idPropertyNode.has("$comment"));
assertTrue(preconditionPropertyNode.has("$comment"));
assertEquals("group:common", idPropertyNode.get("$comment").asText());
assertEquals("group:advanced", preconditionPropertyNode.get("$comment").asText());
}

@Test
void shouldFillGroupInformationForMode() {
var choiceNode = camelYamlDslSchema
.withObject("items")
.withObject("definitions")
.withObject("org.apache.camel.model.ChoiceDefinition");

camelCatalogSchemaEnhancer.fillGroupInformation("choice", choiceNode);

var idPropertyNode = choiceNode.withObject("properties").withObject("id");
var preconditionPropertyNode = choiceNode.withObject("properties").withObject("precondition");

assertTrue(idPropertyNode.has("$comment"));
assertTrue(preconditionPropertyNode.has("$comment"));
assertEquals("group:common", idPropertyNode.get("$comment").asText());
assertEquals("group:advanced", preconditionPropertyNode.get("$comment").asText());
}

@Test
void shouldGetCamelModelByJavaType() {
EipModel setHeaderModel =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class EIPGeneratorTest {
Expand Down Expand Up @@ -94,8 +95,7 @@ void shouldFillRequiredPropertiesFromSchemaIfNeeded() {
void shouldFillRequiredPropertiesFromDefinitionsIfNeeded() {
var eipsMap = eipGenerator.generate();

ObjectNode definitions =
eipsMap.get("setHeader").withObject("propertiesSchema").withObject("definitions");
ObjectNode definitions = eipsMap.get("setHeader").withObject("propertiesSchema").withObject("definitions");

assertTrue(definitions.has("org.apache.camel.model.language.ConstantExpression"));
assertTrue(definitions.withObject("org.apache.camel.model.language.ConstantExpression").has("required"));
Expand All @@ -111,4 +111,32 @@ void shouldFillRequiredPropertiesFromDefinitionsIfNeeded() {
.forEachRemaining(item -> simpleExpressionRequired.add(item.asText()));
assertTrue(simpleExpressionRequired.contains("expression"));
}

@Test
void shouldFillGroupInformation() {
var eipsMap = eipGenerator.generate();

var setHeaderNode = eipsMap.get("setHeader");
var namePropertyNode = setHeaderNode.withObject("propertiesSchema").withObject("properties").withObject("name");

assertTrue(namePropertyNode.has("$comment"));
assertEquals("group:common", namePropertyNode.get("$comment").asText());
}

@Test
void shouldFillGroupInformationFromDefinitions() {
var eipsMap = eipGenerator.generate();

var setHeaderNode = eipsMap.get("setHeader");
var expressionPropertiesNode = setHeaderNode.withObject("propertiesSchema").withObject("definitions")
.withObject("org.apache.camel.model.language.SimpleExpression").withObject("properties");

var expressionPropertyNode = expressionPropertiesNode.withObject("expression");
var trimPropertyNode = expressionPropertiesNode.withObject("trim");

assertTrue(expressionPropertyNode.has("$comment"));
assertTrue(trimPropertyNode.has("$comment"));
assertEquals("group:common", expressionPropertyNode.get("$comment").asText());
assertEquals("group:advanced", trimPropertyNode.get("$comment").asText());
}
}
Loading