|
|
|
@ -18,7 +18,6 @@
|
|
|
|
|
package org.apache.dolphinscheduler.api.service; |
|
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue; |
|
|
|
|
import static org.mockito.ArgumentMatchers.eq; |
|
|
|
|
import static org.mockito.Mockito.when; |
|
|
|
|
|
|
|
|
@ -70,8 +69,6 @@ import org.mockito.Mockito;
|
|
|
|
|
import org.mockito.junit.jupiter.MockitoExtension; |
|
|
|
|
import org.mockito.junit.jupiter.MockitoSettings; |
|
|
|
|
import org.mockito.quality.Strictness; |
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
import org.springframework.mock.web.MockMultipartFile; |
|
|
|
|
|
|
|
|
|
import com.google.common.io.Files; |
|
|
|
@ -83,9 +80,10 @@ import com.google.common.io.Files;
|
|
|
|
|
@MockitoSettings(strictness = Strictness.LENIENT) |
|
|
|
|
public class ResourcesServiceTest { |
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ResourcesServiceTest.class); |
|
|
|
|
|
|
|
|
|
private static final String basePath = "/dolphinscheduler"; |
|
|
|
|
private static final String tenantCode = "123"; |
|
|
|
|
private static final String tenantFileResourceDir = "/dolphinscheduler/123/resources/"; |
|
|
|
|
private static final String tenantUdfResourceDir = "/dolphinscheduler/123/udfs/"; |
|
|
|
|
|
|
|
|
|
@InjectMocks |
|
|
|
|
private ResourcesServiceImpl resourcesService; |
|
|
|
@ -153,18 +151,30 @@ public class ResourcesServiceTest {
|
|
|
|
|
// CURRENT_LOGIN_USER_TENANT_NOT_EXIST
|
|
|
|
|
when(userMapper.selectById(user.getId())).thenReturn(getUser()); |
|
|
|
|
when(tenantMapper.queryById(1)).thenReturn(null); |
|
|
|
|
Assertions.assertThrows(ServiceException.class, |
|
|
|
|
ServiceException serviceException = Assertions.assertThrows(ServiceException.class, |
|
|
|
|
() -> resourcesService.uploadResource(user, "ResourcesServiceTest", ResourceType.FILE, |
|
|
|
|
new MockMultipartFile("test.pdf", "test.pdf", "pdf", "test".getBytes()), "/")); |
|
|
|
|
assertEquals(Status.CURRENT_LOGIN_USER_TENANT_NOT_EXIST.getMsg(), serviceException.getMessage()); |
|
|
|
|
|
|
|
|
|
// set tenant for user
|
|
|
|
|
user.setTenantId(1); |
|
|
|
|
when(tenantMapper.queryById(1)).thenReturn(getTenant()); |
|
|
|
|
when(storageOperate.getDir(ResourceType.ALL, tenantCode)).thenReturn(basePath); |
|
|
|
|
|
|
|
|
|
// ILLEGAL_RESOURCE_FILE
|
|
|
|
|
String illegal_path = "/dolphinscheduler/123/../"; |
|
|
|
|
serviceException = Assertions.assertThrows(ServiceException.class, |
|
|
|
|
() -> { |
|
|
|
|
MockMultipartFile mockMultipartFile = new MockMultipartFile("test.pdf", "".getBytes()); |
|
|
|
|
resourcesService.uploadResource(user, "ResourcesServiceTest", ResourceType.FILE, |
|
|
|
|
mockMultipartFile, illegal_path); |
|
|
|
|
}); |
|
|
|
|
assertEquals(new ServiceException(Status.ILLEGAL_RESOURCE_PATH, illegal_path), serviceException); |
|
|
|
|
|
|
|
|
|
// RESOURCE_FILE_IS_EMPTY
|
|
|
|
|
MockMultipartFile mockMultipartFile = new MockMultipartFile("test.pdf", "".getBytes()); |
|
|
|
|
Result result = resourcesService.uploadResource(user, "ResourcesServiceTest", ResourceType.FILE, |
|
|
|
|
mockMultipartFile, "/"); |
|
|
|
|
logger.info(result.toString()); |
|
|
|
|
mockMultipartFile, tenantFileResourceDir); |
|
|
|
|
assertEquals(Status.RESOURCE_FILE_IS_EMPTY.getMsg(), result.getMsg()); |
|
|
|
|
|
|
|
|
|
// RESOURCE_SUFFIX_FORBID_CHANGE
|
|
|
|
@ -172,8 +182,7 @@ public class ResourcesServiceTest {
|
|
|
|
|
when(Files.getFileExtension("test.pdf")).thenReturn("pdf"); |
|
|
|
|
when(Files.getFileExtension("ResourcesServiceTest.jar")).thenReturn("jar"); |
|
|
|
|
result = resourcesService.uploadResource(user, "ResourcesServiceTest.jar", ResourceType.FILE, mockMultipartFile, |
|
|
|
|
"/"); |
|
|
|
|
logger.info(result.toString()); |
|
|
|
|
tenantFileResourceDir); |
|
|
|
|
assertEquals(Status.RESOURCE_SUFFIX_FORBID_CHANGE.getMsg(), result.getMsg()); |
|
|
|
|
|
|
|
|
|
// UDF_RESOURCE_SUFFIX_NOT_JAR
|
|
|
|
@ -181,45 +190,42 @@ public class ResourcesServiceTest {
|
|
|
|
|
new MockMultipartFile("ResourcesServiceTest.pdf", "ResourcesServiceTest.pdf", "pdf", "test".getBytes()); |
|
|
|
|
when(Files.getFileExtension("ResourcesServiceTest.pdf")).thenReturn("pdf"); |
|
|
|
|
result = resourcesService.uploadResource(user, "ResourcesServiceTest.pdf", ResourceType.UDF, mockMultipartFile, |
|
|
|
|
"/"); |
|
|
|
|
logger.info(result.toString()); |
|
|
|
|
tenantUdfResourceDir); |
|
|
|
|
assertEquals(Status.UDF_RESOURCE_SUFFIX_NOT_JAR.getMsg(), result.getMsg()); |
|
|
|
|
|
|
|
|
|
// FULL_FILE_NAME_TOO_LONG
|
|
|
|
|
String tooLongFileName = getRandomStringWithLength(Constants.RESOURCE_FULL_NAME_MAX_LENGTH) + ".pdf"; |
|
|
|
|
mockMultipartFile = new MockMultipartFile(tooLongFileName, tooLongFileName, "pdf", "test".getBytes()); |
|
|
|
|
when(Files.getFileExtension(tooLongFileName)).thenReturn("pdf"); |
|
|
|
|
|
|
|
|
|
// '/databasePath/tenantCode/RESOURCE/'
|
|
|
|
|
when(storageOperate.getResDir(tenantCode)).thenReturn("/dolphinscheduler/123/resources/"); |
|
|
|
|
result = resourcesService.uploadResource(user, tooLongFileName, ResourceType.FILE, mockMultipartFile, "/"); |
|
|
|
|
logger.info(result.toString()); |
|
|
|
|
when(storageOperate.getResDir(tenantCode)).thenReturn(tenantFileResourceDir); |
|
|
|
|
result = resourcesService.uploadResource(user, tooLongFileName, ResourceType.FILE, mockMultipartFile, |
|
|
|
|
tenantFileResourceDir); |
|
|
|
|
assertEquals(Status.RESOURCE_FULL_NAME_TOO_LONG_ERROR.getMsg(), result.getMsg()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testCreateDirecotry() { |
|
|
|
|
public void testCreateDirecotry() throws IOException { |
|
|
|
|
User user = new User(); |
|
|
|
|
user.setId(1); |
|
|
|
|
user.setUserType(UserType.GENERAL_USER); |
|
|
|
|
|
|
|
|
|
String fileName = "directoryTest"; |
|
|
|
|
// RESOURCE_EXIST
|
|
|
|
|
user.setId(1); |
|
|
|
|
user.setTenantId(1); |
|
|
|
|
when(tenantMapper.queryById(1)).thenReturn(getTenant()); |
|
|
|
|
when(userMapper.selectById(user.getId())).thenReturn(getUser()); |
|
|
|
|
when(storageOperate.getResDir(tenantCode)).thenReturn("/dolphinscheduler/123/resources/"); |
|
|
|
|
try { |
|
|
|
|
when(storageOperate.exists("/dolphinscheduler/123/resources/directoryTest")).thenReturn(true); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
logger.error(e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
Result result = resourcesService.createDirectory(user, "directoryTest", ResourceType.FILE, -1, "/"); |
|
|
|
|
logger.info(result.toString()); |
|
|
|
|
when(storageOperate.getDir(ResourceType.ALL, tenantCode)).thenReturn(basePath); |
|
|
|
|
when(storageOperate.getResDir(tenantCode)).thenReturn(tenantFileResourceDir); |
|
|
|
|
when(storageOperate.exists(tenantFileResourceDir + fileName)).thenReturn(true); |
|
|
|
|
Result result = resourcesService.createDirectory(user, fileName, ResourceType.FILE, -1, tenantFileResourceDir); |
|
|
|
|
assertEquals(Status.RESOURCE_EXIST.getMsg(), result.getMsg()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testUpdateResource() { |
|
|
|
|
public void testUpdateResource() throws Exception { |
|
|
|
|
User user = new User(); |
|
|
|
|
user.setId(1); |
|
|
|
|
user.setUserType(UserType.GENERAL_USER); |
|
|
|
@ -227,7 +233,13 @@ public class ResourcesServiceTest {
|
|
|
|
|
|
|
|
|
|
when(userMapper.selectById(user.getId())).thenReturn(getUser()); |
|
|
|
|
when(tenantMapper.queryById(1)).thenReturn(getTenant()); |
|
|
|
|
when(storageOperate.getResDir(tenantCode)).thenReturn("/dolphinscheduler/123/resources/"); |
|
|
|
|
when(storageOperate.getDir(ResourceType.ALL, tenantCode)).thenReturn(basePath); |
|
|
|
|
when(storageOperate.getResDir(tenantCode)).thenReturn(tenantFileResourceDir); |
|
|
|
|
|
|
|
|
|
// TENANT_NOT_EXIST
|
|
|
|
|
when(tenantMapper.queryById(Mockito.anyInt())).thenReturn(null); |
|
|
|
|
Assertions.assertThrows(ServiceException.class, () -> resourcesService.updateResource(user, |
|
|
|
|
"ResourcesServiceTest1.jar", "", "ResourcesServiceTest", ResourceType.UDF, null)); |
|
|
|
|
|
|
|
|
|
// USER_NO_OPERATION_PERM
|
|
|
|
|
user.setUserType(UserType.GENERAL_USER); |
|
|
|
@ -235,92 +247,58 @@ public class ResourcesServiceTest {
|
|
|
|
|
Tenant tenantWNoPermission = new Tenant(); |
|
|
|
|
tenantWNoPermission.setTenantCode("321"); |
|
|
|
|
when(tenantMapper.queryById(1)).thenReturn(tenantWNoPermission); |
|
|
|
|
Result result = resourcesService.updateResource(user, "/dolphinscheduler/123/resources/ResourcesServiceTest", |
|
|
|
|
tenantCode, "ResourcesServiceTest", ResourceType.FILE, null); |
|
|
|
|
logger.info(result.toString()); |
|
|
|
|
when(storageOperate.getDir(ResourceType.ALL, "321")).thenReturn(basePath); |
|
|
|
|
|
|
|
|
|
String fileName = "ResourcesServiceTest"; |
|
|
|
|
Result result = resourcesService.updateResource(user, tenantFileResourceDir + fileName, |
|
|
|
|
tenantCode, fileName, ResourceType.FILE, null); |
|
|
|
|
assertEquals(Status.NO_CURRENT_OPERATING_PERMISSION.getMsg(), result.getMsg()); |
|
|
|
|
|
|
|
|
|
// SUCCESS
|
|
|
|
|
when(tenantMapper.queryById(1)).thenReturn(getTenant()); |
|
|
|
|
try { |
|
|
|
|
when(storageOperate.exists(Mockito.any())).thenReturn(false); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
logger.error(e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
when(storageOperate.getFileStatus("/dolphinscheduler/123/resources/ResourcesServiceTest", |
|
|
|
|
"/dolphinscheduler/123/resources/", tenantCode, ResourceType.FILE)) |
|
|
|
|
.thenReturn(getStorageEntityResource()); |
|
|
|
|
result = resourcesService.updateResource(user, "/dolphinscheduler/123/resources/ResourcesServiceTest", |
|
|
|
|
tenantCode, "ResourcesServiceTest", ResourceType.FILE, null); |
|
|
|
|
logger.info(result.toString()); |
|
|
|
|
when(storageOperate.getDir(ResourceType.FILE, tenantCode)).thenReturn(tenantFileResourceDir); |
|
|
|
|
when(storageOperate.getFileStatus(tenantFileResourceDir + fileName, |
|
|
|
|
tenantFileResourceDir, tenantCode, ResourceType.FILE)) |
|
|
|
|
.thenReturn(getStorageEntityResource(fileName)); |
|
|
|
|
result = resourcesService.updateResource(user, tenantFileResourceDir + fileName, |
|
|
|
|
tenantCode, fileName, ResourceType.FILE, null); |
|
|
|
|
assertEquals(Status.SUCCESS.getMsg(), result.getMsg()); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error(e.getMessage() + " Resource path: {}", "/dolphinscheduler/123/resources/ResourcesServiceTest", |
|
|
|
|
e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Tests for udf resources.
|
|
|
|
|
// RESOURCE_EXIST
|
|
|
|
|
try { |
|
|
|
|
when(storageOperate.exists("/dolphinscheduler/123/resources/ResourcesServiceTest2.jar")).thenReturn(true); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
logger.error("error occurred when checking resource: " |
|
|
|
|
+ "/dolphinscheduler/123/resources/ResourcesServiceTest2.jar"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
when(storageOperate.getFileStatus("/dolphinscheduler/123/resources/ResourcesServiceTest1.jar", |
|
|
|
|
"/dolphinscheduler/123/resources/", tenantCode, ResourceType.UDF)) |
|
|
|
|
.thenReturn(getStorageEntityUdfResource()); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error(e.getMessage() + " Resource path: {}", |
|
|
|
|
"/dolphinscheduler/123/resources/ResourcesServiceTest1.jar", e); |
|
|
|
|
} |
|
|
|
|
result = resourcesService.updateResource(user, "/dolphinscheduler/123/resources/ResourcesServiceTest1.jar", |
|
|
|
|
tenantCode, "ResourcesServiceTest2.jar", ResourceType.UDF, null); |
|
|
|
|
logger.info(result.toString()); |
|
|
|
|
assertEquals(Status.RESOURCE_EXIST.getMsg(), result.getMsg()); |
|
|
|
|
|
|
|
|
|
// TENANT_NOT_EXIST
|
|
|
|
|
when(tenantMapper.queryById(Mockito.anyInt())).thenReturn(null); |
|
|
|
|
Assertions.assertThrows(ServiceException.class, () -> resourcesService.updateResource(user, |
|
|
|
|
"ResourcesServiceTest1.jar", "", "ResourcesServiceTest", ResourceType.UDF, null)); |
|
|
|
|
|
|
|
|
|
// SUCCESS
|
|
|
|
|
when(tenantMapper.queryById(1)).thenReturn(getTenant()); |
|
|
|
|
|
|
|
|
|
result = resourcesService.updateResource(user, "/dolphinscheduler/123/resources/ResourcesServiceTest1.jar", |
|
|
|
|
tenantCode, "ResourcesServiceTest1.jar", ResourceType.UDF, null); |
|
|
|
|
logger.info(result.toString()); |
|
|
|
|
fileName = "ResourcesServiceTest.jar"; |
|
|
|
|
when(storageOperate.getDir(ResourceType.UDF, tenantCode)).thenReturn(tenantUdfResourceDir); |
|
|
|
|
when(storageOperate.exists(tenantUdfResourceDir + fileName)).thenReturn(true); |
|
|
|
|
when(storageOperate.getFileStatus(tenantUdfResourceDir + fileName, tenantUdfResourceDir, tenantCode, |
|
|
|
|
ResourceType.UDF)) |
|
|
|
|
.thenReturn(getStorageEntityUdfResource(fileName)); |
|
|
|
|
result = resourcesService.updateResource(user, tenantUdfResourceDir + fileName, |
|
|
|
|
tenantCode, fileName, ResourceType.UDF, null); |
|
|
|
|
assertEquals(Status.SUCCESS.getMsg(), result.getMsg()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testQueryResourceListPaging() { |
|
|
|
|
public void testQueryResourceListPaging() throws Exception { |
|
|
|
|
User loginUser = new User(); |
|
|
|
|
loginUser.setId(1); |
|
|
|
|
loginUser.setTenantId(1); |
|
|
|
|
loginUser.setTenantCode("tenant1"); |
|
|
|
|
loginUser.setUserType(UserType.ADMIN_USER); |
|
|
|
|
List<StorageEntity> mockResList = new ArrayList<StorageEntity>(); |
|
|
|
|
mockResList.add(getStorageEntityResource()); |
|
|
|
|
List<User> mockUserList = new ArrayList<User>(); |
|
|
|
|
|
|
|
|
|
String fileName = "ResourcesServiceTest"; |
|
|
|
|
List<StorageEntity> mockResList = new ArrayList<>(); |
|
|
|
|
mockResList.add(getStorageEntityResource(fileName)); |
|
|
|
|
List<User> mockUserList = new ArrayList<>(); |
|
|
|
|
mockUserList.add(getUser()); |
|
|
|
|
when(userMapper.selectList(null)).thenReturn(mockUserList); |
|
|
|
|
when(userMapper.selectById(getUser().getId())).thenReturn(getUser()); |
|
|
|
|
when(tenantMapper.queryById(getUser().getTenantId())).thenReturn(getTenant()); |
|
|
|
|
when(storageOperate.getResDir(tenantCode)).thenReturn("/dolphinscheduler/123/resources/"); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
when(storageOperate.listFilesStatus("/dolphinscheduler/123/resources/", "/dolphinscheduler/123/resources/", |
|
|
|
|
when(storageOperate.getResDir(tenantCode)).thenReturn(tenantFileResourceDir); |
|
|
|
|
when(storageOperate.listFilesStatus(tenantFileResourceDir, tenantFileResourceDir, |
|
|
|
|
tenantCode, ResourceType.FILE)).thenReturn(mockResList); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error("QueryResourceListPaging Error"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Result result = resourcesService.queryResourceListPaging(loginUser, "", "", ResourceType.FILE, "Test", 1, 10); |
|
|
|
|
logger.info(result.toString()); |
|
|
|
|
assertEquals(Status.SUCCESS.getCode(), (int) result.getCode()); |
|
|
|
|
PageInfo pageInfo = (PageInfo) result.getData(); |
|
|
|
|
Assertions.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getTotalList())); |
|
|
|
@ -330,29 +308,30 @@ public class ResourcesServiceTest {
|
|
|
|
|
@Test |
|
|
|
|
public void testQueryResourceList() { |
|
|
|
|
User loginUser = getUser(); |
|
|
|
|
String fileName = "ResourcesServiceTest"; |
|
|
|
|
|
|
|
|
|
when(userMapper.selectList(null)).thenReturn(Collections.singletonList(loginUser)); |
|
|
|
|
when(userMapper.selectById(loginUser.getId())).thenReturn(loginUser); |
|
|
|
|
when(tenantMapper.queryById(Mockito.anyInt())).thenReturn(getTenant()); |
|
|
|
|
when(storageOperate.getDir(ResourceType.ALL, tenantCode)).thenReturn("/dolphinscheduler"); |
|
|
|
|
when(storageOperate.getDir(ResourceType.FILE, tenantCode)).thenReturn("/dolphinscheduler/123/resources/"); |
|
|
|
|
when(storageOperate.getResDir(tenantCode)).thenReturn("/dolphinscheduler/123/resources/"); |
|
|
|
|
when(storageOperate.listFilesStatusRecursively("/dolphinscheduler/123/resources/", |
|
|
|
|
"/dolphinscheduler/123/resources/", tenantCode, ResourceType.FILE)) |
|
|
|
|
.thenReturn(Collections.singletonList(getStorageEntityResource())); |
|
|
|
|
when(storageOperate.getDir(ResourceType.ALL, tenantCode)).thenReturn(basePath); |
|
|
|
|
when(storageOperate.getDir(ResourceType.FILE, tenantCode)).thenReturn(tenantFileResourceDir); |
|
|
|
|
when(storageOperate.getResDir(tenantCode)).thenReturn(tenantFileResourceDir); |
|
|
|
|
when(storageOperate.listFilesStatusRecursively(tenantFileResourceDir, |
|
|
|
|
tenantFileResourceDir, tenantCode, ResourceType.FILE)) |
|
|
|
|
.thenReturn(Collections.singletonList(getStorageEntityResource(fileName))); |
|
|
|
|
Map<String, Object> result = |
|
|
|
|
resourcesService.queryResourceList(loginUser, ResourceType.FILE, "/dolphinscheduler/123/resources/"); |
|
|
|
|
resourcesService.queryResourceList(loginUser, ResourceType.FILE, tenantFileResourceDir); |
|
|
|
|
assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); |
|
|
|
|
List<ResourceComponent> resourceList = (List<ResourceComponent>) result.get(Constants.DATA_LIST); |
|
|
|
|
Assertions.assertTrue(CollectionUtils.isNotEmpty(resourceList)); |
|
|
|
|
|
|
|
|
|
// test udf
|
|
|
|
|
when(storageOperate.getDir(ResourceType.UDF, tenantCode)).thenReturn("/dolphinscheduler/123/udfs/"); |
|
|
|
|
when(storageOperate.getUdfDir(tenantCode)).thenReturn("/dolphinscheduler/123/udfs/"); |
|
|
|
|
when(storageOperate.listFilesStatusRecursively("/dolphinscheduler/123/udfs/", "/dolphinscheduler/123/udfs/", |
|
|
|
|
tenantCode, ResourceType.UDF)).thenReturn(Arrays.asList(getStorageEntityUdfResource())); |
|
|
|
|
when(storageOperate.getDir(ResourceType.UDF, tenantCode)).thenReturn(tenantUdfResourceDir); |
|
|
|
|
when(storageOperate.getUdfDir(tenantCode)).thenReturn(tenantUdfResourceDir); |
|
|
|
|
when(storageOperate.listFilesStatusRecursively(tenantUdfResourceDir, tenantUdfResourceDir, |
|
|
|
|
tenantCode, ResourceType.UDF)).thenReturn(Arrays.asList(getStorageEntityUdfResource("test.jar"))); |
|
|
|
|
loginUser.setUserType(UserType.GENERAL_USER); |
|
|
|
|
result = resourcesService.queryResourceList(loginUser, ResourceType.UDF, "/dolphinscheduler/123/udfs/"); |
|
|
|
|
result = resourcesService.queryResourceList(loginUser, ResourceType.UDF, tenantUdfResourceDir); |
|
|
|
|
assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); |
|
|
|
|
resourceList = (List<ResourceComponent>) result.get(Constants.DATA_LIST); |
|
|
|
|
Assertions.assertTrue(CollectionUtils.isNotEmpty(resourceList)); |
|
|
|
@ -360,7 +339,6 @@ public class ResourcesServiceTest {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testDelete() throws Exception { |
|
|
|
|
|
|
|
|
|
User loginUser = new User(); |
|
|
|
|
loginUser.setId(0); |
|
|
|
|
loginUser.setUserType(UserType.GENERAL_USER); |
|
|
|
@ -372,46 +350,40 @@ public class ResourcesServiceTest {
|
|
|
|
|
Assertions.assertThrows(ServiceException.class, () -> resourcesService.delete(loginUser, "", "")); |
|
|
|
|
|
|
|
|
|
// RESOURCE_NOT_EXIST
|
|
|
|
|
String fileName = "ResourcesServiceTest"; |
|
|
|
|
when(tenantMapper.queryById(Mockito.anyInt())).thenReturn(getTenant()); |
|
|
|
|
when(storageOperate.getDir(ResourceType.ALL, tenantCode)).thenReturn("/dolphinscheduler"); |
|
|
|
|
when(storageOperate.getResDir(getTenant().getTenantCode())).thenReturn("/dolphinscheduler/123/resources/"); |
|
|
|
|
when(storageOperate.getFileStatus("/dolphinscheduler/123/resources/ResourcesServiceTest", |
|
|
|
|
"/dolphinscheduler/123/resources/", tenantCode, null)) |
|
|
|
|
.thenReturn(getStorageEntityResource()); |
|
|
|
|
Result result = resourcesService.delete(loginUser, "/dolphinscheduler/123/resources/ResNotExist", tenantCode); |
|
|
|
|
when(storageOperate.getDir(ResourceType.ALL, tenantCode)).thenReturn(basePath); |
|
|
|
|
when(storageOperate.getResDir(getTenant().getTenantCode())).thenReturn(tenantFileResourceDir); |
|
|
|
|
when(storageOperate.getFileStatus(tenantFileResourceDir + fileName, tenantFileResourceDir, tenantCode, null)) |
|
|
|
|
.thenReturn(getStorageEntityResource(fileName)); |
|
|
|
|
Result result = resourcesService.delete(loginUser, tenantFileResourceDir + "ResNotExist", tenantCode); |
|
|
|
|
assertEquals(Status.RESOURCE_NOT_EXIST.getMsg(), result.getMsg()); |
|
|
|
|
|
|
|
|
|
// SUCCESS
|
|
|
|
|
loginUser.setTenantId(1); |
|
|
|
|
result = resourcesService.delete(loginUser, "/dolphinscheduler/123/resources/ResourcesServiceTest", tenantCode); |
|
|
|
|
result = resourcesService.delete(loginUser, tenantFileResourceDir + fileName, tenantCode); |
|
|
|
|
assertEquals(Status.SUCCESS.getMsg(), result.getMsg()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testVerifyResourceName() { |
|
|
|
|
|
|
|
|
|
public void testVerifyResourceName() throws IOException { |
|
|
|
|
User user = new User(); |
|
|
|
|
user.setId(1); |
|
|
|
|
user.setUserType(UserType.GENERAL_USER); |
|
|
|
|
try { |
|
|
|
|
when(storageOperate.exists("/ResourcesServiceTest.jar")).thenReturn(true); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
logger.error("error occurred when checking resource: /ResourcesServiceTest.jar\""); |
|
|
|
|
} |
|
|
|
|
Result result = resourcesService.verifyResourceName("/ResourcesServiceTest.jar", ResourceType.FILE, user); |
|
|
|
|
logger.info(result.toString()); |
|
|
|
|
|
|
|
|
|
String fileName = "ResourcesServiceTest"; |
|
|
|
|
when(storageOperate.exists(tenantFileResourceDir + fileName)).thenReturn(true); |
|
|
|
|
|
|
|
|
|
Result result = resourcesService.verifyResourceName(tenantFileResourceDir + fileName, ResourceType.FILE, user); |
|
|
|
|
assertEquals(Status.RESOURCE_EXIST.getMsg(), result.getMsg()); |
|
|
|
|
|
|
|
|
|
// RESOURCE_FILE_EXIST
|
|
|
|
|
result = resourcesService.verifyResourceName("/ResourcesServiceTest.jar", ResourceType.FILE, user); |
|
|
|
|
logger.info(result.toString()); |
|
|
|
|
result = resourcesService.verifyResourceName(tenantFileResourceDir + fileName, ResourceType.FILE, user); |
|
|
|
|
Assertions.assertTrue(Status.RESOURCE_EXIST.getCode() == result.getCode()); |
|
|
|
|
|
|
|
|
|
// SUCCESS
|
|
|
|
|
result = resourcesService.verifyResourceName("test2", ResourceType.FILE, user); |
|
|
|
|
logger.info(result.toString()); |
|
|
|
|
assertEquals(Status.SUCCESS.getMsg(), result.getMsg()); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -441,8 +413,8 @@ public class ResourcesServiceTest {
|
|
|
|
|
|
|
|
|
|
// SUCCESS
|
|
|
|
|
when(FileUtils.getResourceViewSuffixes()).thenReturn("jar,sh"); |
|
|
|
|
when(storageOperate.getDir(ResourceType.ALL, tenantCode)).thenReturn("/dolphinscheduler"); |
|
|
|
|
when(storageOperate.getResDir(getTenant().getTenantCode())).thenReturn("/dolphinscheduler/123/resources/"); |
|
|
|
|
when(storageOperate.getDir(ResourceType.ALL, tenantCode)).thenReturn(basePath); |
|
|
|
|
when(storageOperate.getResDir(getTenant().getTenantCode())).thenReturn(tenantFileResourceDir); |
|
|
|
|
when(userMapper.selectById(getUser().getId())).thenReturn(getUser()); |
|
|
|
|
when(tenantMapper.queryById(getUser().getTenantId())).thenReturn(getTenant()); |
|
|
|
|
when(storageOperate.exists(Mockito.any())).thenReturn(true); |
|
|
|
@ -465,15 +437,16 @@ public class ResourcesServiceTest {
|
|
|
|
|
exception.getMessage().contains("Not allow create or update resources without extension name")); |
|
|
|
|
|
|
|
|
|
// SUCCESS
|
|
|
|
|
when(storageOperate.getResDir(user.getTenantCode())).thenReturn("/dolphinscheduler/123/resources/"); |
|
|
|
|
String fileName = "ResourcesServiceTest"; |
|
|
|
|
when(storageOperate.getResDir(user.getTenantCode())).thenReturn(tenantFileResourceDir); |
|
|
|
|
when(FileUtils.getUploadFilename(Mockito.anyString(), Mockito.anyString())).thenReturn("test"); |
|
|
|
|
when(FileUtils.writeContent2File(Mockito.anyString(), Mockito.anyString())).thenReturn(true); |
|
|
|
|
when(storageOperate.getFileStatus(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any())) |
|
|
|
|
.thenReturn(getStorageEntityResource()); |
|
|
|
|
.thenReturn(getStorageEntityResource(fileName)); |
|
|
|
|
StorageEntity storageEntity = |
|
|
|
|
resourcesService.createOrUpdateResource(user.getUserName(), "filename.txt", "my-content"); |
|
|
|
|
Assertions.assertNotNull(storageEntity); |
|
|
|
|
assertEquals("/dolphinscheduler/123/resources/ResourcesServiceTest", storageEntity.getFullName()); |
|
|
|
|
assertEquals(tenantFileResourceDir + fileName, storageEntity.getFullName()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -482,33 +455,35 @@ public class ResourcesServiceTest {
|
|
|
|
|
when(userMapper.selectById(getUser().getId())).thenReturn(getUser()); |
|
|
|
|
when(tenantMapper.queryById(1)).thenReturn(getTenant()); |
|
|
|
|
when(storageOperate.getResDir(Mockito.anyString())).thenReturn("/tmp"); |
|
|
|
|
|
|
|
|
|
String fileName = "ResourcesServiceTest.jar"; |
|
|
|
|
ServiceException serviceException = |
|
|
|
|
Assertions.assertThrows(ServiceException.class, () -> resourcesService.updateResourceContent(getUser(), |
|
|
|
|
"/dolphinscheduler/123/resources/ResourcesServiceTest.jar", tenantCode, "content")); |
|
|
|
|
assertTrue(serviceException.getMessage() |
|
|
|
|
.contains("Resource file: /dolphinscheduler/123/resources/ResourcesServiceTest.jar is illegal")); |
|
|
|
|
tenantFileResourceDir + fileName, tenantCode, "content")); |
|
|
|
|
assertEquals(new ServiceException(Status.ILLEGAL_RESOURCE_PATH, tenantFileResourceDir + fileName), |
|
|
|
|
serviceException); |
|
|
|
|
|
|
|
|
|
// RESOURCE_NOT_EXIST
|
|
|
|
|
when(storageOperate.getDir(ResourceType.ALL, tenantCode)).thenReturn("/dolphinscheduler"); |
|
|
|
|
when(storageOperate.getResDir(Mockito.anyString())).thenReturn("/dolphinscheduler/123/resources"); |
|
|
|
|
when(storageOperate.getFileStatus("/dolphinscheduler/123/resources/ResourcesServiceTest.jar", "", tenantCode, |
|
|
|
|
ResourceType.FILE)).thenReturn(null); |
|
|
|
|
Result result = resourcesService.updateResourceContent(getUser(), |
|
|
|
|
"/dolphinscheduler/123/resources/ResourcesServiceTest.jar", tenantCode, "content"); |
|
|
|
|
when(storageOperate.getDir(ResourceType.ALL, tenantCode)).thenReturn(basePath); |
|
|
|
|
when(storageOperate.getResDir(Mockito.anyString())).thenReturn(tenantFileResourceDir); |
|
|
|
|
when(storageOperate.getFileStatus(tenantFileResourceDir + fileName, "", tenantCode, ResourceType.FILE)) |
|
|
|
|
.thenReturn(null); |
|
|
|
|
Result result = resourcesService.updateResourceContent(getUser(), tenantFileResourceDir + fileName, tenantCode, |
|
|
|
|
"content"); |
|
|
|
|
assertEquals(Status.RESOURCE_NOT_EXIST.getMsg(), result.getMsg()); |
|
|
|
|
|
|
|
|
|
// RESOURCE_SUFFIX_NOT_SUPPORT_VIEW
|
|
|
|
|
when(FileUtils.getResourceViewSuffixes()).thenReturn("class"); |
|
|
|
|
when(storageOperate.getFileStatus("/dolphinscheduler/123/resources", "", tenantCode, ResourceType.FILE)) |
|
|
|
|
.thenReturn(getStorageEntityResource()); |
|
|
|
|
when(storageOperate.getFileStatus(tenantFileResourceDir, "", tenantCode, ResourceType.FILE)) |
|
|
|
|
.thenReturn(getStorageEntityResource(fileName)); |
|
|
|
|
|
|
|
|
|
result = resourcesService.updateResourceContent(getUser(), "/dolphinscheduler/123/resources", tenantCode, |
|
|
|
|
result = resourcesService.updateResourceContent(getUser(), tenantFileResourceDir, tenantCode, |
|
|
|
|
"content"); |
|
|
|
|
assertEquals(Status.RESOURCE_SUFFIX_NOT_SUPPORT_VIEW.getMsg(), result.getMsg()); |
|
|
|
|
|
|
|
|
|
// USER_NOT_EXIST
|
|
|
|
|
when(userMapper.selectById(getUser().getId())).thenReturn(null); |
|
|
|
|
result = resourcesService.updateResourceContent(getUser(), "/dolphinscheduler/123/resources/123.class", |
|
|
|
|
result = resourcesService.updateResourceContent(getUser(), tenantFileResourceDir + "123.class", |
|
|
|
|
tenantCode, |
|
|
|
|
"content"); |
|
|
|
|
Assertions.assertTrue(Status.USER_NOT_EXIST.getCode() == result.getCode()); |
|
|
|
@ -517,11 +492,11 @@ public class ResourcesServiceTest {
|
|
|
|
|
when(userMapper.selectById(getUser().getId())).thenReturn(getUser()); |
|
|
|
|
when(tenantMapper.queryById(1)).thenReturn(null); |
|
|
|
|
Assertions.assertThrows(ServiceException.class, () -> resourcesService.updateResourceContent(getUser(), |
|
|
|
|
"/dolphinscheduler/123/resources/ResourcesServiceTest.jar", tenantCode, "content")); |
|
|
|
|
tenantFileResourceDir + fileName, tenantCode, "content")); |
|
|
|
|
|
|
|
|
|
// SUCCESS
|
|
|
|
|
when(storageOperate.getFileStatus("/dolphinscheduler/123/resources/ResourcesServiceTest.jar", "", tenantCode, |
|
|
|
|
ResourceType.FILE)).thenReturn(getStorageEntityResource()); |
|
|
|
|
when(storageOperate.getFileStatus(tenantFileResourceDir + fileName, "", tenantCode, |
|
|
|
|
ResourceType.FILE)).thenReturn(getStorageEntityResource(fileName)); |
|
|
|
|
|
|
|
|
|
when(Files.getFileExtension(Mockito.anyString())).thenReturn("jar"); |
|
|
|
|
when(FileUtils.getResourceViewSuffixes()).thenReturn("jar"); |
|
|
|
@ -530,19 +505,17 @@ public class ResourcesServiceTest {
|
|
|
|
|
when(FileUtils.getUploadFilename(Mockito.anyString(), Mockito.anyString())).thenReturn("test"); |
|
|
|
|
when(FileUtils.writeContent2File(Mockito.anyString(), Mockito.anyString())).thenReturn(true); |
|
|
|
|
result = resourcesService.updateResourceContent(getUser(), |
|
|
|
|
"/dolphinscheduler/123/resources/ResourcesServiceTest.jar", tenantCode, "content"); |
|
|
|
|
logger.info(result.toString()); |
|
|
|
|
tenantFileResourceDir + fileName, tenantCode, "content"); |
|
|
|
|
assertEquals(Status.SUCCESS.getMsg(), result.getMsg()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testDownloadResource() { |
|
|
|
|
public void testDownloadResource() throws IOException { |
|
|
|
|
when(tenantMapper.queryById(1)).thenReturn(getTenant()); |
|
|
|
|
when(userMapper.selectById(1)).thenReturn(getUser()); |
|
|
|
|
org.springframework.core.io.Resource resourceMock = Mockito.mock(org.springframework.core.io.Resource.class); |
|
|
|
|
Path path = Mockito.mock(Path.class); |
|
|
|
|
when(Paths.get(Mockito.any())).thenReturn(path); |
|
|
|
|
try { |
|
|
|
|
when(java.nio.file.Files.size(Mockito.any())).thenReturn(1L); |
|
|
|
|
// resource null
|
|
|
|
|
org.springframework.core.io.Resource resource = resourcesService.downloadResource(getUser(), ""); |
|
|
|
@ -551,11 +524,6 @@ public class ResourcesServiceTest {
|
|
|
|
|
when(org.apache.dolphinscheduler.api.utils.FileUtils.file2Resource(Mockito.any())).thenReturn(resourceMock); |
|
|
|
|
resource = resourcesService.downloadResource(getUser(), ""); |
|
|
|
|
Assertions.assertNotNull(resource); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error("DownloadResource error", e); |
|
|
|
|
Assertions.assertTrue(false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -605,30 +573,21 @@ public class ResourcesServiceTest {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testCatFile() { |
|
|
|
|
public void testCatFile() throws IOException { |
|
|
|
|
// SUCCESS
|
|
|
|
|
try { |
|
|
|
|
List<String> list = storageOperate.vimFile(Mockito.any(), Mockito.anyString(), eq(1), eq(10)); |
|
|
|
|
Assertions.assertNotNull(list); |
|
|
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
logger.error("hadoop error", e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void testQueryBaseDir() { |
|
|
|
|
void testQueryBaseDir() throws Exception { |
|
|
|
|
User user = getUser(); |
|
|
|
|
String fileName = "ResourcesServiceTest.jar"; |
|
|
|
|
when(userMapper.selectById(user.getId())).thenReturn(getUser()); |
|
|
|
|
when(tenantMapper.queryById(user.getTenantId())).thenReturn(getTenant()); |
|
|
|
|
when(storageOperate.getDir(ResourceType.FILE, tenantCode)).thenReturn("/dolphinscheduler/123/resources/"); |
|
|
|
|
try { |
|
|
|
|
when(storageOperate.getDir(ResourceType.FILE, tenantCode)).thenReturn(tenantFileResourceDir); |
|
|
|
|
when(storageOperate.getFileStatus(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), |
|
|
|
|
Mockito.any())).thenReturn(getStorageEntityResource()); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error(e.getMessage() + " Resource path: {}", "/dolphinscheduler/123/resources/ResourcesServiceTest", |
|
|
|
|
e); |
|
|
|
|
} |
|
|
|
|
Mockito.any())).thenReturn(getStorageEntityResource(fileName)); |
|
|
|
|
Result<Object> result = resourcesService.queryResourceBaseDir(user, ResourceType.FILE); |
|
|
|
|
assertEquals(Status.SUCCESS.getMsg(), result.getMsg()); |
|
|
|
|
} |
|
|
|
@ -648,25 +607,25 @@ public class ResourcesServiceTest {
|
|
|
|
|
return user; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private StorageEntity getStorageEntityResource() { |
|
|
|
|
private StorageEntity getStorageEntityResource(String fileName) { |
|
|
|
|
StorageEntity entity = new StorageEntity(); |
|
|
|
|
entity.setAlias("ResourcesServiceTest"); |
|
|
|
|
entity.setFileName("ResourcesServiceTest"); |
|
|
|
|
entity.setAlias(fileName); |
|
|
|
|
entity.setFileName(fileName); |
|
|
|
|
entity.setDirectory(false); |
|
|
|
|
entity.setUserName(tenantCode); |
|
|
|
|
entity.setType(ResourceType.FILE); |
|
|
|
|
entity.setFullName("/dolphinscheduler/123/resources/ResourcesServiceTest"); |
|
|
|
|
entity.setFullName(tenantFileResourceDir + fileName); |
|
|
|
|
return entity; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private StorageEntity getStorageEntityUdfResource() { |
|
|
|
|
private StorageEntity getStorageEntityUdfResource(String fileName) { |
|
|
|
|
StorageEntity entity = new StorageEntity(); |
|
|
|
|
entity.setAlias("ResourcesServiceTest1.jar"); |
|
|
|
|
entity.setFileName("ResourcesServiceTest1.jar"); |
|
|
|
|
entity.setAlias(fileName); |
|
|
|
|
entity.setFileName(fileName); |
|
|
|
|
entity.setDirectory(false); |
|
|
|
|
entity.setUserName(tenantCode); |
|
|
|
|
entity.setType(ResourceType.UDF); |
|
|
|
|
entity.setFullName("/dolphinscheduler/123/resources/ResourcesServiceTest1.jar"); |
|
|
|
|
entity.setFullName(tenantUdfResourceDir + fileName); |
|
|
|
|
|
|
|
|
|
return entity; |
|
|
|
|
} |
|
|
|
|