diff --git a/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/SpServerError.java b/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/SpServerError.java index 9ba75835d..9f374bb7c 100644 --- a/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/SpServerError.java +++ b/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/SpServerError.java @@ -66,18 +66,6 @@ public enum SpServerError { SP_REST_SORT_PARAM_INVALID_DIRECTION("hawkbit.server.error.rest.param.invalidDirection", "The given sort parameter direction does not exist"), - /** - * - */ - SP_REST_CONFIG_POLLING_TIME_WRONG_FOMRATTED("hawkbit.server.error.rest.param.invalidFormat", - "The given overdue polling time or polling time parameter are not formatted correctly."), - - /** - * - */ - SP_REST_CONFIG_INVALID_DS_TYPE("hawkbit.server.error.rest.param.invalidFormat", - "The given default distribution set type does not exist."), - /** * */ diff --git a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/exception/InvalidDistributionSetTypeException.java b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/exception/InvalidDistributionSetTypeException.java deleted file mode 100644 index 42fbe9c3c..000000000 --- a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/exception/InvalidDistributionSetTypeException.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.eclipse.hawkbit.repository.exception; - -import org.eclipse.hawkbit.exception.SpServerError; -import org.eclipse.hawkbit.exception.SpServerRtException; - -/** - * This Exception is thrown, when the user wants to set a distribution set which - * does not exist, - * - */ -public class InvalidDistributionSetTypeException extends SpServerRtException { - /** - * - */ - private static final long serialVersionUID = 1L; - - /** - * Creates a new CancelActionNotAllowed with - * {@link SpServerError#SP_ACTION_NOT_CANCELABLE} error. - */ - public InvalidDistributionSetTypeException() { - super(SpServerError.SP_REST_CONFIG_INVALID_DS_TYPE); - } - - /** - * @param cause - * for the exception - */ - public InvalidDistributionSetTypeException(final Throwable cause) { - super(SpServerError.SP_REST_CONFIG_INVALID_DS_TYPE, cause); - } - - /** - * @param message - * of the error - */ - public InvalidDistributionSetTypeException(final String message) { - super(message, SpServerError.SP_REST_CONFIG_INVALID_DS_TYPE); - } -} diff --git a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/exception/InvalidPollingTimeException.java b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/exception/InvalidPollingTimeException.java deleted file mode 100644 index 818401563..000000000 --- a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/exception/InvalidPollingTimeException.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.eclipse.hawkbit.repository.exception; - -import org.eclipse.hawkbit.exception.SpServerError; -import org.eclipse.hawkbit.exception.SpServerRtException; - -public class InvalidPollingTimeException extends SpServerRtException { - /** - * - */ - private static final long serialVersionUID = 1L; - - /** - * Creates a new CancelActionNotAllowed with - * {@link SpServerError#SP_ACTION_NOT_CANCELABLE} error. - */ - public InvalidPollingTimeException() { - super(SpServerError.SP_REST_CONFIG_POLLING_TIME_WRONG_FOMRATTED); - } - - /** - * @param cause - * for the exception - */ - public InvalidPollingTimeException(final Throwable cause) { - super(SpServerError.SP_REST_CONFIG_POLLING_TIME_WRONG_FOMRATTED, cause); - } - - /** - * @param message - * of the error - */ - public InvalidPollingTimeException(final String message) { - super(message, SpServerError.SP_REST_CONFIG_POLLING_TIME_WRONG_FOMRATTED); - } -} diff --git a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/helper/PollConfigurationHelper.java b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/helper/PollConfigurationHelper.java deleted file mode 100644 index 55f9395b0..000000000 --- a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/helper/PollConfigurationHelper.java +++ /dev/null @@ -1,255 +0,0 @@ -/** - * Copyright (c) 2015 Bosch Software Innovations GmbH and others. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - */ -package org.eclipse.hawkbit.repository.model.helper; - -import java.time.Duration; -import java.time.format.DateTimeParseException; - -import javax.annotation.PostConstruct; -import javax.validation.constraints.NotNull; - -import org.eclipse.hawkbit.ControllerPollProperties; -import org.eclipse.hawkbit.repository.SystemManagement; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.EnvironmentAware; - -/** - * A singleton bean which holds configuration of the poll time - * {@code hawkbit.server.controller.polling} and - * {@code hawkbit.server.controller.polling.overdue} to have access to the - * configuration in beans not instatinated by spring e.g. JPA entities which - * cannot implement the {@link EnvironmentAware} interface to retrieve - * environment variables. - */ -public final class PollConfigurationHelper { - - /** - * Format of the expected Duration String. Pattern has to be in Valid Format - * for SimpleDateFormat - */ - - private static final Logger LOG = LoggerFactory.getLogger(PollConfigurationHelper.class); - private static final PollConfigurationHelper INSTANCE = new PollConfigurationHelper(); - - private static final int DEFAULT_OVERDUE_HOUR = 0; - private static final int DEFAULT_OVERDUE_MINUTE = 5; - private static final int DEFAULT_OVERDUE_SECOND = 0; - - private static final int DEFAULT_POLL_HOUR = 0; - private static final int DEFAULT_POLL_MINUTE = 5; - private static final int DEFAULT_POLL_SECOND = 0; - - private static final int DEFAULT_MAX_HOUR = 23; - private static final int DEFAULT_MAX_MINUTE = 59; - private static final int DEFAULT_MAX_SECOND = 59; - - private static final int DEFAULT_MIN_HOUR = 0; - private static final int DEFAULT_MIN_MINUTE = 0; - private static final int DEFAULT_MIN_SECOND = 30; - - private static DurationHelper dh = new DurationHelper(); - - @Autowired - private ControllerPollProperties controllerPollProperties; - - @Autowired - private SystemManagement systemManagement; - - private Duration configurationPollTime; - private Duration configurationOverduePollTime; - private Duration configurationMaximumPollTime; - private Duration configurationMinimumPollTime; - - /** - * @return a singleton instance of the environment helper. - */ - public static PollConfigurationHelper getInstance() { - return INSTANCE; - } - - /** - * Bean post construct to calculate the poll time and poll overdue time only - * once. - */ - @PostConstruct - public void initializeConfigurationValues() { - - readGlobalDurationsFromConfiguration(); - - validateGlobalDurations(); - } - - private void readGlobalDurationsFromConfiguration() { - try { - configurationMaximumPollTime = dh.formattedStringToDuration(controllerPollProperties.getMaxPollingTime()); - } catch (DateTimeParseException e) { - // Set to default values - configurationMaximumPollTime = dh.getDurationByTimeValues(DEFAULT_MAX_HOUR, DEFAULT_MAX_MINUTE, - DEFAULT_MAX_SECOND); - } - - try { - configurationMinimumPollTime = dh.formattedStringToDuration(controllerPollProperties.getMinPollingTime()); - } catch (DateTimeParseException e) { - // Set to default values - configurationMinimumPollTime = dh.getDurationByTimeValues(DEFAULT_MIN_HOUR, DEFAULT_MIN_MINUTE, - DEFAULT_MIN_SECOND); - } - - try { - configurationPollTime = dh.formattedStringToDuration(controllerPollProperties.getPollingTime()); - } catch (DateTimeParseException e) { - configurationPollTime = dh.getDurationByTimeValues(DEFAULT_POLL_HOUR, DEFAULT_POLL_MINUTE, - DEFAULT_POLL_SECOND); - } - - try { - configurationOverduePollTime = dh - .formattedStringToDuration(controllerPollProperties.getPollingOverdueTime()); - } catch (DateTimeParseException e) { - configurationOverduePollTime = dh.getDurationByTimeValues(DEFAULT_OVERDUE_HOUR, DEFAULT_OVERDUE_MINUTE, - DEFAULT_OVERDUE_SECOND); - } - - } - - private void validateGlobalDurations() { - - if (configurationMaximumPollTime.compareTo(configurationMinimumPollTime) < 0) { - // min value > max value -> use default values for both durations - LOG.warn("The configured maximum value of the polling time is smaller" - + " than the configured minimum value. Both are replaced by default values."); - - configurationMaximumPollTime = dh.getDurationByTimeValues(DEFAULT_MAX_HOUR, DEFAULT_MAX_MINUTE, - DEFAULT_MAX_SECOND); - configurationMinimumPollTime = dh.getDurationByTimeValues(DEFAULT_MIN_HOUR, DEFAULT_MIN_MINUTE, - DEFAULT_MIN_SECOND); - } - - if (!isWithinRange(configurationPollTime)) { - // poll time value not within allowed range ==> use default value - configurationPollTime = dh.getDurationByTimeValues(DEFAULT_POLL_HOUR, DEFAULT_POLL_MINUTE, - DEFAULT_POLL_SECOND); - } - - if (!isWithinRange(configurationOverduePollTime)) { - // overdue poll time value not within range => use default value - configurationOverduePollTime = dh.getDurationByTimeValues(DEFAULT_OVERDUE_HOUR, DEFAULT_OVERDUE_MINUTE, - DEFAULT_OVERDUE_SECOND); - } - } - - private boolean isWithinRange(@NotNull Duration duration) { - return duration.compareTo(configurationMinimumPollTime) > 0 - && duration.compareTo(configurationMaximumPollTime) < 0; - } - - /** - * @return the poll time interval stored in the tenant meta data. If there - * is no tenant specific configuration the global value, configured - * in the configuration {@code hawkbit.server.controller.polling} or - * the default value which is {@code 00:05:00} never {@code null}. - */ - public Duration getPollTimeInterval() { - Duration tenantPollTimeInterval = systemManagement.getTenantMetadata().getPollingTime(); - - if (tenantPollTimeInterval != null) { - if (isWithinRange(tenantPollTimeInterval)) { - return tenantPollTimeInterval; - } - LOG.warn( - "Tenant {} has stored a pollign interval {} which is not in the allowed range. Configured default value is loaded.", - systemManagement.currentTenant(), tenantPollTimeInterval); - } - return configurationPollTime; - } - - /** - * @return the poll time interval configured in the configuration - * {@code hawkbit.server.controller.polling} or the default value - * which is {@code 00:05:00} never {@code null}. This method ignores - * eventual tenant specific configurations. - */ - public Duration getGlobalPollTimeInterval() { - return configurationPollTime; - } - - /** - * @return the overdue poll time interval stored in the tenant meta data. If - * there is no tenant specific configuration the global value, - * configured in the configuration - * {@code hawkbit.server.controller.polling} or the default value - * which is {@code 00:05:00} never {@code null}. - */ - public Duration getOverduePollTimeInterval() { - Duration tenantOverduePollTimeInterval = systemManagement.getTenantMetadata().getPollingOverdueTime(); - - if (tenantOverduePollTimeInterval != null) { - if (isWithinRange(tenantOverduePollTimeInterval)) { - return tenantOverduePollTimeInterval; - } - LOG.warn( - "Tenant {} has stored an overdue polling interval {} which is not in the allowed range. Configured default value is loaded.", - systemManagement.currentTenant(), tenantOverduePollTimeInterval); - } - return configurationOverduePollTime; - }; - - /** - * @return the overdue poll time interval configured in the configuration - * {@code hawkbit.server.controller.polling.overdue} or the default - * value which is {@code 00:05:00} never {@code null}. - */ - public Duration getGlobalOverduePollTimeInterval() { - return configurationOverduePollTime; - } - - /** - * @return the maximum poll time duration configured in the configuration - * {@code hawkbit.server.controller.polling.overdue} or the default - * value which is {@code 23:59:00} never {@code null}. - */ - public Duration getMaximumPollingInterval() { - return configurationMaximumPollTime; - } - - /** - * @return the minimum poll time duration configured in the configuration - * {@code hawkbit.server.controller.polling.overdue} or the default - * value which is {@code 23:59:00} never {@code null}. - */ - public Duration getMinimumPollingInterval() { - return configurationMinimumPollTime; - } - - /** - * sets the ControllerPollProperties in a not spring handled context. Don't - * forget to call {@code initializeConfigurationValues} afterwards to read - * the values from the PollProperties. - * - * @param controllerPollProperties - * the controller poll properties - */ - public void setControllerPollProperties(ControllerPollProperties controllerPollProperties) { - this.controllerPollProperties = controllerPollProperties; - } - - /** - * sets the SystemManagement instance which is responsible for tenant - * specific configuration. - * - * @param systemManagement - * the SystemManagemnt instance - */ - public void setSystemManagement(SystemManagement systemManagement) { - this.systemManagement = systemManagement; - } -} diff --git a/hawkbit-repository/src/test/java/org/eclipse/hawkbit/repository/PollConfigurationHelperTest.java b/hawkbit-repository/src/test/java/org/eclipse/hawkbit/repository/PollConfigurationHelperTest.java deleted file mode 100644 index 4ee6e92f9..000000000 --- a/hawkbit-repository/src/test/java/org/eclipse/hawkbit/repository/PollConfigurationHelperTest.java +++ /dev/null @@ -1,147 +0,0 @@ -package org.eclipse.hawkbit.repository; - -import static org.fest.assertions.api.Assertions.assertThat; -import static org.mockito.Mockito.when; - -import java.time.Duration; - -import org.eclipse.hawkbit.ControllerPollProperties; -import org.eclipse.hawkbit.repository.model.TenantMetaData; -import org.eclipse.hawkbit.repository.model.helper.DurationHelper; -import org.eclipse.hawkbit.repository.model.helper.PollConfigurationHelper; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; - -@RunWith(MockitoJUnitRunner.class) -public class PollConfigurationHelperTest { - - @Mock - private ControllerPollProperties controllerPollProperties; - - @Mock - private SystemManagement systemManagement; - - @Mock - private TenantMetaData tenantMetaData; - - private PollConfigurationHelper pollConfigurationHelperUnderTest; - - private static final Duration DEFAULT_MIN = Duration.ofSeconds(30); - private static final Duration DEFAULT_MAX = Duration.ofHours(23).plusMinutes(59).plusSeconds(59); - private static final Duration DEFAULT_POLLING = Duration.ofMinutes(5); - private static final Duration DEFAULT_OVERDUE = Duration.ofMinutes(5); - - @Before - public void initMocks() { - - pollConfigurationHelperUnderTest = PollConfigurationHelper.getInstance(); - - setConfigurationValues("00:05:00", "00:05:00", "00:00:30", "23:59:59"); - setTenantConfiguration(null, null); - } - - private void setConfigurationValues(String polling, String overdue, String min, String max) { - when(controllerPollProperties.getPollingTime()).thenReturn(polling); - when(controllerPollProperties.getPollingOverdueTime()).thenReturn(overdue); - when(controllerPollProperties.getMinPollingTime()).thenReturn(min); - when(controllerPollProperties.getMaxPollingTime()).thenReturn(max); - - pollConfigurationHelperUnderTest.setControllerPollProperties(controllerPollProperties); - pollConfigurationHelperUnderTest.initializeConfigurationValues(); - } - - private void setTenantConfiguration(Duration polling, Duration overdue) { - - when(tenantMetaData.getPollingTime()).thenReturn(polling); - when(tenantMetaData.getPollingOverdueTime()).thenReturn(overdue); - - when(systemManagement.getTenantMetadata()).thenReturn(tenantMetaData); - - pollConfigurationHelperUnderTest.setSystemManagement(systemManagement); - } - - @Test - public void getCorrectConfigurationValues() { - - setConfigurationValues("00:08:00", "00:12:00", "00:01:00", "20:00:00"); - - assertThat(pollConfigurationHelperUnderTest.getMaximumPollingInterval()).isEqualTo(Duration.ofHours(20)); - assertThat(pollConfigurationHelperUnderTest.getMinimumPollingInterval()).isEqualTo(Duration.ofMinutes(1)); - assertThat(pollConfigurationHelperUnderTest.getGlobalPollTimeInterval()).isEqualTo(Duration.ofMinutes(8)); - assertThat(pollConfigurationHelperUnderTest.getGlobalOverduePollTimeInterval()) - .isEqualTo(Duration.ofMinutes(12)); - - assertThat(pollConfigurationHelperUnderTest.getPollTimeInterval()).isEqualTo(Duration.ofMinutes(8)); - assertThat(pollConfigurationHelperUnderTest.getOverduePollTimeInterval()).isEqualTo(Duration.ofMinutes(12)); - } - - @Test - public void getWrongFromattedConfiguratonValues() { - setConfigurationValues("00-08:00", "abc", "12:00:000", "20hours"); - - assertThat(pollConfigurationHelperUnderTest.getMaximumPollingInterval()).isEqualTo(DEFAULT_MAX); - assertThat(pollConfigurationHelperUnderTest.getMinimumPollingInterval()).isEqualTo(DEFAULT_MIN); - assertThat(pollConfigurationHelperUnderTest.getGlobalPollTimeInterval()).isEqualTo(DEFAULT_POLLING); - assertThat(pollConfigurationHelperUnderTest.getGlobalOverduePollTimeInterval()).isEqualTo(DEFAULT_OVERDUE); - } - - @Test - public void getMinimumGreaterMaximum() { - setConfigurationValues("00:07:00", "00:07:00", "01:00:00", "00:00:00"); - - assertThat(pollConfigurationHelperUnderTest.getMaximumPollingInterval()).isEqualTo(DEFAULT_MAX); - assertThat(pollConfigurationHelperUnderTest.getMinimumPollingInterval()).isEqualTo(DEFAULT_MIN); - assertThat(pollConfigurationHelperUnderTest.getGlobalPollTimeInterval()).isEqualTo(Duration.ofMinutes(7)); - assertThat(pollConfigurationHelperUnderTest.getGlobalOverduePollTimeInterval()) - .isEqualTo(Duration.ofMinutes(7)); - } - - @Test - public void getPollConfigurationNotWithinRange() { - setConfigurationValues("22:00:00", "00:07:00", "01:00:00", "10:00:00"); - - assertThat(pollConfigurationHelperUnderTest.getMaximumPollingInterval()).isEqualTo(Duration.ofHours(10)); - assertThat(pollConfigurationHelperUnderTest.getMinimumPollingInterval()).isEqualTo(Duration.ofHours(1)); - assertThat(pollConfigurationHelperUnderTest.getGlobalPollTimeInterval()).isEqualTo(DEFAULT_POLLING); - assertThat(pollConfigurationHelperUnderTest.getGlobalOverduePollTimeInterval()).isEqualTo(DEFAULT_OVERDUE); - } - - @Test - public void getPollingValuesFromTenant() { - - setTenantConfiguration(Duration.ofMinutes(11), Duration.ofMinutes(17)); - - assertThat(pollConfigurationHelperUnderTest.getPollTimeInterval()).isEqualTo(Duration.ofMinutes(11)); - assertThat(pollConfigurationHelperUnderTest.getOverduePollTimeInterval()).isEqualTo(Duration.ofMinutes(17)); - - } - - @Test - public void getInvalidPollingValuesFromTenant() { - - setTenantConfiguration(Duration.ZERO, Duration.ofHours(30)); - - assertThat(pollConfigurationHelperUnderTest.getPollTimeInterval()).isEqualTo(DEFAULT_POLLING); - assertThat(pollConfigurationHelperUnderTest.getOverduePollTimeInterval()).isEqualTo(DEFAULT_OVERDUE); - - } - - @Test - public void basicTestingOfDurationHelper() { - DurationHelper dh = new DurationHelper(); - - assertThat(dh.durationToFormattedString(null)).isNull(); - assertThat(dh.durationToFormattedString(Duration.ofHours(1).plusMinutes(2).plusSeconds(1))) - .isEqualTo("01:02:01"); - - assertThat(dh.formattedStringToDuration(null)).isNull(); - assertThat(dh.formattedStringToDuration("01:02:01")) - .isEqualTo(Duration.ofHours(1).plusMinutes(2).plusSeconds(1)); - - assertThat(dh.getDurationByTimeValues(0, 0, 0)).isEqualTo(Duration.ZERO); - - } -}