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();
}
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();
}
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]);
}
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();
}
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");
}
Aggregations