use of com.devonfw.cobigen.api.to.GenerationReportTo 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");
}
use of com.devonfw.cobigen.api.to.GenerationReportTo in project cobigen by devonfw.
the class OpenAPIMatcherTest method testMissingXRootPackageVariableNotMandatory.
/**
* Test if the generation is successful and the report contains warnings, if a requested but not mandatory variable
* isn't given.
*/
@Test
public void testMissingXRootPackageVariableNotMandatory() {
ComponentDef componentDef = new ComponentDef();
componentDef.setName("Tablemanagement");
OpenAPIMatcher matcher = new OpenAPIMatcher();
GenerationReportTo report = new GenerationReportTo();
List<VariableAssignmentTo> vaOptionalXRootPackage = new ArrayList<>();
vaOptionalXRootPackage.add(new VariableAssignmentTo("extension", "rootPackage", "x-rootpackage", false));
matcher.resolveVariables(new MatcherTo("element", "ComponentDef", componentDef), vaOptionalXRootPackage, report);
assertThat(report.getWarnings().get(0)).containsSequence(Constants.getMandatoryMessage(false, "x-rootpackage"));
List<VariableAssignmentTo> vaMandatoryXRootPackage = new ArrayList<>();
vaMandatoryXRootPackage.add(new VariableAssignmentTo("extension", "rootpackage", "x-rootpackage", true));
matcher.resolveVariables(new MatcherTo("element", "ComponentDef", componentDef), vaMandatoryXRootPackage, report);
assertThat(report.getErrors().get(0).getMessage()).containsSequence(Constants.getMandatoryMessage(true, "x-rootpackage"));
}
use of com.devonfw.cobigen.api.to.GenerationReportTo in project cobigen by devonfw.
the class InputReaderMatcherTest method testVariableAssignment_propertyName.
/**
* Tests variable assignment resolution of PROPERTY type at the example of the component version
*
* @throws Exception test fails
*/
@Test
public void testVariableAssignment_propertyName() throws Exception {
CobiGen cobigen = CobiGenFactory.create(Paths.get(testdataRoot, "templates").toUri());
Object openApiFile = cobigen.read(Paths.get(testdataRoot, "one-component.yaml"), TestConstants.UTF_8);
List<Object> inputObjects = cobigen.resolveContainers(openApiFile);
String templateName = "testVariableAssignment_propertyName.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_propertyName.txt").toFile()).exists().hasContent("Table");
}
use of com.devonfw.cobigen.api.to.GenerationReportTo in project cobigen by devonfw.
the class InputReaderMatcherTest method testVariableAssignment_noAttributeFound.
/**
* Tests the case when <b>no</b> ATTRIBUTE was found on the OpenAPI input file for one entity. Therefore an empty
* string should be assigned.<br>
* <br>
* The input test file contains two entities, one has an attribute and the other one does not. We are testing here
* that the first entity gets his attribute and the second entity gets an empty string
*
* @throws Exception test fails
*/
@Test
public void testVariableAssignment_noAttributeFound() throws Exception {
CobiGen cobigen = CobiGenFactory.create(Paths.get(testdataRoot, "templates").toUri());
Object openApiFile = cobigen.read(Paths.get(testdataRoot, "two-components-no-attribute.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("testingAttributeTable");
template = findTemplate(cobigen, inputObjects.get(1), templateName);
targetFolder = this.tmpFolder.newFolder();
report = cobigen.generate(inputObjects.get(1), template, targetFolder.toPath());
assertThat(report.hasWarnings());
assertThat(report).isSuccessful();
assertThat(targetFolder.toPath().resolve("testVariableAssignment_attribute.txt").toFile()).exists().hasContent("");
}
use of com.devonfw.cobigen.api.to.GenerationReportTo in project cobigen by devonfw.
the class GenerateCommand method generate.
/**
* Generates new templates or increments using the inputFile from the inputProject.
*
* @param <T> type of generable artifacts to generate
* @param inputFile input file to be selected by the user
* @param input parsed by CobiGen read method
* @param inputProject input project where the input file is located. We need this in order to build the classpath of
* the input file
* @param generableArtifacts the list of increments or templates that the user is going to use for generation
* @param cg Initialized CobiGen instance
* @param c class type, specifies whether Templates or Increments should be preprocessed
*/
public <T extends GenerableArtifact> void generate(Path inputFile, Object input, Path inputProject, List<T> generableArtifacts, CobiGen cg, Class<T> c) {
boolean isIncrements = c.getSimpleName().equals(IncrementTo.class.getSimpleName());
if (this.outputRootPath == null) {
// If user did not specify the output path of the generated files, we can use
// the current project folder
setOutputRootPath(inputProject);
}
GenerationReportTo report = null;
LOG.info("Generating {} for input '{}, this can take a while...", isIncrements ? "increments" : "templates", inputFile);
report = cg.generate(input, generableArtifacts, this.outputRootPath.toAbsolutePath(), false, (task, progress) -> {
});
ValidationUtils.checkGenerationReport(report);
Set<Path> generatedJavaFiles = report.getGeneratedFiles().stream().filter(e -> e.getFileName().endsWith(".java")).collect(Collectors.toSet());
if (!generatedJavaFiles.isEmpty()) {
try {
ParsingUtils.formatJavaSources(generatedJavaFiles);
} catch (FormatterException e) {
LOG.warn("Generation was successful but we were not able to format your code. Maybe you will see strange formatting.");
}
}
}
Aggregations