Search in sources :

Example 1 with EntityDef

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;
}
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 EntityDef

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");
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef) AssertionFailedError(junit.framework.AssertionFailedError) CobiGen(com.devonfw.cobigen.api.CobiGen) TestConstants(com.devonfw.cobigen.openapiplugin.util.TestConstants) Collectors(java.util.stream.Collectors) File(java.io.File) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) List(java.util.List) CobiGenFactory(com.devonfw.cobigen.impl.CobiGenFactory) CobiGenAsserts.assertThat(com.devonfw.cobigen.api.assertj.CobiGenAsserts.assertThat) Rule(org.junit.Rule) Paths(java.nio.file.Paths) TemporaryFolder(org.junit.rules.TemporaryFolder) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) CobiGen(com.devonfw.cobigen.api.CobiGen) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef) Test(org.junit.Test)

Example 3 with EntityDef

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();
}
Also used : PropertyDef(com.devonfw.cobigen.openapiplugin.model.PropertyDef) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef) Test(org.junit.Test)

Example 4 with EntityDef

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();
}
Also used : PropertyDef(com.devonfw.cobigen.openapiplugin.model.PropertyDef) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef) Test(org.junit.Test)

Example 5 with EntityDef

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

Aggregations

EntityDef (com.devonfw.cobigen.openapiplugin.model.EntityDef)12 Test (org.junit.Test)9 PropertyDef (com.devonfw.cobigen.openapiplugin.model.PropertyDef)4 ResponseDef (com.devonfw.cobigen.openapiplugin.model.ResponseDef)3 LinkedList (java.util.LinkedList)3 MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)2 OpenAPIMatcher (com.devonfw.cobigen.openapiplugin.matcher.OpenAPIMatcher)2 ComponentDef (com.devonfw.cobigen.openapiplugin.model.ComponentDef)2 HeaderDef (com.devonfw.cobigen.openapiplugin.model.HeaderDef)2 OperationDef (com.devonfw.cobigen.openapiplugin.model.OperationDef)2 PathDef (com.devonfw.cobigen.openapiplugin.model.PathDef)2 CobiGen (com.devonfw.cobigen.api.CobiGen)1 CobiGenAsserts.assertThat (com.devonfw.cobigen.api.assertj.CobiGenAsserts.assertThat)1 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)1 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)1 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)1 CobiGenFactory (com.devonfw.cobigen.impl.CobiGenFactory)1 InfoDef (com.devonfw.cobigen.openapiplugin.model.InfoDef)1 OpenAPIFile (com.devonfw.cobigen.openapiplugin.model.OpenAPIFile)1 ServerDef (com.devonfw.cobigen.openapiplugin.model.ServerDef)1