CCM NG: More test cases for the new database driven category system

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3775 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
jensp 2015-12-17 14:07:40 +00:00
parent 06e342af0c
commit 4b5a227772
10 changed files with 696 additions and 177 deletions

View File

@ -6,9 +6,13 @@
</Console> </Console>
</Appenders> </Appenders>
<Loggers> <Loggers>
<Root level="error"> <Root level="info">
<AppenderRef ref="Console"/> <AppenderRef ref="Console"/>
</Root> </Root>
<Logger name="com.arsdigita.packaging.Config"
level="debug">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="com.arsdigita.web.CCMDispatcherServlet" <Logger name="com.arsdigita.web.CCMDispatcherServlet"
level="debug"> level="debug">
<AppenderRef ref="Console"/> <AppenderRef ref="Console"/>

View File

@ -27,6 +27,8 @@ import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
import javax.persistence.TypedQuery; 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 @RequestScoped
public class CategoryRepository extends AbstractEntityRepository<Long, Category> { public class CategoryRepository extends AbstractEntityRepository<Long, Category> {
private static final Logger LOGGER = LogManager.getLogger(
CategoryRepository.class);
@Inject @Inject
private DomainRepository domainRepo; private DomainRepository domainRepo;
@ -111,6 +116,11 @@ public class CategoryRepository extends AbstractEntityRepository<Long, Category>
normalizedPath.length()); normalizedPath.length());
} }
LOGGER.debug(String.format(
"Trying to find category with path \"%s\" in "
+ "domain \"%s\".",
normalizedPath,
domain.getDomainKey()));
final String[] tokens = normalizedPath.split("/"); final String[] tokens = normalizedPath.split("/");
Category current = domain.getRoot(); Category current = domain.getRoot();
for (String token : tokens) { for (String token : tokens) {

View File

@ -42,6 +42,8 @@ import java.math.BigDecimal;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; 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. * Maps between configuration classes and the values stored in the registry.
@ -97,13 +99,11 @@ public class ConfigurationManager {
* registry. * registry.
* *
* @param configuration The configuration to save. The class of the provided * @param configuration The configuration to save. The class of the provided
* object must be annotation with * object must be annotation with {@link Configuration}.
* {@link Configuration}.
* *
* @throws IllegalArgumentException If the {@code configuration} parameter * @throws IllegalArgumentException If the {@code configuration} parameter
* is {@code null} or if the class of the * is {@code null} or if the class of the provided object is not annotation
* provided object is not annotation with * with {@link Configuration}.
* {@link Configuration}.
*/ */
public void saveConfiguration(final Object configuration) { public void saveConfiguration(final Object configuration) {
if (configuration == null) { if (configuration == null) {
@ -118,9 +118,21 @@ public class ConfigurationManager {
Configuration.class.getName())); Configuration.class.getName()));
} }
LOGGER.debug(String.format("Saving configuration \"%s\"...",
configuration.getClass().getName()));
final Field[] fields = configuration.getClass().getDeclaredFields(); final Field[] fields = configuration.getClass().getDeclaredFields();
for (final Field field : fields) { for (final Field field : fields) {
field.setAccessible(true); 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 { try {
setSettingValue(configuration, setSettingValue(configuration,
getSettingName(field), getSettingName(field),
@ -275,8 +287,8 @@ public class ConfigurationManager {
* *
* @param configuration The configuration class to which the settings * @param configuration The configuration class to which the settings
* belongs. * belongs.
* @param name The name of the setting for which the * @param name The name of the setting for which the {@link SettingInfo} is
* {@link SettingInfo} is generated. * generated.
* *
* @return The {@link SettingInfo} for the provided configuration class. * @return The {@link SettingInfo} for the provided configuration class.
*/ */
@ -446,6 +458,9 @@ public class ConfigurationManager {
* 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) { 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); final Setting annotation = field.getAnnotation(Setting.class);
if (annotation.name() == null || annotation.name().isEmpty()) { if (annotation.name() == null || annotation.name().isEmpty()) {
@ -508,21 +523,45 @@ public class ConfigurationManager {
"%s.%s", "%s.%s",
configuration.getClass().getName(), configuration.getClass().getName(),
settingName); settingName);
LOGGER.debug(String.format("Saving setting \"%s\"...", settingPath));
AbstractSetting<T> setting = findSetting(settingPath, valueType); AbstractSetting<T> setting = findSetting(settingPath, valueType);
if (setting == null) { if (setting == null) {
LOGGER.debug(String.format("Setting \"%s\" does not yet exist in "
+ "database. Creating new setting.",
settingPath));
setting = createSettingForValueType(valueType); setting = createSettingForValueType(valueType);
setting.setName(settingName); setting.setName(settingName);
final Domain registry = domainRepository final Domain registry = domainRepository
.findByDomainKey(REGISTRY_DOMAIN); .findByDomainKey(REGISTRY_DOMAIN);
final Category category = categoryRepository Category category = categoryRepository
.findByPath(registry, configuration.getClass().getName()); .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); categoryManager.addObjectToCategory(setting, category);
} }
LOGGER.debug(String.format("New value of setting \"%s\" is: \"%s\"",
settingPath,
value.toString()));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final T settingValue = (T) value; final T settingValue = (T) value;
setting.setValue(settingValue); 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); entityManager.merge(setting);
} }
@ -569,12 +608,10 @@ public class ConfigurationManager {
* *
* @param <T> The type of the configuration. * @param <T> The type of the configuration.
* @param confName The fully qualified name of the configuration in the * @param confName The fully qualified name of the configuration in the
* registry. For normal configuration this is the fully * registry. For normal configuration this is the fully qualified name of
* qualified name of the configuration class. For * the configuration class. For application instance configurations this is
* application instance configurations this is the fully * the fully qualified name of the configuration class joined with the
* qualified name of the configuration class joined with * primary URL of the application instance, separated with a dot.
* the primary URL of the application instance, separated
* with a dot.
* @param confClass The configuration class. * @param confClass The configuration class.
* *
* @return An instance of the configuration class with all setting fields * @return An instance of the configuration class with all setting fields
@ -617,9 +654,10 @@ public class ConfigurationManager {
settingType); settingType);
if (setting != null) { if (setting != null) {
try { try {
LOGGER.debug("Setting \"%s\" found. Value: %s", LOGGER.debug(String.
format("Setting \"%s\" found. Value: %s",
settingPath, settingPath,
setting.getValue().toString()); setting.getValue().toString()));
field.set(conf, setting.getValue()); field.set(conf, setting.getValue());
} catch (IllegalAccessException ex) { } catch (IllegalAccessException ex) {
LOGGER.warn(String.format( LOGGER.warn(String.format(
@ -634,4 +672,61 @@ public class ConfigurationManager {
return conf; 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;
}
} }

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@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;
}
}

View File

@ -18,6 +18,7 @@
*/ */
package org.libreccm.configuration; package org.libreccm.configuration;
import com.example.TestConfiguration;
import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.junit.InSequence; import org.jboss.arquillian.junit.InSequence;
@ -52,6 +53,7 @@ import java.io.File;
import java.math.BigDecimal; import java.math.BigDecimal;
import javax.inject.Inject; import javax.inject.Inject;
import org.jboss.arquillian.persistence.ShouldMatchDataSet;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -105,8 +107,8 @@ public class ConfigurationManagerTest {
return ShrinkWrap return ShrinkWrap
.create(WebArchive.class, .create(WebArchive.class,
"LibreCCM-org.libreccm.categorization.CategoryManagerTest.war") "LibreCCM-org.libreccm.categorization.CategoryManagerTest.war").
.addPackage(CcmObject.class.getPackage()) addPackage(CcmObject.class.getPackage())
.addPackage(Permission.class.getPackage()) .addPackage(Permission.class.getPackage())
.addPackage(CcmApplication.class.getPackage()) .addPackage(CcmApplication.class.getPackage())
.addPackage(Categorization.class.getPackage()) .addPackage(Categorization.class.getPackage())
@ -117,6 +119,7 @@ public class ConfigurationManagerTest {
.addPackage(MimeTypeConverter.class.getPackage()) .addPackage(MimeTypeConverter.class.getPackage())
.addPackage(EqualsVerifier.class.getPackage()) .addPackage(EqualsVerifier.class.getPackage())
.addPackage(IntegrationTest.class.getPackage()) .addPackage(IntegrationTest.class.getPackage())
.addPackage(TestConfiguration.class.getPackage())
.addAsLibraries(libs) .addAsLibraries(libs)
.addAsResource("test-persistence.xml", .addAsResource("test-persistence.xml",
"META-INF/persistence.xml") "META-INF/persistence.xml")
@ -150,6 +153,7 @@ public class ConfigurationManagerTest {
final ExampleConfiguration configuration = configurationManager final ExampleConfiguration configuration = configurationManager
.findConfiguration(ExampleConfiguration.class); .findConfiguration(ExampleConfiguration.class);
assertThat(configuration, is(not(nullValue())));
assertThat(configuration.getPrice(), assertThat(configuration.getPrice(),
is(equalTo(new BigDecimal("98.99")))); is(equalTo(new BigDecimal("98.99"))));
assertThat(configuration.isEnabled(), is(true)); assertThat(configuration.isEnabled(), is(true));
@ -159,4 +163,43 @@ public class ConfigurationManagerTest {
is(equalTo("http://www.example.org"))); 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());
}
} }

View File

@ -45,6 +45,8 @@ public class DatasetsTest extends DatasetsVerifier {
@Parameterized.Parameters(name = "Dataset {0}") @Parameterized.Parameters(name = "Dataset {0}")
public static Collection<String> data() { public static Collection<String> data() {
return Arrays.asList(new String[]{ 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"}); "/datasets/org/libreccm/configuration/ConfigurationManagerTest/data.yml"});
} }

View File

@ -81,7 +81,4 @@ public class ExampleConfiguration {
public void setHelpUrl(final String helpUrl) { public void setHelpUrl(final String helpUrl) {
this.helpUrl = helpUrl; this.helpUrl = helpUrl;
} }
} }

View File

@ -13,5 +13,9 @@
level="debug"> level="debug">
<AppenderRef ref="Console"/> <AppenderRef ref="Console"/>
</Logger> </Logger>
<Logger name="org.libreccm.categorization.CategoryRepository"
level="debug">
<AppenderRef ref="Console"/>
</Logger>
</Loggers> </Loggers>
</Configuration> </Configuration>

View File

@ -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

View File

@ -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