This is an automated email from the ASF dual-hosted git repository.

zhengqiwei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/master by this push:
     new ae4a5b1c6 [refactor] move code from GeneralConfigController to 
ConfigService (#2414)
ae4a5b1c6 is described below

commit ae4a5b1c6393878802b22b11de0121a03730934c
Author: kangli <[email protected]>
AuthorDate: Wed Jul 31 00:48:10 2024 +0800

    [refactor] move code from GeneralConfigController to ConfigService (#2414)
    
    Co-authored-by: Calvin <[email protected]>
---
 .../controller/GeneralConfigController.java        |  42 ++------
 ...eneralConfigService.java => ConfigService.java} |  35 +++----
 .../manager/service/GeneralConfigService.java      |   2 +-
 .../manager/service/impl/ConfigServiceImpl.java    |  81 ++++++++++++++++
 .../manager/service/ConfigServiceTest.java         | 107 +++++++++++++++++++++
 5 files changed, 212 insertions(+), 55 deletions(-)

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 49f48d482..8747bc8b9 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 static 
org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
 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 @@ import 
org.springframework.web.bind.annotation.RestController;
 @Tag(name = "Alert sender Configuration API")
 @Slf4j
 public class GeneralConfigController {
-    private final Map<String, GeneralConfigService> configServiceMap;
+    @Resource
+    private ConfigService configService;
 
-    public GeneralConfigController(List<GeneralConfigService> 
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 class GeneralConfigController {
             @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 class GeneralConfigController {
     public ResponseEntity<Message<Object>> 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 class GeneralConfigController {
     public ResponseEntity<Message<Void>> 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/GeneralConfigService.java
 b/manager/src/main/java/org/apache/hertzbeat/manager/service/ConfigService.java
similarity index 61%
copy from 
manager/src/main/java/org/apache/hertzbeat/manager/service/GeneralConfigService.java
copy to 
manager/src/main/java/org/apache/hertzbeat/manager/service/ConfigService.java
index 0f381bf68..418c7c5e2 100644
--- 
a/manager/src/main/java/org/apache/hertzbeat/manager/service/GeneralConfigService.java
+++ 
b/manager/src/main/java/org/apache/hertzbeat/manager/service/ConfigService.java
@@ -17,34 +17,31 @@
 
 package org.apache.hertzbeat.manager.service;
 
+import org.apache.hertzbeat.manager.pojo.dto.TemplateConfig;
+
 /**
- * <p>ConfigService interface provides CRUD operations for configurations.</p>
- * @param <T> configuration type.
- * @version 1.0
+ * Provides operations for the GeneralConfigService
  */
-public interface GeneralConfigService<T> {
+public interface ConfigService {
 
-    /**
-     * config type: email, sms
-     * @return type string
-     */
-    String type();
-    
     /**
      * save config
-     * @param config need save configuration
+     * @param type      config type
+     * @param config    need save configuration
      */
-    void saveConfig(T config);
+    void saveConfig(String type, Object config);
 
     /**
-     * get config
-     * @return query The configuration is queried
+     *  get config
+     * @param type config type
+     * @return config
      */
-    T getConfig();
-    
+    Object getConfig(String type);
+
     /**
-     * handler after save config
-     * @param config config
+     * Update the app template config
+     * @param app       monitoring type
+     * @param template  template config
      */
-    default void handler(T 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 0f381bf68..3d6c956d8 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;
 
 /**
- * <p>ConfigService interface provides CRUD operations for configurations.</p>
+ * <p>GeneralConfigService interface provides CRUD operations for 
configurations.</p>
  * @param <T> 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 000000000..345e2b3b4
--- /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<String, GeneralConfigService> configServiceMap;
+
+    public ConfigServiceImpl(List<GeneralConfigService> 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 000000000..d0b16d4cc
--- /dev/null
+++ 
b/manager/src/test/java/org/apache/hertzbeat/manager/service/ConfigServiceTest.java
@@ -0,0 +1,107 @@
+/*
+ * 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 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<GeneralConfigService> 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");
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to