Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template set deployables tests #4

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.devonfw.cobigen.systemtest;

import static com.devonfw.cobigen.api.assertj.CobiGenAsserts.assertThat;
import static com.devonfw.cobigen.test.matchers.CustomHamcrestMatchers.hasItemsInList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.internal.matchers.Any.ANY;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.List;

import org.hamcrest.Matcher;
import org.junit.Test;

import com.devonfw.cobigen.api.CobiGen;
Expand All @@ -27,9 +26,11 @@
import com.devonfw.cobigen.api.matchers.VariableAssignmentToMatcher;
import com.devonfw.cobigen.api.to.GenerationReportTo;
import com.devonfw.cobigen.api.to.TemplateTo;
import com.devonfw.cobigen.api.to.VariableAssignmentTo;
import com.devonfw.cobigen.impl.CobiGenFactory;
import com.devonfw.cobigen.impl.extension.PluginRegistry;
import com.devonfw.cobigen.systemtest.common.AbstractApiTest;
import com.devonfw.cobigen.test.matchers.CustomHamcrestMatchers;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

Expand Down Expand Up @@ -69,6 +70,7 @@ public void callClassLoadingTest() throws Exception {
// Verification
File expectedResult = new File(testFileRootPath, "expected/Test.java");
File generatedFile = new File(generationRootFolder, "com/devonfw/Test.java");
report.isSuccessful();
assertThat(report).isSuccessful();
assertThat(generatedFile).exists();
assertThat(generatedFile).isFile().hasSameContentAs(expectedResult);
Expand Down Expand Up @@ -140,24 +142,27 @@ public String toString() {
when(triggerInterpreter.getInputReader()).thenReturn(inputReader);

when(inputReader.isValidInput(any())).thenReturn(true);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("fqn"), ANY, sameInstance(container)))))
.thenReturn(false);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("package"), ANY, sameInstance(container)))))
.thenReturn(true);
when(matcher.matches(argThat(
new MatcherToMatcher(equalTo("fqn"), org.hamcrest.CoreMatchers.any(String.class), sameInstance(container)))))
.thenReturn(false);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("package"), org.hamcrest.CoreMatchers.any(String.class),
sameInstance(container))))).thenReturn(true);

// Simulate container children resolution of any plug-in
when(inputReader.getInputObjects(any(), any(Charset.class))).thenReturn(Lists.newArrayList(firstChildResource));

when(matcher.matches(argThat(new MatcherToMatcher(equalTo("fqn"), ANY, sameInstance(firstChildResource)))))
.thenReturn(true);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("fqn"), org.hamcrest.CoreMatchers.any(String.class),
sameInstance(firstChildResource))))).thenReturn(true);

// Simulate variable resolving of any plug-in
when(matcher.resolveVariables(argThat(new MatcherToMatcher(equalTo("fqn"), ANY, sameInstance(firstChildResource))),
argThat(hasItemsInList(
//
new VariableAssignmentToMatcher(equalTo("regex"), equalTo("rootPackage"), equalTo("1"), equalTo(false)),
new VariableAssignmentToMatcher(equalTo("regex"), equalTo("entityName"), equalTo("3"), equalTo(false)))),
any()))
Matcher<VariableAssignmentTo> m1 = new VariableAssignmentToMatcher(equalTo("regex"), equalTo("rootPackage"),
equalTo("1"), equalTo(false));
Matcher<VariableAssignmentTo> m2 = new VariableAssignmentToMatcher(equalTo("regex"), equalTo("entityName"),
equalTo("3"), equalTo(false));
Matcher<List<VariableAssignmentTo>> tmp = CustomHamcrestMatchers.hasItemsInList(m1, m2);
// IsIterableContaining<VariableAssignmentTo> t1 = new IsIterableContaining<>();
when(matcher.resolveVariables(argThat(new MatcherToMatcher(equalTo("fqn"),
org.hamcrest.CoreMatchers.any(String.class), sameInstance(firstChildResource))), argThat(tmp), any()))
.thenReturn(ImmutableMap.<String, String> builder().put("rootPackage", "com.devonfw")
.put("entityName", "Test").build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import static com.devonfw.cobigen.api.assertj.CobiGenAsserts.assertThat;
import static com.devonfw.cobigen.test.matchers.CustomHamcrestMatchers.hasItemsInList;
import static org.hamcrest.CoreMatchers.any;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyList;
import static org.mockito.Matchers.argThat;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.internal.matchers.Any.ANY;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;

import java.io.File;
import java.nio.charset.Charset;
Expand All @@ -20,6 +19,7 @@

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import com.devonfw.cobigen.api.CobiGen;
import com.devonfw.cobigen.api.extension.GeneratorPluginActivator;
Expand Down Expand Up @@ -251,26 +251,34 @@ public String toString() {
when(triggerInterpreter.getMatcher()).thenReturn(matcher);
when(triggerInterpreter.getInputReader()).thenReturn(inputReader);

when(inputReader.isValidInput(any())).thenReturn(true);
when(inputReader.isValidInput(Mockito.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));
when(matcher.resolveVariables(argThat(new MatcherToMatcher(equalTo("or"), any(String.class), sameInstance(child1))),
anyList(), Mockito.any()))
.thenReturn(ImmutableMap.<String, String> builder().put("variable", "child1").build());
when(matcher.resolveVariables(argThat(new MatcherToMatcher(equalTo("or"), any(String.class), sameInstance(child2))),
anyList(), Mockito.any()))
.thenReturn(ImmutableMap.<String, String> builder().put("variable", "child2").build());
when(inputReader.getInputObjects(Mockito.any(), Mockito.any(Charset.class)))
.thenReturn(Lists.newArrayList(child1, child2));

// match container
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("container"), ANY, sameInstance(container)))))
.thenReturn(true);
when(matcher
.matches(argThat(new MatcherToMatcher(equalTo("container"), any(String.class), 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);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("or"), any(String.class), sameInstance(child1)))))
.thenReturn(true);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("not"), any(String.class), 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);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("or"), any(String.class), sameInstance(child2)))))
.thenReturn(true);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("not"), any(String.class), sameInstance(child2)))))
.thenReturn(false);

PluginRegistry.registerTriggerInterpreter(triggerInterpreter, activator);

Expand Down Expand Up @@ -346,10 +354,10 @@ public String toString() {
when(triggerInterpreter.getMatcher()).thenReturn(matcher);
when(triggerInterpreter.getInputReader()).thenReturn(inputReader);

when(inputReader.isValidInput(any())).thenReturn(true);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("fqn"), ANY, sameInstance(container)))))
when(inputReader.isValidInput(Mockito.any())).thenReturn(true);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("fqn"), any(String.class), sameInstance(container)))))
.thenReturn(false);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("package"), ANY, sameInstance(container)))))
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("package"), any(String.class), sameInstance(container)))))
.thenReturn(true);

// Simulate container children resolution of any plug-in
Expand All @@ -361,22 +369,25 @@ public String toString() {
return "child2";
}
};
when(inputReader.getInputObjects(any(), any(Charset.class)))
when(inputReader.getInputObjects(Mockito.any(), Mockito.any(Charset.class)))
.thenReturn(Lists.newArrayList(firstChildResource, secondChildResource));
} else {
when(inputReader.getInputObjects(any(), any(Charset.class))).thenReturn(Lists.newArrayList(firstChildResource));
when(inputReader.getInputObjects(Mockito.any(), Mockito.any(Charset.class)))
.thenReturn(Lists.newArrayList(firstChildResource));
}

when(matcher.matches(argThat(new MatcherToMatcher(equalTo("fqn"), ANY, sameInstance(firstChildResource)))))
.thenReturn(containerChildMatchesTrigger);
when(matcher
.matches(argThat(new MatcherToMatcher(equalTo("fqn"), any(String.class), sameInstance(firstChildResource)))))
.thenReturn(containerChildMatchesTrigger);

// Simulate variable resolving of any plug-in
when(matcher.resolveVariables(argThat(new MatcherToMatcher(equalTo("fqn"), ANY, sameInstance(firstChildResource))),
when(matcher.resolveVariables(
argThat(new MatcherToMatcher(equalTo("fqn"), any(String.class), sameInstance(firstChildResource))),
argThat(hasItemsInList(
//
new VariableAssignmentToMatcher(equalTo("regex"), equalTo("rootPackage"), equalTo("1"), equalTo(false)),
new VariableAssignmentToMatcher(equalTo("regex"), equalTo("entityName"), equalTo("3"), equalTo(false)))),
any()))
Mockito.any()))
.thenReturn(ImmutableMap.<String, String> builder().put("rootPackage", "com.devonfw")
.put("entityName", "Test").build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import static com.devonfw.cobigen.api.assertj.CobiGenAsserts.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hamcrest.CoreMatchers.any;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.internal.matchers.Any.ANY;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;

import java.io.File;
import java.io.FileWriter;
Expand All @@ -23,6 +22,7 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.mockito.Mockito;

import com.devonfw.cobigen.api.CobiGen;
import com.devonfw.cobigen.api.exception.InvalidConfigurationException;
Expand Down Expand Up @@ -165,13 +165,15 @@ public String toString() {
when(triggerInterpreter.getMatcher()).thenReturn(matcher);
when(triggerInterpreter.getInputReader()).thenReturn(inputReader);

when(inputReader.isValidInput(any())).thenReturn(true);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("fqn"), ANY, sameInstance(input))))).thenReturn(true);
when(inputReader.isValidInput(Mockito.any())).thenReturn(true);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("fqn"), any(String.class), sameInstance(input)))))
.thenReturn(true);

// Simulate variable resolving of any plug-in
HashMap<String, String> variables = new HashMap<>(1);
variables.put("contextVar", "contextValue");
when(matcher.resolveVariables(any(MatcherTo.class), any(List.class), any())).thenReturn(variables);
when(matcher.resolveVariables(Mockito.any(MatcherTo.class), Mockito.any(List.class), Mockito.any()))
.thenReturn(variables);

PluginRegistry.registerTriggerInterpreter(triggerInterpreter, activator);

Expand Down Expand Up @@ -218,13 +220,15 @@ public String toString() {
when(triggerInterpreter.getMatcher()).thenReturn(matcher);
when(triggerInterpreter.getInputReader()).thenReturn(inputReader);

when(inputReader.isValidInput(any())).thenReturn(true);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("fqn"), ANY, sameInstance(input))))).thenReturn(true);
when(inputReader.isValidInput(Mockito.any())).thenReturn(true);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("fqn"), any(String.class), sameInstance(input)))))
.thenReturn(true);

// Simulate variable resolving of any plug-in
HashMap<String, String> variables = new HashMap<>(1);
variables.put("contextVar", "contextValue");
when(matcher.resolveVariables(any(MatcherTo.class), any(List.class), any())).thenReturn(variables);
when(matcher.resolveVariables(Mockito.any(MatcherTo.class), Mockito.any(List.class), Mockito.any()))
.thenReturn(variables);

PluginRegistry.registerTriggerInterpreter(triggerInterpreter, activator);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
import java.nio.file.Path;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.util.CobiGenPaths;
import com.devonfw.cobigen.impl.CobiGenFactory;
import com.devonfw.cobigen.impl.util.ConfigurationFinder;
import com.devonfw.cobigen.systemtest.common.AbstractApiTest;
Expand All @@ -34,25 +38,44 @@ public class TemplateProcessingTest extends AbstractApiTest {
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

/**
* mock the pathObject to use the temporary folder instead of the user folder
*/
private MockedStatic<CobiGenPaths> cobigenPaths;

/**
* temporary project to store CobiGen home
*/
File cobiGenHome;

/**
* Creates a temporary CobiGen home directory for each test
* Creates a temporary CobiGen home directory for each test and create static mock for CobiGenPaths object
*
* @throws IOException if an Exception occurs
*/
@Before
public void prepare() throws IOException {

this.cobiGenHome = this.tempFolder.newFolder("playground", "templatesHome");

this.cobigenPaths = Mockito.mockStatic(CobiGenPaths.class, Mockito.CALLS_REAL_METHODS);
this.cobigenPaths.when(() -> CobiGenPaths.getCobiGenHomePath()).thenReturn(this.cobiGenHome.toPath());

}

/**
* cleanup mockito static mock
*/
@After
public void cleanup() {

this.cobigenPaths.close();
}

/**
* @throws IOException if an Exception occurs
*/
@Test
public void extractTemplateSetsTest() throws IOException {

FileUtils.copyDirectory(new File(testFileRootPath + "templates"),
Expand All @@ -62,21 +85,23 @@ public void extractTemplateSetsTest() throws IOException {
.resolve(ConfigurationConstants.ADAPTED_FOLDER);
Path extractedJar1 = adaptedFolder.resolve("template-test1-0.0.1");
Path extractedJar2 = adaptedFolder.resolve("template-test2-0.0.1");
assertThat(Files.exists(extractedJar1));
assertThat(Files.exists(extractedJar2));
assertThat(extractedJar1.toFile()).exists().isDirectory();
assertThat(extractedJar2.toFile()).exists().isDirectory();
}

/**
* @throws IOException if an Exception occurs
*/
@Test
public void extractTemplatesWithOldConfiguration() throws IOException {

Path cobigenTemplatesProject = this.cobiGenHome.toPath()
.resolve(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATES_PATH)
.resolve(ConfigurationConstants.COBIGEN_TEMPLATES);
Path cobigenTemplatesParent = this.cobiGenHome.toPath()
.resolve(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATES_PATH);
Files.createDirectories(cobigenTemplatesParent);
Path cobigenTemplatesProject = cobigenTemplatesParent.resolve(ConfigurationConstants.COBIGEN_TEMPLATES);
Files.createDirectories(cobigenTemplatesProject);
CobiGenFactory.extractTemplates();
assertThat(Files.exists(cobigenTemplatesProject));
assertThat(cobigenTemplatesProject.toFile()).exists().isDirectory();
}

/**
Expand Down Expand Up @@ -106,7 +131,7 @@ public void testExtractTemplateSets() throws Exception {
/**
* @throws IOException if an Exception occurs
*/
public void findTemplateSetJarsWithBackwardsCompatibilityTest() throws IOException {
private void findTemplateSetJarsWithBackwardsCompatibilityTest() throws IOException {

FileUtils.createParentDirectories(new File(testFileRootPath + "template-sets"));
URI templatesLocationURI = ConfigurationFinder.findTemplatesLocation();
Expand Down
Loading