From 2e96092ef9be441d181e0df9216b8ad45957d44e Mon Sep 17 00:00:00 2001 From: pwallk <1363539513@qq.com> Date: Tue, 30 Jul 2024 16:04:47 +0800 Subject: [PATCH 1/2] move code from GeneralConfigController to ConfigService --- .../controller/GeneralConfigController.java | 42 ++------- .../manager/service/ConfigService.java | 47 ++++++++++ .../manager/service/GeneralConfigService.java | 2 +- .../service/impl/ConfigServiceImpl.java | 81 +++++++++++++++++ .../manager/service/ConfigServiceTest.java | 90 +++++++++++++++++++ 5 files changed, 226 insertions(+), 36 deletions(-) create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/service/ConfigService.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ConfigServiceImpl.java create mode 100644 manager/src/test/java/org/apache/hertzbeat/manager/service/ConfigServiceTest.java diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/GeneralConfigController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/GeneralConfigController.java index 49f48d482d2..8747bc8b9d7 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/GeneralConfigController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/GeneralConfigController.java @@ -21,15 +21,12 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; import jakarta.validation.constraints.NotNull; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.common.entity.dto.Message; import org.apache.hertzbeat.manager.pojo.dto.TemplateConfig; -import org.apache.hertzbeat.manager.service.GeneralConfigService; -import org.apache.hertzbeat.manager.service.impl.TemplateConfigServiceImpl; +import org.apache.hertzbeat.manager.service.ConfigService; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -47,14 +44,9 @@ @Tag(name = "Alert sender Configuration API") @Slf4j public class GeneralConfigController { - private final Map configServiceMap; + @Resource + private ConfigService configService; - public GeneralConfigController(List generalConfigServices) { - configServiceMap = new HashMap<>(8); - if (generalConfigServices != null) { - generalConfigServices.forEach(config -> configServiceMap.put(config.type(), config)); - } - } @PostMapping(path = "/{type}") @Operation(summary = "Save or update common config", description = "Save or update common config") @@ -62,11 +54,7 @@ public ResponseEntity> saveOrUpdateConfig( @Parameter(description = "Config Type", example = "email") @PathVariable("type") @NotNull final String type, @RequestBody Object config) { - GeneralConfigService configService = configServiceMap.get(type); - if (configService == null) { - throw new IllegalArgumentException("Not supported this config type: " + type); - } - configService.saveConfig(config); + configService.saveConfig(type, config); return ResponseEntity.ok(Message.success("Update config success")); } @@ -75,11 +63,7 @@ public ResponseEntity> saveOrUpdateConfig( public ResponseEntity> getConfig( @Parameter(description = "Config Type", example = "email") @PathVariable("type") @NotNull final String type) { - GeneralConfigService configService = configServiceMap.get(type); - if (configService == null) { - throw new IllegalArgumentException("Not supported this config type: " + type); - } - return ResponseEntity.ok(Message.success(configService.getConfig())); + return ResponseEntity.ok(Message.success(configService.getConfig(type))); } @PutMapping(path = "/template/{app}") @@ -87,19 +71,7 @@ public ResponseEntity> getConfig( public ResponseEntity> updateTemplateAppConfig( @PathVariable("app") @NotNull final String app, @RequestBody TemplateConfig.AppTemplate template) { - GeneralConfigService configService = configServiceMap.get("template"); - if (configService == null || !(configService instanceof TemplateConfigServiceImpl)) { - throw new IllegalArgumentException("Not supported this config type: template"); - } - TemplateConfig config = ((TemplateConfigServiceImpl) configService).getConfig(); - if (config == null) { - config = new TemplateConfig(); - } - if (config.getApps() == null) { - config.setApps(new HashMap<>(8)); - } - config.getApps().put(app, template); - configService.saveConfig(config); + configService.updateTemplateAppConfig(app, template); return ResponseEntity.ok(Message.success()); } } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/ConfigService.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/ConfigService.java new file mode 100644 index 00000000000..418c7c5e256 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/ConfigService.java @@ -0,0 +1,47 @@ +/* + * 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.manager.service; + +import org.apache.hertzbeat.manager.pojo.dto.TemplateConfig; + +/** + * Provides operations for the GeneralConfigService + */ +public interface ConfigService { + + /** + * save config + * @param type config type + * @param config need save configuration + */ + void saveConfig(String type, Object config); + + /** + * get config + * @param type config type + * @return config + */ + Object getConfig(String type); + + /** + * Update the app template config + * @param app monitoring type + * @param template template config + */ + void updateTemplateAppConfig(String app, TemplateConfig.AppTemplate template); +} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/GeneralConfigService.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/GeneralConfigService.java index 0f381bf6821..3d6c956d8f0 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/GeneralConfigService.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/GeneralConfigService.java @@ -18,7 +18,7 @@ package org.apache.hertzbeat.manager.service; /** - *

ConfigService interface provides CRUD operations for configurations.

+ *

GeneralConfigService interface provides CRUD operations for configurations.

* @param configuration type. * @version 1.0 */ diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ConfigServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ConfigServiceImpl.java new file mode 100644 index 00000000000..345e2b3b4c9 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ConfigServiceImpl.java @@ -0,0 +1,81 @@ +/* + * 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.manager.service.impl; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.hertzbeat.manager.pojo.dto.TemplateConfig; +import org.apache.hertzbeat.manager.service.ConfigService; +import org.apache.hertzbeat.manager.service.GeneralConfigService; +import org.springframework.stereotype.Component; + + +/** + * GeneralConfigService proxy class + */ +@Component +public class ConfigServiceImpl implements ConfigService { + + private static final String TEMPLATE_CONFIG_TYPE = "template"; + + private final Map configServiceMap; + + public ConfigServiceImpl(List generalConfigServices){ + configServiceMap = new ConcurrentHashMap<>(8); + if (generalConfigServices != null) { + generalConfigServices.forEach(config -> configServiceMap.put(config.type(), config)); + } + } + + @Override + public void saveConfig(String type, Object config) { + GeneralConfigService configService = configServiceMap.get(type); + if (configService == null) { + throw new IllegalArgumentException("Not supported this config type: " + type); + } + configService.saveConfig(config); + } + + @Override + public Object getConfig(String type) { + GeneralConfigService configService = configServiceMap.get(type); + if (configService == null) { + throw new IllegalArgumentException("Not supported this config type: " + type); + } + return configService.getConfig(); + } + + @Override + public void updateTemplateAppConfig(String app, TemplateConfig.AppTemplate template){ + GeneralConfigService configService = configServiceMap.get(TEMPLATE_CONFIG_TYPE); + if (!(configService instanceof TemplateConfigServiceImpl)) { + throw new IllegalArgumentException("Not supported this config type: template"); + } + TemplateConfig config = ((TemplateConfigServiceImpl) configService).getConfig(); + if (config == null) { + config = new TemplateConfig(); + } + if (config.getApps() == null) { + config.setApps(new HashMap<>(8)); + } + config.getApps().put(app, template); + configService.saveConfig(config); + } +} 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 new file mode 100644 index 00000000000..8e002da972f --- /dev/null +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/ConfigServiceTest.java @@ -0,0 +1,90 @@ +package org.apache.hertzbeat.manager.service; + + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.util.ArrayList; +import java.util.List; +import org.apache.hertzbeat.manager.pojo.dto.EmailNoticeSender; +import org.apache.hertzbeat.manager.pojo.dto.ObjectStoreDTO; +import org.apache.hertzbeat.manager.pojo.dto.TemplateConfig; +import org.apache.hertzbeat.manager.service.impl.ConfigServiceImpl; +import org.apache.hertzbeat.manager.service.impl.MailGeneralConfigServiceImpl; +import org.apache.hertzbeat.manager.service.impl.ObjectStoreConfigServiceImpl; +import org.apache.hertzbeat.manager.service.impl.TemplateConfigServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + + +/** + * Test case for {@link ConfigService} + */ +@ExtendWith(MockitoExtension.class) +public class ConfigServiceTest { + + @InjectMocks + private ConfigServiceImpl configService; + @Mock + private ObjectStoreConfigServiceImpl objectStoreConfigService; + @Mock + private TemplateConfigServiceImpl templateConfigService; + @Mock + private MailGeneralConfigServiceImpl mailGeneralConfigService; + + @BeforeEach + public void setUp() { + List generalConfigServices = new ArrayList<>(); + when(objectStoreConfigService.type()).thenReturn("oss"); + when(templateConfigService.type()).thenReturn("template"); + when(mailGeneralConfigService.type()).thenReturn("mail"); + generalConfigServices.add(objectStoreConfigService); + generalConfigServices.add(templateConfigService); + generalConfigServices.add(mailGeneralConfigService); + configService = new ConfigServiceImpl(generalConfigServices); + } + + @Test + public void testSaveConfig() { + configService.saveConfig("oss", new ObjectStoreDTO<>()); + verify(objectStoreConfigService, times(1)).saveConfig(any(ObjectStoreDTO.class)); + + configService.saveConfig("mail", new EmailNoticeSender()); + verify(mailGeneralConfigService, times(1)).saveConfig(any(EmailNoticeSender.class)); + } + + @Test + public void testGetConfig() { + ObjectStoreDTO ossConfig = new ObjectStoreDTO<>(); + when(objectStoreConfigService.getConfig()).thenReturn(ossConfig); + assertNotNull(configService.getConfig("oss")); + + EmailNoticeSender emailNoticeSender = new EmailNoticeSender(); + when(mailGeneralConfigService.getConfig()).thenReturn(emailNoticeSender); + configService.getConfig("mail"); + verify(mailGeneralConfigService, times(1)).getConfig(); + } + + @Test + public void testUpdateTemplateAppConfig(){ + TemplateConfig templateConfig = new TemplateConfig(); + when(templateConfigService.getConfig()).thenReturn(templateConfig); + configService.updateTemplateAppConfig("custom", new TemplateConfig.AppTemplate()); + + verify(templateConfigService, times(1)).getConfig(); + verify(templateConfigService, times(1)).saveConfig(templateConfig); + } + + @Test + public void testException(){ + assertThrows(IllegalArgumentException.class, () -> configService.saveConfig("test", new ObjectStoreDTO<>())); + assertThrows(IllegalArgumentException.class, () -> configService.getConfig("test2"), "Not supported this config type: test2"); + } +} From 066b9331ed32476da92fbea1d21622a11476abd8 Mon Sep 17 00:00:00 2001 From: pwallk <1363539513@qq.com> Date: Tue, 30 Jul 2024 16:08:06 +0800 Subject: [PATCH 2/2] add license --- .../manager/service/ConfigServiceTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 8e002da972f..d0b16d4cca6 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 @@ -1,3 +1,20 @@ +/* + * 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.manager.service;