Add settings to Db for Custom Logo (#11583)

* Add settings to Db for Custom Logo

* Add test for Settings
This commit is contained in:
Mohit Yadav 2023-05-12 23:34:02 +05:30 committed by GitHub
parent 4c77eaea72
commit d12d32bf65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 106 additions and 6 deletions

View File

@ -45,6 +45,7 @@ import org.jdbi.v3.sqlobject.customizer.BindMap;
import org.jdbi.v3.sqlobject.customizer.Define;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import org.openmetadata.api.configuration.LogoConfiguration;
import org.openmetadata.common.utils.CommonUtil;
import org.openmetadata.schema.TokenInterface;
import org.openmetadata.schema.analytics.ReportData;
@ -3132,6 +3133,9 @@ public interface CollectionDAO {
case EMAIL_CONFIGURATION:
value = JsonUtils.readValue(json, SmtpSettings.class);
break;
case CUSTOM_LOGO_CONFIGURATION:
value = JsonUtils.readValue(json, LogoConfiguration.class);
break;
default:
throw new IllegalArgumentException("Invalid Settings Type " + configType);
}

View File

@ -13,6 +13,7 @@
package org.openmetadata.service.resources.settings;
import static org.openmetadata.schema.settings.SettingsType.CUSTOM_LOGO_CONFIGURATION;
import static org.openmetadata.schema.settings.SettingsType.EMAIL_CONFIGURATION;
import com.google.common.cache.CacheBuilder;
@ -21,6 +22,7 @@ import com.google.common.cache.LoadingCache;
import java.util.concurrent.TimeUnit;
import javax.annotation.CheckForNull;
import lombok.extern.slf4j.Slf4j;
import org.openmetadata.api.configuration.LogoConfiguration;
import org.openmetadata.schema.email.SmtpSettings;
import org.openmetadata.schema.settings.Settings;
import org.openmetadata.schema.settings.SettingsType;
@ -44,11 +46,12 @@ public class SettingsCache {
CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(3, TimeUnit.MINUTES).build(new SettingsLoader());
systemRepository = new SystemRepository(dao.systemDAO());
INITIALIZED = true;
createEmailConfiguration(config);
createDefaultConfiguration(config);
}
}
private static void createEmailConfiguration(OpenMetadataApplicationConfig applicationConfig) {
private static void createDefaultConfiguration(OpenMetadataApplicationConfig applicationConfig) {
// Initialise Email Setting
Settings storedSettings = systemRepository.getConfigWithKey(EMAIL_CONFIGURATION.toString());
if (storedSettings == null) {
// Only in case a config doesn't exist in DB we insert it
@ -56,6 +59,17 @@ public class SettingsCache {
Settings setting = new Settings().withConfigType(EMAIL_CONFIGURATION).withConfigValue(emailConfig);
systemRepository.createNewSetting(setting);
}
// Initialise Logo Setting
Settings storedCustomLogoConf = systemRepository.getConfigWithKey(CUSTOM_LOGO_CONFIGURATION.toString());
if (storedCustomLogoConf == null) {
// Only in case a config doesn't exist in DB we insert it
LogoConfiguration logoConfig = applicationConfig.getApplicationConfiguration().getLogoConfig();
if (logoConfig != null) {
Settings setting = new Settings().withConfigType(CUSTOM_LOGO_CONFIGURATION).withConfigValue(logoConfig);
systemRepository.createNewSetting(setting);
}
}
}
public static SettingsCache getInstance() {

View File

@ -2,17 +2,27 @@ package org.openmetadata.service.resources.system;
import static org.openmetadata.service.util.TestUtils.ADMIN_AUTH_HEADERS;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.dropwizard.configuration.ConfigurationException;
import io.dropwizard.configuration.FileConfigurationSourceProvider;
import io.dropwizard.configuration.YamlConfigurationFactory;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.jersey.validation.Validators;
import java.io.IOException;
import java.net.URISyntaxException;
import javax.validation.Validator;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.HttpResponseException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestMethodOrder;
import org.openmetadata.api.configuration.LogoConfiguration;
import org.openmetadata.schema.api.data.CreateContainer;
import org.openmetadata.schema.api.data.CreateDashboard;
import org.openmetadata.schema.api.data.CreateGlossary;
@ -30,11 +40,15 @@ import org.openmetadata.schema.api.teams.CreateTeam;
import org.openmetadata.schema.api.teams.CreateUser;
import org.openmetadata.schema.api.tests.CreateTestSuite;
import org.openmetadata.schema.auth.SSOAuthMechanism;
import org.openmetadata.schema.email.SmtpSettings;
import org.openmetadata.schema.entity.data.Table;
import org.openmetadata.schema.entity.teams.AuthenticationMechanism;
import org.openmetadata.schema.security.client.GoogleSSOClientConfig;
import org.openmetadata.schema.settings.Settings;
import org.openmetadata.schema.settings.SettingsType;
import org.openmetadata.schema.util.EntitiesCount;
import org.openmetadata.schema.util.ServicesCount;
import org.openmetadata.service.OpenMetadataApplicationConfig;
import org.openmetadata.service.OpenMetadataApplicationTest;
import org.openmetadata.service.resources.EntityResourceTest;
import org.openmetadata.service.resources.dashboards.DashboardResourceTest;
@ -53,11 +67,23 @@ import org.openmetadata.service.resources.storages.ContainerResourceTest;
import org.openmetadata.service.resources.teams.TeamResourceTest;
import org.openmetadata.service.resources.teams.UserResourceTest;
import org.openmetadata.service.resources.topics.TopicResourceTest;
import org.openmetadata.service.util.JsonUtils;
import org.openmetadata.service.util.TestUtils;
@Slf4j
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class SystemResourceTest extends OpenMetadataApplicationTest {
static OpenMetadataApplicationConfig config;
@BeforeAll
static void setup() throws IOException, ConfigurationException {
// Get config object from test yaml file
ObjectMapper objectMapper = Jackson.newObjectMapper();
Validator validator = Validators.newValidator();
YamlConfigurationFactory<OpenMetadataApplicationConfig> factory =
new YamlConfigurationFactory<>(OpenMetadataApplicationConfig.class, validator, objectMapper, "dw");
config = factory.build(new FileConfigurationSourceProvider(), CONFIG_PATH);
}
@BeforeAll
public static void setup(TestInfo test) throws IOException, URISyntaxException {
@ -66,7 +92,7 @@ public class SystemResourceTest extends OpenMetadataApplicationTest {
}
@Test
public void entitiesCount(TestInfo test) throws HttpResponseException {
void entitiesCount(TestInfo test) throws HttpResponseException {
// Get count before adding entities
EntitiesCount beforeCount = getEntitiesCount();
@ -139,7 +165,52 @@ public class SystemResourceTest extends OpenMetadataApplicationTest {
}
@Test
public void servicesCount(TestInfo test) throws HttpResponseException {
@Order(1)
void testSystemConfigs() throws HttpResponseException {
// Test Email Config
Settings emailSettings = getSystemConfig(SettingsType.EMAIL_CONFIGURATION);
SmtpSettings smtp = JsonUtils.convertValue(emailSettings.getConfigValue(), SmtpSettings.class);
// Password for Email is always sent in hidden
SmtpSettings expected = config.getSmtpSettings();
expected.setPassword("***********");
Assertions.assertEquals(expected, smtp);
// Test Custom Log Config
Settings logoConfigWrapped = getSystemConfig(SettingsType.CUSTOM_LOGO_CONFIGURATION);
LogoConfiguration loginConfiguration =
JsonUtils.convertValue(logoConfigWrapped.getConfigValue(), LogoConfiguration.class);
Assertions.assertEquals(config.getApplicationConfiguration().getLogoConfig(), loginConfiguration);
}
@Test
void testSystemConfigsUpdate(TestInfo test) throws HttpResponseException {
// Test Email Config
SmtpSettings smtpSettings = config.getSmtpSettings();
// Update a few Email fields
smtpSettings.setUsername(test.getDisplayName());
smtpSettings.setEmailingEntity(test.getDisplayName());
updateSystemConfig(new Settings().withConfigType(SettingsType.EMAIL_CONFIGURATION).withConfigValue(smtpSettings));
SmtpSettings updateEmailSettings =
JsonUtils.convertValue(getSystemConfig(SettingsType.EMAIL_CONFIGURATION).getConfigValue(), SmtpSettings.class);
Assertions.assertEquals(updateEmailSettings.getUsername(), test.getDisplayName());
Assertions.assertEquals(updateEmailSettings.getEmailingEntity(), test.getDisplayName());
// Test Custom Logo Update
LogoConfiguration updateConfigReq =
new LogoConfiguration().withCustomLogoUrlPath("http://test.com").withCustomMonogramUrlPath("http://test.com");
// Update Custom Logo Settings
updateSystemConfig(
new Settings().withConfigType(SettingsType.CUSTOM_LOGO_CONFIGURATION).withConfigValue(updateConfigReq));
LogoConfiguration updatedConfig =
JsonUtils.convertValue(
getSystemConfig(SettingsType.CUSTOM_LOGO_CONFIGURATION).getConfigValue(), LogoConfiguration.class);
Assertions.assertEquals(updateConfigReq, updatedConfig);
}
@Test
void servicesCount(TestInfo test) throws HttpResponseException {
// Get count before adding services
ServicesCount beforeCount = getServicesCount();
@ -183,7 +254,7 @@ public class SystemResourceTest extends OpenMetadataApplicationTest {
}
@Test
public void botUserCountCheck(TestInfo test) throws HttpResponseException {
void botUserCountCheck(TestInfo test) throws HttpResponseException {
int beforeUserCount = getEntitiesCount().getUserCount();
// Create a bot user.
@ -216,4 +287,14 @@ public class SystemResourceTest extends OpenMetadataApplicationTest {
WebTarget target = getResource("system/services/count");
return TestUtils.get(target, ServicesCount.class, ADMIN_AUTH_HEADERS);
}
private static Settings getSystemConfig(SettingsType settingsType) throws HttpResponseException {
WebTarget target = getResource(String.format("system/settings/%s", settingsType.value()));
return TestUtils.get(target, Settings.class, ADMIN_AUTH_HEADERS);
}
private static void updateSystemConfig(Settings updatedSetting) throws HttpResponseException {
WebTarget target = getResource("system/settings");
TestUtils.put(target, updatedSetting, Response.Status.OK, ADMIN_AUTH_HEADERS);
}
}

View File

@ -22,7 +22,8 @@
"secretsManagerConfiguration",
"sandboxModeEnabled",
"slackChat",
"emailConfiguration"
"emailConfiguration",
"customLogoConfiguration"
]
}
},