Refactor the tenantcy configuration (#2080)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-11-19 08:45:50 +02:00
committed by GitHub
parent 7ac0a13054
commit 4861e7c29f
10 changed files with 102 additions and 159 deletions

View File

@@ -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<String> 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<String, Serializable> configuration = new HashMap<>() {{
final Map<String, Serializable> 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<String, Void> keynames = new HashMap<>();
final Map<String, Void> 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);
}
}
}