Skip to content

Commit

Permalink
[TEST] fill up alert group service test (#15777)
Browse files Browse the repository at this point in the history
Co-authored-by: abzymeinsjtu <abzymeinsjtu@B-54Q8MD6R-0244.local>
Co-authored-by: Eric Gao <ericgao.apache@gmail.com>
  • Loading branch information
3 people authored Mar 29, 2024
1 parent 723126b commit ae1fe84
Showing 1 changed file with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ALERT_GROUP_CREATE;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ALERT_GROUP_DELETE;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ALERT_GROUP_UPDATE;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ALERT_GROUP_VIEW;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand All @@ -41,6 +42,7 @@
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Assertions;
Expand All @@ -67,6 +69,12 @@ public class AlertGroupServiceTest {
private static final Logger baseServiceLogger = LoggerFactory.getLogger(BaseServiceImpl.class);
private static final Logger logger = LoggerFactory.getLogger(AlertGroupServiceTest.class);
private static final Logger alertGroupServiceLogger = LoggerFactory.getLogger(AlertGroupServiceImpl.class);
private String tooLongDescription =
"this is a toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
+
"ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
+
"ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo long description";

@InjectMocks
private AlertGroupServiceImpl alertGroupService;
Expand All @@ -81,10 +89,25 @@ public class AlertGroupServiceTest {

@Test
public void testQueryAlertGroup() {
User user = getLoginUser();

when(alertGroupMapper.queryAllGroupList()).thenReturn(getList());
List<AlertGroup> alertGroups = alertGroupService.queryAllAlertGroup(getLoginUser());
List<AlertGroup> alertGroups = alertGroupService.queryAllAlertGroup(user);
Assertions.assertEquals(2, alertGroups.size());

user.setUserType(UserType.GENERAL_USER);
user.setId(2);

when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.ALERT_GROUP, 2,
alertGroupServiceLogger))
.thenReturn(Collections.emptySet());
Assertions.assertEquals(alertGroupService.queryAllAlertGroup(user).size(), 0);

user.setId(3);
when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.ALERT_GROUP, 3,
alertGroupServiceLogger))
.thenReturn(Collections.singleton(1));
assertDoesNotThrow(() -> alertGroupService.queryAllAlertGroup(user));
}

@Test
Expand All @@ -95,6 +118,35 @@ public void testQueryNormalAlertGroup() {
Assertions.assertEquals(1, alertGroups.size());
}

@Test
public void testQueryAlertGroupById() {
User user = getLoginUser();
user.setId(2);
user.setUserType(UserType.GENERAL_USER);

when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, 2, ALERT_GROUP_VIEW,
baseServiceLogger))
.thenReturn(false);

assertThrowsServiceException(Status.USER_NO_OPERATION_PERM,
() -> alertGroupService.queryAlertGroupById(user, 1));

user.setId(1);
when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, 1, ALERT_GROUP_VIEW,
baseServiceLogger))
.thenReturn(true);
when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, new Object[]{999}, 1,
baseServiceLogger))
.thenReturn(true);
when(alertGroupMapper.selectById(999)).thenReturn(null);

assertThrowsServiceException(Status.ALERT_GROUP_NOT_EXIST,
() -> alertGroupService.queryAlertGroupById(user, 999));

when(alertGroupMapper.selectById(999)).thenReturn(getEntity());
assertDoesNotThrow(() -> alertGroupService.queryAlertGroupById(user, 999));
}

@Test
public void testListPaging() {
IPage<AlertGroup> page = new Page<>(1, 10);
Expand All @@ -114,6 +166,18 @@ public void testListPaging() {
alertGroupPageInfo = alertGroupService.listPaging(user, groupName, 1, 10);
Assertions.assertTrue(CollectionUtils.isNotEmpty(alertGroupPageInfo.getTotalList()));

user.setUserType(UserType.GENERAL_USER);
user.setId(99);
page.setTotal(1);
page.setRecords(Collections.singletonList(getEntity()));

when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.ALERT_GROUP, user.getId(),
alertGroupServiceLogger))
.thenReturn(Collections.singleton(1));
when(alertGroupMapper.queryAlertGroupPageByIds(any(Page.class), any(List.class), eq(groupName)))
.thenReturn(page);

alertGroupService.listPaging(user, groupName, 1, 10).getTotal();
}

@Test
Expand All @@ -134,8 +198,19 @@ public void testCreateAlertgroup() {
ALERT_GROUP_CREATE, baseServiceLogger)).thenReturn(true);
when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, null, user.getId(),
baseServiceLogger)).thenReturn(true);

assertThrowsServiceException(Status.DESCRIPTION_TOO_LONG_ERROR,
() -> alertGroupService.createAlertGroup(user, groupName, tooLongDescription, null));
AlertGroup alertGroup = alertGroupService.createAlertGroup(user, groupName, groupName, null);
assertNotNull(alertGroup);

when(alertGroupMapper.insert(any(AlertGroup.class))).thenReturn(-1);
assertThrowsServiceException(Status.CREATE_ALERT_GROUP_ERROR,
() -> alertGroupService.createAlertGroup(user, groupName, groupName, null));

when(alertGroupMapper.insert(any(AlertGroup.class))).thenThrow(DuplicateKeyException.class);
assertThrowsServiceException(Status.ALERT_GROUP_EXIST,
() -> alertGroupService.createAlertGroup(user, groupName, groupName, null));
}

@Test
Expand All @@ -162,7 +237,7 @@ public void testUpdateAlertgroup() {
user.setUserType(UserType.GENERAL_USER);
assertThrowsServiceException(Status.USER_NO_OPERATION_PERM,
() -> alertGroupService.updateAlertGroupById(user, 1, groupName, groupName, null));
user.setUserType(UserType.ADMIN_USER);

// not exist
user.setUserType(UserType.ADMIN_USER);
when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, user.getId(),
Expand All @@ -171,6 +246,10 @@ public void testUpdateAlertgroup() {
baseServiceLogger)).thenReturn(true);
assertThrowsServiceException(Status.ALERT_GROUP_NOT_EXIST,
() -> alertGroupService.updateAlertGroupById(user, 1, groupName, groupName, null));

assertThrowsServiceException(Status.DESCRIPTION_TOO_LONG_ERROR,
() -> alertGroupService.updateAlertGroupById(user, 1, groupName, tooLongDescription, null));

// success
when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, new Object[]{3},
user.getId(), baseServiceLogger)).thenReturn(true);
Expand Down

0 comments on commit ae1fe84

Please sign in to comment.