diff --git a/ccm-bundle-devel-wildfly/src/main/resources/log4j2.xml b/ccm-bundle-devel-wildfly/src/main/resources/log4j2.xml index 43c721572..11049a118 100644 --- a/ccm-bundle-devel-wildfly/src/main/resources/log4j2.xml +++ b/ccm-bundle-devel-wildfly/src/main/resources/log4j2.xml @@ -6,9 +6,13 @@ - + + + + diff --git a/ccm-core/src/main/java/org/libreccm/categorization/CategoryRepository.java b/ccm-core/src/main/java/org/libreccm/categorization/CategoryRepository.java index ee0d790a2..4f1526dfc 100644 --- a/ccm-core/src/main/java/org/libreccm/categorization/CategoryRepository.java +++ b/ccm-core/src/main/java/org/libreccm/categorization/CategoryRepository.java @@ -27,6 +27,8 @@ import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.persistence.NoResultException; import javax.persistence.TypedQuery; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; /** * @@ -35,6 +37,9 @@ import javax.persistence.TypedQuery; @RequestScoped public class CategoryRepository extends AbstractEntityRepository { + private static final Logger LOGGER = LogManager.getLogger( + CategoryRepository.class); + @Inject private DomainRepository domainRepo; @@ -56,7 +61,7 @@ public class CategoryRepository extends AbstractEntityRepository */ public List getTopLevelCategories() { final TypedQuery query = getEntityManager().createNamedQuery( - "Category.topLevelCategories", Category.class); + "Category.topLevelCategories", Category.class); return query.getResultList(); } @@ -69,14 +74,14 @@ public class CategoryRepository extends AbstractEntityRepository final String[] tokens = path.split(":"); if (tokens.length > 2) { throw new InvalidCategoryPathException( - "The provided path is invalid: More than one colon found. " - + "Valid path format: domainKey:path"); + "The provided path is invalid: More than one colon found. " + + "Valid path format: domainKey:path"); } if (tokens.length < 2) { throw new InvalidCategoryPathException( - "The provided path is invalid: No domain found in path. " - + "Valid path format: domainKey:path"); + "The provided path is invalid: No domain found in path. " + + "Valid path format: domainKey:path"); } final Domain domain; @@ -84,11 +89,11 @@ public class CategoryRepository extends AbstractEntityRepository domain = domainRepo.findByDomainKey(tokens[0]); } catch (NoResultException ex) { throw new InvalidCategoryPathException(String.format( - "No domain identified by the key '%s' found.", - tokens[0]), + "No domain identified by the key '%s' found.", + tokens[0]), ex); } - + return findByPath(domain, tokens[1]); } @@ -111,15 +116,20 @@ public class CategoryRepository extends AbstractEntityRepository normalizedPath.length()); } + LOGGER.debug(String.format( + "Trying to find category with path \"%s\" in " + + "domain \"%s\".", + normalizedPath, + domain.getDomainKey())); final String[] tokens = normalizedPath.split("/"); Category current = domain.getRoot(); for (String token : tokens) { final Optional result = current.getSubCategories() - .stream() - .filter((c) -> { - return c.getName().equals(token); - }) - .findFirst(); + .stream() + .filter((c) -> { + return c.getName().equals(token); + }) + .findFirst(); if (result.isPresent()) { current = result.get(); } else { @@ -135,7 +145,7 @@ public class CategoryRepository extends AbstractEntityRepository * subcategory or the an {@link Domain} as root category. * * @return A list of all orphaned categories. Normally this list should be - * empty. + * empty. */ public List getOrphanedCategories() { // TODO implement method diff --git a/ccm-core/src/main/java/org/libreccm/configuration/ConfigurationManager.java b/ccm-core/src/main/java/org/libreccm/configuration/ConfigurationManager.java index e82a0fc60..0b8e7c1c2 100644 --- a/ccm-core/src/main/java/org/libreccm/configuration/ConfigurationManager.java +++ b/ccm-core/src/main/java/org/libreccm/configuration/ConfigurationManager.java @@ -42,6 +42,8 @@ import java.math.BigDecimal; import java.util.Arrays; import java.util.List; import java.util.Optional; +import java.util.UUID; +import org.apache.logging.log4j.message.FormattedMessage; /** * Maps between configuration classes and the values stored in the registry. @@ -52,7 +54,7 @@ import java.util.Optional; public class ConfigurationManager { private static final Logger LOGGER = LogManager.getLogger( - ConfigurationManager.class); + ConfigurationManager.class); @Inject private CategoryManager categoryManager; @@ -69,11 +71,11 @@ public class ConfigurationManager { /** * Load all settings of the provided configuration class. * - * @param Type of the configuration class. + * @param Type of the configuration class. * @param confClass The configuration class. * * @return An instance of the configuration class with all settings set to - * the values stored in the registry. + * the values stored in the registry. */ public T findConfiguration(final Class confClass) { if (confClass == null) { @@ -82,9 +84,9 @@ public class ConfigurationManager { if (confClass.getAnnotation(Configuration.class) == null) { throw new IllegalArgumentException(String.format( - "Provided class \"%s\" is not annotated with \"%s\".", - confClass.getName(), - Configuration.class.getName())); + "Provided class \"%s\" is not annotated with \"%s\".", + confClass.getName(), + Configuration.class.getName())); } final String confName = confClass.getName(); @@ -97,13 +99,11 @@ public class ConfigurationManager { * registry. * * @param configuration The configuration to save. The class of the provided - * object must be annotation with - * {@link Configuration}. + * object must be annotation with {@link Configuration}. * * @throws IllegalArgumentException If the {@code configuration} parameter - * is {@code null} or if the class of the - * provided object is not annotation with - * {@link Configuration}. + * is {@code null} or if the class of the provided object is not annotation + * with {@link Configuration}. */ public void saveConfiguration(final Object configuration) { if (configuration == null) { @@ -112,15 +112,27 @@ public class ConfigurationManager { if (configuration.getClass().getAnnotation(Configuration.class) == null) { throw new IllegalArgumentException(String.format( - "The class \"%s\" of the provided object is not annotated " - + "with \"%s\".", - configuration.getClass().getName(), - Configuration.class.getName())); + "The class \"%s\" of the provided object is not annotated " + + "with \"%s\".", + configuration.getClass().getName(), + Configuration.class.getName())); } + LOGGER.debug(String.format("Saving configuration \"%s\"...", + configuration.getClass().getName())); final Field[] fields = configuration.getClass().getDeclaredFields(); for (final Field field : fields) { field.setAccessible(true); + + if (field.getAnnotation(Setting.class) == null) { + LOGGER.debug(String.format( + "Field \"%s\" of class \"%s\" is not " + + "a setting. Ignoring it.", + configuration.getClass().getName(), + field.getName())); + continue; + } + try { setSettingValue(configuration, getSettingName(field), @@ -128,16 +140,16 @@ public class ConfigurationManager { field.get(configuration)); } catch (IllegalAccessException ex) { LOGGER.error(String.format( - "Failed to write setting value for setting \"%s\" " - + "of configuration \"%s\"", - getSettingName(field), - configuration.getClass().getName()), + "Failed to write setting value for setting \"%s\" " + + "of configuration \"%s\"", + getSettingName(field), + configuration.getClass().getName()), ex); throw new IllegalStateException(String.format( - "Failed to write setting value for setting \"%s\" " - + "of configuration \"%s\"", - getSettingName(field), - configuration.getClass().getName()), + "Failed to write setting value for setting \"%s\" " + + "of configuration \"%s\"", + getSettingName(field), + configuration.getClass().getName()), ex); } } @@ -147,15 +159,15 @@ public class ConfigurationManager { * Finds an application instance specific configuration and loads it values * from the registry. * - * @param The type of the configuration. + * @param The type of the configuration. * @param confClass The configuration class. - * @param instance The application instance for which the settings are - * loaded. + * @param instance The application instance for which the settings are + * loaded. * * @return The configuration for the provided application instance. */ public T findConfiguration( - final Class confClass, final CcmApplication instance) { + final Class confClass, final CcmApplication instance) { if (confClass == null) { throw new IllegalArgumentException("confClass can't be null"); } @@ -166,9 +178,9 @@ public class ConfigurationManager { if (confClass.getAnnotation(Configuration.class) == null) { throw new IllegalArgumentException(String.format( - "Provided class \"%s\" is not annotated with \"%s\".", - confClass.getName(), - Configuration.class.getName())); + "Provided class \"%s\" is not annotated with \"%s\".", + confClass.getName(), + Configuration.class.getName())); } final String confName = String.format("%s.%s", @@ -182,8 +194,8 @@ public class ConfigurationManager { * Saves a application instance configuration. * * @param configuration The configuration to save. - * @param instance The application instance of which the configuration - * stores the settings. + * @param instance The application instance of which the configuration + * stores the settings. */ public void saveConfiguration(final ApplicationConfiguration configuration, final CcmApplication instance) { @@ -193,10 +205,10 @@ public class ConfigurationManager { if (configuration.getClass().getAnnotation(Configuration.class) == null) { throw new IllegalArgumentException(String.format( - "The class \"%s\" of the provided object is not annotated " - + "with \"%s\".", - configuration.getClass().getName(), - Configuration.class.getName())); + "The class \"%s\" of the provided object is not annotated " + + "with \"%s\".", + configuration.getClass().getName(), + Configuration.class.getName())); } if (instance == null) { @@ -214,16 +226,16 @@ public class ConfigurationManager { field.get(configuration)); } catch (IllegalAccessException ex) { LOGGER.error(String.format( - "Failed to write setting value for setting \"%s\" " - + "of configuration \"%s\"", - getSettingName(field), - configuration.getClass().getName()), + "Failed to write setting value for setting \"%s\" " + + "of configuration \"%s\"", + getSettingName(field), + configuration.getClass().getName()), ex); throw new IllegalStateException(String.format( - "Failed to write setting value for setting \"%s\" " - + "of configuration \"%s\"", - getSettingName(field), - configuration.getClass().getName()), + "Failed to write setting value for setting \"%s\" " + + "of configuration \"%s\"", + getSettingName(field), + configuration.getClass().getName()), ex); } } @@ -235,7 +247,7 @@ public class ConfigurationManager { * @param configuration The configuration for which the info is generated. * * @return a {@link ConfigurationInfo} instance describing the provided - * configuration. + * configuration. */ public ConfigurationInfo getConfigurationInfo(final Class configuration) { if (configuration == null) { @@ -244,14 +256,14 @@ public class ConfigurationManager { if (configuration.getAnnotation(Configuration.class) == null) { throw new IllegalArgumentException(String.format( - "The class \"%s\" of the provided object is not annotated " - + "with \"%s\".", - configuration.getClass().getName(), - Configuration.class.getName())); + "The class \"%s\" of the provided object is not annotated " + + "with \"%s\".", + configuration.getClass().getName(), + Configuration.class.getName())); } final Configuration annotation = configuration.getAnnotation( - Configuration.class); + Configuration.class); final ConfigurationInfo confInfo = new ConfigurationInfo(); confInfo.setName(configuration.getClass().getName()); @@ -274,9 +286,9 @@ public class ConfigurationManager { * Create a {@link SettingInfo} instance for a setting. * * @param configuration The configuration class to which the settings - * belongs. - * @param name The name of the setting for which the - * {@link SettingInfo} is generated. + * belongs. + * @param name The name of the setting for which the {@link SettingInfo} is + * generated. * * @return The {@link SettingInfo} for the provided configuration class. */ @@ -288,14 +300,14 @@ public class ConfigurationManager { if (configuration.getAnnotation(Configuration.class) == null) { throw new IllegalArgumentException(String.format( - "The class \"%s\" of the provided object is not annotated " - + "with \"%s\".", - configuration.getClass().getName(), - Configuration.class.getName())); + "The class \"%s\" of the provided object is not annotated " + + "with \"%s\".", + configuration.getClass().getName(), + Configuration.class.getName())); } final Configuration confAnnotation = configuration.getAnnotation( - Configuration.class); + Configuration.class); final String descBundle = confAnnotation.descBundle(); final Field field; @@ -303,10 +315,10 @@ public class ConfigurationManager { field = configuration.getDeclaredField(name); } catch (SecurityException | NoSuchFieldException ex) { LOGGER.warn(String.format( - "Failed to generate SettingInfo for field \"%s\" of " - + "configuration \"%s\". Ignoring field.", - configuration.getClass().getName(), - name), + "Failed to generate SettingInfo for field \"%s\" of " + + "configuration \"%s\". Ignoring field.", + configuration.getClass().getName(), + name), ex); return null; } @@ -318,7 +330,7 @@ public class ConfigurationManager { final Setting settingAnnotation = field.getAnnotation(Setting.class); final SettingInfo settingInfo = new SettingInfo(); if (settingAnnotation.name() == null - || settingAnnotation.name().isEmpty()) { + || settingAnnotation.name().isEmpty()) { settingInfo.setName(field.getName()); } else { settingInfo.setName(settingAnnotation.name()); @@ -331,7 +343,7 @@ public class ConfigurationManager { settingInfo.setDefaultValue(field.get(conf).toString()); } catch (InstantiationException | IllegalAccessException ex) { LOGGER.warn(String.format("Failed to create instance of \"%s\" to " - + "get default values.", + + "get default values.", configuration.getName()), ex); } @@ -347,19 +359,19 @@ public class ConfigurationManager { /** * A low level method for finding a setting in the registry. * - * @param Type of the value of the setting - * @param name The fully qualified name of the setting. + * @param Type of the value of the setting + * @param name The fully qualified name of the setting. * @param clazz The class of the setting. * * @return The requested setting if it exists in the registry, {@code null} - * otherwise. + * otherwise. */ public AbstractSetting findSetting(final String name, final Class clazz) { LOGGER.debug(String.format( - "Trying to find setting \"%s\" of type \"%s\"", - name, - clazz.getName())); + "Trying to find setting \"%s\" of type \"%s\"", + name, + clazz.getName())); final String[] tokens = name.split("\\."); LOGGER.debug(String.format("Setting name \"%s\" has %d tokens.", name, @@ -372,29 +384,29 @@ public class ConfigurationManager { categoryPath)); final Domain registry = domainRepository - .findByDomainKey(REGISTRY_DOMAIN); + .findByDomainKey(REGISTRY_DOMAIN); final Category category = categoryRepository.findByPath(registry, categoryPath); if (category == null) { LOGGER.warn(String.format(String.format( - "Category \"%s\" for setting \"%s\" not found.", - categoryPath, - name))); + "Category \"%s\" for setting \"%s\" not found.", + categoryPath, + name))); return null; } LOGGER.debug(String.format("Category has %d objects. Filtering.", category.getObjects().size())); final Optional result = category - .getObjects() - .stream() - .filter((Categorization c) - -> c.getCategorizedObject() instanceof AbstractSetting) - .filter((Categorization c) - -> ((AbstractSetting) c.getCategorizedObject()) - .getName() - .equals(tokens[tokens.length - 1])) - .findFirst(); + .getObjects() + .stream() + .filter((Categorization c) + -> c.getCategorizedObject() instanceof AbstractSetting) + .filter((Categorization c) + -> ((AbstractSetting) c.getCategorizedObject()) + .getName() + .equals(tokens[tokens.length - 1])) + .findFirst(); if (result.isPresent()) { final CcmObject object = result.get().getCategorizedObject(); @@ -403,20 +415,20 @@ public class ConfigurationManager { if (clazz.isInstance(entry.getValue())) { @SuppressWarnings("unchecked") final AbstractSetting resultEntry - = (AbstractSetting) entry; + = (AbstractSetting) entry; return resultEntry; } else { LOGGER.warn(String.format("Setting \"%s\" found but is not of " - + "the requested type \"%s\".", + + "the requested type \"%s\".", name, clazz.getName())); return null; } } else { LOGGER.warn(String.format( - "Setting \"%s\" was not found in category \"%s\".", - name, - categoryPath)); + "Setting \"%s\" was not found in category \"%s\".", + name, + categoryPath)); return null; } } @@ -443,9 +455,12 @@ public class ConfigurationManager { * @param field The setting field. * * @return The name of the field or if the {@link Setting} annotation of the - * field has a name value, the value of that field. + * field has a name value, the value of that field. */ private String getSettingName(final Field field) { + LOGGER.debug(String.format("Trying to get setting name from field: " + + "\"%s\"", + field.getName())); final Setting annotation = field.getAnnotation(Setting.class); if (annotation.name() == null || annotation.name().isEmpty()) { @@ -458,17 +473,17 @@ public class ConfigurationManager { /** * Create a setting instance of a specific value type. * - * @param Type variable. + * @param Type variable. * @param valueType The type of the value of the setting to create. * * @return An setting instance of the provided value type. * * @throws IllegalArgumentException If there is not setting type for the - * provided value type. + * provided value type. */ @SuppressWarnings("unchecked") private AbstractSetting createSettingForValueType( - final Class valueType) { + final Class valueType) { final String valueTypeName = valueType.getName(); if (BigDecimal.class.getName().equals(valueTypeName)) { @@ -487,54 +502,78 @@ public class ConfigurationManager { return (AbstractSetting) new StringSetting(); } else { throw new IllegalArgumentException(String.format( - "No setting type for value type \"s\".", valueTypeName)); + "No setting type for value type \"s\".", valueTypeName)); } } /** * Sets a value on a setting in the registry. * - * @param The value type of the setting. + * @param The value type of the setting. * @param configuration The configuration to which the settings belongs. - * @param settingName The name of the setting. - * @param valueType The type of the value of the setting. - * @param value The value to set. + * @param settingName The name of the setting. + * @param valueType The type of the value of the setting. + * @param value The value to set. */ private void setSettingValue(final Object configuration, final String settingName, final Class valueType, final Object value) { final String settingPath = String.format( - "%s.%s", - configuration.getClass().getName(), - settingName); + "%s.%s", + configuration.getClass().getName(), + settingName); + LOGGER.debug(String.format("Saving setting \"%s\"...", settingPath)); AbstractSetting setting = findSetting(settingPath, valueType); if (setting == null) { + LOGGER.debug(String.format("Setting \"%s\" does not yet exist in " + + "database. Creating new setting.", + settingPath)); setting = createSettingForValueType(valueType); setting.setName(settingName); final Domain registry = domainRepository - .findByDomainKey(REGISTRY_DOMAIN); - final Category category = categoryRepository - .findByPath(registry, configuration.getClass().getName()); + .findByDomainKey(REGISTRY_DOMAIN); + Category category = categoryRepository + .findByPath(registry, configuration.getClass().getName()); + if (category == null) { + final String[] tokens = configuration.getClass().getName(). + split("\\."); + final StringBuilder categoryPath = new StringBuilder( + configuration.getClass().getName().length()); + for (String token : tokens) { + if (categoryPath.length() > 0) { + categoryPath.append('.'); + } + categoryPath.append(token); + category = createCategoryIfNotExists(categoryPath.toString()); + } + } categoryManager.addObjectToCategory(setting, category); } + LOGGER.debug(String.format("New value of setting \"%s\" is: \"%s\"", + settingPath, + value.toString())); @SuppressWarnings("unchecked") final T settingValue = (T) value; setting.setValue(settingValue); + LOGGER.debug(String.format("Value of setting \"%s\" is now: \"%s\"", + settingPath, + setting.getValue().toString())); + LOGGER.debug("Saving changed setting to DB..."); entityManager.merge(setting); } /** * Sets the value of a setting of application instance configuration. * - * @param The value type of the setting. + * @param The value type of the setting. * @param configuration The configuration to which the settings belongs. - * @param instance The application instance to which - * @param settingName The name of the setting. - * @param valueType The type of the value of the setting. - * @param value The value to set. + * @param instance The application instance to which + * @param settingName The name of the setting. + * @param valueType The type of the value of the setting. + * @param value The value to set. */ private void setSettingValue(final Object configuration, final CcmApplication instance, @@ -542,18 +581,18 @@ public class ConfigurationManager { final Class valueType, final Object value) { final String settingPath = String.format( - "%s.%s.%s", - configuration.getClass().getName(), - instance.getPrimaryUrl(), - settingName); + "%s.%s.%s", + configuration.getClass().getName(), + instance.getPrimaryUrl(), + settingName); AbstractSetting setting = findSetting(settingPath, valueType); if (setting == null) { setting = createSettingForValueType(valueType); setting.setName(settingName); final Domain registry = domainRepository - .findByDomainKey(REGISTRY_DOMAIN); + .findByDomainKey(REGISTRY_DOMAIN); final Category category = categoryRepository - .findByPath(registry, configuration.getClass().getName()); + .findByPath(registry, configuration.getClass().getName()); categoryManager.addObjectToCategory(setting, category); } @@ -567,23 +606,21 @@ public class ConfigurationManager { /** * Helper method for loading a configuration from the registry. * - * @param The type of the configuration. - * @param confName The fully qualified name of the configuration in the - * registry. For normal configuration this is the fully - * qualified name of the configuration class. For - * application instance configurations this is the fully - * qualified name of the configuration class joined with - * the primary URL of the application instance, separated - * with a dot. + * @param The type of the configuration. + * @param confName The fully qualified name of the configuration in the + * registry. For normal configuration this is the fully qualified name of + * the configuration class. For application instance configurations this is + * the fully qualified name of the configuration class joined with the + * primary URL of the application instance, separated with a dot. * @param confClass The configuration class. * * @return An instance of the configuration class with all setting fields - * set to the values stored in the registry. + * set to the values stored in the registry. */ private T findConfiguration(final String confName, final Class confClass) { final Domain registry = domainRepository - .findByDomainKey(REGISTRY_DOMAIN); + .findByDomainKey(REGISTRY_DOMAIN); final Category category = categoryRepository.findByPath(registry, confName); @@ -596,8 +633,8 @@ public class ConfigurationManager { conf = confClass.newInstance(); } catch (InstantiationException | IllegalAccessException ex) { LOGGER.warn(String.format( - "Failed to instantiate configuration \"%s\".", - confClass.getName()), + "Failed to instantiate configuration \"%s\".", + confClass.getName()), ex); return null; } @@ -617,15 +654,16 @@ public class ConfigurationManager { settingType); if (setting != null) { try { - LOGGER.debug("Setting \"%s\" found. Value: %s", - settingPath, - setting.getValue().toString()); + LOGGER.debug(String. + format("Setting \"%s\" found. Value: %s", + settingPath, + setting.getValue().toString())); field.set(conf, setting.getValue()); } catch (IllegalAccessException ex) { LOGGER.warn(String.format( - "Failed to set value of configuration class \"%s\". " + "Failed to set value of configuration class \"%s\". " + "Ignoring.", - confClass.getName()), + confClass.getName()), ex); } } @@ -634,4 +672,61 @@ public class ConfigurationManager { return conf; } + private Category createCategoryIfNotExists(final String categoryPath) { + LOGGER.debug(String.format("Checking if category \"%s\" exists. If not " + + "the category will be created.", + categoryPath)); + + final Domain registry = domainRepository. + findByDomainKey(REGISTRY_DOMAIN); + final Category root = registry.getRoot(); + final String[] tokens = categoryPath.split("\\."); + + Category category = categoryRepository.findByPath(registry, + categoryPath); + if (category == null) { + LOGGER.debug(String.format( + "Category \"%s\" was not found. Creating category.", + categoryPath)); + category = new Category(); + category.setName(tokens[tokens.length - 1]); + category.setUniqueId(UUID.randomUUID().toString()); + category.setEnabled(true); + category.setVisible(true); + category.setAbstractCategory(false); + if (tokens.length > 1) { + final StringBuilder parentPath = new StringBuilder(); + for (int i = 0; i < tokens.length - 1; i++) { + if (i > 0) { + parentPath.append('.'); + } + parentPath.append(tokens); + } + final Category parent = categoryRepository.findByPath( + registry, + parentPath.toString()); + if (parent == null) { + throw new IllegalStateException(String.format( + "Parent category \"%s\" of for new category \"%s\" does" + + "not exist, but should. Can't continue.", + parentPath.toString(), + categoryPath)); + } + categoryManager.addSubCategoryToCategory(category, parent); + LOGGER.debug(new FormattedMessage( + "Created category \"%s\" as child of category \"%s\".", + categoryPath, + parent.getName())); + } else { + categoryManager.addSubCategoryToCategory(category, root); + LOGGER.debug(new FormattedMessage( + "Created category \"%s\" as child of the registry root " + + "category.", + categoryPath)); + } + } + + return category; + } + } diff --git a/ccm-core/src/test/java/com/example/TestConfiguration.java b/ccm-core/src/test/java/com/example/TestConfiguration.java new file mode 100644 index 000000000..de73aeb5a --- /dev/null +++ b/ccm-core/src/test/java/com/example/TestConfiguration.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package com.example; + +import org.libreccm.configuration.Configuration; +import org.libreccm.configuration.Setting; + +/** + * + * @author Jens Pelzetter + */ +@Configuration +public class TestConfiguration { + + @Setting + private Boolean enabled = false; + + @Setting + private Long itemsPerPage = 40L; + + public Boolean getEnabled() { + return enabled; + } + + public void setEnabled(final Boolean enabled) { + this.enabled = enabled; + } + + public Long getItemsPerPage() { + return itemsPerPage; + } + + public void setItemsPerPage(final Long itemsPerPage) { + this.itemsPerPage = itemsPerPage; + } +} diff --git a/ccm-core/src/test/java/org/libreccm/configuration/ConfigurationManagerTest.java b/ccm-core/src/test/java/org/libreccm/configuration/ConfigurationManagerTest.java index a1d6f47b2..6f36e6cf0 100644 --- a/ccm-core/src/test/java/org/libreccm/configuration/ConfigurationManagerTest.java +++ b/ccm-core/src/test/java/org/libreccm/configuration/ConfigurationManagerTest.java @@ -18,6 +18,7 @@ */ package org.libreccm.configuration; +import com.example.TestConfiguration; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.junit.InSequence; @@ -52,6 +53,7 @@ import java.io.File; import java.math.BigDecimal; import javax.inject.Inject; +import org.jboss.arquillian.persistence.ShouldMatchDataSet; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; @@ -92,10 +94,10 @@ public class ConfigurationManagerTest { @Deployment public static WebArchive createDeployment() { final PomEquippedResolveStage pom = Maven - .resolver() - .loadPomFromFile("pom.xml"); + .resolver() + .loadPomFromFile("pom.xml"); final PomEquippedResolveStage dependencies = pom - .importCompileAndRuntimeDependencies(); + .importCompileAndRuntimeDependencies(); final File[] libs = dependencies.resolve().withTransitivity().asFile(); for (File lib : libs) { @@ -104,28 +106,29 @@ public class ConfigurationManagerTest { } return ShrinkWrap - .create(WebArchive.class, - "LibreCCM-org.libreccm.categorization.CategoryManagerTest.war") - .addPackage(CcmObject.class.getPackage()) - .addPackage(Permission.class.getPackage()) - .addPackage(CcmApplication.class.getPackage()) - .addPackage(Categorization.class.getPackage()) - .addPackage(Configuration.class.getPackage()) - .addPackage(LocalizedString.class.getPackage()) - .addPackage(Workflow.class.getPackage()) - .addPackage(EntityManagerProducer.class.getPackage()) - .addPackage(MimeTypeConverter.class.getPackage()) - .addPackage(EqualsVerifier.class.getPackage()) - .addPackage(IntegrationTest.class.getPackage()) - .addAsLibraries(libs) - .addAsResource("test-persistence.xml", - "META-INF/persistence.xml") - .addAsResource( - "configs/org/libreccm/configuration/ConfigurationManagerTest/" - + "log4j2.xml", - "log4j2.xml") - .addAsWebInfResource("test-web.xml", "WEB-INF/web.xml") - .addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml"); + .create(WebArchive.class, + "LibreCCM-org.libreccm.categorization.CategoryManagerTest.war"). + addPackage(CcmObject.class.getPackage()) + .addPackage(Permission.class.getPackage()) + .addPackage(CcmApplication.class.getPackage()) + .addPackage(Categorization.class.getPackage()) + .addPackage(Configuration.class.getPackage()) + .addPackage(LocalizedString.class.getPackage()) + .addPackage(Workflow.class.getPackage()) + .addPackage(EntityManagerProducer.class.getPackage()) + .addPackage(MimeTypeConverter.class.getPackage()) + .addPackage(EqualsVerifier.class.getPackage()) + .addPackage(IntegrationTest.class.getPackage()) + .addPackage(TestConfiguration.class.getPackage()) + .addAsLibraries(libs) + .addAsResource("test-persistence.xml", + "META-INF/persistence.xml") + .addAsResource( + "configs/org/libreccm/configuration/ConfigurationManagerTest/" + + "log4j2.xml", + "log4j2.xml") + .addAsWebInfResource("test-web.xml", "WEB-INF/web.xml") + .addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml"); } @Test @@ -136,7 +139,7 @@ public class ConfigurationManagerTest { @Test @UsingDataSet( - "datasets/org/libreccm/configuration/ConfigurationManagerTest/data.yml") + "datasets/org/libreccm/configuration/ConfigurationManagerTest/data.yml") @InSequence(2) public void datasetOnly() { System.out.println("Dataset loaded successfully."); @@ -144,12 +147,13 @@ public class ConfigurationManagerTest { @Test @UsingDataSet( - "datasets/org/libreccm/configuration/ConfigurationManagerTest/data.yml") + "datasets/org/libreccm/configuration/ConfigurationManagerTest/data.yml") @InSequence(1100) public void loadConfiguration() { final ExampleConfiguration configuration = configurationManager - .findConfiguration(ExampleConfiguration.class); + .findConfiguration(ExampleConfiguration.class); + assertThat(configuration, is(not(nullValue()))); assertThat(configuration.getPrice(), is(equalTo(new BigDecimal("98.99")))); assertThat(configuration.isEnabled(), is(true)); @@ -159,4 +163,43 @@ public class ConfigurationManagerTest { is(equalTo("http://www.example.org"))); } + @Test + @UsingDataSet( + "datasets/org/libreccm/configuration/ConfigurationManagerTest/data.yml") + @ShouldMatchDataSet( + "datasets/org/libreccm/configuration/ConfigurationManagerTest/" + + "after-save-changed.yml") + @InSequence(1200) + public void saveConfiguration() { + final ExampleConfiguration configuration = configurationManager + .findConfiguration(ExampleConfiguration.class); + + configuration.setPrice(new BigDecimal("109.99")); + configuration.setItemsPerPage(30L); + + configurationManager.saveConfiguration(configuration); + } + + @Test + @UsingDataSet( + "datasets/org/libreccm/configuration/ConfigurationManagerTest/data.yml") + @InSequence(2100) + public void loadNewConfiguration() { + final TestConfiguration configuration = configurationManager + .findConfiguration(TestConfiguration.class); + + assertThat(configuration, is(nullValue())); + } + + @Test + @UsingDataSet( + "datasets/org/libreccm/configuration/ConfigurationManagerTest/data.yml") + @ShouldMatchDataSet( + "datasets/org/libreccm/configuration/ConfigurationManagerTest/" + + "after-save-new.yml") + @InSequence(2200) + public void saveNewConfiguration() { + configurationManager.saveConfiguration(new TestConfiguration()); + } + } diff --git a/ccm-core/src/test/java/org/libreccm/configuration/DatasetsTest.java b/ccm-core/src/test/java/org/libreccm/configuration/DatasetsTest.java index a39151a7d..7b6652825 100644 --- a/ccm-core/src/test/java/org/libreccm/configuration/DatasetsTest.java +++ b/ccm-core/src/test/java/org/libreccm/configuration/DatasetsTest.java @@ -45,6 +45,8 @@ public class DatasetsTest extends DatasetsVerifier { @Parameterized.Parameters(name = "Dataset {0}") public static Collection data() { return Arrays.asList(new String[]{ + "/datasets/org/libreccm/configuration/ConfigurationManagerTest/after-save-changed.yml", + "/datasets/org/libreccm/configuration/ConfigurationManagerTest/after-save-new.yml", "/datasets/org/libreccm/configuration/ConfigurationManagerTest/data.yml"}); } diff --git a/ccm-core/src/test/java/org/libreccm/configuration/ExampleConfiguration.java b/ccm-core/src/test/java/org/libreccm/configuration/ExampleConfiguration.java index 960f30bd9..221b2bfaf 100644 --- a/ccm-core/src/test/java/org/libreccm/configuration/ExampleConfiguration.java +++ b/ccm-core/src/test/java/org/libreccm/configuration/ExampleConfiguration.java @@ -81,7 +81,4 @@ public class ExampleConfiguration { public void setHelpUrl(final String helpUrl) { this.helpUrl = helpUrl; } - - - } diff --git a/ccm-core/src/test/resources/configs/org/libreccm/configuration/ConfigurationManagerTest/log4j2.xml b/ccm-core/src/test/resources/configs/org/libreccm/configuration/ConfigurationManagerTest/log4j2.xml index 42a5e199f..16478e075 100644 --- a/ccm-core/src/test/resources/configs/org/libreccm/configuration/ConfigurationManagerTest/log4j2.xml +++ b/ccm-core/src/test/resources/configs/org/libreccm/configuration/ConfigurationManagerTest/log4j2.xml @@ -13,5 +13,9 @@ level="debug"> + + + \ No newline at end of file diff --git a/ccm-core/src/test/resources/datasets/org/libreccm/configuration/ConfigurationManagerTest/after-save-changed.yml b/ccm-core/src/test/resources/datasets/org/libreccm/configuration/ConfigurationManagerTest/after-save-changed.yml new file mode 100644 index 000000000..8856b55e4 --- /dev/null +++ b/ccm-core/src/test/resources/datasets/org/libreccm/configuration/ConfigurationManagerTest/after-save-changed.yml @@ -0,0 +1,135 @@ +ccm_core.ccm_objects: + - object_id: -1000 + display_name: registry + - object_id: -2000 + display_name: registry_root + - object_id: -2100 + display_name: org + - object_id: -2200 + display_name: libreccm + - object_id: -2300 + display_name: configuration + - object_id: -2400 + display_name: ExampleConfiguration + - object_id: -3100 + display_name: price + - object_id: -3200 + display_name: enabled + - object_id: -3300 + display_name: minTemperature + - object_id: -3400 + display_name: itemsPerPage + - object_id: -3500 + display_name: helpUri + +ccm_core.categories: + - object_id: -2000 + unique_id: bb93a964-bf66-424c-a22d-074d001db3b8 + name: registry-root + enabled: true + visible: true + abstract_category: false + category_order: 0 + - object_id: -2100 + unique_id: 62c22973-a078-47bc-8267-bef879c7566e + name: org + enabled: true + visible: true + abstract_category: false + parent_category_id: -2000 + category_order: 1 + - object_id: -2200 + unique_id: a8fbf310-7cb9-47dd-81d5-a16b80e96446 + name: libreccm + enabled: true + visible: true + abstract_category: false + parent_category_id: -2100 + category_order: 1 + - object_id: -2300 + unique_id: 61c30c73-857a-49ff-8272-c9fb038d3e35 + name: configuration + enabled: true + visible: true + abstract_category: false + parent_category_id: -2200 + category_order: 1 + - object_id: -2400 + unique_id: bf5d295c-6ad3-4484-a1e6-5641cea037b3 + name: ExampleConfiguration + enabled: true + visible: true + abstract_category: false + parent_category_id: -2300 + category_order: 1 + +ccm_core.category_domains: + - object_id: -1000 + domain_key: registry + root_category_id: -2000 + version: 1.0 + +ccm_core.categorizations: + - categorization_id: -10100 + category_id: -2400 + object_id: -3100 + category_order: 1 + object_order: 1 + category_index: false + - categorization_id: -10200 + category_id: -2400 + object_id: -3200 + category_order: 1 + object_order: 2 + category_index: false + - categorization_id: -10300 + category_id: -2400 + object_id: -3300 + category_order: 1 + object_order: 3 + category_index: false + - categorization_id: -10400 + category_id: -2400 + object_id: -3400 + category_order: 1 + object_order: 4 + category_index: false + - categorization_id: -10500 + category_id: -2400 + object_id: -3500 + category_order: 1 + object_order: 5 + category_index: false + +ccm_core.settings: + - object_id: -3100 + name: price + - object_id: -3200 + name: enabled + - object_id: -3300 + name: minTemperature + - object_id: -3400 + name: itemsPerPage + - object_id: -3500 + name: helpUrl + +ccm_core.settings_big_decimal: + - object_id: -3100 + setting_value: 109.99 + +ccm_core.settings_boolean: + - object_id: -3200 + setting_value: true + +ccm_core.settings_double: + - object_id: -3300 + setting_value: 23.5 + +ccm_core.settings_long: + - object_id: -3400 + setting_value: 30 + +ccm_core.settings_string: + - object_id: -3500 + setting_value: http://www.example.org + diff --git a/ccm-core/src/test/resources/datasets/org/libreccm/configuration/ConfigurationManagerTest/after-save-new.yml b/ccm-core/src/test/resources/datasets/org/libreccm/configuration/ConfigurationManagerTest/after-save-new.yml new file mode 100644 index 000000000..b16c65fea --- /dev/null +++ b/ccm-core/src/test/resources/datasets/org/libreccm/configuration/ConfigurationManagerTest/after-save-new.yml @@ -0,0 +1,177 @@ +ccm_core.ccm_objects: + - object_id: -1000 + display_name: registry + - object_id: -2000 + display_name: registry_root + - object_id: -2100 + display_name: org + - object_id: -2200 + display_name: libreccm + - object_id: -2300 + display_name: configuration + - object_id: -2400 + display_name: ExampleConfiguration + - object_id: -3100 + display_name: price + - object_id: -3200 + display_name: enabled + - object_id: -3300 + display_name: minTemperature + - object_id: -3400 + display_name: itemsPerPage + - object_id: -3500 + display_name: helpUri + - object_id: -2500 + display_name: com + - object_id: -2600 + display_name: example + - object_id: -2700 + display_name: TestConfiguration + - object_id: -3600 + display_name: enabled + - object_id: -3700 + display_name: itemsPerPage + +ccm_core.categories: + - object_id: -2000 + unique_id: bb93a964-bf66-424c-a22d-074d001db3b8 + name: registry-root + enabled: true + visible: true + abstract_category: false + category_order: 0 + - object_id: -2100 + unique_id: 62c22973-a078-47bc-8267-bef879c7566e + name: org + enabled: true + visible: true + abstract_category: false + parent_category_id: -2000 + category_order: 1 + - object_id: -2200 + unique_id: a8fbf310-7cb9-47dd-81d5-a16b80e96446 + name: libreccm + enabled: true + visible: true + abstract_category: false + parent_category_id: -2100 + category_order: 1 + - object_id: -2300 + unique_id: 61c30c73-857a-49ff-8272-c9fb038d3e35 + name: configuration + enabled: true + visible: true + abstract_category: false + parent_category_id: -2200 + category_order: 1 + - object_id: -2400 + unique_id: bf5d295c-6ad3-4484-a1e6-5641cea037b3 + name: ExampleConfiguration + enabled: true + visible: true + abstract_category: false + parent_category_id: -2300 + category_order: 1 + - object_id: -2500 + unique_id: 36223799-5df7-4875-8191-f1ced0965237 + name: com + enabled: true + visible: true + abstract_category: false + parent_category_id: -2000 + category_order: 1 + - object_id: -2600 + unique_id: 22f6f7c6-2ca1-457b-9b3f-185a2c6f39be + name: example + enabled: true + visible: true + abstract_category: false + parent_category_id: -2500 + category_order: 1 + - object_id: -2700 + unique_id: af6c0e93-d60b-4c5f-8fe4-5f82a7f8f923 + name: TestConfiguration + enabled: true + visible: true + abstract_category: false + parent_category_id: -2600 + category_order: 1 + +ccm_core.category_domains: + - object_id: -1000 + domain_key: registry + root_category_id: -2000 + version: 1.0 + +ccm_core.categorizations: + - categorization_id: -10100 + category_id: -2400 + object_id: -3100 + category_order: 1 + object_order: 1 + category_index: false + - categorization_id: -10200 + category_id: -2400 + object_id: -3200 + category_order: 1 + object_order: 2 + category_index: false + - categorization_id: -10300 + category_id: -2400 + object_id: -3300 + category_order: 1 + object_order: 3 + category_index: false + - categorization_id: -10400 + category_id: -2400 + object_id: -3400 + category_order: 1 + object_order: 4 + category_index: false + - categorization_id: -10500 + category_id: -2400 + object_id: -3500 + category_order: 1 + object_order: 5 + category_index: false + +ccm_core.settings: + - object_id: -3100 + name: price + - object_id: -3200 + name: enabled + - object_id: -3300 + name: minTemperature + - object_id: -3400 + name: itemsPerPage + - object_id: -3500 + name: helpUrl + - object_id: -3600 + name: enabled + - object_id: -3700 + name: itemsPerPage + +ccm_core.settings_big_decimal: + - object_id: -3100 + setting_value: 98.99 + +ccm_core.settings_boolean: + - object_id: -3200 + setting_value: true + - object_id: -3600 + setting_value: false + +ccm_core.settings_double: + - object_id: -3300 + setting_value: 23.5 + +ccm_core.settings_long: + - object_id: -3400 + setting_value: 20 + - object_id: -3700 + setting_value: 40 + +ccm_core.settings_string: + - object_id: -3500 + setting_value: http://www.example.org +