Search in sources :

Example 21 with TemplateTo

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

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

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

the class CobiGenWrapper method getAllIncrements.

/**
 * @return all available increments
 */
public ComparableIncrement[] getAllIncrements() {
    Set<ComparableIncrement> result = Sets.newHashSet();
    List<IncrementTo> matchingIncrements = Lists.newLinkedList();
    if (this.inputs != null && this.inputs.size() != 0) {
        for (Object input : this.cobiGen.resolveContainers(this.inputs.get(0))) {
            matchingIncrements.addAll(this.cobiGen.getMatchingIncrements(input));
        }
    }
    // convert to comparable increments
    for (IncrementTo increment : matchingIncrements) {
        result.add(new ComparableIncrement(increment.getId(), increment.getDescription(), increment.getTriggerId(), increment.getTemplates(), increment.getDependentIncrements()));
    }
    // add "all" increment, which should include all possible templates
    ComparableIncrement all = new ComparableIncrement(ALL_INCREMENT_ID, ALL_INCREMENT_NAME, null, Lists.<TemplateTo>newLinkedList(), Lists.<IncrementTo>newLinkedList());
    for (TemplateTo t : this.matchingTemplates) {
        all.addTemplate(t);
    }
    result.add(all);
    ComparableIncrement[] array = result.toArray(new ComparableIncrement[0]);
    Arrays.sort(array);
    LOG.debug("Available Increments: {}", result);
    return array;
}
Also used : IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) ComparableIncrement(com.devonfw.cobigen.eclipse.generator.entity.ComparableIncrement) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo)

Example 24 with TemplateTo

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

the class CobiGenWrapper method getTemplateDestinationPaths.

/**
 * Returns the set of all workspace relative destination paths for the templates of the given
 * {@link ComparableIncrement} mapped to the correlated {@link TemplateTo}. This is done for the
 * {@link #getCurrentRepresentingInput() current representing input}.
 *
 * @param <T> {@link IncrementTo} or any sub type
 * @param increments {@link IncrementTo Increments} the template destination paths should be retrieved for
 * @param input input for path resolution
 * @return the mapping of destination paths to its templates
 */
private <T extends IncrementTo> Map<String, Set<TemplateTo>> getTemplateDestinationPaths(Collection<T> increments, Object input) {
    List<IncrementTo> matchingIncrements = this.cobiGen.getMatchingIncrements(input);
    boolean cachingEnabled = input == getCurrentRepresentingInput();
    Map<String, Set<TemplateTo>> result = Maps.newHashMap();
    for (IncrementTo increment : increments) {
        if (increment.getId().equals(ALL_INCREMENT_ID)) {
            continue;
        }
        // check cache
        if (cachingEnabled) {
            Map<String, Set<TemplateTo>> cachedTemplatePaths = this.incrementToTemplateWorkspacePathsCache.get(increment);
            if (cachedTemplatePaths != null) {
                MapUtils.deepMapAddAll(result, cachedTemplatePaths);
                continue;
            }
        }
        // Now we need to check whether the input matches the increment
        boolean inputNotMatchesIncrement = true;
        for (IncrementTo inc : matchingIncrements) {
            // If at least one triggerID is present, then the input is valid for this increment
            if (inc.getTriggerId().equals(increment.getTriggerId())) {
                inputNotMatchesIncrement = false;
                break;
            }
        }
        // If it does not match, we should not keep the execution for this increment
        if (inputNotMatchesIncrement) {
            continue;
        }
        // process normal
        Map<String, Set<TemplateTo>> thisIncrementResult = Maps.newHashMap();
        for (TemplateTo template : increment.getTemplates()) {
            String path = resolveWorkspaceDependentTemplateDestinationPath(template, input);
            MapUtils.deepMapAdd(thisIncrementResult, path, template);
        }
        if (cachingEnabled) {
            this.incrementToTemplateWorkspacePathsCache.put(increment, thisIncrementResult);
        }
        MapUtils.deepMapAddAll(result, thisIncrementResult);
    }
    return result;
}
Also used : IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) HashSet(java.util.HashSet) Set(java.util.Set) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo)

Example 25 with TemplateTo

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

the class CobiGenWrapper method getMergeableFiles.

/**
 * Returns project dependent paths of all resources which are marked to be mergeable (just of
 * {@link #getCurrentRepresentingInput()})
 *
 * @param increments to be considered
 * @return The set of all mergeable project dependent file paths
 */
public Set<String> getMergeableFiles(Collection<IncrementTo> increments) {
    if (increments.contains(new ComparableIncrement(ALL_INCREMENT_ID, ALL_INCREMENT_NAME, null, Lists.<TemplateTo>newLinkedList(), Lists.<IncrementTo>newLinkedList()))) {
        increments = this.cobiGen.getMatchingIncrements(getCurrentRepresentingInput());
    }
    Set<String> mergeablePaths = Sets.newHashSet();
    Map<String, Set<TemplateTo>> templateDestinationPaths = getTemplateDestinationPaths(increments);
    for (Entry<String, Set<TemplateTo>> entry : templateDestinationPaths.entrySet()) {
        if (isMergableFile(entry.getValue())) {
            mergeablePaths.add(entry.getKey());
        }
    }
    return mergeablePaths;
}
Also used : IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) HashSet(java.util.HashSet) Set(java.util.Set) ComparableIncrement(com.devonfw.cobigen.eclipse.generator.entity.ComparableIncrement) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo)

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