use of com.devonfw.cobigen.openapiplugin.model.ComponentDef 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.ComponentDef in project cobigen by devonfw.
the class OpenAPIMatcherTest method testInvalidComponentDefMatching.
/**
* Test non valid {@link ComponentDef} matching
*/
@Test
public void testInvalidComponentDefMatching() {
ComponentDef componentDef = new ComponentDef();
componentDef.setName("Tablemanagement");
OpenAPIMatcher matcher = new OpenAPIMatcher();
boolean matches = matcher.matches(new MatcherTo("element", "ComponentDefs", componentDef));
assertThat(matches).isFalse();
}
use of com.devonfw.cobigen.openapiplugin.model.ComponentDef in project cobigen by devonfw.
the class OpenAPIMatcherTest method testMissingXRootPackageVariableNotMandatory.
/**
* Test if the generation is successful and the report contains warnings, if a requested but not mandatory variable
* isn't given.
*/
@Test
public void testMissingXRootPackageVariableNotMandatory() {
ComponentDef componentDef = new ComponentDef();
componentDef.setName("Tablemanagement");
OpenAPIMatcher matcher = new OpenAPIMatcher();
GenerationReportTo report = new GenerationReportTo();
List<VariableAssignmentTo> vaOptionalXRootPackage = new ArrayList<>();
vaOptionalXRootPackage.add(new VariableAssignmentTo("extension", "rootPackage", "x-rootpackage", false));
matcher.resolveVariables(new MatcherTo("element", "ComponentDef", componentDef), vaOptionalXRootPackage, report);
assertThat(report.getWarnings().get(0)).containsSequence(Constants.getMandatoryMessage(false, "x-rootpackage"));
List<VariableAssignmentTo> vaMandatoryXRootPackage = new ArrayList<>();
vaMandatoryXRootPackage.add(new VariableAssignmentTo("extension", "rootpackage", "x-rootpackage", true));
matcher.resolveVariables(new MatcherTo("element", "ComponentDef", componentDef), vaMandatoryXRootPackage, report);
assertThat(report.getErrors().get(0).getMessage()).containsSequence(Constants.getMandatoryMessage(true, "x-rootpackage"));
}
use of com.devonfw.cobigen.openapiplugin.model.ComponentDef in project cobigen by devonfw.
the class OpenAPIInputReaderTest method getParametersOfOperations.
private List<ParameterDef> getParametersOfOperations(String testInputFilename) throws Exception {
List<Object> inputObjects = getInputs(testInputFilename);
List<ComponentDef> cmps = new LinkedList<>();
for (Object o : inputObjects) {
if (isComponentDef(o)) {
cmps.add(((ComponentDef) o));
} else {
cmps.add(((EntityDef) o).getComponent());
}
}
List<ParameterDef> parameters = new LinkedList<>();
for (ComponentDef cmp : cmps) {
for (PathDef path : cmp.getPaths()) {
for (OperationDef op : path.getOperations()) {
for (ParameterDef param : op.getParameters()) {
parameters.add(param);
}
}
}
}
return parameters;
}
use of com.devonfw.cobigen.openapiplugin.model.ComponentDef in project cobigen by devonfw.
the class OpenAPIMatcherTest method testValidComponentDefMatching.
/**
* Test valid {@link ComponentDef} matching
*/
@Test
public void testValidComponentDefMatching() {
ComponentDef componentDef = new ComponentDef();
componentDef.setName("Tablemanagement");
OpenAPIMatcher matcher = new OpenAPIMatcher();
boolean matches = matcher.matches(new MatcherTo("element", "ComponentDef", componentDef));
assertThat(matches).isTrue();
}
Aggregations