Search in sources :

Example 11 with GenerationReportTo

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

the class TransactionalGenerationTest method testNoPartialApplicationOfGeneration.

/**
 * Tests, whether no partial generation will be applied to the target if generation fails in between.
 *
 * @throws Throwable test fails
 */
@Test
public void testNoPartialApplicationOfGeneration() throws Throwable {
    // arrange
    Object generationInput = PluginMockFactory.createSimpleJavaConfigurationMock();
    File targetRoot = this.tmpFolder.newFolder();
    CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "templates").toURI());
    List<IncrementTo> matchingIncrements = cobigen.getMatchingIncrements(generationInput);
    // act
    GenerationReportTo report = cobigen.generate(generationInput, matchingIncrements, targetRoot.toPath());
    // assert
    assertThat(report.isSuccessful()).isFalse();
    assertThat(new File(targetRoot, "valid.txt")).doesNotExist();
    assertThat(new File(targetRoot, "invalid.txt")).doesNotExist();
}
Also used : IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest) Test(org.junit.Test)

Example 12 with GenerationReportTo

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

the class ContainerMatcherTest method testContextVariableResolvingOnGeneration.

/**
 * Tests whether variable resolving works for a contains's children during generation
 *
 * @throws Exception test fails
 */
@Test
public void testContextVariableResolvingOnGeneration() throws Exception {
    // Mocking
    Object containerInput = createTestDataAndConfigureMock(true);
    File generationRootFolder = this.tmpFolder.newFolder("generationRootFolder");
    // pre-processing
    File templatesFolder = new File(testFileRootPath + "templates");
    CobiGen target = CobiGenFactory.create(templatesFolder.toURI());
    List<TemplateTo> templates = target.getMatchingTemplates(containerInput);
    // Execution
    GenerationReportTo report = target.generate(containerInput, templates.get(0), Paths.get(generationRootFolder.toURI()), false);
    // assertion
    assertThat(report).isSuccessful();
}
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) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest) Test(org.junit.Test)

Example 13 with GenerationReportTo

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

the class ContainerMatcherTest method testContainerChildrenWillIndividuallyBeMatched.

/**
 * Create a new {@link ContainerMatcher}, which contains two children which do not match the same trigger.
 *
 * @throws Exception test fails
 */
@Test
@SuppressWarnings("unchecked")
public void testContainerChildrenWillIndividuallyBeMatched() throws Exception {
    Object container = new Object() {

        @Override
        public String toString() {
            return "container";
        }
    };
    Object child1 = new Object() {

        @Override
        public String toString() {
            return "child1";
        }
    };
    Object child2 = new Object() {

        @Override
        public String toString() {
            return "child2";
        }
    };
    // Pre-processing: Mocking
    GeneratorPluginActivator activator = mock(GeneratorPluginActivator.class);
    TriggerInterpreter triggerInterpreter = mock(TriggerInterpreter.class);
    MatcherInterpreter matcher = mock(MatcherInterpreter.class);
    InputReader inputReader = mock(InputReader.class);
    when(triggerInterpreter.getType()).thenReturn("test");
    when(triggerInterpreter.getMatcher()).thenReturn(matcher);
    when(triggerInterpreter.getInputReader()).thenReturn(inputReader);
    when(inputReader.isValidInput(any())).thenReturn(true);
    // Simulate container children resolution of any plug-in
    when(matcher.resolveVariables(argThat(new MatcherToMatcher(equalTo("or"), ANY, sameInstance(child1))), anyList(), any())).thenReturn(ImmutableMap.<String, String>builder().put("variable", "child1").build());
    when(matcher.resolveVariables(argThat(new MatcherToMatcher(equalTo("or"), ANY, sameInstance(child2))), anyList(), any())).thenReturn(ImmutableMap.<String, String>builder().put("variable", "child2").build());
    when(inputReader.getInputObjects(any(), any(Charset.class))).thenReturn(Lists.newArrayList(child1, child2));
    // match container
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("container"), ANY, sameInstance(container))))).thenReturn(true);
    // do not match first child
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("or"), ANY, sameInstance(child1))))).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("not"), ANY, sameInstance(child1))))).thenReturn(true);
    // match second child
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("or"), ANY, sameInstance(child2))))).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("not"), ANY, sameInstance(child2))))).thenReturn(false);
    PluginRegistry.registerTriggerInterpreter(triggerInterpreter, activator);
    // create CobiGen instance
    File templatesFolder = new File(testFileRootPath + "selectiveContainerGeneration");
    CobiGen target = CobiGenFactory.create(templatesFolder.toURI());
    File folder = this.tmpFolder.newFolder();
    // Execution
    GenerationReportTo report = target.generate(container, target.getMatchingTemplates(container), Paths.get(folder.toURI()), false);
    assertThat(report).isSuccessful();
    // Verification
    assertNotNull(folder.list());
    assertEquals(1, folder.list().length);
    assertEquals("child2.txt", folder.list()[0]);
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) MatcherToMatcher(com.devonfw.cobigen.api.matchers.MatcherToMatcher) InputReader(com.devonfw.cobigen.api.extension.InputReader) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) MatcherInterpreter(com.devonfw.cobigen.api.extension.MatcherInterpreter) Charset(java.nio.charset.Charset) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) GeneratorPluginActivator(com.devonfw.cobigen.api.extension.GeneratorPluginActivator) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest) Test(org.junit.Test)

Example 14 with GenerationReportTo

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

use of com.devonfw.cobigen.api.to.GenerationReportTo 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)

Aggregations

GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)33 CobiGen (com.devonfw.cobigen.api.CobiGen)27 Test (org.junit.Test)26 File (java.io.File)25 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)24 AbstractApiTest (com.devonfw.cobigen.systemtest.common.AbstractApiTest)9 Path (java.nio.file.Path)9 AssertionFailedError (junit.framework.AssertionFailedError)8 List (java.util.List)7 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)5 MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)5 Paths (java.nio.file.Paths)5 GenerationReportToAssert (com.devonfw.cobigen.api.assertj.GenerationReportToAssert)4 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)4 IncrementTo (com.devonfw.cobigen.api.to.IncrementTo)4 AbstractIntegrationTest (com.devonfw.cobigen.javaplugin.integrationtest.common.AbstractIntegrationTest)4 Charset (java.nio.charset.Charset)4 CobiGenAsserts.assertThat (com.devonfw.cobigen.api.assertj.CobiGenAsserts.assertThat)3 PluginNotAvailableException (com.devonfw.cobigen.api.exception.PluginNotAvailableException)3 GeneratorPluginActivator (com.devonfw.cobigen.api.extension.GeneratorPluginActivator)3