@ -14,18 +14,29 @@
* See the License for the specific language governing permissions and
* See the License for the specific language governing permissions and
* limitations under the License .
* limitations under the License .
* /
* /
package org.apache.dolphinscheduler.dao.mapper ;
package org.apache.dolphinscheduler.dao.mapper ;
import static java.util.stream.Collectors.toList ;
import static org.hamcrest.Matchers.greaterThan ;
import static org.junit.Assert.assertNotNull ;
import static org.junit.Assert.assertThat ;
import com.baomidou.mybatisplus.core.metadata.IPage ;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import org.apache.dolphinscheduler.common.Constants ;
import org.apache.dolphinscheduler.common.Constants ;
import org.apache.dolphinscheduler.common.enums.ResourceType ;
import org.apache.dolphinscheduler.common.enums.ResourceType ;
import org.apache.dolphinscheduler.common.enums.UserType ;
import org.apache.dolphinscheduler.common.enums.UserType ;
import org.apache.dolphinscheduler.common.utils.CollectionUtils ;
import org.apache.dolphinscheduler.dao.entity.Resource ;
import org.apache.dolphinscheduler.dao.entity.Resource ;
import org.apache.dolphinscheduler.dao.entity.ResourcesUser ;
import org.apache.dolphinscheduler.dao.entity.ResourcesUser ;
import org.apache.dolphinscheduler.dao.entity.Tenant ;
import org.apache.dolphinscheduler.dao.entity.Tenant ;
import org.apache.dolphinscheduler.dao.entity.User ;
import org.apache.dolphinscheduler.dao.entity.User ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.Date ;
import java.util.List ;
import org.junit.Assert ;
import org.junit.Assert ;
import org.junit.Test ;
import org.junit.Test ;
import org.junit.runner.RunWith ;
import org.junit.runner.RunWith ;
@ -35,15 +46,8 @@ import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner ;
import org.springframework.test.context.junit4.SpringRunner ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.transaction.annotation.Transactional ;
import java.util.ArrayList ;
import com.baomidou.mybatisplus.core.metadata.IPage ;
import java.util.Arrays ;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import java.util.Date ;
import java.util.List ;
import static java.util.stream.Collectors.toList ;
import static org.hamcrest.Matchers.greaterThan ;
import static org.junit.Assert.assertNotNull ;
import static org.junit.Assert.assertThat ;
@RunWith ( SpringRunner . class )
@RunWith ( SpringRunner . class )
@SpringBootTest
@SpringBootTest
@ -238,14 +242,16 @@ public class ResourceMapperTest {
0 ,
0 ,
- 1 ,
- 1 ,
resource . getType ( ) . ordinal ( ) ,
resource . getType ( ) . ordinal ( ) ,
""
"" ,
new ArrayList < > ( )
) ;
) ;
IPage < Resource > resourceIPage1 = resourceMapper . queryResourcePaging (
IPage < Resource > resourceIPage1 = resourceMapper . queryResourcePaging (
page ,
page ,
1110 ,
1110 ,
- 1 ,
- 1 ,
resource . getType ( ) . ordinal ( ) ,
resource . getType ( ) . ordinal ( ) ,
""
"" ,
null
) ;
) ;
Assert . assertNotEquals ( resourceIPage . getTotal ( ) , 0 ) ;
Assert . assertNotEquals ( resourceIPage . getTotal ( ) , 0 ) ;
Assert . assertNotEquals ( resourceIPage1 . getTotal ( ) , 0 ) ;
Assert . assertNotEquals ( resourceIPage1 . getTotal ( ) , 0 ) ;
@ -259,7 +265,8 @@ public class ResourceMapperTest {
public void testQueryResourceListAuthored ( ) {
public void testQueryResourceListAuthored ( ) {
Resource resource = insertOne ( ) ;
Resource resource = insertOne ( ) ;
List < Resource > resources = resourceMapper . queryAuthorizedResourceList ( resource . getUserId ( ) ) ;
List < Integer > resIds = resourceUserMapper . queryResourcesIdListByUserIdAndPerm ( resource . getUserId ( ) , Constants . AUTHORIZE_WRITABLE_PERM ) ;
List < Resource > resources = CollectionUtils . isEmpty ( resIds ) ? new ArrayList < > ( ) : resourceMapper . queryResourceListById ( resIds ) ;
ResourcesUser resourcesUser = new ResourcesUser ( ) ;
ResourcesUser resourcesUser = new ResourcesUser ( ) ;
@ -268,7 +275,8 @@ public class ResourceMapperTest {
resourcesUser . setPerm ( Constants . AUTHORIZE_WRITABLE_PERM ) ;
resourcesUser . setPerm ( Constants . AUTHORIZE_WRITABLE_PERM ) ;
resourceUserMapper . insert ( resourcesUser ) ;
resourceUserMapper . insert ( resourcesUser ) ;
List < Resource > resources1 = resourceMapper . queryAuthorizedResourceList ( 1110 ) ;
List < Integer > resIds1 = resourceUserMapper . queryResourcesIdListByUserIdAndPerm ( 1110 , Constants . AUTHORIZE_WRITABLE_PERM ) ;
List < Resource > resources1 = CollectionUtils . isEmpty ( resIds1 ) ? new ArrayList < > ( ) : resourceMapper . queryResourceListById ( resIds1 ) ;
Assert . assertEquals ( 0 , resources . size ( ) ) ;
Assert . assertEquals ( 0 , resources . size ( ) ) ;
Assert . assertNotEquals ( 0 , resources1 . size ( ) ) ;
Assert . assertNotEquals ( 0 , resources1 . size ( ) ) ;
@ -282,7 +290,8 @@ public class ResourceMapperTest {
public void testQueryAuthorizedResourceList ( ) {
public void testQueryAuthorizedResourceList ( ) {
Resource resource = insertOne ( ) ;
Resource resource = insertOne ( ) ;
List < Resource > resources = resourceMapper . queryAuthorizedResourceList ( resource . getUserId ( ) ) ;
List < Integer > resIds = resourceUserMapper . queryResourcesIdListByUserIdAndPerm ( resource . getUserId ( ) , Constants . AUTHORIZE_WRITABLE_PERM ) ;
List < Resource > resources = CollectionUtils . isEmpty ( resIds ) ? new ArrayList < > ( ) : resourceMapper . queryResourceListById ( resIds ) ;
resourceMapper . deleteById ( resource . getId ( ) ) ;
resourceMapper . deleteById ( resource . getId ( ) ) ;
Assert . assertEquals ( 0 , resources . size ( ) ) ;
Assert . assertEquals ( 0 , resources . size ( ) ) ;
@ -306,7 +315,6 @@ public class ResourceMapperTest {
@Test
@Test
public void testQueryTenantCodeByResourceName ( ) {
public void testQueryTenantCodeByResourceName ( ) {
Tenant tenant = new Tenant ( ) ;
Tenant tenant = new Tenant ( ) ;
tenant . setTenantCode ( "ut tenant code for resource" ) ;
tenant . setTenantCode ( "ut tenant code for resource" ) ;
int tenantInsertStatus = tenantMapper . insert ( tenant ) ;
int tenantInsertStatus = tenantMapper . insert ( tenant ) ;
@ -324,7 +332,6 @@ public class ResourceMapperTest {
Assert . fail ( "insert user data error" ) ;
Assert . fail ( "insert user data error" ) ;
}
}
Resource resource = insertOne ( ) ;
Resource resource = insertOne ( ) ;
resource . setUserId ( user . getId ( ) ) ;
resource . setUserId ( user . getId ( ) ) ;
int userUpdateStatus = resourceMapper . updateById ( resource ) ;
int userUpdateStatus = resourceMapper . updateById ( resource ) ;
@ -332,12 +339,13 @@ public class ResourceMapperTest {
Assert . fail ( "update user data error" ) ;
Assert . fail ( "update user data error" ) ;
}
}
String resource1 = resourceMapper . queryTenantCodeByResourceName (
List < Resource > resourceList = resourceMapper . queryResource ( resource . getFullName ( ) , ResourceType . FILE . ordinal ( ) ) ;
resource . getFullName ( ) , ResourceType . FILE . ordinal ( )
) ;
int resourceUserId = resourceList . get ( 0 ) . getUserId ( ) ;
User resourceUser = userMapper . selectById ( resourceUserId ) ;
Tenant resourceTenant = tenantMapper . selectById ( resourceUser . getTenantId ( ) ) ;
Assert . assertEquals ( "ut tenant code for resource" , resource1 ) ;
Assert . assertEquals ( "ut tenant code for resource" , resourceTenant . getTenantCode ( ) ) ;
}
}
@ -358,7 +366,6 @@ public class ResourceMapperTest {
Assert . assertEquals ( generalUser2 . getId ( ) , resource . getUserId ( ) ) ;
Assert . assertEquals ( generalUser2 . getId ( ) , resource . getUserId ( ) ) ;
Assert . assertFalse ( resources . stream ( ) . map ( t - > t . getFullName ( ) ) . collect ( toList ( ) ) . containsAll ( Arrays . asList ( resNames ) ) ) ;
Assert . assertFalse ( resources . stream ( ) . map ( t - > t . getFullName ( ) ) . collect ( toList ( ) ) . containsAll ( Arrays . asList ( resNames ) ) ) ;
// authorize object unauthorizedResource to generalUser
// authorize object unauthorizedResource to generalUser
createResourcesUser ( unauthorizedResource , generalUser2 ) ;
createResourcesUser ( unauthorizedResource , generalUser2 ) ;
List < Resource > authorizedResources = resourceMapper . listAuthorizedResource ( generalUser2 . getId ( ) , resNames ) ;
List < Resource > authorizedResources = resourceMapper . listAuthorizedResource ( generalUser2 . getId ( ) , resNames ) ;
@ -390,10 +397,10 @@ public class ResourceMapperTest {
Resource resource = createResource ( generalUser1 ) ;
Resource resource = createResource ( generalUser1 ) ;
createResourcesUser ( resource , generalUser2 ) ;
createResourcesUser ( resource , generalUser2 ) ;
List < Resource > resourceList = resourceMapper . queryResourceListAuthored ( generalUser2 . getId ( ) , ResourceType . FILE . ordinal ( ) , 0 ) ;
List < Resource > resourceList = resourceMapper . queryResourceListAuthored ( generalUser2 . getId ( ) , ResourceType . FILE . ordinal ( ) ) ;
Assert . assertNotNull ( resourceList ) ;
Assert . assertNotNull ( resourceList ) ;
resourceList = resourceMapper . queryResourceListAuthored ( generalUser2 . getId ( ) , ResourceType . FILE . ordinal ( ) , 4 ) ;
resourceList = resourceMapper . queryResourceListAuthored ( generalUser2 . getId ( ) , ResourceType . FILE . ordinal ( ) ) ;
Assert . assertFalse ( resourceList . contains ( resource ) ) ;
Assert . assertFalse ( resourceList . contains ( resource ) ) ;
}
}
@ -423,3 +430,4 @@ public class ResourceMapperTest {
Assert . assertTrue ( resourceMapper . existResource ( fullName , userId , type ) ) ;
Assert . assertTrue ( resourceMapper . existResource ( fullName , userId , type ) ) ;
}
}
}
}