Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] move code from GeneralConfigController to ConfigService #2414

Merged
merged 5 commits into from
Jul 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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,26 +44,17 @@
@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")
public ResponseEntity<Message<String>> 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,31 +63,15 @@ public ResponseEntity<Message<String>> saveOrUpdateConfig(
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}")
@Operation(summary = "Update the app template config")
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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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
*/
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}