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 4e3eb812d09..4c25b12adc3 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 @@ -37,7 +37,7 @@ public class SystemSecretServiceImpl extends AbstractGeneralConfigServiceImpl getTypeReference() { + public TypeReference getTypeReference() { return new TypeReference<>() { @Override public Type getType() { diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/AvailableAlertDefineInitTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/AvailableAlertDefineInitTest.java new file mode 100644 index 00000000000..e2524e7c901 --- /dev/null +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/AvailableAlertDefineInitTest.java @@ -0,0 +1,103 @@ +/* + * 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 java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.hertzbeat.alert.dao.AlertDefineDao; +import org.apache.hertzbeat.common.constants.CommonConstants; +import org.apache.hertzbeat.common.entity.alerter.AlertDefine; +import org.apache.hertzbeat.common.entity.job.Job; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * test case for {@link AvailableAlertDefineInit} + */ + +class AvailableAlertDefineInitTest { + + @Mock + private AlertDefineDao alertDefineDao; + + @Mock + private AppService appService; + + @InjectMocks + private AvailableAlertDefineInit availableAlertDefineInit; + + private Map map; + + @BeforeEach + void setUp() { + + MockitoAnnotations.openMocks(this); + + map = new HashMap<>(); + map.put("testApp", new Job()); + } + + @Test + void testRunAlertDefineIsEmpty() throws Exception { + + + when(appService.getAllAppDefines()).thenReturn(map); + + when(alertDefineDao.queryAlertDefineByAppAndMetric("testApp", CommonConstants.AVAILABILITY)) + .thenReturn(Collections.emptyList()); + + availableAlertDefineInit.run(); + + verify(alertDefineDao, times(1)).save(any(AlertDefine.class)); + } + + @Test + void testRunAlertDefineExists() throws Exception { + + when(appService.getAllAppDefines()).thenReturn(map); + when(alertDefineDao.queryAlertDefineByAppAndMetric("testApp", CommonConstants.AVAILABILITY)) + .thenReturn(List.of(new AlertDefine())); + availableAlertDefineInit.run(); + + verify(alertDefineDao, never()).save(any(AlertDefine.class)); + } + + @Test + void testRunExceptionHandling() throws Exception { + + when(appService.getAllAppDefines()).thenReturn(map); + when(alertDefineDao.queryAlertDefineByAppAndMetric("testApp", CommonConstants.AVAILABILITY)) + .thenThrow(new RuntimeException("Database error")); + + availableAlertDefineInit.run(); + + verify(alertDefineDao, never()).save(any(AlertDefine.class)); + } + +} diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/StatusPageServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/StatusPageServiceTest.java index 822531cbe58..864c4a0e2fb 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/StatusPageServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/StatusPageServiceTest.java @@ -45,7 +45,7 @@ * test case for {@link StatusPageServiceImpl} */ -class StatusPageServiceImplTest { +class StatusPageServiceTest { @Mock private StatusPageOrgDao statusPageOrgDao; 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 729cd130f61..688847e3c2b 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 @@ -17,11 +17,51 @@ package org.apache.hertzbeat.manager.service; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.hertzbeat.manager.dao.GeneralConfigDao; +import org.apache.hertzbeat.manager.pojo.dto.SystemSecret; import org.apache.hertzbeat.manager.service.impl.SystemSecretServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.junit.jupiter.api.Assertions.assertEquals; /** * test case for {@link SystemSecretServiceImpl} */ class SystemSecretServiceTest { + + @Mock + private GeneralConfigDao generalConfigDao; + + @Mock + private ObjectMapper objectMapper; + + private SystemSecretServiceImpl systemSecretService; + + @BeforeEach + void setUp() { + + MockitoAnnotations.openMocks(this); + + this.systemSecretService = new SystemSecretServiceImpl(generalConfigDao, objectMapper); + } + + @Test + void testType() { + + assertEquals("secret", systemSecretService.type()); + } + + @Test + void testGetTypeReference() { + + TypeReference typeReference = systemSecretService.getTypeReference(); + assertEquals(SystemSecret.class, typeReference.getType()); + } + }