Search in sources :

Example 16 with TemplateTo

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

the class TemplateScanTest method testCorrectDestinationResoution_emptyPathElements.

/**
 * Tests the correct destination resolution for resources obtained by template-scans in the case of multiple empty
 * path elements
 *
 * @throws Exception test fails
 */
@Test
public void testCorrectDestinationResoution_emptyPathElements() throws Exception {
    Object input = PluginMockFactory.createSimpleJavaConfigurationMock();
    File generationRootFolder = this.tmpFolder.newFolder("generationRootFolder");
    // Useful to see generates if necessary, comment the generationRootFolder above then
    // File generationRootFolder = new File(testFileRootPath + "generates");
    // pre-processing
    File templatesFolder = new File(testFileRootPath);
    CobiGen target = CobiGenFactory.create(templatesFolder.toURI());
    List<TemplateTo> templates = target.getMatchingTemplates(input);
    assertThat(templates).isNotNull();
    TemplateTo targetTemplate = getTemplateById(templates, "prefix_MultiEmpty.java");
    assertThat(targetTemplate).isNotNull();
    // Execution
    GenerationReportTo report = target.generate(input, targetTemplate, Paths.get(generationRootFolder.toURI()), false);
    assertThat(report).isSuccessful();
    // Validation
    assertThat(new File(generationRootFolder.getAbsolutePath() + SystemUtils.FILE_SEPARATOR + "src" + SystemUtils.FILE_SEPARATOR + "main" + SystemUtils.FILE_SEPARATOR + "java" + SystemUtils.FILE_SEPARATOR + "base" + SystemUtils.FILE_SEPARATOR + "MultiEmpty.java")).exists();
}
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) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest)

Example 17 with TemplateTo

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

the class TemplateScanTest method testScanTemplatesFromArchivFile.

/**
 * Test template scan within an archive file.
 *
 * @throws Exception test fails
 */
@Test
public void testScanTemplatesFromArchivFile() throws Exception {
    // pre-processing: mocking
    Object input = PluginMockFactory.createSimpleJavaConfigurationMock();
    // test processing
    CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "valid.zip").toURI());
    List<TemplateTo> templates = cobigen.getMatchingTemplates(input);
    // checking
    assertThat(templates, notNullValue());
    assertThat(templates.size(), equalTo(6));
}
Also used : CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) Test(org.junit.Test) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest)

Example 18 with TemplateTo

use of com.devonfw.cobigen.api.to.TemplateTo 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 19 with TemplateTo

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

the class InputReaderMatcherTest method testVariableAssignment_attribute.

/**
 * Tests variable assignment resolution of ATTRIBUTE type, thus that the user can define any custom variables inside
 * the schema of OpenAPI files. <br>
 * <br>
 * The input test file contains one attribute per entity. We are testing here that both attributes are correctly
 * generated
 *
 * @throws Exception test fails
 */
@Test
public void testVariableAssignment_attribute() throws Exception {
    CobiGen cobigen = CobiGenFactory.create(Paths.get(testdataRoot, "templates").toUri());
    Object openApiFile = cobigen.read(Paths.get(testdataRoot, "two-components.yaml"), TestConstants.UTF_8);
    // Previous version: List<Object> inputObjects = cobigen.getInputObjects(openApiFile,
    // TestConstants.UTF_8);
    List<Object> inputObjects = cobigen.resolveContainers(openApiFile);
    String templateName = "testVariableAssignment_attribute.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("testVariableAssignment_attribute.txt").toFile()).exists().hasContent("testingAttributeTableiChangeGlobalVariable");
    template = findTemplate(cobigen, inputObjects.get(1), templateName);
    targetFolder = this.tmpFolder.newFolder();
    report = cobigen.generate(inputObjects.get(1), template, targetFolder.toPath());
    assertThat(report).isSuccessful();
    assertThat(targetFolder.toPath().resolve("testVariableAssignment_attribute.txt").toFile()).exists().hasContent("testingAttributeSalesitIsGlobal");
}
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)

Example 20 with TemplateTo

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

the class InputReaderMatcherTest method testVariableAssignment_rootPackage.

/**
 * 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_rootPackage() 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 = "testVariableAssignment_rootPackage.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("testVariableAssignment_rootPackage.txt").toFile()).exists().hasContent("testingRootName");
}
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