|
@@ -16,38 +16,601 @@
|
|
|
*/
|
|
|
package org.apache.dolphinscheduler.api.service;
|
|
|
|
|
|
-import org.apache.dolphinscheduler.api.ApiApplicationServer;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import org.apache.dolphinscheduler.api.enums.Status;
|
|
|
+import org.apache.dolphinscheduler.api.utils.PageInfo;
|
|
|
+import org.apache.dolphinscheduler.api.utils.Result;
|
|
|
import org.apache.dolphinscheduler.common.Constants;
|
|
|
import org.apache.dolphinscheduler.common.enums.ResourceType;
|
|
|
import org.apache.dolphinscheduler.common.enums.UserType;
|
|
|
+import org.apache.dolphinscheduler.common.utils.CollectionUtils;
|
|
|
+import org.apache.dolphinscheduler.common.utils.FileUtils;
|
|
|
+import org.apache.dolphinscheduler.common.utils.HadoopUtils;
|
|
|
+import org.apache.dolphinscheduler.common.utils.PropertyUtils;
|
|
|
+import org.apache.dolphinscheduler.dao.entity.Resource;
|
|
|
+import org.apache.dolphinscheduler.dao.entity.Tenant;
|
|
|
+import org.apache.dolphinscheduler.dao.entity.UdfFunc;
|
|
|
import org.apache.dolphinscheduler.dao.entity.User;
|
|
|
+import org.apache.dolphinscheduler.dao.mapper.*;
|
|
|
import org.junit.Assert;
|
|
|
+import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
import org.junit.runner.RunWith;
|
|
|
+import org.mockito.InjectMocks;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.Mockito;
|
|
|
+import org.powermock.api.mockito.PowerMockito;
|
|
|
+import org.powermock.core.classloader.annotations.PrepareForTest;
|
|
|
+import org.powermock.modules.junit4.PowerMockRunner;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.boot.test.context.SpringBootTest;
|
|
|
-import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+import org.springframework.mock.web.MockMultipartFile;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
-@RunWith(SpringRunner.class)
|
|
|
-@SpringBootTest(classes = ApiApplicationServer.class)
|
|
|
+@RunWith(PowerMockRunner.class)
|
|
|
+@PrepareForTest({HadoopUtils.class,PropertyUtils.class, FileUtils.class,org.apache.dolphinscheduler.api.utils.FileUtils.class})
|
|
|
public class ResourcesServiceTest {
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ResourcesServiceTest.class);
|
|
|
|
|
|
- @Autowired
|
|
|
+ @InjectMocks
|
|
|
private ResourcesService resourcesService;
|
|
|
+ @Mock
|
|
|
+ private ResourceMapper resourcesMapper;
|
|
|
+ @Mock
|
|
|
+ private TenantMapper tenantMapper;
|
|
|
+ @Mock
|
|
|
+ private ResourceUserMapper resourceUserMapper;
|
|
|
+ @Mock
|
|
|
+ private HadoopUtils hadoopUtils;
|
|
|
+ @Mock
|
|
|
+ private UserMapper userMapper;
|
|
|
+ @Mock
|
|
|
+ private UdfFuncMapper udfFunctionMapper;
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void setUp() {
|
|
|
+
|
|
|
+ PowerMockito.mockStatic(HadoopUtils.class);
|
|
|
+ PowerMockito.mockStatic(FileUtils.class);
|
|
|
+ PowerMockito.mockStatic(org.apache.dolphinscheduler.api.utils.FileUtils.class);
|
|
|
+ try {
|
|
|
+ // new HadoopUtils
|
|
|
+ PowerMockito.whenNew(HadoopUtils.class).withNoArguments().thenReturn(hadoopUtils);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ PowerMockito.when(HadoopUtils.getInstance()).thenReturn(hadoopUtils);
|
|
|
+ PowerMockito.mockStatic(PropertyUtils.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testCreateResource(){
|
|
|
+
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(false);
|
|
|
+ User user = new User();
|
|
|
+ //HDFS_NOT_STARTUP
|
|
|
+ Result result = resourcesService.createResource(user,"ResourcesServiceTest","ResourcesServiceTest",ResourceType.FILE,null);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.HDFS_NOT_STARTUP.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //RESOURCE_FILE_IS_EMPTY
|
|
|
+ MockMultipartFile mockMultipartFile = new MockMultipartFile("test.pdf",new String().getBytes());
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(true);
|
|
|
+ result = resourcesService.createResource(user,"ResourcesServiceTest","ResourcesServiceTest",ResourceType.FILE,mockMultipartFile);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_FILE_IS_EMPTY.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //RESOURCE_SUFFIX_FORBID_CHANGE
|
|
|
+ mockMultipartFile = new MockMultipartFile("test.pdf","test.pdf","pdf",new String("test").getBytes());
|
|
|
+ PowerMockito.when(FileUtils.suffix("test.pdf")).thenReturn("pdf");
|
|
|
+ PowerMockito.when(FileUtils.suffix("ResourcesServiceTest.jar")).thenReturn("jar");
|
|
|
+ result = resourcesService.createResource(user,"ResourcesServiceTest.jar","ResourcesServiceTest",ResourceType.FILE,mockMultipartFile);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_SUFFIX_FORBID_CHANGE.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //UDF_RESOURCE_SUFFIX_NOT_JAR
|
|
|
+ mockMultipartFile = new MockMultipartFile("ResourcesServiceTest.pdf","ResourcesServiceTest.pdf","pdf",new String("test").getBytes());
|
|
|
+ result = resourcesService.createResource(user,"ResourcesServiceTest.pdf","ResourcesServiceTest",ResourceType.UDF,mockMultipartFile);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.UDF_RESOURCE_SUFFIX_NOT_JAR.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+
|
|
|
+ //UDF_RESOURCE_SUFFIX_NOT_JAR
|
|
|
+ Mockito.when(tenantMapper.queryById(0)).thenReturn(getTenant());
|
|
|
+ Mockito.when(resourcesMapper.queryResourceList("ResourcesServiceTest.jar", 0, 1)).thenReturn(getResourceList());
|
|
|
+ mockMultipartFile = new MockMultipartFile("ResourcesServiceTest.jar","ResourcesServiceTest.jar","pdf",new String("test").getBytes());
|
|
|
+ result = resourcesService.createResource(user,"ResourcesServiceTest.jar","ResourcesServiceTest",ResourceType.UDF,mockMultipartFile);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_EXIST.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //SUCCESS
|
|
|
+ Mockito.when(resourcesMapper.queryResourceList("ResourcesServiceTest.jar", 0, 1)).thenReturn(new ArrayList<>());
|
|
|
+ result = resourcesService.createResource(user,"ResourcesServiceTest.jar","ResourcesServiceTest",ResourceType.UDF,mockMultipartFile);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.SUCCESS.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testUpdateResource(){
|
|
|
+
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(false);
|
|
|
+ User user = new User();
|
|
|
+ //HDFS_NOT_STARTUP
|
|
|
+ Result result = resourcesService.updateResource(user,1,"ResourcesServiceTest","ResourcesServiceTest",ResourceType.FILE);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.HDFS_NOT_STARTUP.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //RESOURCE_NOT_EXIST
|
|
|
+ Mockito.when(resourcesMapper.selectById(1)).thenReturn(getResource());
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(true);
|
|
|
+ result = resourcesService.updateResource(user,0,"ResourcesServiceTest","ResourcesServiceTest",ResourceType.FILE);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_NOT_EXIST.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //USER_NO_OPERATION_PERM
|
|
|
+ result = resourcesService.updateResource(user,1,"ResourcesServiceTest","ResourcesServiceTest",ResourceType.FILE);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.USER_NO_OPERATION_PERM.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //SUCCESS
|
|
|
+ user.setId(1);
|
|
|
+ result = resourcesService.updateResource(user,1,"ResourcesServiceTest.jar","ResourcesServiceTest.jar",ResourceType.FILE);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.SUCCESS.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //RESOURCE_EXIST
|
|
|
+ Mockito.when(resourcesMapper.queryResourceList("ResourcesServiceTest1.jar", 0, 0)).thenReturn(getResourceList());
|
|
|
+ result = resourcesService.updateResource(user,1,"ResourcesServiceTest1.jar","ResourcesServiceTest1.jar",ResourceType.FILE);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_EXIST.getMsg(),result.getMsg());
|
|
|
+ //USER_NOT_EXIST
|
|
|
+ result = resourcesService.updateResource(user,1,"ResourcesServiceTest1.jar","ResourcesServiceTest1.jar",ResourceType.UDF);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertTrue(Status.USER_NOT_EXIST.getCode() == result.getCode());
|
|
|
+
|
|
|
+ //TENANT_NOT_EXIST
|
|
|
+ Mockito.when(userMapper.queryDetailsById(1)).thenReturn(getUser());
|
|
|
+ result = resourcesService.updateResource(user,1,"ResourcesServiceTest1.jar","ResourcesServiceTest1.jar",ResourceType.UDF);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.TENANT_NOT_EXIST.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //RESOURCE_NOT_EXIST
|
|
|
+ Mockito.when(tenantMapper.queryById(1)).thenReturn(getTenant());
|
|
|
+ PowerMockito.when(HadoopUtils.getHdfsFilename(Mockito.any(), Mockito.any())).thenReturn("test1");
|
|
|
+
|
|
|
+ try {
|
|
|
+ Mockito.when(hadoopUtils.exists("test")).thenReturn(true);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ result = resourcesService.updateResource(user,1,"ResourcesServiceTest1.jar","ResourcesServiceTest1.jar",ResourceType.UDF);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_NOT_EXIST.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //SUCCESS
|
|
|
+ PowerMockito.when(HadoopUtils.getHdfsFilename(Mockito.any(), Mockito.any())).thenReturn("test");
|
|
|
+ result = resourcesService.updateResource(user,1,"ResourcesServiceTest1.jar","ResourcesServiceTest1.jar",ResourceType.UDF);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.SUCCESS.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testQueryResourceListPaging(){
|
|
|
+ User loginUser = new User();
|
|
|
+ loginUser.setUserType(UserType.ADMIN_USER);
|
|
|
+ IPage<Resource> resourcePage = new Page<>(1,10);
|
|
|
+ resourcePage.setTotal(1);
|
|
|
+ resourcePage.setRecords(getResourceList());
|
|
|
+ Mockito.when(resourcesMapper.queryResourcePaging(Mockito.any(Page.class),
|
|
|
+ Mockito.eq(0), Mockito.eq(0), Mockito.eq("test"))).thenReturn(resourcePage);
|
|
|
+ Map<String, Object> result = resourcesService.queryResourceListPaging(loginUser,ResourceType.FILE,"test",1,10);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
|
|
|
+ PageInfo pageInfo = (PageInfo) result.get(Constants.DATA_LIST);
|
|
|
+ Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists()));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testQueryResourceList(){
|
|
|
+ User loginUser = new User();
|
|
|
+ loginUser.setId(0);
|
|
|
+ loginUser.setUserType(UserType.ADMIN_USER);
|
|
|
+ Mockito.when(resourcesMapper.queryResourceListAuthored(0, 0)).thenReturn(getResourceList());
|
|
|
+ Map<String, Object> result = resourcesService.queryResourceList(loginUser, ResourceType.FILE);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
|
|
|
+ List<Resource> resourceList = (List<Resource>) result.get(Constants.DATA_LIST);
|
|
|
+ Assert.assertTrue(CollectionUtils.isNotEmpty(resourceList));
|
|
|
+ }
|
|
|
|
|
|
@Test
|
|
|
- public void querytResourceList(){
|
|
|
+ public void testDelete(){
|
|
|
+
|
|
|
User loginUser = new User();
|
|
|
- loginUser.setId(-1);
|
|
|
- loginUser.setUserType(UserType.GENERAL_USER);
|
|
|
+ loginUser.setId(0);
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(false);
|
|
|
+ Mockito.when(tenantMapper.queryById(1)).thenReturn(getTenant());
|
|
|
+
|
|
|
+ try {
|
|
|
+ // HDFS_NOT_STARTUP
|
|
|
+ Result result = resourcesService.delete(loginUser,1);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.HDFS_NOT_STARTUP.getMsg(), result.getMsg());
|
|
|
+
|
|
|
+ //RESOURCE_NOT_EXIST
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(true);
|
|
|
+ Mockito.when(resourcesMapper.selectById(1)).thenReturn(getResource());
|
|
|
+ result = resourcesService.delete(loginUser,2);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_NOT_EXIST.getMsg(), result.getMsg());
|
|
|
+
|
|
|
+ // USER_NO_OPERATION_PERM
|
|
|
+ result = resourcesService.delete(loginUser,2);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_NOT_EXIST.getMsg(), result.getMsg());
|
|
|
+
|
|
|
+ //TENANT_NOT_EXIST
|
|
|
+ loginUser.setUserType(UserType.ADMIN_USER);
|
|
|
+ loginUser.setTenantId(2);
|
|
|
+ result = resourcesService.delete(loginUser,1);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.TENANT_NOT_EXIST.getMsg(), result.getMsg());
|
|
|
+
|
|
|
+ //SUCCESS
|
|
|
+ loginUser.setTenantId(1);
|
|
|
+ Mockito.when(hadoopUtils.delete(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(true);
|
|
|
+ result = resourcesService.delete(loginUser,1);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.SUCCESS.getMsg(), result.getMsg());
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("delete error",e);
|
|
|
+ Assert.assertTrue(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testVerifyResourceName(){
|
|
|
+
|
|
|
+ User user = new User();
|
|
|
+ user.setId(1);
|
|
|
+ Mockito.when(resourcesMapper.queryResourceList("test", 0, 0)).thenReturn(getResourceList());
|
|
|
+ Result result = resourcesService.verifyResourceName("test",ResourceType.FILE,user);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_EXIST.getMsg(), result.getMsg());
|
|
|
+
|
|
|
+ //TENANT_NOT_EXIST
|
|
|
+ Mockito.when(tenantMapper.queryById(1)).thenReturn(getTenant());
|
|
|
+ result = resourcesService.verifyResourceName("test1",ResourceType.FILE,user);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.TENANT_NOT_EXIST.getMsg(), result.getMsg());
|
|
|
+
|
|
|
+
|
|
|
+ //RESOURCE_FILE_EXIST
|
|
|
+ user.setTenantId(1);
|
|
|
+ try {
|
|
|
+ Mockito.when(hadoopUtils.exists("test")).thenReturn(true);
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("hadoop error",e);
|
|
|
+ }
|
|
|
+ PowerMockito.when(HadoopUtils.getHdfsFilename("123", "test1")).thenReturn("test");
|
|
|
+ result = resourcesService.verifyResourceName("test1",ResourceType.FILE,user);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertTrue(Status.RESOURCE_FILE_EXIST.getCode()==result.getCode());
|
|
|
+
|
|
|
+ //SUCCESS
|
|
|
+ result = resourcesService.verifyResourceName("test2",ResourceType.FILE,user);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.SUCCESS.getMsg(), result.getMsg());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testReadResource(){
|
|
|
+
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(false);
|
|
|
+
|
|
|
+ //HDFS_NOT_STARTUP
|
|
|
+ Result result = resourcesService.readResource(1,1,10);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.HDFS_NOT_STARTUP.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //RESOURCE_NOT_EXIST
|
|
|
+ Mockito.when(resourcesMapper.selectById(1)).thenReturn(getResource());
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(true);
|
|
|
+ result = resourcesService.readResource(2,1,10);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_NOT_EXIST.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+
|
|
|
+ //RESOURCE_SUFFIX_NOT_SUPPORT_VIEW
|
|
|
+ PowerMockito.when(FileUtils.getResourceViewSuffixs()).thenReturn("class");
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(true);
|
|
|
+ result = resourcesService.readResource(1,1,10);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_SUFFIX_NOT_SUPPORT_VIEW.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //USER_NOT_EXIST
|
|
|
+ PowerMockito.when(FileUtils.getResourceViewSuffixs()).thenReturn("jar");
|
|
|
+ PowerMockito.when(FileUtils.suffix("ResourcesServiceTest.jar")).thenReturn("jar");
|
|
|
+ result = resourcesService.readResource(1,1,10);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertTrue(Status.USER_NOT_EXIST.getCode()==result.getCode());
|
|
|
+
|
|
|
+
|
|
|
+ //TENANT_NOT_EXIST
|
|
|
+ Mockito.when(userMapper.queryDetailsById(1)).thenReturn(getUser());
|
|
|
+ result = resourcesService.readResource(1,1,10);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.TENANT_NOT_EXIST.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+
|
|
|
+ //RESOURCE_FILE_NOT_EXIST
|
|
|
+ Mockito.when(tenantMapper.queryById(1)).thenReturn(getTenant());
|
|
|
+ try {
|
|
|
+ Mockito.when(hadoopUtils.exists(Mockito.anyString())).thenReturn(false);
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("hadoop error",e);
|
|
|
+ }
|
|
|
+ result = resourcesService.readResource(1,1,10);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertTrue(Status.RESOURCE_FILE_NOT_EXIST.getCode()==result.getCode());
|
|
|
+
|
|
|
+ //SUCCESS
|
|
|
+ try {
|
|
|
+ Mockito.when(hadoopUtils.exists(null)).thenReturn(true);
|
|
|
+ Mockito.when(hadoopUtils.catFile(null,1,10)).thenReturn(getContent());
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("hadoop error",e);
|
|
|
+ }
|
|
|
+ result = resourcesService.readResource(1,1,10);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.SUCCESS.getMsg(),result.getMsg());
|
|
|
|
|
|
- Map<String, Object> map = resourcesService.queryResourceList(loginUser, ResourceType.FILE);
|
|
|
- Assert.assertEquals(Status.SUCCESS, map.get(Constants.STATUS));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testOnlineCreateResource() {
|
|
|
+
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(false);
|
|
|
+ PowerMockito.when(HadoopUtils.getHdfsResDir("hdfsdDir")).thenReturn("hdfsDir");
|
|
|
+ PowerMockito.when(HadoopUtils.getHdfsUdfDir("udfDir")).thenReturn("udfDir");
|
|
|
+ User user = getUser();
|
|
|
+ //HDFS_NOT_STARTUP
|
|
|
+ Result result = resourcesService.onlineCreateResource(user,ResourceType.FILE,"test","jar","desc","content");
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.HDFS_NOT_STARTUP.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //RESOURCE_SUFFIX_NOT_SUPPORT_VIEW
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(true);
|
|
|
+ PowerMockito.when(FileUtils.getResourceViewSuffixs()).thenReturn("class");
|
|
|
+ result = resourcesService.onlineCreateResource(user,ResourceType.FILE,"test","jar","desc","content");
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_SUFFIX_NOT_SUPPORT_VIEW.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //RuntimeException
|
|
|
+ try {
|
|
|
+ PowerMockito.when(FileUtils.getResourceViewSuffixs()).thenReturn("jar");
|
|
|
+ Mockito.when(tenantMapper.queryById(1)).thenReturn(getTenant());
|
|
|
+ result = resourcesService.onlineCreateResource(user, ResourceType.FILE, "test", "jar", "desc", "content");
|
|
|
+ }catch (RuntimeException ex){
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_NOT_EXIST.getMsg(), ex.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ //SUCCESS
|
|
|
+ Mockito.when(FileUtils.getUploadFilename(Mockito.anyString(), Mockito.anyString())).thenReturn("test");
|
|
|
+ PowerMockito.when(FileUtils.writeContent2File(Mockito.anyString(), Mockito.anyString())).thenReturn(true);
|
|
|
+ result = resourcesService.onlineCreateResource(user,ResourceType.FILE,"test","jar","desc","content");
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.SUCCESS.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testUpdateResourceContent(){
|
|
|
+
|
|
|
+ User loginUser = new User();
|
|
|
+ loginUser.setId(0);
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(false);
|
|
|
+
|
|
|
+ // HDFS_NOT_STARTUP
|
|
|
+ Result result = resourcesService.updateResourceContent(1,"content");
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.HDFS_NOT_STARTUP.getMsg(), result.getMsg());
|
|
|
+
|
|
|
+ //RESOURCE_NOT_EXIST
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(true);
|
|
|
+ Mockito.when(resourcesMapper.selectById(1)).thenReturn(getResource());
|
|
|
+ result = resourcesService.updateResourceContent(2,"content");
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_NOT_EXIST.getMsg(), result.getMsg());
|
|
|
+
|
|
|
+ //RESOURCE_SUFFIX_NOT_SUPPORT_VIEW
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(true);
|
|
|
+ PowerMockito.when(FileUtils.getResourceViewSuffixs()).thenReturn("class");
|
|
|
+ result = resourcesService.updateResourceContent(1,"content");
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.RESOURCE_SUFFIX_NOT_SUPPORT_VIEW.getMsg(),result.getMsg());
|
|
|
+
|
|
|
+ //USER_NOT_EXIST
|
|
|
+ PowerMockito.when(FileUtils.getResourceViewSuffixs()).thenReturn("jar");
|
|
|
+ PowerMockito.when(FileUtils.suffix("ResourcesServiceTest.jar")).thenReturn("jar");
|
|
|
+ result = resourcesService.updateResourceContent(1,"content");
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertTrue(Status.USER_NOT_EXIST.getCode() == result.getCode());
|
|
|
+
|
|
|
+
|
|
|
+ //TENANT_NOT_EXIST
|
|
|
+ Mockito.when(userMapper.queryDetailsById(1)).thenReturn(getUser());
|
|
|
+ result = resourcesService.updateResourceContent(1,"content");
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertTrue(Status.TENANT_NOT_EXIST.getCode() == result.getCode());
|
|
|
+
|
|
|
+ //SUCCESS
|
|
|
+ Mockito.when(tenantMapper.queryById(1)).thenReturn(getTenant());
|
|
|
+ Mockito.when(FileUtils.getUploadFilename(Mockito.anyString(), Mockito.anyString())).thenReturn("test");
|
|
|
+ PowerMockito.when(FileUtils.writeContent2File(Mockito.anyString(), Mockito.anyString())).thenReturn(true);
|
|
|
+ result = resourcesService.updateResourceContent(1,"content");
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.SUCCESS.getMsg(), result.getMsg());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testDownloadResource(){
|
|
|
+
|
|
|
+ PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(true);
|
|
|
+ Mockito.when(tenantMapper.queryById(1)).thenReturn(getTenant());
|
|
|
+ Mockito.when(userMapper.queryDetailsById(1)).thenReturn(getUser());
|
|
|
+ org.springframework.core.io.Resource resourceMock = Mockito.mock(org.springframework.core.io.Resource.class);
|
|
|
+ try {
|
|
|
+ //resource null
|
|
|
+ org.springframework.core.io.Resource resource = resourcesService.downloadResource(1);
|
|
|
+ Assert.assertNull(resource);
|
|
|
+
|
|
|
+ Mockito.when(resourcesMapper.selectById(1)).thenReturn(getResource());
|
|
|
+ PowerMockito.when(org.apache.dolphinscheduler.api.utils.FileUtils.file2Resource(Mockito.any())).thenReturn(resourceMock);
|
|
|
+ resource = resourcesService.downloadResource(1);
|
|
|
+ Assert.assertNotNull(resource);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("DownloadResource error",e);
|
|
|
+ Assert.assertTrue(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testUnauthorizedFile(){
|
|
|
+ User user = getUser();
|
|
|
+ //USER_NO_OPERATION_PERM
|
|
|
+ Map<String, Object> result = resourcesService.unauthorizedFile(user,1);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS));
|
|
|
+
|
|
|
+ //SUCCESS
|
|
|
+ user.setUserType(UserType.ADMIN_USER);
|
|
|
+ Mockito.when(resourcesMapper.queryResourceExceptUserId(1)).thenReturn(getResourceList());
|
|
|
+ result = resourcesService.unauthorizedFile(user,1);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS));
|
|
|
+ List<Resource> resources = (List<Resource>) result.get(Constants.DATA_LIST);
|
|
|
+ Assert.assertTrue(CollectionUtils.isNotEmpty(resources));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testUnauthorizedUDFFunction(){
|
|
|
+
|
|
|
+ User user = getUser();
|
|
|
+ //USER_NO_OPERATION_PERM
|
|
|
+ Map<String, Object> result = resourcesService.unauthorizedUDFFunction(user,1);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS));
|
|
|
+
|
|
|
+ //SUCCESS
|
|
|
+ user.setUserType(UserType.ADMIN_USER);
|
|
|
+ Mockito.when(udfFunctionMapper.queryUdfFuncExceptUserId(1)).thenReturn(getUdfFuncList());
|
|
|
+ result = resourcesService.unauthorizedUDFFunction(user,1);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS));
|
|
|
+ List<UdfFunc> udfFuncs = (List<UdfFunc>) result.get(Constants.DATA_LIST);
|
|
|
+ Assert.assertTrue(CollectionUtils.isNotEmpty(udfFuncs));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testAuthorizedUDFFunction(){
|
|
|
+ User user = getUser();
|
|
|
+ //USER_NO_OPERATION_PERM
|
|
|
+ Map<String, Object> result = resourcesService.authorizedUDFFunction(user,1);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS));
|
|
|
+ //SUCCESS
|
|
|
+ user.setUserType(UserType.ADMIN_USER);
|
|
|
+ Mockito.when(udfFunctionMapper.queryAuthedUdfFunc(1)).thenReturn(getUdfFuncList());
|
|
|
+ result = resourcesService.authorizedUDFFunction(user,1);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS));
|
|
|
+ List<UdfFunc> udfFuncs = (List<UdfFunc>) result.get(Constants.DATA_LIST);
|
|
|
+ Assert.assertTrue(CollectionUtils.isNotEmpty(udfFuncs));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testAuthorizedFile(){
|
|
|
+
|
|
|
+ User user = getUser();
|
|
|
+ //USER_NO_OPERATION_PERM
|
|
|
+ Map<String, Object> result = resourcesService.authorizedFile(user,1);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS));
|
|
|
+ //SUCCESS
|
|
|
+ user.setUserType(UserType.ADMIN_USER);
|
|
|
+ Mockito.when(resourcesMapper.queryAuthorizedResourceList(1)).thenReturn(getResourceList());
|
|
|
+ result = resourcesService.authorizedFile(user,1);
|
|
|
+ logger.info(result.toString());
|
|
|
+ Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS));
|
|
|
+ List<Resource> resources = (List<Resource>) result.get(Constants.DATA_LIST);
|
|
|
+ Assert.assertTrue(CollectionUtils.isNotEmpty(resources));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private List<Resource> getResourceList(){
|
|
|
+
|
|
|
+ List<Resource> resources = new ArrayList<>();
|
|
|
+ resources.add(getResource());
|
|
|
+ return resources;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private Tenant getTenant() {
|
|
|
+ Tenant tenant = new Tenant();
|
|
|
+ tenant.setTenantCode("123");
|
|
|
+ return tenant;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Resource getResource(){
|
|
|
+
|
|
|
+ Resource resource = new Resource();
|
|
|
+ resource.setUserId(1);
|
|
|
+ resource.setDescription("ResourcesServiceTest.jar");
|
|
|
+ resource.setAlias("ResourcesServiceTest.jar");
|
|
|
+ resource.setType(ResourceType.FILE);
|
|
|
+ return resource;
|
|
|
+ }
|
|
|
+
|
|
|
+ private UdfFunc getUdfFunc(){
|
|
|
+
|
|
|
+ UdfFunc udfFunc = new UdfFunc();
|
|
|
+ udfFunc.setId(1);
|
|
|
+ return udfFunc;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<UdfFunc> getUdfFuncList(){
|
|
|
+
|
|
|
+ List<UdfFunc> udfFuncs = new ArrayList<>();
|
|
|
+ udfFuncs.add(getUdfFunc());
|
|
|
+ return udfFuncs;
|
|
|
+ }
|
|
|
+
|
|
|
+ private User getUser(){
|
|
|
+ User user = new User();
|
|
|
+ user.setId(1);
|
|
|
+ user.setTenantId(1);
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+ private List<String> getContent(){
|
|
|
+ List<String> contentList = new ArrayList<>();
|
|
|
+ contentList.add("test");
|
|
|
+ return contentList;
|
|
|
}
|
|
|
}
|