Changed return type of SystemManagement.getConfigurationValue()
* changed Return type to wrapper object, adding additional meta data stored in the database * updated all calls of this method * updated function calls in tests * verified correct execution of correspending tests Signed-off-by: Nonnenmacher Fabian <fabian.nonnenmacher@bosch-si.com>
This commit is contained in:
committed by
Nonnenmacher Fabian
parent
f2e7cdb92a
commit
07fce42469
@@ -10,7 +10,6 @@ package org.eclipse.hawkbit.repository;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -21,15 +20,11 @@ import org.eclipse.hawkbit.cache.TenancyCacheManager;
|
||||
import org.eclipse.hawkbit.im.authentication.SpPermission.SpringEvalExpressions;
|
||||
import org.eclipse.hawkbit.report.model.SystemUsageReport;
|
||||
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
|
||||
import org.eclipse.hawkbit.repository.exception.InvalidDistributionSetTypeException;
|
||||
import org.eclipse.hawkbit.repository.exception.InvalidPollingTimeException;
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSetType;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
|
||||
import org.eclipse.hawkbit.repository.model.TenantConfiguration;
|
||||
import org.eclipse.hawkbit.repository.model.TenantConfigurationValue;
|
||||
import org.eclipse.hawkbit.repository.model.TenantMetaData;
|
||||
import org.eclipse.hawkbit.repository.model.helper.DurationHelper;
|
||||
import org.eclipse.hawkbit.repository.specifications.DistributionSetTypeSpecification;
|
||||
import org.eclipse.hawkbit.rest.resource.model.system.SystemConfigurationRequestBodyPut;
|
||||
import org.eclipse.hawkbit.tenancy.TenantAware;
|
||||
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey;
|
||||
import org.eclipse.persistence.config.PersistenceUnitProperties;
|
||||
@@ -330,6 +325,8 @@ public class SystemManagement implements EnvironmentAware {
|
||||
* Retrieves a configuration value from the e.g. tenant overwritten
|
||||
* configuration values or in case the tenant does not a have a specific
|
||||
* configuration the global default value hold in the {@link Environment}.
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @param configurationKey
|
||||
* the key of the configuration
|
||||
@@ -345,15 +342,26 @@ public class SystemManagement implements EnvironmentAware {
|
||||
* {@code propertyType}
|
||||
*/
|
||||
@Cacheable(value = "tenantConfiguration", key = "#configurationKey.getKeyName()")
|
||||
public <T> T getConfigurationValue(final TenantConfigurationKey configurationKey, final Class<T> propertyType) {
|
||||
public <T> TenantConfigurationValue<T> getConfigurationValue(final TenantConfigurationKey configurationKey,
|
||||
final Class<T> propertyType) {
|
||||
|
||||
final TenantConfiguration tenantConfiguration = tenantConfigurationRepository
|
||||
.findByKey(configurationKey.getKeyName());
|
||||
|
||||
if (tenantConfiguration != null) {
|
||||
return conversionService.convert(tenantConfiguration.getValue(), propertyType);
|
||||
return TenantConfigurationValue.<T> builder().isGlobal(false).createdBy(tenantConfiguration.getCreatedBy())
|
||||
.createdAt(tenantConfiguration.getCreatedAt())
|
||||
.lastModifiedAt(tenantConfiguration.getLastModifiedAt())
|
||||
.lastModifiedBy(tenantConfiguration.getLastModifiedBy())
|
||||
.value(conversionService.convert(tenantConfiguration.getValue(), propertyType)).build();
|
||||
|
||||
} else if (configurationKey.getDefaultKeyName() != null) {
|
||||
final T defaultKeyNameValue = environment.getProperty(configurationKey.getDefaultKeyName(), propertyType);
|
||||
return defaultKeyNameValue != null ? defaultKeyNameValue
|
||||
: conversionService.convert(configurationKey.getDefaultValue(), propertyType);
|
||||
final T valueInProperties = environment.getProperty(configurationKey.getDefaultKeyName(), propertyType);
|
||||
|
||||
return TenantConfigurationValue.<T> builder().isGlobal(true).createdBy(null).createdAt(null)
|
||||
.lastModifiedAt(null).lastModifiedBy(null).value(valueInProperties != null ? valueInProperties
|
||||
: conversionService.convert(configurationKey.getDefaultValue(), propertyType))
|
||||
.build();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -467,29 +475,33 @@ public class SystemManagement implements EnvironmentAware {
|
||||
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Modifying
|
||||
public void updateTenantConfiguration(SystemConfigurationRequestBodyPut systemConReq) {
|
||||
|
||||
DurationHelper dh = new DurationHelper();
|
||||
|
||||
TenantMetaData tenantMetaData = getTenantMetadata();
|
||||
|
||||
String ddstypeKey = systemConReq.getDefaultDistributionSetType();
|
||||
|
||||
if (distributionSetTypeRepository.findAll(DistributionSetTypeSpecification.byKey(ddstypeKey)).isEmpty()) {
|
||||
throw new InvalidDistributionSetTypeException(
|
||||
String.format("The specified default distribution set type %s doe not exist.", ddstypeKey));
|
||||
}
|
||||
|
||||
try {
|
||||
tenantMetaData.setPollingOverdueTime(dh.formattedStringToDuration(systemConReq.getPollingOverdueTime()));
|
||||
tenantMetaData.setPollingTime(dh.formattedStringToDuration(systemConReq.getPollingTime()));
|
||||
} catch (DateTimeParseException ex) {
|
||||
throw new InvalidPollingTimeException(ex);
|
||||
}
|
||||
|
||||
updateTenantMetadata(tenantMetaData);
|
||||
}
|
||||
// @Transactional
|
||||
// @Modifying
|
||||
// public void updateTenantConfiguration(SystemConfigurationRequestBodyPut
|
||||
// systemConReq) {
|
||||
//
|
||||
// DurationHelper dh = new DurationHelper();
|
||||
//
|
||||
// TenantMetaData tenantMetaData = getTenantMetadata();
|
||||
//
|
||||
// String ddstypeKey = systemConReq.getDefaultDistributionSetType();
|
||||
//
|
||||
// if
|
||||
// (distributionSetTypeRepository.findAll(DistributionSetTypeSpecification.byKey(ddstypeKey)).isEmpty())
|
||||
// {
|
||||
// throw new InvalidDistributionSetTypeException(
|
||||
// String.format("The specified default distribution set type %s doe not
|
||||
// exist.", ddstypeKey));
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// tenantMetaData.setPollingOverdueTime(dh.formattedStringToDuration(systemConReq.getPollingOverdueTime()));
|
||||
// tenantMetaData.setPollingTime(dh.formattedStringToDuration(systemConReq.getPollingTime()));
|
||||
// } catch (DateTimeParseException ex) {
|
||||
// throw new InvalidPollingTimeException(ex);
|
||||
// }
|
||||
//
|
||||
// updateTenantMetadata(tenantMetaData);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
package org.eclipse.hawkbit.repository.model;
|
||||
|
||||
/**
|
||||
* represents a tenant configuration value including some meta data
|
||||
*
|
||||
* @param <T>
|
||||
* type of the configuration value
|
||||
*/
|
||||
public class TenantConfigurationValue<T> {
|
||||
|
||||
private TenantConfigurationValue() {
|
||||
}
|
||||
|
||||
private T value = null;
|
||||
private boolean isGlobal = true;
|
||||
private Long lastModifiedAt = null;
|
||||
private String lastModifiedBy = null;
|
||||
private Long createdAt = null;
|
||||
private String createdBy = null;
|
||||
|
||||
/**
|
||||
* Gets the value.
|
||||
*
|
||||
* @return the value
|
||||
*/
|
||||
public T getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is global.
|
||||
*
|
||||
* @return true, if is global
|
||||
*/
|
||||
public boolean isGlobal() {
|
||||
return isGlobal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the last modified at.
|
||||
*
|
||||
* @return the last modified at
|
||||
*/
|
||||
public long getLastModifiedAt() {
|
||||
return lastModifiedAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the last modified by.
|
||||
*
|
||||
* @return the last modified by
|
||||
*/
|
||||
public String getLastModifiedBy() {
|
||||
return lastModifiedBy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the created at.
|
||||
*
|
||||
* @return the created at
|
||||
*/
|
||||
public long getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the created by.
|
||||
*
|
||||
* @return the created by
|
||||
*/
|
||||
public String getCreatedBy() {
|
||||
return createdBy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder.
|
||||
*
|
||||
* @param <K>
|
||||
* the key type
|
||||
* @return the tenant configuration value builder
|
||||
*/
|
||||
public static <K> TenantConfigurationValueBuilder<K> builder() {
|
||||
|
||||
return new TenantConfigurationValueBuilder<K>();
|
||||
}
|
||||
|
||||
/**
|
||||
* builds the tenant configuration value including some meta data
|
||||
*
|
||||
* @param <T>
|
||||
* type of the configuration value
|
||||
*/
|
||||
public static class TenantConfigurationValueBuilder<T> {
|
||||
|
||||
private final TenantConfigurationValue<T> configuration = new TenantConfigurationValue<>();
|
||||
|
||||
/**
|
||||
* Builds the.
|
||||
*
|
||||
* @return the tenant configuration value
|
||||
*/
|
||||
public TenantConfigurationValue<T> build() {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the configuration value itself
|
||||
*
|
||||
* @param value
|
||||
* the value
|
||||
* @return the tenant configuration value builder
|
||||
*/
|
||||
public TenantConfigurationValueBuilder<T> value(final T value) {
|
||||
this.configuration.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the is global attribute.
|
||||
*
|
||||
* @param isGlobal
|
||||
* true when there is no tenant specific value, false
|
||||
* otherwise
|
||||
* @return the tenant configuration value builder
|
||||
*/
|
||||
public TenantConfigurationValueBuilder<T> isGlobal(final boolean isGlobal) {
|
||||
this.configuration.isGlobal = isGlobal;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the last modified at attribute
|
||||
*
|
||||
* @param lastModifiedAt
|
||||
* timestamp of last modification
|
||||
* @return the tenant configuration value builder
|
||||
*/
|
||||
public TenantConfigurationValueBuilder<T> lastModifiedAt(final Long lastModifiedAt) {
|
||||
this.configuration.lastModifiedAt = lastModifiedAt;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the last modified by attribute
|
||||
*
|
||||
* @param lastModifiedBy
|
||||
* the last modified by
|
||||
* @return the tenant configuration value builder
|
||||
*/
|
||||
public TenantConfigurationValueBuilder<T> lastModifiedBy(final String lastModifiedBy) {
|
||||
this.configuration.lastModifiedBy = lastModifiedBy;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the created at attribute
|
||||
*
|
||||
* @param createdAt
|
||||
* defined when the configuration has been created
|
||||
* @return the tenant configuration value builder
|
||||
*/
|
||||
public TenantConfigurationValueBuilder<T> createdAt(final Long createdAt) {
|
||||
this.configuration.createdAt = createdAt;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the created by attribute
|
||||
*
|
||||
* @param createdBy
|
||||
* defines by whom the configuration has been created
|
||||
* @return the tenant configuration value builder
|
||||
*/
|
||||
public TenantConfigurationValueBuilder<T> createdBy(final String createdBy) {
|
||||
this.configuration.createdBy = createdBy;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -159,7 +159,7 @@ public class SystemManagementTest extends AbstractIntegrationTestWithMongoDB {
|
||||
assertThat(envPropertyDefault).isNotNull();
|
||||
|
||||
// get the configuration from the system management
|
||||
final String defaultConfigValue = systemManagement.getConfigurationValue(configKey, String.class);
|
||||
final String defaultConfigValue = systemManagement.getConfigurationValue(configKey, String.class).getValue();
|
||||
assertThat(envPropertyDefault).isEqualTo(defaultConfigValue);
|
||||
|
||||
// update the tenant specific configuration
|
||||
@@ -169,7 +169,8 @@ public class SystemManagementTest extends AbstractIntegrationTestWithMongoDB {
|
||||
.addOrUpdateConfiguration(new TenantConfiguration(configKey.getKeyName(), newConfigurationValue));
|
||||
|
||||
// verify that new configuration value is used
|
||||
final String updatedConfigurationValue = systemManagement.getConfigurationValue(configKey, String.class);
|
||||
final String updatedConfigurationValue = systemManagement.getConfigurationValue(configKey, String.class)
|
||||
.getValue();
|
||||
assertThat(updatedConfigurationValue).isEqualTo(newConfigurationValue);
|
||||
assertThat(systemManagement.getTenantConfigurations()).hasSize(1);
|
||||
}
|
||||
@@ -183,11 +184,11 @@ public class SystemManagementTest extends AbstractIntegrationTestWithMongoDB {
|
||||
|
||||
// add value first
|
||||
systemManagement.addOrUpdateConfiguration(new TenantConfiguration(configKey.getKeyName(), value1));
|
||||
assertThat(systemManagement.getConfigurationValue(configKey, String.class)).isEqualTo(value1);
|
||||
assertThat(systemManagement.getConfigurationValue(configKey, String.class).getValue()).isEqualTo(value1);
|
||||
|
||||
// update to value second
|
||||
systemManagement.addOrUpdateConfiguration(new TenantConfiguration(configKey.getKeyName(), value2));
|
||||
assertThat(systemManagement.getConfigurationValue(configKey, String.class)).isEqualTo(value2);
|
||||
assertThat(systemManagement.getConfigurationValue(configKey, String.class).getValue()).isEqualTo(value2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -197,7 +198,7 @@ public class SystemManagementTest extends AbstractIntegrationTestWithMongoDB {
|
||||
final Integer value1 = 123;
|
||||
systemManagement
|
||||
.addOrUpdateConfiguration(new TenantConfiguration(configKey.getKeyName(), String.valueOf(value1)));
|
||||
assertThat(systemManagement.getConfigurationValue(configKey, Integer.class)).isEqualTo(value1);
|
||||
assertThat(systemManagement.getConfigurationValue(configKey, Integer.class).getValue()).isEqualTo(value1);
|
||||
}
|
||||
|
||||
@Test(expected = ConversionFailedException.class)
|
||||
@@ -219,7 +220,7 @@ public class SystemManagementTest extends AbstractIntegrationTestWithMongoDB {
|
||||
|
||||
// gateway token does not have default value so no configuration value
|
||||
// is should be available
|
||||
final String defaultConfigValue = systemManagement.getConfigurationValue(configKey, String.class);
|
||||
final String defaultConfigValue = systemManagement.getConfigurationValue(configKey, String.class).getValue();
|
||||
assertThat(defaultConfigValue).isNull();
|
||||
|
||||
// update the tenant specific configuration
|
||||
@@ -229,13 +230,14 @@ public class SystemManagementTest extends AbstractIntegrationTestWithMongoDB {
|
||||
.addOrUpdateConfiguration(new TenantConfiguration(configKey.getKeyName(), newConfigurationValue));
|
||||
|
||||
// verify that new configuration value is used
|
||||
final String updatedConfigurationValue = systemManagement.getConfigurationValue(configKey, String.class);
|
||||
final String updatedConfigurationValue = systemManagement.getConfigurationValue(configKey, String.class)
|
||||
.getValue();
|
||||
assertThat(updatedConfigurationValue).isEqualTo(newConfigurationValue);
|
||||
|
||||
// delete the tenant specific configuration
|
||||
systemManagement.deleteConfiguration(configKey);
|
||||
// ensure that now gateway token is set again, because is deleted and
|
||||
// must be null now
|
||||
assertThat(systemManagement.getConfigurationValue(configKey, String.class)).isNull();
|
||||
assertThat(systemManagement.getConfigurationValue(configKey, String.class).getValue()).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user