Skip to content

Commit

Permalink
Merge branch 'master' into 0808-yuluo/test-2
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsun28 authored Aug 8, 2024
2 parents 723dd81 + 8faa13e commit 3261e1a
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class SystemSecretServiceImpl extends AbstractGeneralConfigServiceImpl<Sy
* @param generalConfigDao ConfigDao object
* @param objectMapper JSON tool object
*/
protected SystemSecretServiceImpl(GeneralConfigDao generalConfigDao, ObjectMapper objectMapper) {
public SystemSecretServiceImpl(GeneralConfigDao generalConfigDao, ObjectMapper objectMapper) {
super(generalConfigDao, objectMapper);
}

Expand All @@ -47,7 +47,7 @@ public String type() {
}

@Override
protected TypeReference<SystemSecret> getTypeReference() {
public TypeReference<SystemSecret> getTypeReference() {
return new TypeReference<>() {
@Override
public Type getType() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Job> 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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* test case for {@link StatusPageServiceImpl}
*/

class StatusPageServiceImplTest {
class StatusPageServiceTest {

@Mock
private StatusPageOrgDao statusPageOrgDao;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SystemSecret> typeReference = systemSecretService.getTypeReference();
assertEquals(SystemSecret.class, typeReference.getType());
}

}

0 comments on commit 3261e1a

Please sign in to comment.