Browse Source

[TEST] fill up alert group service test (#15777)

Co-authored-by: abzymeinsjtu <abzymeinsjtu@B-54Q8MD6R-0244.local>
Co-authored-by: Eric Gao <ericgao.apache@gmail.com>
dev_wenjun_refactorMaster
Evan Sun 8 months ago committed by GitHub
parent
commit
ae1fe84e85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 83
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java

83
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java

@ -22,6 +22,7 @@ import static org.apache.dolphinscheduler.api.AssertionsHelper.assertThrowsServi
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ALERT_GROUP_CREATE; 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_DELETE;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ALERT_GROUP_UPDATE; 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.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
@ -41,6 +42,7 @@ import org.apache.dolphinscheduler.dao.mapper.AlertGroupMapper;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
@ -67,6 +69,12 @@ public class AlertGroupServiceTest {
private static final Logger baseServiceLogger = LoggerFactory.getLogger(BaseServiceImpl.class); private static final Logger baseServiceLogger = LoggerFactory.getLogger(BaseServiceImpl.class);
private static final Logger logger = LoggerFactory.getLogger(AlertGroupServiceTest.class); private static final Logger logger = LoggerFactory.getLogger(AlertGroupServiceTest.class);
private static final Logger alertGroupServiceLogger = LoggerFactory.getLogger(AlertGroupServiceImpl.class); private static final Logger alertGroupServiceLogger = LoggerFactory.getLogger(AlertGroupServiceImpl.class);
private String tooLongDescription =
"this is a toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
+
"ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
+
"ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo long description";
@InjectMocks @InjectMocks
private AlertGroupServiceImpl alertGroupService; private AlertGroupServiceImpl alertGroupService;
@ -81,10 +89,25 @@ public class AlertGroupServiceTest {
@Test @Test
public void testQueryAlertGroup() { public void testQueryAlertGroup() {
User user = getLoginUser();
when(alertGroupMapper.queryAllGroupList()).thenReturn(getList()); when(alertGroupMapper.queryAllGroupList()).thenReturn(getList());
List<AlertGroup> alertGroups = alertGroupService.queryAllAlertGroup(getLoginUser()); List<AlertGroup> alertGroups = alertGroupService.queryAllAlertGroup(user);
Assertions.assertEquals(2, alertGroups.size()); 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 @Test
@ -95,6 +118,35 @@ public class AlertGroupServiceTest {
Assertions.assertEquals(1, alertGroups.size()); 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 @Test
public void testListPaging() { public void testListPaging() {
IPage<AlertGroup> page = new Page<>(1, 10); IPage<AlertGroup> page = new Page<>(1, 10);
@ -114,6 +166,18 @@ public class AlertGroupServiceTest {
alertGroupPageInfo = alertGroupService.listPaging(user, groupName, 1, 10); alertGroupPageInfo = alertGroupService.listPaging(user, groupName, 1, 10);
Assertions.assertTrue(CollectionUtils.isNotEmpty(alertGroupPageInfo.getTotalList())); 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 @Test
@ -134,8 +198,19 @@ public class AlertGroupServiceTest {
ALERT_GROUP_CREATE, baseServiceLogger)).thenReturn(true); ALERT_GROUP_CREATE, baseServiceLogger)).thenReturn(true);
when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, null, user.getId(), when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, null, user.getId(),
baseServiceLogger)).thenReturn(true); baseServiceLogger)).thenReturn(true);
assertThrowsServiceException(Status.DESCRIPTION_TOO_LONG_ERROR,
() -> alertGroupService.createAlertGroup(user, groupName, tooLongDescription, null));
AlertGroup alertGroup = alertGroupService.createAlertGroup(user, groupName, groupName, null); AlertGroup alertGroup = alertGroupService.createAlertGroup(user, groupName, groupName, null);
assertNotNull(alertGroup); 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 @Test
@ -162,7 +237,7 @@ public class AlertGroupServiceTest {
user.setUserType(UserType.GENERAL_USER); user.setUserType(UserType.GENERAL_USER);
assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, assertThrowsServiceException(Status.USER_NO_OPERATION_PERM,
() -> alertGroupService.updateAlertGroupById(user, 1, groupName, groupName, null)); () -> alertGroupService.updateAlertGroupById(user, 1, groupName, groupName, null));
user.setUserType(UserType.ADMIN_USER);
// not exist // not exist
user.setUserType(UserType.ADMIN_USER); user.setUserType(UserType.ADMIN_USER);
when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, user.getId(), when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, user.getId(),
@ -171,6 +246,10 @@ public class AlertGroupServiceTest {
baseServiceLogger)).thenReturn(true); baseServiceLogger)).thenReturn(true);
assertThrowsServiceException(Status.ALERT_GROUP_NOT_EXIST, assertThrowsServiceException(Status.ALERT_GROUP_NOT_EXIST,
() -> alertGroupService.updateAlertGroupById(user, 1, groupName, groupName, null)); () -> alertGroupService.updateAlertGroupById(user, 1, groupName, groupName, null));
assertThrowsServiceException(Status.DESCRIPTION_TOO_LONG_ERROR,
() -> alertGroupService.updateAlertGroupById(user, 1, groupName, tooLongDescription, null));
// success // success
when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, new Object[]{3}, when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, new Object[]{3},
user.getId(), baseServiceLogger)).thenReturn(true); user.getId(), baseServiceLogger)).thenReturn(true);

Loading…
Cancel
Save