From 4861e7c29fadd5d52b586095f6781e66427dbe2a Mon Sep 17 00:00:00 2001 From: Avgustin Marinov Date: Tue, 19 Nov 2024 08:45:50 +0200 Subject: [PATCH] Refactor the tenantcy configuration (#2080) Signed-off-by: Avgustin Marinov --- .../event/remote/TargetPollEvent.java | 2 +- .../TenantConfigurationProperties.java | 59 ++++---- .../TenantConfigurationBooleanValidator.java | 3 +- .../TenantConfigurationIntegerValidator.java | 5 +- .../TenantConfigurationLongValidator.java | 5 +- ...ConfigurationPollingDurationValidator.java | 11 +- .../TenantConfigurationStringValidator.java | 2 +- .../TenantConfigurationValidator.java | 8 +- .../hawkbit-repository-defaults.properties | 26 +--- .../TenantConfigurationManagementTest.java | 140 +++++++----------- 10 files changed, 102 insertions(+), 159 deletions(-) diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/event/remote/TargetPollEvent.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/event/remote/TargetPollEvent.java index 58af99f32..efaa3e980 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/event/remote/TargetPollEvent.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/event/remote/TargetPollEvent.java @@ -19,7 +19,7 @@ import lombok.ToString; import org.eclipse.hawkbit.repository.model.Target; /** - * Event is send in case a target polls either through DDI or DMF. + * Event is sent in case a target polls either through DDI or DMF. */ @NoArgsConstructor(access = AccessLevel.PUBLIC) // for serialization libs like jackson @Getter diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/TenantConfigurationProperties.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/TenantConfigurationProperties.java index 9579a13c4..27a019e36 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/TenantConfigurationProperties.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/TenantConfigurationProperties.java @@ -13,11 +13,15 @@ import java.io.Serializable; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import lombok.Data; import lombok.ToString; import org.eclipse.hawkbit.repository.exception.InvalidTenantConfigurationKeyException; import org.eclipse.hawkbit.repository.exception.TenantConfigurationValidatorException; +import org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationBooleanValidator; +import org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationIntegerValidator; +import org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationLongValidator; import org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationStringValidator; import org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationValidator; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -25,6 +29,8 @@ import org.springframework.context.ApplicationContext; /** * Properties for tenant configuration default values. + *

+ * Configuration are read from the properties where their type and validators could be specified also. Default type and validator is String. */ @Data @ToString @@ -45,14 +51,14 @@ public class TenantConfigurationProperties { * @return the TenantConfigurationKey with the name keyName */ public TenantConfigurationKey fromKeyName(final String keyName) { - return configuration.values().stream().filter(conf -> conf.getKeyName().equals(keyName)).findAny() - .orElseThrow(() -> new InvalidTenantConfigurationKeyException( - "The given configuration key " + keyName + " does not exist.")); + return configuration.values().stream() + .filter(conf -> conf.getKeyName().equals(keyName)) + .findAny() + .orElseThrow(() -> new InvalidTenantConfigurationKeyException("The given configuration key " + keyName + " does not exist.")); } /** - * Tenant specific configurations which can be configured for each tenant - * separately by means of override of the system defaults. + * Tenant specific configurations which can be configured for each tenant separately by means of override of the system defaults. */ @Data @ToString @@ -62,121 +68,112 @@ public class TenantConfigurationProperties { * Header based authentication enabled. */ public static final String AUTHENTICATION_MODE_HEADER_ENABLED = "authentication.header.enabled"; - /** * Header based authentication authority name. */ public static final String AUTHENTICATION_MODE_HEADER_AUTHORITY_NAME = "authentication.header.authority"; - /** * Target token based authentication enabled. */ public static final String AUTHENTICATION_MODE_TARGET_SECURITY_TOKEN_ENABLED = "authentication.targettoken.enabled"; - /** * Gateway token based authentication enabled. */ public static final String AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_ENABLED = "authentication.gatewaytoken.enabled"; - /** * Gateway token value. */ public static final String AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY = "authentication.gatewaytoken.key"; - /** * See system default in * {@link ControllerPollProperties#getPollingTime()}. */ public static final String POLLING_TIME_INTERVAL = "pollingTime"; - /** * See system default in * {@link ControllerPollProperties#getMinPollingTime()}. */ public static final String MIN_POLLING_TIME_INTERVAL = "minPollingTime"; - /** * See system default in * {@link ControllerPollProperties#getMaintenanceWindowPollCount()}. */ public static final String MAINTENANCE_WINDOW_POLL_COUNT = "maintenanceWindowPollCount"; - /** * See system default in * {@link ControllerPollProperties#getPollingOverdueTime()}. */ public static final String POLLING_OVERDUE_TIME_INTERVAL = "pollingOverdueTime"; - /** * If anonymous downloads are enabled */ public static final String ANONYMOUS_DOWNLOAD_MODE_ENABLED = "anonymous.download.enabled"; - /** * Represents setting if approval for a rollout is needed. */ public static final String ROLLOUT_APPROVAL_ENABLED = "rollout.approval.enabled"; - /** * Repository on autoclose mode instead of canceling in case of new DS * assignment over active actions. */ public static final String REPOSITORY_ACTIONS_AUTOCLOSE_ENABLED = "repository.actions.autoclose.enabled"; - /** * Switch to enable/disable automatic action cleanup. */ public static final String ACTION_CLEANUP_ENABLED = "action.cleanup.enabled"; - /** * Specifies the action expiry in milli-seconds. */ public static final String ACTION_CLEANUP_ACTION_EXPIRY = "action.cleanup.actionExpiry"; - /** * Specifies the action status. */ public static final String ACTION_CLEANUP_ACTION_STATUS = "action.cleanup.actionStatus"; - /** * Switch to enable/disable the multi-assignment feature. */ public static final String MULTI_ASSIGNMENTS_ENABLED = "multi.assignments.enabled"; - /** * Switch to enable/disable the batch-assignment feature. */ public static final String BATCH_ASSIGNMENTS_ENABLED = "batch.assignments.enabled"; - /** * Switch to enable/disable the user-confirmation flow */ public static final String USER_CONFIRMATION_ENABLED = "user.confirmation.flow.enabled"; - /** * Switch to enable/disable the implicit locking */ public static final String IMPLICIT_LOCK_ENABLED = "implicit.lock.enabled"; + private static final Map, TenantConfigurationValidator> DEFAULT_TYPE_VALIDATORS = Map.of( + Boolean.class, new TenantConfigurationBooleanValidator(), + Integer.class, new TenantConfigurationIntegerValidator(), + Long.class, new TenantConfigurationLongValidator(), + String.class, new TenantConfigurationStringValidator()); + private String keyName; private String defaultValue = ""; private Class dataType = String.class; - private Class validator = TenantConfigurationStringValidator.class; + private Class validator; /** - * validates if a object matches the allowed data format of the corresponding key + * Validates if an object matches the allowed data format of the corresponding key * * @param context application context * @param value which will be validated * @throws TenantConfigurationValidatorException is thrown, when object is invalid */ public void validate(final ApplicationContext context, final Object value) { - final TenantConfigurationValidator createdBean = context.getAutowireCapableBeanFactory() - .createBean(validator); - try { - createdBean.validate(value); - } finally { - context.getAutowireCapableBeanFactory().destroyBean(createdBean); + if (validator == null) { + Objects.requireNonNull(DEFAULT_TYPE_VALIDATORS.get(dataType), "No validator defined for " + keyName).validate(value); + } else { + final TenantConfigurationValidator createdBean = context.getAutowireCapableBeanFactory().createBean(validator); + try { + createdBean.validate(value); + } finally { + context.getAutowireCapableBeanFactory().destroyBean(createdBean); + } } } } diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationBooleanValidator.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationBooleanValidator.java index cbbf435bd..09c8e59a0 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationBooleanValidator.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationBooleanValidator.java @@ -10,8 +10,7 @@ package org.eclipse.hawkbit.tenancy.configuration.validator; /** - * specific tenant configuration validator, which validates that the given value - * is a booleans. + * specific tenant configuration validator, which validates that the given value is a booleans. */ public class TenantConfigurationBooleanValidator implements TenantConfigurationValidator { diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationIntegerValidator.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationIntegerValidator.java index 31829aa4c..e4330301e 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationIntegerValidator.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationIntegerValidator.java @@ -11,8 +11,7 @@ package org.eclipse.hawkbit.tenancy.configuration.validator; /** - * Specific tenant configuration validator, which validates that the given value - * is an Integer. + * Specific tenant configuration validator, which validates that the given value is an Integer. */ public class TenantConfigurationIntegerValidator implements TenantConfigurationValidator { @@ -20,4 +19,4 @@ public class TenantConfigurationIntegerValidator implements TenantConfigurationV public Class validateToClass() { return Integer.class; } -} +} \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationLongValidator.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationLongValidator.java index a451e8336..fd96e13b7 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationLongValidator.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationLongValidator.java @@ -10,8 +10,7 @@ package org.eclipse.hawkbit.tenancy.configuration.validator; /** - * Specific tenant configuration validator, which validates that the given value - * is a {@link Long}. + * Specific tenant configuration validator, which validates that the given value is a {@link Long}. */ public class TenantConfigurationLongValidator implements TenantConfigurationValidator { @@ -19,4 +18,4 @@ public class TenantConfigurationLongValidator implements TenantConfigurationVali public Class validateToClass() { return Long.class; } -} +} \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationPollingDurationValidator.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationPollingDurationValidator.java index 8369a18b8..33c71d15e 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationPollingDurationValidator.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationPollingDurationValidator.java @@ -18,21 +18,20 @@ import org.eclipse.hawkbit.tenancy.configuration.DurationHelper; import org.springframework.beans.factory.annotation.Autowired; /** - * This class is used to validate, that the property is a String and that it is - * in the correct duration format. + * This class is used to validate, that the property is a String and that it is in the correct duration format. */ public class TenantConfigurationPollingDurationValidator implements TenantConfigurationValidator { private final Duration minDuration; - private final Duration maxDuration; /** - * Constructor. + * This constructor is called by {@link org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationProperties} using + * ApplicationContext.getAutowireCapableBeanFactory().createBean(Class) to validate the polling duration configuration. + * This insures the wiring of the properties is done correctly. * * @param properties property accessor for poll configuration */ - @Autowired public TenantConfigurationPollingDurationValidator(final ControllerPollProperties properties) { minDuration = DurationHelper.formattedStringToDuration(properties.getMinPollingTime()); maxDuration = DurationHelper.formattedStringToDuration(properties.getMaxPollingTime()); @@ -66,4 +65,4 @@ public class TenantConfigurationPollingDurationValidator implements TenantConfig public Class validateToClass() { return String.class; } -} +} \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationStringValidator.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationStringValidator.java index 875d3fb96..d5452a362 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationStringValidator.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationStringValidator.java @@ -19,4 +19,4 @@ public class TenantConfigurationStringValidator implements TenantConfigurationVa public Class validateToClass() { return String.class; } -} +} \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationValidator.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationValidator.java index ded57dc8f..80815c012 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationValidator.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/tenancy/configuration/validator/TenantConfigurationValidator.java @@ -23,12 +23,10 @@ public interface TenantConfigurationValidator { * @throws TenantConfigurationValidatorException is thrown, when parameter is invalid. */ default void validate(final Object tenantConfigurationValue) { - if (tenantConfigurationValue != null - && validateToClass().isAssignableFrom(tenantConfigurationValue.getClass())) { + if (tenantConfigurationValue != null && validateToClass().isAssignableFrom(tenantConfigurationValue.getClass())) { return; } - throw new TenantConfigurationValidatorException( - "The given configuration value is expected as a " + validateToClass().getSimpleName()); + throw new TenantConfigurationValidatorException("The given configuration value is expected as a " + validateToClass().getSimpleName()); } /** @@ -39,4 +37,4 @@ public interface TenantConfigurationValidator { default Class validateToClass() { return Object.class; } -} +} \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-core/src/main/resources/hawkbit-repository-defaults.properties b/hawkbit-repository/hawkbit-repository-core/src/main/resources/hawkbit-repository-defaults.properties index 9a3b86f21..ad2637d7a 100644 --- a/hawkbit-repository/hawkbit-repository-core/src/main/resources/hawkbit-repository-defaults.properties +++ b/hawkbit-repository/hawkbit-repository-core/src/main/resources/hawkbit-repository-defaults.properties @@ -14,26 +14,27 @@ hawkbit.controller.pollingOverdueTime=00:05:00 hawkbit.controller.maxPollingTime=23:59:59 hawkbit.controller.minPollingTime=00:00:30 -# This configuration value is used to change the polling interval so that -# controller tries to poll at least these many times between the last polling -# and before start of maintenance window. The polling interval is bounded by -# configured pollingTime and minPollingTime. The polling interval is -# modified as per following scheme: +# This configuration value is used to change the polling interval so that controller tries to poll at least these many +# times between the last polling and before start of maintenance window. The polling interval is bounded by configured +# pollingTime and minPollingTime. The polling interval is modified as per following scheme: +# # pollingTime(@time=t) = (maintenanceStartTime - t)/maintenanceWindowPollCount hawkbit.controller.maintenanceWindowPollCount=3 # Attention: if you want to use a maximumPollingTime greater 23:59:59 you have to update the DurationField in the configuration window # Default tenant configuration - START +# DefaultType is java.lang.String, with default value - empty string +# There are default java.lang.Boolean, java.lang.Integer, java.lang.Long, java.lang.String validators. If their +# validation is sufficient - no need to be specified explicitly. Could be explicitly overridden by specifying a +# validator hawkbit.server.tenant.configuration.authentication-header-enabled.keyName=authentication.header.enabled hawkbit.server.tenant.configuration.authentication-header-enabled.defaultValue=${hawkbit.server.ddi.security.authentication.header.enabled} hawkbit.server.tenant.configuration.authentication-header-enabled.dataType=java.lang.Boolean -hawkbit.server.tenant.configuration.authentication-header-enabled.validator=org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationBooleanValidator hawkbit.server.tenant.configuration.repository-actions-autoclose-enabled.keyName=repository.actions.autoclose.enabled hawkbit.server.tenant.configuration.repository-actions-autoclose-enabled.defaultValue=false hawkbit.server.tenant.configuration.repository-actions-autoclose-enabled.dataType=java.lang.Boolean -hawkbit.server.tenant.configuration.repository-actions-autoclose-enabled.validator=org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationBooleanValidator hawkbit.server.tenant.configuration.authentication-header-authority.keyName=authentication.header.authority hawkbit.server.tenant.configuration.authentication-header-authority.defaultValue=${hawkbit.server.ddi.security.authentication.header.authority} @@ -41,12 +42,10 @@ hawkbit.server.tenant.configuration.authentication-header-authority.defaultValue hawkbit.server.tenant.configuration.authentication-targettoken-enabled.keyName=authentication.targettoken.enabled hawkbit.server.tenant.configuration.authentication-targettoken-enabled.defaultValue=${hawkbit.server.ddi.security.authentication.targettoken.enabled} hawkbit.server.tenant.configuration.authentication-targettoken-enabled.dataType=java.lang.Boolean -hawkbit.server.tenant.configuration.authentication-targettoken-enabled.validator=org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationBooleanValidator hawkbit.server.tenant.configuration.authentication-gatewaytoken-enabled.keyName=authentication.gatewaytoken.enabled hawkbit.server.tenant.configuration.authentication-gatewaytoken-enabled.defaultValue=${hawkbit.server.ddi.security.authentication.gatewaytoken.enabled} hawkbit.server.tenant.configuration.authentication-gatewaytoken-enabled.dataType=java.lang.Boolean -hawkbit.server.tenant.configuration.authentication-gatewaytoken-enabled.validator=org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationBooleanValidator hawkbit.server.tenant.configuration.authentication-gatewaytoken-key.keyName=authentication.gatewaytoken.key hawkbit.server.tenant.configuration.authentication-gatewaytoken-key.defaultValue=${hawkbit.server.ddi.security.authentication.gatewaytoken.key} @@ -66,28 +65,23 @@ hawkbit.server.tenant.configuration.polling-overdue-time.validator=org.eclipse.h hawkbit.server.tenant.configuration.maintenance-window-poll-count.keyName=maintenanceWindowPollCount hawkbit.server.tenant.configuration.maintenance-window-poll-count.defaultValue=${hawkbit.controller.maintenanceWindowPollCount} hawkbit.server.tenant.configuration.maintenance-window-poll-count.dataType=java.lang.Integer -hawkbit.server.tenant.configuration.maintenance-window-poll-count.validator=org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationIntegerValidator hawkbit.server.tenant.configuration.anonymous-download-enabled.keyName=anonymous.download.enabled hawkbit.server.tenant.configuration.anonymous-download-enabled.defaultValue=${hawkbit.server.download.anonymous.enabled} hawkbit.server.tenant.configuration.anonymous-download-enabled.dataType=java.lang.Boolean -hawkbit.server.tenant.configuration.anonymous-download-enabled.validator=org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationBooleanValidator hawkbit.server.tenant.configuration.rollout-approval-enabled.keyName=rollout.approval.enabled hawkbit.server.tenant.configuration.rollout-approval-enabled.defaultValue=false hawkbit.server.tenant.configuration.rollout-approval-enabled.dataType=java.lang.Boolean -hawkbit.server.tenant.configuration.rollout-approval-enabled.validator=org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationBooleanValidator hawkbit.server.tenant.configuration.action-cleanup-enabled.keyName=action.cleanup.enabled hawkbit.server.tenant.configuration.action-cleanup-enabled.defaultValue=false hawkbit.server.tenant.configuration.action-cleanup-enabled.dataType=java.lang.Boolean -hawkbit.server.tenant.configuration.action-cleanup-enabled.validator=org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationBooleanValidator hawkbit.server.tenant.configuration.action-cleanup-action-expiry.keyName=action.cleanup.actionExpiry # default: 30 days hawkbit.server.tenant.configuration.action-cleanup-action-expiry.defaultValue=2592000000 hawkbit.server.tenant.configuration.action-cleanup-action-expiry.dataType=java.lang.Long -hawkbit.server.tenant.configuration.action-cleanup-action-expiry.validator=org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationLongValidator hawkbit.server.tenant.configuration.action-cleanup-action-status.keyName=action.cleanup.actionStatus hawkbit.server.tenant.configuration.action-cleanup-action-status.defaultValue=CANCELED,ERROR @@ -95,21 +89,17 @@ hawkbit.server.tenant.configuration.action-cleanup-action-status.defaultValue=CA hawkbit.server.tenant.configuration.multi-assignments-enabled.keyName=multi.assignments.enabled hawkbit.server.tenant.configuration.multi-assignments-enabled.defaultValue=false hawkbit.server.tenant.configuration.multi-assignments-enabled.dataType=java.lang.Boolean -hawkbit.server.tenant.configuration.multi-assignments-enabled.validator=org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationBooleanValidator hawkbit.server.tenant.configuration.batch-assignments-enabled.keyName=batch.assignments.enabled hawkbit.server.tenant.configuration.batch-assignments-enabled.defaultValue=false hawkbit.server.tenant.configuration.batch-assignments-enabled.dataType=java.lang.Boolean -hawkbit.server.tenant.configuration.batch-assignments-enabled.validator=org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationBooleanValidator hawkbit.server.tenant.configuration.user-confirmation-enabled.keyName=user.confirmation.flow.enabled hawkbit.server.tenant.configuration.user-confirmation-enabled.defaultValue=false hawkbit.server.tenant.configuration.user-confirmation-enabled.dataType=java.lang.Boolean -hawkbit.server.tenant.configuration.user-confirmation-enabled.validator=org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationBooleanValidator hawkbit.server.tenant.configuration.implicit-lock-enabled.keyName=implicit.lock.enabled hawkbit.server.tenant.configuration.implicit-lock-enabled.defaultValue=true hawkbit.server.tenant.configuration.implicit-lock-enabled.dataType=java.lang.Boolean -hawkbit.server.tenant.configuration.implicit-lock-enabled.validator=org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationBooleanValidator # Default tenant configuration - END diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TenantConfigurationManagementTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TenantConfigurationManagementTest.java index ceaf08f18..15973e020 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TenantConfigurationManagementTest.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TenantConfigurationManagementTest.java @@ -10,6 +10,7 @@ package org.eclipse.hawkbit.repository.jpa.management; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.fail; import java.io.Serializable; @@ -26,7 +27,6 @@ import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest; import org.eclipse.hawkbit.repository.model.TenantConfigurationValue; import org.eclipse.hawkbit.tenancy.configuration.DurationHelper; import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationProperties.TenantConfigurationKey; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.context.EnvironmentAware; import org.springframework.core.env.Environment; @@ -35,7 +35,7 @@ import org.springframework.core.env.Environment; @Story("Tenant Configuration Management") public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTest implements EnvironmentAware { - private Environment environment = null; + private Environment environment; @Override public void setEnvironment(final Environment environment) { @@ -64,8 +64,7 @@ public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTes // verify that new configuration value is used final TenantConfigurationValue updatedConfigurationValue = tenantConfigurationManagement - .getConfigurationValue(TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY, - String.class); + .getConfigurationValue(TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY, String.class); assertThat(updatedConfigurationValue.isGlobal()).isEqualTo(false); assertThat(updatedConfigurationValue.getValue()).isEqualTo(newConfigurationValue); @@ -81,13 +80,11 @@ public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTes // add value first tenantConfigurationManagement.addOrUpdateConfiguration(configKey, value1); - assertThat(tenantConfigurationManagement.getConfigurationValue(configKey, String.class).getValue()) - .isEqualTo(value1); + assertThat(tenantConfigurationManagement.getConfigurationValue(configKey, String.class).getValue()).isEqualTo(value1); // update to value second tenantConfigurationManagement.addOrUpdateConfiguration(configKey, value2); - assertThat(tenantConfigurationManagement.getConfigurationValue(configKey, String.class).getValue()) - .isEqualTo(value2); + assertThat(tenantConfigurationManagement.getConfigurationValue(configKey, String.class).getValue()).isEqualTo(value2); } @Test @@ -100,8 +97,8 @@ public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTes // add value first tenantConfigurationManagement.addOrUpdateConfiguration(configuration); - assertThat(tenantConfigurationManagement.getConfigurationValue(TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY, - String.class).getValue()) + assertThat(tenantConfigurationManagement.getConfigurationValue( + TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY, String.class).getValue()) .isEqualTo("token_123"); assertThat( tenantConfigurationManagement.getConfigurationValue(TenantConfigurationKey.ROLLOUT_APPROVAL_ENABLED, Boolean.class).getValue()) @@ -114,12 +111,10 @@ public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTes final String configKey = TenantConfigurationKey.AUTHENTICATION_MODE_HEADER_ENABLED; final Boolean value1 = true; tenantConfigurationManagement.addOrUpdateConfiguration(configKey, value1); - assertThat(tenantConfigurationManagement.getConfigurationValue(configKey, Boolean.class).getValue()) - .isEqualTo(value1); + assertThat(tenantConfigurationManagement.getConfigurationValue(configKey, Boolean.class).getValue()).isEqualTo(value1); final Boolean value2 = false; tenantConfigurationManagement.addOrUpdateConfiguration(configKey, value2); - assertThat(tenantConfigurationManagement.getConfigurationValue(configKey, Boolean.class).getValue()) - .isEqualTo(value2); + assertThat(tenantConfigurationManagement.getConfigurationValue(configKey, Boolean.class).getValue()).isEqualTo(value2); } @Test @@ -129,18 +124,15 @@ public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTes final String value1 = "thisIsNotABoolean"; // add value as String - try { - tenantConfigurationManagement.addOrUpdateConfiguration(configKey, value1); - fail("should not have worked as string is not a boolean"); - } catch (final TenantConfigurationValidatorException e) { - - } + assertThatThrownBy(() -> tenantConfigurationManagement.addOrUpdateConfiguration(configKey, value1)) + .as("Should not have worked as value is not a boolean") + .isInstanceOf(TenantConfigurationValidatorException.class); } @Test @Description("Tests that the get configuration throws exception in case the value is the wrong type") public void batchWrongTenantConfigurationValueTypeThrowsException() { - Map configuration = new HashMap<>() {{ + final Map configuration = new HashMap<>() {{ put(TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY, "token_123"); put(TenantConfigurationKey.ROLLOUT_APPROVAL_ENABLED, true); put(TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_ENABLED, "wrong"); @@ -151,11 +143,12 @@ public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTes fail("should not have worked as type is wrong"); } catch (final TenantConfigurationValidatorException e) { assertThat( - tenantConfigurationManagement.getConfigurationValue(TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY, - String.class).getValue()) + tenantConfigurationManagement.getConfigurationValue( + TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY, String.class).getValue()) .isNotEqualTo("token_123"); - assertThat(tenantConfigurationManagement.getConfigurationValue(TenantConfigurationKey.ROLLOUT_APPROVAL_ENABLED, Boolean.class) - .getValue()) + assertThat( + tenantConfigurationManagement.getConfigurationValue( + TenantConfigurationKey.ROLLOUT_APPROVAL_ENABLED, Boolean.class).getValue()) .isNotEqualTo(true); } } @@ -165,10 +158,8 @@ public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTes public void deleteConfigurationReturnNullConfiguration() { final String configKey = TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY; - // gateway token does not have default value so no configuration value - // should be available - final String defaultConfigValue = tenantConfigurationManagement.getConfigurationValue(configKey, String.class) - .getValue(); + // gateway token does not have default value so no configuration value should be available + final String defaultConfigValue = tenantConfigurationManagement.getConfigurationValue(configKey, String.class).getValue(); assertThat(defaultConfigValue).isEmpty(); // update the tenant specific configuration @@ -177,8 +168,7 @@ public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTes tenantConfigurationManagement.addOrUpdateConfiguration(configKey, newConfigurationValue); // verify that new configuration value is used - final String updatedConfigurationValue = tenantConfigurationManagement - .getConfigurationValue(configKey, String.class).getValue(); + final String updatedConfigurationValue = tenantConfigurationManagement.getConfigurationValue(configKey, String.class).getValue(); assertThat(updatedConfigurationValue).isEqualTo(newConfigurationValue); // delete the tenant specific configuration @@ -192,39 +182,30 @@ public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTes @Description("Test that an Exception is thrown, when an integer is stored but a string expected.") public void storesIntegerWhenStringIsExpected() { final String configKey = TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY; - final Integer wrongDataype = 123; - try { - tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDataype); - fail("should not have worked as integer is not a string"); - } catch (final TenantConfigurationValidatorException e) { - - } + final Integer wrongDatType = 123; + assertThatThrownBy(() -> tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDatType)) + .as("Should not have worked as integer is not a string") + .isInstanceOf(TenantConfigurationValidatorException.class); } @Test @Description("Test that an Exception is thrown, when an integer is stored but a boolean expected.") public void storesIntegerWhenBooleanIsExpected() { final String configKey = TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_ENABLED; - final Integer wrongDataype = 123; - try { - tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDataype); - fail("should not have worked as integer is not a boolean"); - } catch (final TenantConfigurationValidatorException e) { - - } + final Integer wrongDataType = 123; + assertThatThrownBy(() -> tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDataType)) + .as("Should not have worked as integer is not a boolean") + .isInstanceOf(TenantConfigurationValidatorException.class); } @Test @Description("Test that an Exception is thrown, when an integer is stored as PollingTime.") public void storesIntegerWhenPollingIntervalIsExpected() { final String configKey = TenantConfigurationKey.POLLING_TIME_INTERVAL; - final Integer wrongDataype = 123; - try { - tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDataype); - fail("should not have worked as integer is not a time field"); - } catch (final TenantConfigurationValidatorException e) { - - } + final Integer wrongDataType = 123; + assertThatThrownBy(() -> tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDataType)) + .as("Should not have worked as integer is not a time field") + .isInstanceOf(TenantConfigurationValidatorException.class); } @Test @@ -232,12 +213,9 @@ public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTes public void storesWrongFormattedStringAsPollingInterval() { final String configKey = TenantConfigurationKey.POLLING_TIME_INTERVAL; final String wrongFormatted = "wrongFormatted"; - try { - tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongFormatted); - fail("should not have worked as string is not a time field"); - } catch (final TenantConfigurationValidatorException e) { - - } + assertThatThrownBy(() -> tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongFormatted)) + .as("should not have worked as string is not a time field") + .isInstanceOf(TenantConfigurationValidatorException.class); } @Test @@ -247,12 +225,9 @@ public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTes final String tooSmallDuration = DurationHelper .durationToFormattedString(DurationHelper.getDurationByTimeValues(0, 0, 1)); - try { - tenantConfigurationManagement.addOrUpdateConfiguration(configKey, tooSmallDuration); - fail("should not have worked as string has an invalid format"); - } catch (final TenantConfigurationValidatorException e) { - - } + assertThatThrownBy(() -> tenantConfigurationManagement.addOrUpdateConfiguration(configKey, tooSmallDuration)) + .as("Should not have worked as string has an invalid format") + .isInstanceOf(TenantConfigurationValidatorException.class); } @Test @@ -263,38 +238,29 @@ public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTes final Duration duration = DurationHelper.getDurationByTimeValues(1, 2, 0); assertThat(duration).isEqualTo(Duration.ofHours(1).plusMinutes(2)); - tenantConfigurationManagement.addOrUpdateConfiguration(configKey, - DurationHelper.durationToFormattedString(duration)); + tenantConfigurationManagement.addOrUpdateConfiguration(configKey, DurationHelper.durationToFormattedString(duration)); - final String storedDurationString = tenantConfigurationManagement.getConfigurationValue(configKey, String.class) - .getValue(); + final String storedDurationString = tenantConfigurationManagement.getConfigurationValue(configKey, String.class).getValue(); assertThat(duration).isEqualTo(DurationHelper.formattedStringToDuration(storedDurationString)); } @Test @Description("Request a config value in a wrong Value") public void requestConfigValueWithWrongType() { - try { - tenantConfigurationManagement.getConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, - Serializable.class); - Assertions.fail(""); - } catch (final TenantConfigurationValidatorException e) { - - } + assertThatThrownBy(() -> tenantConfigurationManagement.getConfigurationValue( + TenantConfigurationKey.POLLING_TIME_INTERVAL, Serializable.class)) + .isInstanceOf(TenantConfigurationValidatorException.class); } @Test @Description("Verifies that every TenenatConfiguraationKeyName exists only once") public void verifyThatAllKeysAreDifferent() { - final Map keynames = new HashMap<>(); - + final Map keyNames = new HashMap<>(); tenantConfigurationProperties.getConfigurationKeys().forEach(key -> { - - if (keynames.containsKey(key.getKeyName())) { + if (keyNames.containsKey(key.getKeyName())) { throw new IllegalStateException("The key names are not unique"); } - - keynames.put(key.getKeyName(), null); + keyNames.put(key.getKeyName(), null); }); } @@ -302,7 +268,6 @@ public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTes @Description("Get TenantConfigurationKeyByName") public void getTenantConfigurationKeyByName() { final String configKey = TenantConfigurationKey.POLLING_TIME_INTERVAL; - assertThat(tenantConfigurationProperties.fromKeyName(configKey).getKeyName()).isEqualTo(configKey); } @@ -310,11 +275,8 @@ public class TenantConfigurationManagementTest extends AbstractJpaIntegrationTes @Description("Tenant configuration which is not declared throws exception") public void storeTenantConfigurationWhichIsNotDeclaredThrowsException() { final String configKeyWhichDoesNotExists = "configKeyWhichDoesNotExists"; - try { - tenantConfigurationManagement.addOrUpdateConfiguration(configKeyWhichDoesNotExists, "value"); - fail("Expected InvalidTenantConfigurationKeyException for tenant configuration key which is not declared"); - } catch (final InvalidTenantConfigurationKeyException e) { - // expected exception - } + assertThatThrownBy(() -> tenantConfigurationManagement.addOrUpdateConfiguration(configKeyWhichDoesNotExists, "value")) + .as("Expected InvalidTenantConfigurationKeyException for tenant configuration key which is not declared") + .isInstanceOf(InvalidTenantConfigurationKeyException.class); } -} +} \ No newline at end of file