Search in sources :

Example 16 with GenerationReportTo

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");
}
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 17 with GenerationReportTo

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"));
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) VariableAssignmentTo(com.devonfw.cobigen.api.to.VariableAssignmentTo) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) OpenAPIMatcher(com.devonfw.cobigen.openapiplugin.matcher.OpenAPIMatcher) ArrayList(java.util.ArrayList) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) Test(org.junit.Test)

Example 18 with GenerationReportTo

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");
}
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 19 with GenerationReportTo

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

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.");
        }
    }
}
Also used : IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) InputReaderException(com.devonfw.cobigen.api.exception.InputReaderException) Parameters(picocli.CommandLine.Parameters) LoggerFactory(org.slf4j.LoggerFactory) Entry.comparingByValue(java.util.Map.Entry.comparingByValue) HashMap(java.util.HashMap) InputMismatchException(java.util.InputMismatchException) CobiGen(com.devonfw.cobigen.api.CobiGen) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) CobiGenCLI(com.devonfw.cobigen.cli.CobiGenCLI) URI(java.net.URI) FormatterException(com.google.googlejavaformat.java.FormatterException) Command(picocli.CommandLine.Command) Path(java.nio.file.Path) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) ConfigurationFinder(com.devonfw.cobigen.impl.util.ConfigurationFinder) UserAbortException(com.devonfw.cobigen.cli.exceptions.UserAbortException) ValidationUtils(com.devonfw.cobigen.cli.utils.ValidationUtils) Logger(org.slf4j.Logger) Tuple(com.devonfw.cobigen.api.util.Tuple) Files(java.nio.file.Files) MavenUtil(com.devonfw.cobigen.api.util.MavenUtil) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ParsingUtils(com.devonfw.cobigen.cli.utils.ParsingUtils) StandardCharsets(java.nio.charset.StandardCharsets) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGenUtils(com.devonfw.cobigen.cli.utils.CobiGenUtils) List(java.util.List) Option(picocli.CommandLine.Option) MessagesConstants(com.devonfw.cobigen.cli.constants.MessagesConstants) Paths(java.nio.file.Paths) GenerableArtifact(com.devonfw.cobigen.api.to.GenerableArtifact) JaccardDistance(org.apache.commons.text.similarity.JaccardDistance) FileSystemUtil(com.devonfw.cobigen.impl.util.FileSystemUtil) Path(java.nio.file.Path) IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) FormatterException(com.google.googlejavaformat.java.FormatterException) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo)

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