Added validation before saving TenantConfiguration and updated tests

* run the validation methodes inside the storing methode, to make sure that only
  valid values are written inside the database
* updated TenantConfigurationManagementTest to match the validation
* added new Tests to test all validators

Signed-off-by: Nonnenmacher Fabian <fabian.nonnenmacher@bosch-si.com>
This commit is contained in:
Fabian Nonnenmacher
2016-01-28 18:16:31 +01:00
committed by Nonnenmacher Fabian
parent d83286c94a
commit 4be880587a
9 changed files with 193 additions and 74 deletions

View File

@@ -2,11 +2,17 @@ package org.eclipse.hawkbit.repository;
import static org.fest.assertions.api.Assertions.assertThat;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.hawkbit.AbstractIntegrationTestWithMongoDB;
import org.eclipse.hawkbit.repository.model.TenantConfiguration;
import org.eclipse.hawkbit.repository.model.TenantConfigurationValue;
import org.eclipse.hawkbit.tenancy.configuration.DurationHelper;
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey;
import org.eclipse.hawkbit.tenancy.configuration.validator.exceptions.TenantConfigurationValidatorException;
import org.junit.Test;
import org.springframework.core.convert.ConversionFailedException;
import ru.yandex.qatools.allure.annotations.Description;
import ru.yandex.qatools.allure.annotations.Features;
@@ -16,70 +22,75 @@ import ru.yandex.qatools.allure.annotations.Stories;
@Stories("Tenant Configuration Management")
public class TenantConfigurationManagementTest extends AbstractIntegrationTestWithMongoDB {
final DurationHelper durationHelper = new DurationHelper();
@Test
@Description("Tests that tenant specific configuration can be persisted and in case the tenant does not have specific configuration the default from environment is used instead.")
public void storeTenantSpecificConfiguration() {
final TenantConfigurationKey configKey = TenantConfigurationKey.AUTHENTICATION_MODE_HEADER_ENABLED;
public void storeTenantSpecificConfigurationAsString() {
final TenantConfigurationKey configKey = TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_NAME;
final String envPropertyDefault = environment.getProperty(configKey.getDefaultKeyName());
assertThat(envPropertyDefault).isNotNull();
// get the configuration from the system management
final String defaultConfigValue = tenantConfigurationManagement.getConfigurationValue(configKey, String.class)
.getValue();
assertThat(envPropertyDefault).isEqualTo(defaultConfigValue);
final TenantConfigurationValue<String> defaultConfigValue = tenantConfigurationManagement
.getConfigurationValue(configKey, String.class);
assertThat(defaultConfigValue.isGlobal()).isEqualTo(true);
assertThat(defaultConfigValue.getValue()).isEqualTo(envPropertyDefault);
// update the tenant specific configuration
final String newConfigurationValue = "thisIsAnotherValueForPolling";
assertThat(newConfigurationValue).isNotEqualTo(defaultConfigValue);
tenantConfigurationManagement
.addOrUpdateConfiguration(new TenantConfiguration(configKey.getKeyName(), newConfigurationValue));
final String newConfigurationValue = "thisIsAnotherTokenName";
assertThat(newConfigurationValue).isNotEqualTo(defaultConfigValue.getValue());
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, newConfigurationValue);
// verify that new configuration value is used
final String updatedConfigurationValue = tenantConfigurationManagement
.getConfigurationValue(configKey, String.class).getValue();
assertThat(updatedConfigurationValue).isEqualTo(newConfigurationValue);
assertThat(tenantConfigurationManagement.getTenantConfigurations()).hasSize(1);
final TenantConfigurationValue<String> updatedConfigurationValue = tenantConfigurationManagement
.getConfigurationValue(configKey, String.class);
assertThat(updatedConfigurationValue.isGlobal()).isEqualTo(false);
assertThat(updatedConfigurationValue.getValue()).isEqualTo(newConfigurationValue);
// assertThat(tenantConfigurationManagement.getTenantConfigurations()).hasSize(1);
}
@Test
@Description("Tests that the tenant specific configuration can be updated")
public void updateTenantSpecifcConfiguration() {
final TenantConfigurationKey configKey = TenantConfigurationKey.AUTHENTICATION_MODE_HEADER_ENABLED;
final TenantConfigurationKey configKey = TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_NAME;
final String value1 = "firstValue";
final String value2 = "secondValue";
// add value first
tenantConfigurationManagement.addOrUpdateConfiguration(new TenantConfiguration(configKey.getKeyName(), value1));
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, value1);
assertThat(tenantConfigurationManagement.getConfigurationValue(configKey, String.class).getValue())
.isEqualTo(value1);
// update to value second
tenantConfigurationManagement.addOrUpdateConfiguration(new TenantConfiguration(configKey.getKeyName(), value2));
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, value2);
assertThat(tenantConfigurationManagement.getConfigurationValue(configKey, String.class).getValue())
.isEqualTo(value2);
}
@Test
@Description("Tests that the configuration value can be converted from String to Integer automatically")
public void tenantConfigurationValueConversion() {
public void storeAndUpdateTenantSpecificConfigurationAsBoolean() {
final TenantConfigurationKey configKey = TenantConfigurationKey.AUTHENTICATION_MODE_HEADER_ENABLED;
final Integer value1 = 123;
tenantConfigurationManagement
.addOrUpdateConfiguration(new TenantConfiguration(configKey.getKeyName(), String.valueOf(value1)));
assertThat(tenantConfigurationManagement.getConfigurationValue(configKey, Integer.class).getValue())
final Boolean value1 = true;
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, 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);
}
@Test(expected = ConversionFailedException.class)
@Description("Tests that the get configuration throws exception in case the value cannot be automatically converted from String to Integer")
public void wrongTenantConfigurationValueConversionThrowsException() {
@Test(expected = TenantConfigurationValidatorException.class)
@Description("Tests that the get configuration throws exception in case the value cannot be automatically converted from String to Boolean")
public void wrongTenantConfigurationValueTypeThrowsException() {
final TenantConfigurationKey configKey = TenantConfigurationKey.AUTHENTICATION_MODE_HEADER_ENABLED;
final String value1 = "thisIsNotANumber";
final String value1 = "thisIsNotABoolean";
// add value as String
tenantConfigurationManagement
.addOrUpdateConfiguration(new TenantConfiguration(configKey.getKeyName(), String.valueOf(value1)));
// try to get it as Integer
tenantConfigurationManagement.getConfigurationValue(configKey, Integer.class);
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, value1);
}
@Test
@@ -96,8 +107,7 @@ public class TenantConfigurationManagementTest extends AbstractIntegrationTestWi
// update the tenant specific configuration
final String newConfigurationValue = "thisIsAnotherValueForPolling";
assertThat(newConfigurationValue).isNotEqualTo(defaultConfigValue);
tenantConfigurationManagement
.addOrUpdateConfiguration(new TenantConfiguration(configKey.getKeyName(), newConfigurationValue));
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, newConfigurationValue);
// verify that new configuration value is used
final String updatedConfigurationValue = tenantConfigurationManagement
@@ -110,4 +120,92 @@ public class TenantConfigurationManagementTest extends AbstractIntegrationTestWi
// must be null now
assertThat(tenantConfigurationManagement.getConfigurationValue(configKey, String.class).getValue()).isNull();
}
@Test(expected = TenantConfigurationValidatorException.class)
@Description("Test that an Exception is thrown, when an integer is stored but a string expected.")
public void storesIntegerWhenStringIsExpected() {
final TenantConfigurationKey configKey = TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_NAME;
final Integer wrongDataype = 123;
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDataype);
}
@Test(expected = TenantConfigurationValidatorException.class)
@Description("Test that an Exception is thrown, when an integer is stored but a boolean expected.")
public void storesIntegerWhenBooleanIsExpected() {
final TenantConfigurationKey configKey = TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_ENABLED;
final Integer wrongDataype = 123;
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDataype);
}
@Test(expected = TenantConfigurationValidatorException.class)
@Description("Test that an Exception is thrown, when an integer is stored as PollingTime.")
public void storesIntegerWhenPollingIntervalIsExpected() {
final TenantConfigurationKey configKey = TenantConfigurationKey.POLLING_TIME_INTERVAL;
final Integer wrongDataype = 123;
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDataype);
}
@Test(expected = TenantConfigurationValidatorException.class)
@Description("Test that an Exception is thrown, when an invalid formatted string is stored as PollingTime.")
public void storesWrongFormattedStringAsPollingInterval() {
final TenantConfigurationKey configKey = TenantConfigurationKey.POLLING_TIME_INTERVAL;
final String wrongFormatted = "wrongFormatted";
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongFormatted);
}
@Test(expected = TenantConfigurationValidatorException.class)
@Description("Test that an Exception is thrown, when an invalid formatted string is stored as PollingTime.")
public void storesTooSmallDurationAsPollingInterval() {
final TenantConfigurationKey configKey = TenantConfigurationKey.POLLING_TIME_INTERVAL;
final String tooSmallDuration = durationHelper
.durationToFormattedString(durationHelper.getDurationByTimeValues(0, 0, 1));
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, tooSmallDuration);
}
@Test
@Description("Stores a correct formatted PollignTime and reads it again.")
public void storesCorrectDurationAsPollingInterval() {
final TenantConfigurationKey configKey = TenantConfigurationKey.POLLING_TIME_INTERVAL;
final Duration duration = durationHelper.getDurationByTimeValues(1, 2, 0);
assertThat(duration).isEqualTo(Duration.ofHours(1).plusMinutes(2));
tenantConfigurationManagement.addOrUpdateConfiguration(configKey,
durationHelper.durationToFormattedString(duration));
final String storedDurationString = tenantConfigurationManagement.getConfigurationValue(configKey, String.class)
.getValue();
assertThat(duration).isEqualTo(durationHelper.formattedStringToDuration(storedDurationString));
}
@Test(expected = IllegalAccessError.class)
@Description("Request a config value in a wrong Value")
public void requestConfigValueWithWrongType() {
tenantConfigurationManagement.getConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, Object.class);
}
@Test
@Description("Verifies that every TenenatConfiguraationKeyName exists only once")
public void verifyThatAllKeysAreDifferent() {
final Map<String, Void> keynames = new HashMap<String, Void>();
Arrays.stream(TenantConfigurationKey.values()).forEach(key -> {
if (keynames.containsKey(key.getKeyName())) {
throw new IllegalStateException("The key names are not unique");
}
keynames.put(key.getKeyName(), null);
});
}
@Test
@Description("Get TenantConfigurationKeyByName")
public void getTenantConfigurationKeyByName() {
final TenantConfigurationKey configKey = TenantConfigurationKey.POLLING_TIME_INTERVAL;
assertThat(TenantConfigurationKey.fromKeyName(configKey.getKeyName())).isEqualTo(configKey);
}
}

View File

@@ -11,6 +11,7 @@ spring.data.mongodb.uri=mongodb://localhost/spArtifactRepository${random.value}
spring.data.mongodb.port=28017
hawkbit.server.controller.security.authentication.header.enabled=true
hawkbit.server.controller.security.authentication.gatewaytoken.name=TestToken
hawkbit.server.artifact.repo.upload.maxFileSize=5MB