Search in sources :

Example 1 with ComponentDef

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;
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) HeaderDef(com.devonfw.cobigen.openapiplugin.model.HeaderDef) LinkedList(java.util.LinkedList) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException)

Example 2 with ComponentDef

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();
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) OpenAPIMatcher(com.devonfw.cobigen.openapiplugin.matcher.OpenAPIMatcher) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) Test(org.junit.Test)

Example 3 with ComponentDef

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"));
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) VariableAssignmentTo(com.devonfw.cobigen.api.to.VariableAssignmentTo) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) OpenAPIMatcher(com.devonfw.cobigen.openapiplugin.matcher.OpenAPIMatcher) ArrayList(java.util.ArrayList) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) Test(org.junit.Test)

Example 4 with ComponentDef

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;
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) ParameterDef(com.devonfw.cobigen.openapiplugin.model.ParameterDef) OperationDef(com.devonfw.cobigen.openapiplugin.model.OperationDef) PathDef(com.devonfw.cobigen.openapiplugin.model.PathDef) LinkedList(java.util.LinkedList)

Example 5 with ComponentDef

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();
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) OpenAPIMatcher(com.devonfw.cobigen.openapiplugin.matcher.OpenAPIMatcher) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) Test(org.junit.Test)

Aggregations

ComponentDef (com.devonfw.cobigen.openapiplugin.model.ComponentDef)11 Test (org.junit.Test)8 MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)5 OpenAPIMatcher (com.devonfw.cobigen.openapiplugin.matcher.OpenAPIMatcher)5 PathDef (com.devonfw.cobigen.openapiplugin.model.PathDef)4 LinkedList (java.util.LinkedList)4 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)3 VariableAssignmentTo (com.devonfw.cobigen.api.to.VariableAssignmentTo)3 OperationDef (com.devonfw.cobigen.openapiplugin.model.OperationDef)3 ArrayList (java.util.ArrayList)3 EntityDef (com.devonfw.cobigen.openapiplugin.model.EntityDef)2 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)1 HeaderDef (com.devonfw.cobigen.openapiplugin.model.HeaderDef)1 ParameterDef (com.devonfw.cobigen.openapiplugin.model.ParameterDef)1 ResponseDef (com.devonfw.cobigen.openapiplugin.model.ResponseDef)1 Path (com.reprezen.kaizen.oasparser.model3.Path)1