This is an automated email from the ASF dual-hosted git repository. hefengen pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push: new 238ffaf5eb Export Selector Data should be combined with Discovery Data #5492 (#5493) 238ffaf5eb is described below commit 238ffaf5ebf5e2149c6a49731d409191d48e894d Author: aias00 <rok...@163.com> AuthorDate: Wed Mar 20 19:38:35 2024 +0800 Export Selector Data should be combined with Discovery Data #5492 (#5493) * Export Selector Data should be combined with Discovery Data #5492 * Export Selector Data should be combined with Discovery Data #5492 * Export Selector Data should be combined with Discovery Data , modify the compare method #5492 * Add unit test * Add unit test for alertReceiverService --- .../controller/ConfigsExportImportController.java | 15 ++- .../service/impl/AlertReceiverServiceImpl.java | 15 ++- .../admin/service/impl/AppAuthServiceImpl.java | 1 - .../admin/service/impl/DiscoveryServiceImpl.java | 4 - .../admin/service/impl/PluginServiceImpl.java | 1 - .../shenyu/admin/service/impl/RuleServiceImpl.java | 1 - .../ConfigsExportImportControllerTest.java | 8 ++ .../admin/service/AlertReceiverServiceTest.java | 144 +++++++++++++++++++++ 8 files changed, 174 insertions(+), 15 deletions(-) diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ConfigsExportImportController.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ConfigsExportImportController.java index 0454fc6859..fe12d0a996 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ConfigsExportImportController.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ConfigsExportImportController.java @@ -21,8 +21,10 @@ import com.alibaba.nacos.common.utils.DateFormatUtils; import org.apache.shenyu.admin.aspect.annotation.RestApi; import org.apache.shenyu.admin.model.result.ShenyuAdminResult; import org.apache.shenyu.admin.service.ConfigsService; +import org.apache.shenyu.admin.service.SyncDataService; import org.apache.shenyu.admin.utils.ShenyuResultMessage; import org.apache.shenyu.common.constant.ExportImportConstants; +import org.apache.shenyu.common.enums.DataEventTypeEnum; import org.apache.shenyu.common.exception.CommonErrorCode; import org.apache.shenyu.common.exception.ShenyuException; import org.slf4j.Logger; @@ -52,8 +54,12 @@ public class ConfigsExportImportController { */ private final ConfigsService configsService; - public ConfigsExportImportController(final ConfigsService configsService) { + private final SyncDataService syncDataService; + + public ConfigsExportImportController(final ConfigsService configsService, + final SyncDataService syncDataService) { this.configsService = configsService; + this.syncDataService = syncDataService; } /** @@ -97,7 +103,12 @@ public class ConfigsExportImportController { return ShenyuAdminResult.error(ShenyuResultMessage.PARAMETER_ERROR); } try { - return configsService.configsImport(file.getBytes()); + ShenyuAdminResult importResult = configsService.configsImport(file.getBytes()); + if (Objects.equals(CommonErrorCode.SUCCESSFUL, importResult.getCode())) { + // sync data + syncDataService.syncAll(DataEventTypeEnum.REFRESH); + } + return importResult; } catch (IOException e) { LOG.error("parsing data failed", e); return ShenyuAdminResult.error(ShenyuResultMessage.PARAMETER_ERROR); diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AlertReceiverServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AlertReceiverServiceImpl.java index b1e43fffa7..c87ffb0cda 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AlertReceiverServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AlertReceiverServiceImpl.java @@ -29,7 +29,6 @@ import org.apache.shenyu.admin.transfer.AlertTransfer; import org.apache.shenyu.alert.model.AlertReceiverDTO; import org.apache.shenyu.common.dto.AlarmContent; import org.apache.shenyu.common.utils.UUIDUtils; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.sql.Timestamp; @@ -47,12 +46,16 @@ public class AlertReceiverServiceImpl implements AlertReceiverService { private static final String ALERT_TEST_CONTENT = "test send msg! \n This is the test data. It is proved that it can be received successfully"; - @Autowired - private AlertReceiverMapper alertReceiverMapper; - - @Autowired - private AlertDispatchService alertDispatchService; + private final AlertReceiverMapper alertReceiverMapper; + private final AlertDispatchService alertDispatchService; + + public AlertReceiverServiceImpl(final AlertReceiverMapper alertReceiverMapper, + final AlertDispatchService alertDispatchService) { + this.alertReceiverMapper = alertReceiverMapper; + this.alertDispatchService = alertDispatchService; + } + @Override public void addReceiver(final AlertReceiverDTO alertReceiverDTO) { AlertReceiverDO receiverDO = AlertTransfer.INSTANCE.mapToAlertReciverDO(alertReceiverDTO); diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java index 0fa0afa700..49ed0403c4 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java @@ -312,7 +312,6 @@ public class AppAuthServiceImpl implements AppAuthService { } } } - this.syncData(); if (StringUtils.isNotEmpty(errorMsgBuilder)) { return ConfigImportResult.fail(successCount, "import fail appKey: " + errorMsgBuilder); } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryServiceImpl.java index 069cfdfa55..637fe0b0ae 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryServiceImpl.java @@ -378,10 +378,6 @@ public class DiscoveryServiceImpl implements DiscoveryService { discoveryRelMapper.insertSelective(discoveryRelDO); } } - // sync data - if (successCount > 0) { - this.syncData(); - } if (StringUtils.isNotEmpty(errorMsgBuilder)) { errorMsgBuilder.setLength(errorMsgBuilder.length() - 1); diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java index 8f251dad67..f3da88bc75 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java @@ -280,7 +280,6 @@ public class PluginServiceImpl implements PluginService { pluginId = pluginDO.getId(); if (pluginMapper.insertSelective(pluginDO) > 0) { // publish create event. init plugin data - pluginEventPublisher.onCreated(pluginDO); successCount++; } } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/RuleServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/RuleServiceImpl.java index f724c79c20..7e59f5e1f5 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/RuleServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/RuleServiceImpl.java @@ -278,7 +278,6 @@ public class RuleServiceImpl implements RuleService { final int ruleCount = ruleMapper.insertSelective(ruleDO); addCondition(ruleDO, ruleDTO.getRuleConditions()); if (ruleCount > 0) { - ruleEventPublisher.onCreated(ruleDO, ruleDTO.getRuleConditions()); successCount++; } } diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/controller/ConfigsExportImportControllerTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/controller/ConfigsExportImportControllerTest.java index 48d079837d..e16c1f9d52 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/controller/ConfigsExportImportControllerTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/controller/ConfigsExportImportControllerTest.java @@ -20,6 +20,7 @@ package org.apache.shenyu.admin.controller; import org.apache.shenyu.admin.exception.ExceptionHandlers; import org.apache.shenyu.admin.model.result.ShenyuAdminResult; import org.apache.shenyu.admin.service.ConfigsService; +import org.apache.shenyu.admin.service.SyncDataService; import org.apache.shenyu.admin.utils.ShenyuResultMessage; import org.apache.shenyu.admin.utils.ZipUtil; import org.assertj.core.util.Lists; @@ -64,12 +65,19 @@ public class ConfigsExportImportControllerTest { @Mock private ConfigsService configsService; + /** + * The configs service. + */ + @Mock + private SyncDataService syncDataService; + @BeforeEach public void setUp() { this.mockMvc = MockMvcBuilders .standaloneSetup(configsExportImportController) .setControllerAdvice(new ExceptionHandlers(null)) .setControllerAdvice(configsService) + .setControllerAdvice(syncDataService) .build(); } diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/AlertReceiverServiceTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/AlertReceiverServiceTest.java new file mode 100644 index 0000000000..6bc0f47bf9 --- /dev/null +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/AlertReceiverServiceTest.java @@ -0,0 +1,144 @@ +/* + * 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.shenyu.admin.service; + +import com.google.common.collect.Lists; +import org.apache.shenyu.admin.mapper.AlertReceiverMapper; +import org.apache.shenyu.admin.model.entity.AlertReceiverDO; +import org.apache.shenyu.admin.model.page.CommonPager; +import org.apache.shenyu.admin.model.page.PageParameter; +import org.apache.shenyu.admin.model.query.AlertReceiverQuery; +import org.apache.shenyu.admin.service.impl.AlertReceiverServiceImpl; +import org.apache.shenyu.admin.transfer.AlertTransfer; +import org.apache.shenyu.alert.model.AlertReceiverDTO; +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; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; + +/** + * Test cases for AlertReceiverService. + */ +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +public final class AlertReceiverServiceTest { + + @InjectMocks + private AlertReceiverServiceImpl alertReceiverService; + + @Mock + private AlertReceiverMapper alertReceiverMapper; + + @Mock + private AlertDispatchService alertDispatchService; + + @BeforeEach + public void setUp() { + alertReceiverService = new AlertReceiverServiceImpl(alertReceiverMapper, alertDispatchService); + } + + @Test + public void testAddReceiver() { + given(alertReceiverMapper.insert(any())).willReturn(1); + alertReceiverService.addReceiver(new AlertReceiverDTO()); + } + + @Test + public void testDeleteReceiver() { + given(alertReceiverMapper.deleteByIds(any())).willReturn(1); + alertReceiverService.deleteReceiver(Lists.newArrayList("1")); + } + + @Test + public void testUpdateReceiver() { + given(alertReceiverMapper.updateByPrimaryKey(any())).willReturn(1); + alertReceiverService.updateReceiver(new AlertReceiverDTO()); + } + + @Test + public void testGetAll() { + AlertReceiverDTO alertReceiverDTO = buildAlertReceiverDTO("123"); + List<AlertReceiverDTO> list = Lists.newArrayList(alertReceiverDTO); + given(alertReceiverMapper.selectAll()).willReturn(list); + List<AlertReceiverDTO> all = alertReceiverService.getAll(); + assertNotNull(all); + assertEquals(all.size(), list.size()); + } + + @Test + public void testListByPage() { + PageParameter pageParameter = new PageParameter(); + pageParameter.setPageSize(5); + pageParameter.setTotalCount(10); + pageParameter.setTotalPage(pageParameter.getTotalCount() / pageParameter.getPageSize()); + AlertReceiverQuery alertReceiverQuery = new AlertReceiverQuery(pageParameter); + List<AlertReceiverDO> receiverDOList = IntStream.range(0, 10).mapToObj(i -> buildAlertReceiverDO(String.valueOf(i))).collect(Collectors.toList()); + given(this.alertReceiverMapper.selectByQuery(alertReceiverQuery)).willReturn(receiverDOList); + final CommonPager<AlertReceiverDTO> commonPager = this.alertReceiverService.listByPage(alertReceiverQuery); + assertEquals(commonPager.getDataList().size(), receiverDOList.size()); + } + + @Test + public void testDetail() { + AlertReceiverDO receiverDO = buildAlertReceiverDO("123"); + given(this.alertReceiverMapper.selectByPrimaryKey(anyString())).willReturn(receiverDO); + AlertReceiverDTO receiverDTO = alertReceiverService.detail("123"); + assertNotNull(receiverDTO); + assertEquals(receiverDTO.getId(), receiverDO.getId()); + } + + @Test + public void testSendTestMsg() { + AlertReceiverDTO alertReceiverDTO = buildAlertReceiverDTO("123"); + given(this.alertDispatchService.sendNoticeMsg(any(), any())).willReturn(true); + boolean sent = alertReceiverService.sendTestMsg(alertReceiverDTO); + assertTrue(sent); + } + + private AlertReceiverDO buildAlertReceiverDO(final String id) { + AlertReceiverDO alertReceiverDO = AlertTransfer.INSTANCE.mapToAlertReciverDO(buildAlertReceiverDTO(id)); + Timestamp now = Timestamp.valueOf(LocalDateTime.now()); + alertReceiverDO.setDateCreated(now); + alertReceiverDO.setDateUpdated(now); + return alertReceiverDO; + } + + private AlertReceiverDTO buildAlertReceiverDTO(final String id) { + AlertReceiverDTO alertReceiverDTO = new AlertReceiverDTO(); + alertReceiverDTO.setEnable(true); + alertReceiverDTO.setId(id); + return alertReceiverDTO; + } +}