From 7ac21d31712849847b7b7901d28e6337bafd5d90 Mon Sep 17 00:00:00 2001 From: b_mountain <49973336+MRgenial@users.noreply.github.com> Date: Mon, 19 Aug 2024 00:41:49 +0800 Subject: [PATCH] [improve] Add GeneralConfigTypeEnum. (#2555) Co-authored-by: Calvin --- .../constants/GeneralConfigTypeEnum.java | 54 +++++++++++++++++++ .../impl/MailGeneralConfigServiceImpl.java | 3 +- .../impl/ObjectStoreConfigServiceImpl.java | 3 +- .../impl/SmsGeneralConfigServiceImpl.java | 3 +- .../impl/SystemGeneralConfigServiceImpl.java | 3 +- .../service/impl/SystemSecretServiceImpl.java | 3 +- .../impl/TemplateConfigServiceImpl.java | 3 +- .../manager/service/ConfigServiceTest.java | 16 +++--- .../service/MailGeneralConfigServiceTest.java | 3 +- .../service/ObjectStoreConfigServiceTest.java | 3 +- .../service/SmsGeneralConfigServiceTest.java | 3 +- .../SystemGeneralConfigServiceTest.java | 3 +- .../service/SystemSecretServiceTest.java | 3 +- .../service/TemplateConfigServiceTest.java | 3 +- 14 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 common/src/main/java/org/apache/hertzbeat/common/constants/GeneralConfigTypeEnum.java diff --git a/common/src/main/java/org/apache/hertzbeat/common/constants/GeneralConfigTypeEnum.java b/common/src/main/java/org/apache/hertzbeat/common/constants/GeneralConfigTypeEnum.java new file mode 100644 index 00000000000..6d8d0d74ed9 --- /dev/null +++ b/common/src/main/java/org/apache/hertzbeat/common/constants/GeneralConfigTypeEnum.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hertzbeat.common.constants; + +/** + * General Config Type Enum + */ +public enum GeneralConfigTypeEnum { + + /** + * template config + */ + template, + + /** + * system secret config + */ + secret, + + /** + * sms general config + */ + sms, + + /** + * system config + */ + system, + + /** + * mail general config + */ + email, + + /** + * system store config + */ + oss; +} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MailGeneralConfigServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MailGeneralConfigServiceImpl.java index aac05ad6b88..4397e1729f3 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MailGeneralConfigServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MailGeneralConfigServiceImpl.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.lang.reflect.Type; +import org.apache.hertzbeat.common.constants.GeneralConfigTypeEnum; import org.apache.hertzbeat.manager.dao.GeneralConfigDao; import org.apache.hertzbeat.manager.pojo.dto.EmailNoticeSender; import org.springframework.stereotype.Service; @@ -46,7 +47,7 @@ public MailGeneralConfigServiceImpl(GeneralConfigDao generalConfigDao, ObjectMap @Override public String type() { - return "email"; + return GeneralConfigTypeEnum.email.name(); } /** diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ObjectStoreConfigServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ObjectStoreConfigServiceImpl.java index 5a0dd1424b9..9682935d8ad 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ObjectStoreConfigServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ObjectStoreConfigServiceImpl.java @@ -23,6 +23,7 @@ import java.lang.reflect.Type; import javax.annotation.Resource; import lombok.extern.slf4j.Slf4j; +import org.apache.hertzbeat.common.constants.GeneralConfigTypeEnum; import org.apache.hertzbeat.manager.dao.GeneralConfigDao; import org.apache.hertzbeat.manager.pojo.dto.ObjectStoreConfigChangeEvent; import org.apache.hertzbeat.manager.pojo.dto.ObjectStoreDTO; @@ -63,7 +64,7 @@ public ObjectStoreConfigServiceImpl(GeneralConfigDao generalConfigDao, ObjectMap @Override public String type() { - return "oss"; + return GeneralConfigTypeEnum.oss.name(); } @Override diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SmsGeneralConfigServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SmsGeneralConfigServiceImpl.java index ebd8da36578..c7cd3647575 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SmsGeneralConfigServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SmsGeneralConfigServiceImpl.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.lang.reflect.Type; +import org.apache.hertzbeat.common.constants.GeneralConfigTypeEnum; import org.apache.hertzbeat.manager.dao.GeneralConfigDao; import org.apache.hertzbeat.manager.pojo.dto.SmsNoticeSender; import org.springframework.stereotype.Service; @@ -46,7 +47,7 @@ public SmsGeneralConfigServiceImpl(GeneralConfigDao generalConfigDao, ObjectMapp @Override public String type() { - return "sms"; + return GeneralConfigTypeEnum.sms.name(); } /** diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemGeneralConfigServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemGeneralConfigServiceImpl.java index 5d2b629a92f..396721afee5 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemGeneralConfigServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemGeneralConfigServiceImpl.java @@ -22,6 +22,7 @@ import jakarta.annotation.Resource; import java.lang.reflect.Type; import java.util.Objects; +import org.apache.hertzbeat.common.constants.GeneralConfigTypeEnum; import org.apache.hertzbeat.common.support.event.SystemConfigChangeEvent; import org.apache.hertzbeat.common.util.TimeZoneUtil; import org.apache.hertzbeat.manager.dao.GeneralConfigDao; @@ -60,7 +61,7 @@ public void handler(SystemConfig systemConfig) { @Override public String type() { - return "system"; + return GeneralConfigTypeEnum.system.name(); } @Override diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemSecretServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemSecretServiceImpl.java index 4c25b12adc3..2915298dd4d 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemSecretServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemSecretServiceImpl.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.lang.reflect.Type; +import org.apache.hertzbeat.common.constants.GeneralConfigTypeEnum; import org.apache.hertzbeat.manager.dao.GeneralConfigDao; import org.apache.hertzbeat.manager.pojo.dto.SystemSecret; import org.springframework.stereotype.Service; @@ -43,7 +44,7 @@ public SystemSecretServiceImpl(GeneralConfigDao generalConfigDao, ObjectMapper o @Override public String type() { - return "secret"; + return GeneralConfigTypeEnum.secret.name(); } @Override diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/TemplateConfigServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/TemplateConfigServiceImpl.java index 099e794d380..492918ca244 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/TemplateConfigServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/TemplateConfigServiceImpl.java @@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.annotation.Resource; import java.lang.reflect.Type; +import org.apache.hertzbeat.common.constants.GeneralConfigTypeEnum; import org.apache.hertzbeat.manager.dao.GeneralConfigDao; import org.apache.hertzbeat.manager.pojo.dto.TemplateConfig; import org.apache.hertzbeat.manager.service.AppService; @@ -55,7 +56,7 @@ public void handler(TemplateConfig templateConfig) { @Override public String type() { - return "template"; + return GeneralConfigTypeEnum.template.name(); } @Override diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/ConfigServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/ConfigServiceTest.java index ed7f1ac4e76..af7e1613601 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/ConfigServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/ConfigServiceTest.java @@ -25,6 +25,8 @@ import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.List; + +import org.apache.hertzbeat.common.constants.GeneralConfigTypeEnum; import org.apache.hertzbeat.manager.pojo.dto.EmailNoticeSender; import org.apache.hertzbeat.manager.pojo.dto.ObjectStoreDTO; import org.apache.hertzbeat.manager.pojo.dto.TemplateConfig; @@ -57,9 +59,9 @@ public class ConfigServiceTest { @BeforeEach public void setUp() { List generalConfigServices = new ArrayList<>(); - when(objectStoreConfigService.type()).thenReturn("oss"); - when(templateConfigService.type()).thenReturn("template"); - when(mailGeneralConfigService.type()).thenReturn("mail"); + when(objectStoreConfigService.type()).thenReturn(GeneralConfigTypeEnum.oss.name()); + when(templateConfigService.type()).thenReturn(GeneralConfigTypeEnum.template.name()); + when(mailGeneralConfigService.type()).thenReturn(GeneralConfigTypeEnum.email.name()); generalConfigServices.add(objectStoreConfigService); generalConfigServices.add(templateConfigService); generalConfigServices.add(mailGeneralConfigService); @@ -68,10 +70,10 @@ public void setUp() { @Test public void testSaveConfig() { - configService.saveConfig("oss", new ObjectStoreDTO<>()); + configService.saveConfig(GeneralConfigTypeEnum.oss.name(), new ObjectStoreDTO<>()); verify(objectStoreConfigService, times(1)).saveConfig(any(ObjectStoreDTO.class)); - configService.saveConfig("mail", new EmailNoticeSender()); + configService.saveConfig(GeneralConfigTypeEnum.email.name(), new EmailNoticeSender()); verify(mailGeneralConfigService, times(1)).saveConfig(any(EmailNoticeSender.class)); } @@ -79,11 +81,11 @@ public void testSaveConfig() { public void testGetConfig() { ObjectStoreDTO ossConfig = new ObjectStoreDTO<>(); when(objectStoreConfigService.getConfig()).thenReturn(ossConfig); - assertNotNull(configService.getConfig("oss")); + assertNotNull(configService.getConfig(GeneralConfigTypeEnum.oss.name())); EmailNoticeSender emailNoticeSender = new EmailNoticeSender(); when(mailGeneralConfigService.getConfig()).thenReturn(emailNoticeSender); - configService.getConfig("mail"); + configService.getConfig(GeneralConfigTypeEnum.email.name()); verify(mailGeneralConfigService, times(1)).getConfig(); } diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/MailGeneralConfigServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/MailGeneralConfigServiceTest.java index 5a9fc345458..9a7a662c460 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/MailGeneralConfigServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/MailGeneralConfigServiceTest.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.hertzbeat.common.constants.GeneralConfigTypeEnum; import org.apache.hertzbeat.manager.dao.GeneralConfigDao; import org.apache.hertzbeat.manager.pojo.dto.EmailNoticeSender; import org.apache.hertzbeat.manager.service.impl.MailGeneralConfigServiceImpl; @@ -54,7 +55,7 @@ void setUp() { @Test void testType() { - assertEquals("email", mailGeneralConfigService.type()); + assertEquals(GeneralConfigTypeEnum.email.name(), mailGeneralConfigService.type()); } @Test diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/ObjectStoreConfigServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/ObjectStoreConfigServiceTest.java index 95f123ffbf8..d9a0087888e 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/ObjectStoreConfigServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/ObjectStoreConfigServiceTest.java @@ -18,6 +18,7 @@ package org.apache.hertzbeat.manager.service; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.hertzbeat.common.constants.GeneralConfigTypeEnum; import org.apache.hertzbeat.manager.pojo.dto.ObjectStoreConfigChangeEvent; import org.apache.hertzbeat.manager.pojo.dto.ObjectStoreDTO; import org.apache.hertzbeat.manager.service.impl.ObjectStoreConfigServiceImpl; @@ -66,7 +67,7 @@ void setUp() { void testGetType() { String type = objectStoreConfigService.type(); - assertEquals("oss", type); + assertEquals(GeneralConfigTypeEnum.oss.name(), type); } @Test diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/SmsGeneralConfigServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/SmsGeneralConfigServiceTest.java index ab98888a968..2a1e8be3c58 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/SmsGeneralConfigServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/SmsGeneralConfigServiceTest.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.hertzbeat.common.constants.GeneralConfigTypeEnum; import org.apache.hertzbeat.manager.dao.GeneralConfigDao; import org.apache.hertzbeat.manager.pojo.dto.SmsNoticeSender; import org.apache.hertzbeat.manager.service.impl.SmsGeneralConfigServiceImpl; @@ -60,7 +61,7 @@ void setUp() { @Test void testType() { String result = service.type(); - assertEquals("sms", result); + assertEquals(GeneralConfigTypeEnum.sms.name(), result); } @Test diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/SystemGeneralConfigServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/SystemGeneralConfigServiceTest.java index 942928f3f4b..defb241b06c 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/SystemGeneralConfigServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/SystemGeneralConfigServiceTest.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.hertzbeat.common.constants.GeneralConfigTypeEnum; import org.apache.hertzbeat.manager.dao.GeneralConfigDao; import org.apache.hertzbeat.manager.pojo.dto.SystemConfig; import org.apache.hertzbeat.manager.service.impl.SystemGeneralConfigServiceImpl; @@ -58,7 +59,7 @@ void setUp() { void testType() { String result = service.type(); - assertEquals("system", result); + assertEquals(GeneralConfigTypeEnum.system.name(), result); } @Test diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/SystemSecretServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/SystemSecretServiceTest.java index 688847e3c2b..21f1986dcf1 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/SystemSecretServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/SystemSecretServiceTest.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.hertzbeat.common.constants.GeneralConfigTypeEnum; import org.apache.hertzbeat.manager.dao.GeneralConfigDao; import org.apache.hertzbeat.manager.pojo.dto.SystemSecret; import org.apache.hertzbeat.manager.service.impl.SystemSecretServiceImpl; @@ -54,7 +55,7 @@ void setUp() { @Test void testType() { - assertEquals("secret", systemSecretService.type()); + assertEquals(GeneralConfigTypeEnum.secret.name(), systemSecretService.type()); } @Test diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/TemplateConfigServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/TemplateConfigServiceTest.java index 81ba4f6dd2b..872167d5bd5 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/TemplateConfigServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/TemplateConfigServiceTest.java @@ -18,6 +18,7 @@ package org.apache.hertzbeat.manager.service; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.hertzbeat.common.constants.GeneralConfigTypeEnum; import org.apache.hertzbeat.manager.dao.GeneralConfigDao; import org.apache.hertzbeat.manager.pojo.dto.TemplateConfig; import org.apache.hertzbeat.manager.service.impl.TemplateConfigServiceImpl; @@ -88,7 +89,7 @@ void testHandlerNullTemplateConfig() { void testType() { String type = templateConfigServiceImpl.type(); - assertEquals("template", type); + assertEquals(GeneralConfigTypeEnum.template.name(), type); } }