use of com.devonfw.cobigen.openapiplugin.model.EntityDef in project cobigen by devonfw.
the class OpenAPIInputReader method extractComponents.
/**
* Get a list of entities defined at an OpenaApi3 file returning a list of {@link EntityDef}'s
*
* @param openApi the model for an OpenApi3 file
* @return list of entities
*/
private List<EntityDef> extractComponents(OpenApi3 openApi) {
HeaderDef header = new HeaderDef();
header.setServers(extractServers(openApi));
header.setInfo(extractInfo(openApi));
List<EntityDef> objects = new LinkedList<>();
this.components = new LinkedList<>();
for (String key : openApi.getSchemas().keySet()) {
EntityDef entityDef = new EntityDef();
entityDef.setName(key);
entityDef.setDescription(openApi.getSchema(key).getDescription());
ComponentDef componentDef = new ComponentDef();
entityDef.setProperties(extractProperties(openApi, key));
// If no x-component tag was found on the input file, throw invalid configuration
if (openApi.getSchema(key).getExtensions().get(Constants.COMPONENT_EXT) == null) {
throw new InvalidConfigurationException("Your Swagger file is not correctly formatted, it lacks of x-component tags.\n\n" + "Go to the documentation " + "(https://github.com/devonfw/cobigen/wiki/cobigen-openapiplugin#full-example) " + "to check how to correctly format it." + " If it is still not working, check your file indentation!");
}
String componentName = openApi.getSchema(key).getExtensions().get(Constants.COMPONENT_EXT).toString();
entityDef.setComponentName(componentName);
// If the path's tag was not found on the input file, throw invalid configuration
if (openApi.getPaths().size() == 0) {
throw new InvalidConfigurationException("Your Swagger file is not correctly formatted, it lacks of the correct path syntax.\n\n" + "Go to the documentation (https://github.com/devonfw/cobigen" + "/wiki/cobigen-openapiplugin#paths) to check how to correctly format it." + " If it is still not working, check your file indentation!");
}
// Sets a Map containing all the extensions of the info part of the OpenAPI file
if (Overlay.isPresent((JsonOverlay<?>) openApi.getInfo())) {
entityDef.setUserPropertiesMap(openApi.getInfo().getExtensions());
}
// Traverse the extensions of the entity for setting those attributes to the Map
Iterator<String> it = openApi.getSchema(key).getExtensions().keySet().iterator();
while (it.hasNext()) {
String keyMap = it.next();
entityDef.setUserProperty(keyMap, openApi.getSchema(key).getExtensions().get(keyMap).toString());
}
componentDef.setPaths(extractPaths(openApi.getPaths(), componentName));
componentDef.setName(componentName);
this.components.add(componentDef);
entityDef.setComponent(componentDef);
entityDef.setHeader(header);
objects.add(entityDef);
}
return objects;
}
use of com.devonfw.cobigen.openapiplugin.model.EntityDef in project cobigen by devonfw.
the class InputReaderMatcherTest method testBasicElementMatcher_oneComponent_matchRegex.
/**
* Tests the correct basic retrieval of ComponentDef inputs
*
* @throws Exception test fails
*/
@Test
public void testBasicElementMatcher_oneComponent_matchRegex() throws Exception {
CobiGen cobigen = CobiGenFactory.create(Paths.get(testdataRoot, "templates-regex").toUri());
Object openApiFile = cobigen.read(Paths.get(testdataRoot, "one-component.yaml"), TestConstants.UTF_8);
assertThat(openApiFile).isNotNull();
List<Object> resolveContainers = cobigen.resolveContainers(openApiFile);
assertThat(resolveContainers).hasSize(1).first().isInstanceOf(EntityDef.class).extracting(e -> ((EntityDef) e).getName()).containsExactly("Table");
List<TemplateTo> matchingTemplates = resolveContainers.stream().flatMap(e -> cobigen.getMatchingTemplates(e).stream()).collect(Collectors.toList());
assertThat(matchingTemplates).extracting(TemplateTo::getId).containsExactly("table_template.txt");
}
use of com.devonfw.cobigen.openapiplugin.model.EntityDef in project cobigen by devonfw.
the class OpenAPIInputReaderTest method testPropertyRefManyToOne.
@Test
public void testPropertyRefManyToOne() throws Exception {
List<Object> inputObjects = getInputs("property-ref-many-to-one.yaml");
boolean found = false;
for (Object o : inputObjects) {
if (isEntityDef(o)) {
EntityDef entityDef = (EntityDef) o;
if (entityDef.getName().equals("SampleData")) {
assertThat(entityDef.getProperties()).hasSize(1);
PropertyDef prop = entityDef.getProperties().get(0);
assertThat(prop.getType()).isEqualTo("MoreData");
assertThat(prop.getName()).isEqualTo("mainData");
assertThat(prop.getSameComponent()).isTrue();
assertThat(prop.getDescription()).isEqualTo("a single ref to MoreData (many-to-one)");
found = true;
}
}
}
assertThat(found).as("SampleData component schema not found!").isTrue();
}
use of com.devonfw.cobigen.openapiplugin.model.EntityDef in project cobigen by devonfw.
the class OpenAPIInputReaderTest method testPropertyRefOneToOne.
@Test
public void testPropertyRefOneToOne() throws Exception {
List<Object> inputObjects = getInputs("property-ref-one-to-one.yaml");
boolean found = false;
for (Object o : inputObjects) {
if (isEntityDef(o)) {
EntityDef entityDef = (EntityDef) o;
if (entityDef.getName().equals("SampleData")) {
assertThat(entityDef.getProperties()).hasSize(1);
PropertyDef prop = entityDef.getProperties().get(0);
assertThat(prop.getType()).isEqualTo("MoreData");
assertThat(prop.getName()).isEqualTo("mainData");
assertThat(prop.getSameComponent()).isTrue();
// The description of the property will be ignored in compliance with the JSON
// specification:
// https://github.com/RepreZen/KaiZen-OpenApi-Parser/issues/148
assertThat(prop.getDescription()).isEqualTo("MoreData Desc");
found = true;
}
}
}
assertThat(found).as("SampleData component schema not found!").isTrue();
}
use of com.devonfw.cobigen.openapiplugin.model.EntityDef in project cobigen by devonfw.
the class OpenAPIMatcherTest method testValidEntityDefMatching.
/**
* Test valid {@link EntityDef} matching
*/
@Test
public void testValidEntityDefMatching() {
EntityDef entityDef = new EntityDef();
entityDef.setComponentName("Tablemanagement");
OpenAPIMatcher matcher = new OpenAPIMatcher();
boolean matches = matcher.matches(new MatcherTo("element", "EntityDef", entityDef));
assertThat(matches).isTrue();
}
Aggregations