Skip to content

Commit

Permalink
1511 enable custom template set group ids for lookup (devonfw#1525)
Browse files Browse the repository at this point in the history
* devonfw#1511 added new method to read properties in ConfigurationFinder.java
new TemplateSetConfiguration Class to save properties
new test cases in ConfigurationFinderTest.java

* devonfw#1511 fixed logic in method readTemplateSetConfiguration in ConfigurationFinder
added hideTemplates variable in TemplateSetConfiguration
completed 2 out of 3 tests cases

* devonfw#1511 corrected CheckTemplateSetConfiguration logic in ConfigurationFinder.java
Test cases Correction for CheckTemplateSetConfiguration in ConfigurationFinderTest.java
New Constants for the new properties created in ConfigurationConstants.java
CheckTemplateSetConfiguration Called in load function in CobiGenPropertiesReader.java

* devonfw#1511 canceled the method call in load properties in CobiGenPropertiesReader.java

* devonfw#1511 written Documentation
Modified the properties function to read properties
corrected tests

* devonfw#1511 documentation correction

* devonfw#1511 Documentation correction

* devonfw#1511 Documentation Correction

* devonfw#1511 requested changes

* devonfw#1511 requested changes #2

* devonfw#1511 requested changes 2

* devonfw#1511 requested changes

* devonfw#1511 fix defaultGroupId logic
  • Loading branch information
mdukhan authored May 11, 2022
1 parent 4e4b6d3 commit a1ca29a
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ public class ConfigurationConstants {
*/
public static final String CONFIG_PROPERTY_TEMPLATE_SETS_PATH = "template-sets";

/**
* Name of configuration key to be able to configure multiple (comma separated) groupIds
*/
public static final String CONFIG_PROPERTY_TEMPLATE_SETS_GROUPIDS = "template-sets.groupIds";

/**
* Name of configuration key to allow snapshots of template-sets to be offered
*/
public static final String CONFIG_PROPERTY_TEMPLATE_SETS_SNAPSHOTS = "template-sets.allow-snapshots";

/**
* Name of configuration key to disable by default querying of default public groupIds
*/
public static final String CONFIG_PROPERTY_TEMPLATE_SETS_DISABLE_LOOKUP = "template-sets.disable-default-lookup";

/**
* Name of configuration key to hide very specific template sets
*/
public static final String CONFIG_PROPERTY_TEMPLATE_SETS_HIDE = "template-sets.hide";

/**
* Default (public) cobigen GroupId
*/
public static final String CONFIG_PROPERTY_TEMPLATE_SETS_DEFAULT_GROUPID = "com.devonfw.cobigen.templates";

// cobigen configuration environment variables

/** Name of the environment variable pointing to cobigen configuration file */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.devonfw.cobigen.impl.config;

import java.util.List;

/**
* mdukhan This Class is used to set specific properties if not found, or save them if correctly found. These properties
* are groupIds, allowSnapshots and hideTemplates.
*/
public class TemplateSetConfiguration {

/** variable for template-set artifacts */
private List<String> groupIds;

/** allow snapshots of template-sets */
private boolean allowSnapshots;

/** variable to hide very specific template sets or versions of template sets */
private List<String> hideTemplates;

/**
* The constructor. load properties from a given source
*
* @param groupIds
* @param allowSnapshots
* @param hideTemplates
*/
public TemplateSetConfiguration(List<String> groupIds, boolean allowSnapshots, List<String> hideTemplates) {

super();
this.groupIds = groupIds;
this.allowSnapshots = allowSnapshots;
this.hideTemplates = hideTemplates;
}

/**
* return a list of the saved groupIds
*
* @return groupIds
*/
public List<String> getGroupIds() {

return this.groupIds;
}

/**
* set a list of the groupIds from a source
*
* @param groupIds new value of {@link #getgroupIds}.
*/
public void setGroupIds(List<String> groupIds) {

this.groupIds = groupIds;
}

/**
* return a boolean which states if specific Snapshots should be allowed.
*
* @return allowSnapshots
*/
public boolean isAllowSnapshots() {

return this.allowSnapshots;
}

/**
* set a value on the snapshot
*
* @param allowSnapshots new value of {@link #getallowSnapshots}.
*/
public void setAllowSnapshots(boolean allowSnapshots) {

this.allowSnapshots = allowSnapshots;
}

/**
* return a list of the saved templates to be hidden
*
* @return hideTemplates
*/
public List<String> getHideTemplates() {

return this.hideTemplates;
}

/**
* set a list of the HideTemplate from a source
*
* @param hideTemplates new value of {@link #gethideTemplates}.
*/
public void setHideTemplates(List<String> hideTemplates) {

this.hideTemplates = hideTemplates;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ private void writeBrokenPatchFile(String targetCharset, File tmpOriginalFile, St
}

/**
* Builds the model for he given input.
* Builds the model for the given input.
*
* @param triggerInterpreter {@link TriggerInterpreter} to be used
* @param trigger activated {@link Trigger}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;

Expand All @@ -15,9 +18,10 @@
import org.slf4j.LoggerFactory;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.exception.CobiGenRuntimeException;
import com.devonfw.cobigen.api.exception.InvalidConfigurationException;
import com.devonfw.cobigen.api.util.CobiGenPaths;
import com.devonfw.cobigen.api.util.TemplatesJarUtil;
import com.devonfw.cobigen.impl.config.TemplateSetConfiguration;

/**
* Utilities related to the cobigen configurations including:
Expand All @@ -29,6 +33,45 @@ public class ConfigurationFinder {
/** Logger instance */
private static final Logger LOG = LoggerFactory.getLogger(ConfigurationFinder.class);

/**
* load properties from .properties file into TemplateSetConfiguration if found valid properties otherwise load
* default values
*
* @param path to a .properties file
* @return TemplateSetConfiguration instance
*/
public static TemplateSetConfiguration loadTemplateSetConfigurations(Path path) {

Properties props = new Properties();
try {
props = readConfigurationFile(path);
} catch (InvalidConfigurationException e) {
LOG.info("This path {} is invalid. The default Config values will be loaded instead.", path);
}

String groupId = ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_GROUPIDS;
String snapshot = ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_SNAPSHOTS;
String hide = ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_HIDE;
String disableLookup = ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_DISABLE_LOOKUP;
String defaultGroupId = ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_DEFAULT_GROUPID;

List<String> groupIdsList = (props.getProperty(groupId) != null)
? Arrays.asList(props.getProperty(groupId).split(","))
: new ArrayList<>();
// Creating a new ArrayList object which can be modified and prevents UnsupportedOperationException.
List<String> groupIds = new ArrayList<>(groupIdsList);
if (props.getProperty(disableLookup) == null || props.getProperty(disableLookup).equals("false"))
if (!groupIds.contains(defaultGroupId))
groupIds.add(defaultGroupId);

boolean useSnapshots = props.getProperty(snapshot) == null || props.getProperty(snapshot).equals("false") ? false
: true;
List<String> hiddenIds = (props.getProperty(hide) != null) ? Arrays.asList(props.getProperty(hide).split(","))
: new ArrayList<>();

return new TemplateSetConfiguration(groupIds, useSnapshots, hiddenIds);
}

/**
* The method finds location of templates. It could be CobiGen_Templates folder or a template artifact
*
Expand Down Expand Up @@ -96,6 +139,7 @@ private static Path getTemplatesFolderLocation(Path cobigenHome, Path configFile
* This is a helper method to read a given cobigen configuration file
*
* @param cobigenConfigFile cobigen configuration file
* @throws InvalidConfigurationException if the file isn't present or the path is invalid
* @return Properties containing configuration
*/
private static Properties readConfigurationFile(Path cobigenConfigFile) {
Expand All @@ -109,7 +153,7 @@ private static Properties readConfigurationFile(Path cobigenConfigFile) {
props.load(strReader);
}
} catch (IOException e) {
throw new CobiGenRuntimeException("An error occured while reading the config file " + cobigenConfigFile, e);
throw new InvalidConfigurationException("An error occured while reading the config file " + cobigenConfigFile, e);
}
return props;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.devonfw.cobigen.unittest.config;

import static org.assertj.core.api.Assertions.assertThat;

import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Test;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.impl.config.TemplateSetConfiguration;
import com.devonfw.cobigen.impl.util.ConfigurationFinder;

/**
* mdukhan Class to test the method loadTemplateSetConfigurations in ConfigurationFinder
*
*/
public class ConfigurationFinderTest {

/**
* Test loadTemplateSetConfigurations Method in ConfigurationFinder if invalid properties found, to load the default
* values.
*/
@Test
public void emptyConfigurationTest() {

Path emptyConfiguration = Paths
.get("src/test/resources/testdata/unittest/config/properties/emptyConfigProperties/config.properties");
TemplateSetConfiguration conf = ConfigurationFinder.loadTemplateSetConfigurations(emptyConfiguration);

assertThat(conf.getGroupIds()).contains(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_DEFAULT_GROUPID);
assertThat(conf.getHideTemplates()).isEmpty();
assertThat(conf.isAllowSnapshots()).isFalse();
}

/**
* Test loadTemplateSetConfigurations Method in ConfigurationFinder if valid properties found, to load these valid
* properties correctly.
*/
@Test
public void validConfigurationTest() {

Path validConfiguration = Paths
.get("src/test/resources/testdata/unittest/config/properties/validConfigProperties/config.properties");
TemplateSetConfiguration conf = ConfigurationFinder.loadTemplateSetConfigurations(validConfiguration);

assertThat(conf.getGroupIds()).containsSequence("devonfw-cobigen-bla", "abcd", "blablob",
ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_DEFAULT_GROUPID);
assertThat(conf.isAllowSnapshots()).isTrue();
assertThat(conf.getHideTemplates()).contains("com.devonfw(:test-artifact(:3.2.1-SNAPSHOT))");
}

/**
* Test loadTemplateSetConfigurations Method in ConfigurationFinder if file *.properties not found , to load the
* default values.
*
*/
@Test
public void invalidPathTest() {

Path invalidPath = Paths.get("path/which/does/not/exist");
TemplateSetConfiguration conf = ConfigurationFinder.loadTemplateSetConfigurations(invalidPath);

assertThat(conf.getGroupIds()).contains(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATE_SETS_DEFAULT_GROUPID);
assertThat(conf.getHideTemplates()).isEmpty();
assertThat(conf.isAllowSnapshots()).isFalse();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo=foo
bar=bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
template-sets.groupIds=devonfw-cobigen-bla,abcd,blablob
template-sets.allow-snapshots=true
template-sets.disable-default-lookup=false
template-sets.hide=com.devonfw(:test-artifact(:3.2.1-SNAPSHOT))
Loading

0 comments on commit a1ca29a

Please sign in to comment.