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