@ -17,10 +17,13 @@
package org.apache.dolphinscheduler.api.service ;
import com.baomidou.mybatisplus.core.metadata.IPage ;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import org.apache.commons.collections.CollectionUtils ;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.TENANT_CREATE ;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.TENANT_DELETE ;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.TENANT_UPDATE ;
import org.apache.dolphinscheduler.api.enums.Status ;
import org.apache.dolphinscheduler.api.exceptions.ServiceException ;
import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService ;
import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl ;
import org.apache.dolphinscheduler.api.service.impl.TenantServiceImpl ;
import org.apache.dolphinscheduler.api.utils.PageInfo ;
@ -28,7 +31,6 @@ import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants ;
import org.apache.dolphinscheduler.common.enums.AuthorizationType ;
import org.apache.dolphinscheduler.common.enums.UserType ;
import org.apache.dolphinscheduler.common.storage.StorageOperate ;
import org.apache.dolphinscheduler.common.utils.PropertyUtils ;
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition ;
import org.apache.dolphinscheduler.dao.entity.ProcessInstance ;
@ -38,9 +40,19 @@ import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper;
import org.apache.dolphinscheduler.dao.mapper.ProcessInstanceMapper ;
import org.apache.dolphinscheduler.dao.mapper.TenantMapper ;
import org.apache.dolphinscheduler.dao.mapper.UserMapper ;
import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService ;
import org.apache.commons.collections.CollectionUtils ;
import java.text.MessageFormat ;
import java.util.ArrayList ;
import java.util.HashSet ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
import org.junit.Assert ;
import org.junit.Test ;
import org.junit.jupiter.api.Assertions ;
import org.junit.runner.RunWith ;
import org.mockito.InjectMocks ;
import org.mockito.Mock ;
@ -50,13 +62,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import java.util.ArrayList ;
import java.util.HashSet ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.* ;
import com.baomidou.mybatisplus.core.metadata.IPage ;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
/ * *
* tenant service test
@ -65,7 +72,6 @@ import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationCon
@PrepareForTest ( { PropertyUtils . class } )
public class TenantServiceTest {
private static final Logger baseServiceLogger = LoggerFactory . getLogger ( BaseServiceImpl . class ) ;
private static final Logger logger = LoggerFactory . getLogger ( TenantServiceTest . class ) ;
private static final Logger tenantServiceImplLogger = LoggerFactory . getLogger ( TenantServiceImpl . class ) ;
@InjectMocks
@ -83,89 +89,93 @@ public class TenantServiceTest {
@Mock
private UserMapper userMapper ;
@Mock
private StorageOperate storageOperate ;
@Mock
private ResourcePermissionCheckService resourcePermissionCheckService ;
private static final String tenantCode = "hayden" ;
private static final String tenantDesc = "This is the tenant desc" ;
@Test
public void testCreateTenant ( ) {
public void testCreateTenant ( ) throws Exception {
User loginUser = getLoginUser ( ) ;
Mockito . when ( tenantMapper . existTenant ( tenantCode ) ) . thenReturn ( true ) ;
Mockito . when ( resourcePermissionCheckService . operationPermissionCheck ( AuthorizationType . TENANT , loginUser . getId ( ) , TENANT_CREATE , baseServiceLogger ) ) . thenReturn ( true ) ;
Mockito . when ( resourcePermissionCheckService . operationPermissionCheck ( AuthorizationType . TENANT , loginUser . getId ( ) , TENANT_CREATE , baseServiceLogger ) ) . thenReturn ( true ) ;
Mockito . when ( resourcePermissionCheckService . resourcePermissionCheck ( AuthorizationType . TENANT , null , 0 , baseServiceLogger ) ) . thenReturn ( true ) ;
try {
//check tenantCode
Map < String , Object > result =
tenantService . createTenant ( getLoginUser ( ) , "%!1111" , 1 , "TenantServiceTest" ) ;
logger . info ( result . toString ( ) ) ;
Assert . assertEquals ( Status . CHECK_OS_TENANT_CODE_ERROR , result . get ( Constants . STATUS ) ) ;
//check exist
result = tenantService . createTenant ( loginUser , tenantCode , 1 , "TenantServiceTest" ) ;
logger . info ( result . toString ( ) ) ;
Assert . assertEquals ( Status . OS_TENANT_CODE_EXIST , result . get ( Constants . STATUS ) ) ;
// success
result = tenantService . createTenant ( loginUser , "test" , 1 , "TenantServiceTest" ) ;
logger . info ( result . toString ( ) ) ;
Assert . assertEquals ( Status . SUCCESS , result . get ( Constants . STATUS ) ) ;
} catch ( Exception e ) {
logger . error ( "create tenant error" , e ) ;
Assert . fail ( ) ;
}
Map < String , Object > result ;
//check exist
String emptyTenantCode = "" ;
Throwable exception = Assertions . assertThrows ( ServiceException . class , ( ) - > tenantService . createTenant ( loginUser , emptyTenantCode , 1 , tenantDesc ) ) ;
String formatter = MessageFormat . format ( Status . REQUEST_PARAMS_NOT_VALID_ERROR . getMsg ( ) , emptyTenantCode ) ;
Assertions . assertEquals ( formatter , exception . getMessage ( ) ) ;
//check tenant code too long
String longStr = "this_is_a_very_long_string_this_is_a_very_long_string_this_is_a_very_long_string_this_is_a_very_long_string" ;
exception = Assertions . assertThrows ( ServiceException . class , ( ) - > tenantService . createTenant ( loginUser , longStr , 1 , tenantDesc ) ) ;
Assert . assertEquals ( Status . TENANT_FULL_NAME_TOO_LONG_ERROR . getMsg ( ) , exception . getMessage ( ) ) ;
//check tenant code invalid
exception = Assertions . assertThrows ( ServiceException . class , ( ) - > tenantService . createTenant ( getLoginUser ( ) , "%!1111" , 1 , tenantDesc ) ) ;
Assert . assertEquals ( Status . CHECK_OS_TENANT_CODE_ERROR . getMsg ( ) , exception . getMessage ( ) ) ;
//check exist
exception = Assertions . assertThrows ( ServiceException . class , ( ) - > tenantService . createTenant ( loginUser , tenantCode , 1 , tenantDesc ) ) ;
formatter = MessageFormat . format ( Status . OS_TENANT_CODE_EXIST . getMsg ( ) , tenantCode ) ;
Assert . assertEquals ( formatter , exception . getMessage ( ) ) ;
// success
result = tenantService . createTenant ( loginUser , "test" , 1 , tenantDesc ) ;
Assert . assertEquals ( Status . SUCCESS , result . get ( Constants . STATUS ) ) ;
}
@Test
public void testCreateTenantError ( ) {
Mockito . when ( tenantMapper . existTenant ( tenantCode ) ) . thenReturn ( true ) ;
// tenantCode exist
Throwable exception = Assertions . assertThrows ( ServiceException . class , ( ) - > tenantService . verifyTenantCode ( getTenant ( ) . getTenantCode ( ) ) ) ;
String expect = MessageFormat . format ( Status . OS_TENANT_CODE_EXIST . getMsg ( ) , getTenant ( ) . getTenantCode ( ) ) ;
Assertions . assertEquals ( expect , exception . getMessage ( ) ) ;
// success
Result result = tenantService . verifyTenantCode ( "s00000000000l887888885554444sfjdskfjslakslkdf" ) ;
Assert . assertEquals ( Status . SUCCESS . getMsg ( ) , result . getMsg ( ) ) ;
}
@Test
@SuppressWarnings ( "unchecked" )
public void testQueryTenantListPage ( ) {
IPage < Tenant > page = new Page < > ( 1 , 10 ) ;
page . setRecords ( getList ( ) ) ;
page . setTotal ( 1L ) ;
Set < Integer > ids = new HashSet < > ( ) ;
ids . add ( 1 ) ;
Mockito . when ( resourcePermissionCheckService . userOwnedResourceIdsAcquisition ( AuthorizationType . TENANT , getLoginUser ( ) . getId ( ) , tenantServiceImplLogger ) ) . thenReturn ( ids ) ;
Mockito . when ( tenantMapper . queryTenantPaging ( Mockito . any ( Page . class ) , Mockito . anyList ( ) , Mockito . eq ( "TenantServiceTest" ) ) )
. thenReturn ( page ) ;
Result result = tenantService . queryTenantList ( getLoginUser ( ) , "TenantServiceTest" , 1 , 10 ) ;
logger . info ( result . toString ( ) ) ;
Mockito . when ( tenantMapper . queryTenantPaging ( Mockito . any ( Page . class ) , Mockito . anyList ( ) , Mockito . eq ( tenantDesc ) ) )
. thenReturn ( page ) ;
Result result = tenantService . queryTenantList ( getLoginUser ( ) , tenantDesc , 1 , 10 ) ;
PageInfo < Tenant > pageInfo = ( PageInfo < Tenant > ) result . getData ( ) ;
Assert . assertTrue ( CollectionUtils . isNotEmpty ( pageInfo . getTotalList ( ) ) ) ;
}
@Test
public void testUpdateTenant ( ) {
public void testUpdateTenant ( ) throws Exception {
Mockito . when ( tenantMapper . queryById ( 1 ) ) . thenReturn ( getTenant ( ) ) ;
Mockito . when ( resourcePermissionCheckService . operationPermissionCheck ( AuthorizationType . TENANT , getLoginUser ( ) . getId ( ) , TENANT_UPDATE , baseServiceLogger ) ) . thenReturn ( true ) ;
Mockito . when ( resourcePermissionCheckService . operationPermissionCheck ( AuthorizationType . TENANT , getLoginUser ( ) . getId ( ) , TENANT_UPDATE , baseServiceLogger ) ) . thenReturn ( true ) ;
Mockito . when ( resourcePermissionCheckService . resourcePermissionCheck ( AuthorizationType . TENANT , null , 0 , baseServiceLogger ) ) . thenReturn ( true ) ;
try {
// id not exist
Map < String , Object > result =
tenantService . updateTenant ( getLoginUser ( ) , 912222 , tenantCode , 1 , "desc" ) ;
logger . info ( result . toString ( ) ) ;
// success
Assert . assertEquals ( Status . TENANT_NOT_EXIST , result . get ( Constants . STATUS ) ) ;
result = tenantService . updateTenant ( getLoginUser ( ) , 1 , tenantCode , 1 , "desc" ) ;
logger . info ( result . toString ( ) ) ;
Assert . assertEquals ( Status . SUCCESS , result . get ( Constants . STATUS ) ) ;
} catch ( Exception e ) {
logger . error ( "update tenant error" , e ) ;
Assert . fail ( ) ;
}
Throwable exception = Assertions . assertThrows ( ServiceException . class , ( ) - > tenantService . updateTenant ( getLoginUser ( ) , 912222 , tenantCode , 1 , tenantDesc ) ) ;
Assertions . assertEquals ( Status . TENANT_NOT_EXIST . getMsg ( ) , exception . getMessage ( ) ) ;
Map < String , Object > result = tenantService . updateTenant ( getLoginUser ( ) , 1 , tenantCode , 1 , tenantDesc ) ;
Assert . assertEquals ( Status . SUCCESS , result . get ( Constants . STATUS ) ) ;
}
@Test
public void testDeleteTenantById ( ) {
Mockito . when ( resourcePermissionCheckService . operationPermissionCheck ( AuthorizationType . TENANT , getLoginUser ( ) . getId ( ) , TENANT_DELETE , baseServiceLogger ) ) . thenReturn ( true ) ;
public void testDeleteTenantById ( ) throws Exception {
Mockito . when ( resourcePermissionCheckService . operationPermissionCheck ( AuthorizationType . TENANT , getLoginUser ( ) . getId ( ) , TENANT_DELETE , baseServiceLogger ) ) . thenReturn ( true ) ;
Mockito . when ( resourcePermissionCheckService . resourcePermissionCheck ( AuthorizationType . TENANT , null , 0 , baseServiceLogger ) ) . thenReturn ( true ) ;
Mockito . when ( tenantMapper . queryById ( 1 ) ) . thenReturn ( getTenant ( ) ) ;
Mockito . when ( processInstanceMapper . queryByTenantIdAndStatus ( 1 , Constants . NOT_TERMINATED_STATES ) )
@ -173,58 +183,50 @@ public class TenantServiceTest {
Mockito . when ( processDefinitionMapper . queryDefinitionListByTenant ( 2 ) ) . thenReturn ( getDefinitionsList ( ) ) ;
Mockito . when ( userMapper . queryUserListByTenant ( 3 ) ) . thenReturn ( getUserList ( ) ) ;
try {
//TENANT_NOT_EXIST
Map < String , Object > result = tenantService . deleteTenantById ( getLoginUser ( ) , 12 ) ;
logger . info ( result . toString ( ) ) ;
Assert . assertEquals ( Status . TENANT_NOT_EXIST , result . get ( Constants . STATUS ) ) ;
//DELETE_TENANT_BY_ID_FAIL
result = tenantService . deleteTenantById ( getLoginUser ( ) , 1 ) ;
logger . info ( result . toString ( ) ) ;
Assert . assertEquals ( Status . DELETE_TENANT_BY_ID_FAIL , result . get ( Constants . STATUS ) ) ;
//DELETE_TENANT_BY_ID_FAIL_DEFINES
Mockito . when ( tenantMapper . queryById ( 2 ) ) . thenReturn ( getTenant ( 2 ) ) ;
result = tenantService . deleteTenantById ( getLoginUser ( ) , 2 ) ;
logger . info ( result . toString ( ) ) ;
Assert . assertEquals ( Status . DELETE_TENANT_BY_ID_FAIL_DEFINES , result . get ( Constants . STATUS ) ) ;
//DELETE_TENANT_BY_ID_FAIL_USERS
Mockito . when ( tenantMapper . queryById ( 3 ) ) . thenReturn ( getTenant ( 3 ) ) ;
result = tenantService . deleteTenantById ( getLoginUser ( ) , 3 ) ;
logger . info ( result . toString ( ) ) ;
Assert . assertEquals ( Status . DELETE_TENANT_BY_ID_FAIL_USERS , result . get ( Constants . STATUS ) ) ;
// success
Mockito . when ( tenantMapper . queryById ( 4 ) ) . thenReturn ( getTenant ( 4 ) ) ;
result = tenantService . deleteTenantById ( getLoginUser ( ) , 4 ) ;
logger . info ( result . toString ( ) ) ;
Assert . assertEquals ( Status . SUCCESS , result . get ( Constants . STATUS ) ) ;
} catch ( Exception e ) {
logger . error ( "delete tenant error" , e ) ;
Assert . fail ( ) ;
}
//TENANT_NOT_EXIST
Throwable exception = Assertions . assertThrows ( ServiceException . class , ( ) - > tenantService . deleteTenantById ( getLoginUser ( ) , 12 ) ) ;
Assertions . assertEquals ( Status . TENANT_NOT_EXIST . getMsg ( ) , exception . getMessage ( ) ) ;
//DELETE_TENANT_BY_ID_FAIL
exception = Assertions . assertThrows ( ServiceException . class , ( ) - > tenantService . deleteTenantById ( getLoginUser ( ) , 1 ) ) ;
String prefix = Status . DELETE_TENANT_BY_ID_FAIL . getMsg ( ) . substring ( 1 , 5 ) ;
Assertions . assertTrue ( exception . getMessage ( ) . contains ( prefix ) ) ;
//DELETE_TENANT_BY_ID_FAIL_DEFINES
Mockito . when ( tenantMapper . queryById ( 2 ) ) . thenReturn ( getTenant ( 2 ) ) ;
exception = Assertions . assertThrows ( ServiceException . class , ( ) - > tenantService . deleteTenantById ( getLoginUser ( ) , 2 ) ) ;
prefix = Status . DELETE_TENANT_BY_ID_FAIL_DEFINES . getMsg ( ) . substring ( 1 , 5 ) ;
Assertions . assertTrue ( exception . getMessage ( ) . contains ( prefix ) ) ;
//DELETE_TENANT_BY_ID_FAIL_USERS
Mockito . when ( tenantMapper . queryById ( 3 ) ) . thenReturn ( getTenant ( 3 ) ) ;
exception = Assertions . assertThrows ( ServiceException . class , ( ) - > tenantService . deleteTenantById ( getLoginUser ( ) , 3 ) ) ;
prefix = Status . DELETE_TENANT_BY_ID_FAIL_USERS . getMsg ( ) . substring ( 1 , 5 ) ;
Assertions . assertTrue ( exception . getMessage ( ) . contains ( prefix ) ) ;
// success
Mockito . when ( tenantMapper . queryById ( 4 ) ) . thenReturn ( getTenant ( 4 ) ) ;
Map < String , Object > result = tenantService . deleteTenantById ( getLoginUser ( ) , 4 ) ;
Assert . assertEquals ( Status . SUCCESS , result . get ( Constants . STATUS ) ) ;
}
@Test
public void testVerifyTenantCode ( ) {
Mockito . when ( tenantMapper . existTenant ( tenantCode ) ) . thenReturn ( true ) ;
// tenantCode not exist
// tenantCode exist
Throwable exception = Assertions . assertThrows ( ServiceException . class , ( ) - > tenantService . verifyTenantCode ( getTenant ( ) . getTenantCode ( ) ) ) ;
String expect = MessageFormat . format ( Status . OS_TENANT_CODE_EXIST . getMsg ( ) , getTenant ( ) . getTenantCode ( ) ) ;
Assertions . assertEquals ( expect , exception . getMessage ( ) ) ;
// success
Result result = tenantService . verifyTenantCode ( "s00000000000l887888885554444sfjdskfjslakslkdf" ) ;
logger . info ( result . toString ( ) ) ;
Assert . assertEquals ( Status . SUCCESS . getMsg ( ) , result . getMsg ( ) ) ;
// tenantCode exist
result = tenantService . verifyTenantCode ( getTenant ( ) . getTenantCode ( ) ) ;
Assert . assertEquals ( Status . OS_TENANT_CODE_EXIST . getCode ( ) , result . getCode ( ) . intValue ( ) ) ;
}
/ * *
* get user
* /
private User getLoginUser ( ) {
User loginUser = new User ( ) ;
loginUser . setUserType ( UserType . ADMIN_USER ) ;
return loginUser ;