Search in sources :

Example 26 with TemplateTo

use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.

the class GenerateMojo method collectTemplates.

/**
 * Generates all templates for each input.
 *
 * @param cobiGen generator instance to be used for generation
 * @param inputs to be used for generation
 * @return the collected templates to be generated
 * @throws MojoFailureException if any problem occurred while generation
 */
private List<GenerableArtifact> collectTemplates(CobiGen cobiGen, List<Object> inputs) throws MojoFailureException {
    List<GenerableArtifact> generableArtifacts = new ArrayList<>();
    if (this.templates != null && !this.templates.isEmpty()) {
        if (this.templates.contains(ALL)) {
            if (this.templates.size() > 1) {
                throw new MojoFailureException("You specified the 'ALL' template to generate all available templates next to another template, which was most probably not intended.");
            }
            for (Object input : inputs) {
                generableArtifacts.addAll(cobiGen.getMatchingTemplates(input));
            }
        } else {
            for (Object input : inputs) {
                List<TemplateTo> matchingTemplates = cobiGen.getMatchingTemplates(input);
                List<String> configuredTemplates = new LinkedList<>(this.templates);
                for (TemplateTo template : matchingTemplates) {
                    if (this.templates.contains(template.getId())) {
                        generableArtifacts.add(template);
                        configuredTemplates.remove(template.getId());
                    }
                }
                // error handling for increments not found
                if (!configuredTemplates.isEmpty()) {
                    throw new MojoFailureException("Templates with ids '" + configuredTemplates + "' did not match package in folder '" + getStringRepresentation(input) + "'.");
                }
            }
        }
    }
    return generableArtifacts;
}
Also used : GenerableArtifact(com.devonfw.cobigen.api.to.GenerableArtifact) ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) LinkedList(java.util.LinkedList)

Example 27 with TemplateTo

use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.

the class ModelCreationTest method testCorrectGenericTypeExtraction.

/**
 * Tests the correct reading and writing of parametric types as found in the input sources.
 *
 * @throws Exception test fails
 */
@Test
public void testCorrectGenericTypeExtraction() throws Exception {
    CobiGen cobiGen = CobiGenFactory.create(this.cobigenConfigFolder.toURI());
    File tmpFolderCobiGen = this.tmpFolder.newFolder("cobigen_output");
    Object input = cobiGen.read(new File("src/test/resources/testdata/integrationtest/javaSources/ModelCreationTest.java").toPath(), Charset.forName("UTF-8"), getClass().getClassLoader());
    List<TemplateTo> templates = cobiGen.getMatchingTemplates(input);
    boolean methodTemplateFound = false;
    for (TemplateTo template : templates) {
        if (template.getId().equals("genericTypes.txt")) {
            GenerationReportTo report = cobiGen.generate(input, template, Paths.get(tmpFolderCobiGen.getAbsolutePath()), false);
            assertThat(report).isSuccessful();
            File expectedFile = new File(tmpFolderCobiGen.getAbsoluteFile() + SystemUtils.FILE_SEPARATOR + "genericTypes.txt");
            assertThat(expectedFile).exists();
            assertThat(expectedFile).hasContent("List<String> testField");
            methodTemplateFound = true;
            break;
        }
    }
    if (!methodTemplateFound) {
        throw new AssertionFailedError("Test template not found");
    }
}
Also used : GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGen(com.devonfw.cobigen.api.CobiGen) AssertionFailedError(junit.framework.AssertionFailedError) File(java.io.File) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) Test(org.junit.Test) AbstractIntegrationTest(com.devonfw.cobigen.javaplugin.integrationtest.common.AbstractIntegrationTest)

Example 28 with TemplateTo

use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.

the class ModelCreationTest method testCorrectAnnotationValueExtraction.

/**
 * Tests that annotation string values for methods are not quoted. See issue #251
 *
 * @throws Exception test fails
 */
@Test
public void testCorrectAnnotationValueExtraction() throws Exception {
    CobiGen cobiGen = CobiGenFactory.create(this.cobigenConfigFolder.toURI());
    File tmpFolderCobiGen = this.tmpFolder.newFolder("cobigen_output");
    Object input = cobiGen.read(new File("src/test/resources/testdata/integrationtest/javaSources/ModelCreationTest.java").toPath(), StandardCharsets.UTF_8, getClass().getClassLoader());
    List<TemplateTo> templates = cobiGen.getMatchingTemplates(input);
    boolean methodTemplateFound = false;
    for (TemplateTo template : templates) {
        if (template.getId().equals("correctAnnotationValueExtraction.txt")) {
            GenerationReportTo report = cobiGen.generate(input, template, Paths.get(tmpFolderCobiGen.getAbsolutePath()), false);
            assertThat(report).isSuccessful();
            Path expectedFile = tmpFolderCobiGen.toPath().resolve("correctAnnotationValueExtraction.txt");
            assertThat(expectedFile).exists();
            assertThat(expectedFile).hasContent("\"/foo/{id}/\"");
            methodTemplateFound = true;
            break;
        }
    }
    if (!methodTemplateFound) {
        throw new AssertionFailedError("Test template not found");
    }
}
Also used : Path(java.nio.file.Path) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGen(com.devonfw.cobigen.api.CobiGen) AssertionFailedError(junit.framework.AssertionFailedError) File(java.io.File) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) Test(org.junit.Test) AbstractIntegrationTest(com.devonfw.cobigen.javaplugin.integrationtest.common.AbstractIntegrationTest)

Example 29 with TemplateTo

use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.

the class InputReaderMatcherTest method testBasicElementMatcher_twoComponents_matchRegex.

/**
 * Tests the correct basic retrieval of ComponentDef inputs
 *
 * @throws Exception test fails
 */
@Test
public void testBasicElementMatcher_twoComponents_matchRegex() throws Exception {
    CobiGen cobigen = CobiGenFactory.create(Paths.get(testdataRoot, "templates-regex").toUri());
    Object openApiFile = cobigen.read(Paths.get(testdataRoot, "two-components.yaml"), TestConstants.UTF_8);
    assertThat(openApiFile).isNotNull();
    List<TemplateTo> matchingTemplates = cobigen.getMatchingTemplates(openApiFile);
    assertThat(matchingTemplates).extracting(TemplateTo::getId).containsExactly("sales_template.txt", "table_template.txt");
}
Also used : CobiGen(com.devonfw.cobigen.api.CobiGen) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) Test(org.junit.Test)

Example 30 with TemplateTo

use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.

the class InputReaderMatcherTest method testVariableAssignment_rootComponent.

/**
 * Tests variable assignment resolution of ROOTPACKAGE type, thus that the user can define the root package in the
 * "info" part of the OpenAPI file
 *
 * @throws Exception test fails
 */
@Test
public void testVariableAssignment_rootComponent() throws Exception {
    CobiGen cobigen = CobiGenFactory.create(Paths.get(testdataRoot, "templates").toUri());
    Object openApiFile = cobigen.read(Paths.get(testdataRoot, "root-package.yaml"), TestConstants.UTF_8);
    // Previous version: List<Object> inputObjects = cobigen.getInputObjects(openApiFile,
    // TestConstants.UTF_8);
    List<Object> inputObjects = cobigen.resolveContainers(openApiFile);
    String templateName = "testModel_rootComponentProperty.txt";
    TemplateTo template = findTemplate(cobigen, inputObjects.get(0), templateName);
    File targetFolder = this.tmpFolder.newFolder();
    GenerationReportTo report = cobigen.generate(inputObjects.get(0), template, targetFolder.toPath());
    assertThat(report).isSuccessful();
    assertThat(targetFolder.toPath().resolve(templateName).toFile()).exists().hasContent("tablemanagement");
}
Also used : GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) Test(org.junit.Test)

Aggregations

TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)41 CobiGen (com.devonfw.cobigen.api.CobiGen)30 File (java.io.File)28 Test (org.junit.Test)27 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)25 AbstractApiTest (com.devonfw.cobigen.systemtest.common.AbstractApiTest)12 IncrementTo (com.devonfw.cobigen.api.to.IncrementTo)9 Path (java.nio.file.Path)9 List (java.util.List)9 AssertionFailedError (junit.framework.AssertionFailedError)9 Paths (java.nio.file.Paths)6 Set (java.util.Set)6 HashSet (java.util.HashSet)5 Collectors (java.util.stream.Collectors)5 CobiGenAsserts.assertThat (com.devonfw.cobigen.api.assertj.CobiGenAsserts.assertThat)4 GenerationReportToAssert (com.devonfw.cobigen.api.assertj.GenerationReportToAssert)4 ComparableIncrement (com.devonfw.cobigen.eclipse.generator.entity.ComparableIncrement)4 CobiGenFactory (com.devonfw.cobigen.impl.CobiGenFactory)4 AbstractIntegrationTest (com.devonfw.cobigen.javaplugin.integrationtest.common.AbstractIntegrationTest)4 StandardCharsets (java.nio.charset.StandardCharsets)4