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

1510 allow template sets to be installed #1554

Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e3e9185
initial commit
EduardKrieger Jul 7, 2022
d03e10d
template-sets will be installed at cobigen startup
EduardKrieger Jul 15, 2022
4ecb3bc
added tests
EduardKrieger Jul 19, 2022
3afb45a
removed old test
EduardKrieger Jul 19, 2022
060458d
removed comments
EduardKrieger Jul 19, 2022
7116b69
added mavenCoordinates dataholder and reworked the functions
EduardKrieger Jul 28, 2022
e4094a8
added systemtest
EduardKrieger Aug 2, 2022
3e39e32
added creation of downloaded folder
EduardKrieger Aug 4, 2022
0d2c8f9
trying to fix cli tests
EduardKrieger Aug 10, 2022
b6e179e
removed unnessesary download of templates
EduardKrieger Aug 10, 2022
787e592
added check for downloaded folder
EduardKrieger Aug 16, 2022
9612a43
revert changes to an old test
EduardKrieger Aug 16, 2022
89e0455
added javadoc and adjusted documentation
EduardKrieger Aug 17, 2022
0013541
fixed wrong push
EduardKrieger Aug 17, 2022
30edb31
#1510 implemented requested changes
jan-vcapgemini Aug 17, 2022
aca4e82
implemented requested changes
EduardKrieger Aug 18, 2022
1ebed3d
initial implenetation of mocking the downloads
EduardKrieger Aug 19, 2022
c71d13f
added todo
EduardKrieger Aug 24, 2022
494dba1
removed usage of Path
EduardKrieger Aug 24, 2022
e407089
removed initial try to mock downloads in tests
EduardKrieger Aug 24, 2022
4cbdaf3
chaged assertions to assertThat
EduardKrieger Aug 25, 2022
4d43f22
changed variables to correct camel case and used constants, Files and…
EduardKrieger Aug 26, 2022
3d9de79
fixed typo
EduardKrieger Aug 29, 2022
b0f400c
implemented requested changes
EduardKrieger Aug 31, 2022
41324cb
fixed nullPointerbug with lates commit
EduardKrieger Aug 31, 2022
6849aa6
implemented requested changes
EduardKrieger Sep 6, 2022
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
Expand Up @@ -32,4 +32,5 @@
<tags>
<tag name="bla" />
</tags>

</contextConfiguration>
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public class ConfigurationConstants {
*/
public static final String CONFIG_PROPERTY_TEMPLATE_SETS_HIDE = "template-sets.hide";

/**
* Name of configuration key to preinstall specific template sets
*/
public static final String CONFIG_PROPERTY_TEMPLATE_SETS_INSTALLED = "template-sets.installed";
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved

/**
* Default (public) cobigen GroupId
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ public class TemplatesJarConstants {
*/
public static final String DOWNLOADED_JAR_FOLDER = "/.metadata/cobigen_jars";

/**
* Regular expression to check the correct definition of maven coordinates to download a template-set with the
* configuration key template-sets.installed in the properties.
*/
public static final String MAVEN_COORDINATES_CHECK = "([a-zA-Z0-9_\\-\\.]+):([a-zA-Z0-9_-]+)(:([0-9]+(\\.[0-9]+)*(-SNAPSHOT)?|LATEST))?";

/**
* Jar regular expression name to be used in a file name filter, so that we can check whether the templates are
* already downloaded. Checks "templates-anystring-anydigitbetweendots.jar"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.devonfw.cobigen.api.util;

/**
* This MavenCoordinate class is just a dataholder with maven coordinates.
*/
public class MavenCoordinate {

/**
* the groupID of the maven artifact
*/
private String groupID;
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved

/**
* the artifactID of the maven artifact
*/
private String artifactID;
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved

/**
* the version of the maven artifact
*/
private String version;

/**
* Creates a new {@link MavenCoordinate} object with the given properties
*
* @param groupID a {@link String} with the groupID of the maven artifact
* @param artifactID {@link String} with the artifactID of the maven artifact
* @param version {@link String} with the version of the maven artifact
*/
public MavenCoordinate(String groupID, String artifactID, String version) {
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved

this.groupID = groupID;
this.artifactID = artifactID;
this.version = version;
}

/**
* Returns the value of the artifactID
*
* @return {@link String} artifactID
*/
public String getArtifactID() {
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved

return this.artifactID;
}

/**
* Returns the value of the groupID
*
* @return {@link String} groupID
*/
public String getGroupID() {

return this.groupID;
}

/**
* Returns the value of the version
*
* @return {@link String} version
*/
public String getVersion() {

return this.version;
}

@Override
public boolean equals(Object obj) {

if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
MavenCoordinate mavenCoordinate = (MavenCoordinate) obj;

if (this.artifactID != mavenCoordinate.getArtifactID()) {
return false;
} else if (this.groupID != mavenCoordinate.getGroupID()) {
return false;
} else if (this.version != mavenCoordinate.getVersion()) {
return false;
}
return true;
}

@Override
public int hashCode() {

return this.artifactID.hashCode() + this.groupID.hashCode() + this.version.hashCode();
}
maybeec marked this conversation as resolved.
Show resolved Hide resolved

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand All @@ -21,6 +23,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.constants.TemplatesJarConstants;
import com.devonfw.cobigen.api.exception.CobiGenRuntimeException;

Expand All @@ -41,11 +44,12 @@ public class TemplatesJarUtil {
* @param templatesDirectory directory where the templates jar are located
* @return fileName Name of the file downloaded
*/
private static String downloadJar(String groupId, String artifactId, String version, boolean isDownloadSource,
public static String downloadJar(String groupId, String artifactId, String version, boolean isDownloadSource,
File templatesDirectory) {

// By default the version should be latest
if (version.isEmpty() || version == null) {
if (version == null || version.isEmpty()) {
maybeec marked this conversation as resolved.
Show resolved Hide resolved

version = "LATEST";
}

Expand Down Expand Up @@ -85,6 +89,8 @@ private static String downloadJar(String groupId, String artifactId, String vers
}

/**
* Downloads the latest devon4j templates
*
* @param isDownloadSource true if downloading source jar file
* @param templatesDirectory directory where the templates jar are located
* @return fileName Name of the file downloaded
Expand All @@ -95,6 +101,68 @@ public static String downloadLatestDevon4jTemplates(boolean isDownloadSource, Fi
TemplatesJarConstants.DEVON4J_TEMPLATES_ARTIFACTID, "LATEST", isDownloadSource, templatesDirectory);
}

/**
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved
* Downloads multiple jar files defined by the maven coordinates. Only downloads if files are not present or adapted
* folder does not exist.
*
* @param templatesDirectory directory where the templates jar are located
* @param mavenCoordinates list with {@link MavenCoordinate} that will be loaded
*/
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved
public static void downloadTemplatesByMavenCoordinates(Path templatesDirectory,
List<MavenCoordinate> mavenCoordinates) {

if (mavenCoordinates == null || mavenCoordinates.isEmpty()) {
return;
// no templates specified
}

Set<MavenCoordinate> existingTemplates = new HashSet<>();
Path adapted = templatesDirectory.resolve(ConfigurationConstants.ADAPTED_FOLDER);
Path downloaded = templatesDirectory.resolve(ConfigurationConstants.DOWNLOADED_FOLDER);
// search for already available template-sets
for (MavenCoordinate mavenCoordinate : mavenCoordinates) {
if (Files.exists(adapted)) {
try {
if (Files.list(adapted)
.anyMatch(f -> (f.getFileName().toString().contains(mavenCoordinate.getArtifactID())))) {
existingTemplates.add(mavenCoordinate);
}
} catch (IOException e) {
LOG.warn("Failed to get all files and directories from adapted folder", e);
}
}
if (Files.exists(downloaded)) {
try {
if (Files.list(downloaded)
.anyMatch(f -> (f.getFileName().toString().contains(mavenCoordinate.getArtifactID())))) {
existingTemplates.add(mavenCoordinate);
}
} catch (IOException e) {
LOG.warn("Failed to get all files and directories from downloaded folder", e);
}
maybeec marked this conversation as resolved.
Show resolved Hide resolved
} else {
LOG.info("downloaded folder could not be found and will be created ");
try {
Files.createDirectory(templatesDirectory.resolve(ConfigurationConstants.DOWNLOADED_FOLDER));
} catch (IOException e) {
throw new CobiGenRuntimeException("Could not create Download Folder", e);
}
}
}
// nullcheck ?
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved
if (existingTemplates.size() > 0) {
mavenCoordinates.removeAll(existingTemplates);
}

for (MavenCoordinate mavenCoordinate : mavenCoordinates) {
downloadJar(mavenCoordinate.getGroupID(), mavenCoordinate.getArtifactID(), mavenCoordinate.getVersion(), false,
downloaded.toFile());
downloadJar(mavenCoordinate.getGroupID(), mavenCoordinate.getArtifactID(), mavenCoordinate.getVersion(), true,
downloaded.toFile());
}

}

/**
* Checks whether there is a newer version of the templates on Maven
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package com.devonfw.cobigen.systemtest;

import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThat;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;

import org.hamcrest.Matchers;
import org.junit.Test;

import com.devonfw.cobigen.api.CobiGen;
import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.impl.CobiGenFactory;
import com.devonfw.cobigen.systemtest.common.AbstractApiTest;

/**
* Test suite for template installation with the cobigen startup
*
*/
public class TemplatesInstallationTest extends AbstractApiTest {

/**
* Tests if the templates specified in the .cobigen file won´t be loaded with the monolithic structure.
*
* @throws Exception test fails.
*/
@Test
public void testInstallTemplatesAtStartupMonolithicStructure() throws Exception {

File folder = this.tmpFolder.newFolder("TemplateSetsInstalledTest");
withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, folder.getAbsolutePath()).execute(() -> {
File templates = this.tmpFolder.newFolder("TemplateSetsInstalledTest", ConfigurationConstants.TEMPLATES_FOLDER);
File cobigenDir = templates.toPath().resolve(ConfigurationConstants.COBIGEN_TEMPLATES).toFile();
Files.createDirectories(cobigenDir.toPath());
File target = new File(folder, ".cobigen");
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved
try (BufferedWriter writer = new BufferedWriter(new FileWriter(target))) {
writer.write("template-sets.installed=com.devonfw.cobigen:templates-devon4j:2021.12.006");
}
CobiGenFactory.create(templates.toURI(), true);
assertThat(cobigenDir.listFiles()).hasSize(0);
});
}

/**
* Tests if the templates specified in the .cobigen file will be loaded at startup with the template-set structure and
* an existing downloaded folder.
*
* @throws Exception test fails.
*/
@Test
public void testInstallTemplatesAtStartup() throws Exception {

File folder = this.tmpFolder.newFolder("TemplateSetsInstalledTest");
withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, folder.getAbsolutePath()).execute(() -> {
File templateSets = this.tmpFolder.newFolder("TemplateSetsInstalledTest",
ConfigurationConstants.TEMPLATE_SETS_FOLDER);
File downloaded = this.tmpFolder.newFolder("TemplateSetsInstalledTest",
ConfigurationConstants.TEMPLATE_SETS_FOLDER, "downloaded");
File target = new File(folder, ".cobigen");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(target))) {
writer.write("template-sets.installed=com.devonfw.cobigen:templates-devon4j:2021.12.006");
}
CobiGen cobigen = CobiGenFactory.create(templateSets.toURI());
assertThat(downloaded.listFiles()).hasSize(2);
for (File f : downloaded.listFiles()) {
assertThat(f.getName(), Matchers.either(Matchers.is("templates-devon4j-2021.12.006.jar"))
.or(Matchers.is("templates-devon4j-2021.12.006-sources.jar")));
}
});

}

/**
* Tests if the templates specified in the .cobigen file won´t be loaded when a adapted folder already exists.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an adapted folder

*
* @throws Exception test fails.
*/
@Test
public void testInstallTemplatesAtStartupAdapted() throws Exception {

File folder = this.tmpFolder.newFolder("TemplateSetsInstalledTest");
withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, folder.getAbsolutePath()).execute(() -> {
File templateSets = this.tmpFolder.newFolder("TemplateSetsInstalledTest",
ConfigurationConstants.TEMPLATE_SETS_FOLDER);
File adapted = this.tmpFolder.newFolder("TemplateSetsInstalledTest", ConfigurationConstants.TEMPLATE_SETS_FOLDER,
ConfigurationConstants.ADAPTED_FOLDER);
File downloaded = this.tmpFolder.newFolder("TemplateSetsInstalledTest",
ConfigurationConstants.TEMPLATE_SETS_FOLDER, ConfigurationConstants.DOWNLOADED_FOLDER);
File target = new File(folder, ".cobigen");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(target))) {
writer.write("template-sets.installed=com.devonfw.cobigen:templates-devon4j:2021.12.005");
}
CobiGen cobigen = CobiGenFactory.create(templateSets.toURI());
assertThat(downloaded.listFiles()).hasSize(2);
});

}

/**
* Tests if the templates specified in the .cobigen file will be loaded at startup with the template-set structure
* without an existing downloaded folder.
*
* @throws Exception test fails.
*/
@Test
public void testInstallTemplatesAtStartupWithoutDownloaded() throws Exception {

File folder = this.tmpFolder.newFolder("TemplateSetsInstalledTest");
withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, folder.getAbsolutePath()).execute(() -> {
File templateSets = this.tmpFolder.newFolder("TemplateSetsInstalledTest",
ConfigurationConstants.TEMPLATE_SETS_FOLDER);
File downloaded = templateSets.toPath().resolve(ConfigurationConstants.DOWNLOADED_FOLDER).toFile();
File target = new File(folder, ".cobigen");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(target))) {
writer.write("template-sets.installed=com.devonfw.cobigen:templates-devon4j:2021.12.006");
}
CobiGen cobigen = CobiGenFactory.create(templateSets.toURI());
assertThat(downloaded.listFiles()).hasSize(2);
for (File f : downloaded.listFiles()) {
assertThat(f.getName(), Matchers.either(Matchers.is("templates-devon4j-2021.12.006.jar"))
.or(Matchers.is("templates-devon4j-2021.12.006-sources.jar")));
}
});
}

/**
* Tests if the templates specified in the .cobigen file will checked for the correct format defined in the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be checked

* documentation.
*
* @throws Exception test fails.
*/
@Test
public void testInstallTemplatesAtStartupWitWrongCoordinates() throws Exception {
EduardKrieger marked this conversation as resolved.
Show resolved Hide resolved

File folder = this.tmpFolder.newFolder("TemplateSetsInstalledTest");
withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, folder.getAbsolutePath()).execute(() -> {
File templateSets = this.tmpFolder.newFolder("TemplateSetsInstalledTest",
ConfigurationConstants.TEMPLATE_SETS_FOLDER);
File downloaded = this.tmpFolder.newFolder("TemplateSetsInstalledTest",
ConfigurationConstants.TEMPLATE_SETS_FOLDER, "downloaded");
File target = new File(folder, ".cobigen");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(target))) {
writer.write("template-sets.installed=com.com.com:com-com:com.com");
}
CobiGen cobigen = CobiGenFactory.create(templateSets.toURI());
assertThat(downloaded.listFiles()).hasSize(0);
});
}
}
Loading