Skip to content

Commit

Permalink
fix UT
Browse files Browse the repository at this point in the history
  • Loading branch information
caishunfeng authored and ruanwenjun committed Mar 25, 2024
1 parent 9fb080f commit b87241a
Showing 1 changed file with 32 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
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.ResourcesServiceImpl;
import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result;
Expand All @@ -36,7 +35,6 @@
import org.apache.dolphinscheduler.common.utils.FileUtils;
import org.apache.dolphinscheduler.common.utils.PropertyUtils;
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.ProcessDefinitionMapper;
import org.apache.dolphinscheduler.dao.mapper.TenantMapper;
Expand All @@ -56,11 +54,9 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -89,6 +85,8 @@ public class ResourcesServiceTest {

private static final Logger logger = LoggerFactory.getLogger(ResourcesServiceTest.class);

private static final String tenantCode = "123";

@InjectMocks
private ResourcesServiceImpl resourcesService;

Expand All @@ -110,10 +108,6 @@ public class ResourcesServiceTest {
@Mock
private ResourcePermissionCheckService resourcePermissionCheckService;

private static final Logger serviceLogger = LoggerFactory.getLogger(BaseServiceImpl.class);

private static final Logger resourceLogger = LoggerFactory.getLogger(ResourcesServiceImpl.class);

private MockedStatic<FileUtils> mockedStaticFileUtils;

private MockedStatic<Files> mockedStaticFiles;
Expand All @@ -126,7 +120,7 @@ public class ResourcesServiceTest {

private MockedStatic<java.nio.file.Files> filesMockedStatic;

private Throwable exception;
private Exception exception;

@BeforeEach
public void setUp() {
Expand Down Expand Up @@ -335,30 +329,29 @@ public void testQueryResourceListPaging() {

@Test
public void testQueryResourceList() {
User loginUser = new User();
loginUser.setId(0);
loginUser.setUserType(UserType.ADMIN_USER);
User loginUser = getUser();

when(userMapper.selectList(null)).thenReturn(Arrays.asList(loginUser));
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.FILE, tenantCode)).thenReturn("/dolphinscheduler/123/resources/");
when(storageOperate.getResDir("123")).thenReturn("/dolphinscheduler/123/resources/");
when(storageOperate.listFilesStatusRecursively("/dolphinscheduler/123/resources/",
"/dolphinscheduler/123/resources/", "123", ResourceType.FILE))
.thenReturn(Arrays.asList(getStorageEntityResource()));
Map<String, Object> result = resourcesService.queryResourceList(loginUser, ResourceType.FILE, "");
logger.info(result.toString());
.thenReturn(Collections.singletonList(getStorageEntityResource()));
Map<String, Object> result =
resourcesService.queryResourceList(loginUser, ResourceType.FILE, "/dolphinscheduler/123/resources/");
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("123")).thenReturn("/dolphinscheduler/123/udfs/");
when(storageOperate.listFilesStatusRecursively("/dolphinscheduler/123/udfs/", "/dolphinscheduler/123/udfs/",
"123", ResourceType.UDF)).thenReturn(Arrays.asList(getStorageEntityUdfResource()));
loginUser.setUserType(UserType.GENERAL_USER);
result = resourcesService.queryResourceList(loginUser, ResourceType.UDF, "");
logger.info(result.toString());
result = resourcesService.queryResourceList(loginUser, ResourceType.UDF, "/dolphinscheduler/123/udfs/");
assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
resourceList = (List<ResourceComponent>) result.get(Constants.DATA_LIST);
Assertions.assertTrue(CollectionUtils.isNotEmpty(resourceList));
Expand All @@ -379,18 +372,17 @@ public void testDelete() throws Exception {

// RESOURCE_NOT_EXIST
when(tenantMapper.queryById(Mockito.anyInt())).thenReturn(getTenant());
when(storageOperate.getFileStatus("/dolphinscheduler/123/resources/ResourcesServiceTest", null, "123", null))
.thenReturn(getStorageEntityResource());
when(storageOperate.getResDir(getTenant().getTenantCode())).thenReturn("/dolphinscheduler/123/resources/");
when(storageOperate.getFileStatus("/dolphinscheduler/123/resources/ResourcesServiceTest",
"/dolphinscheduler/123/resources/", "123", null))
.thenReturn(getStorageEntityResource());
Result result = resourcesService.delete(loginUser, "/dolphinscheduler/123/resources/ResNotExist", "123");
logger.info(result.toString());
assertEquals(Status.RESOURCE_NOT_EXIST.getMsg(), result.getMsg());

// SUCCESS
loginUser.setTenantId(1);
result = resourcesService.delete(loginUser, "/dolphinscheduler/123/resources/ResourcesServiceTest", "123");
logger.info(result.toString());
assertEquals(Status.SUCCESS.getMsg(), result.getMsg());

}

@Test
Expand Down Expand Up @@ -421,26 +413,23 @@ public void testVerifyResourceName() {
}

@Test
public void testReadResource() {
public void testReadResource() throws IOException {
// RESOURCE_NOT_EXIST
when(userMapper.selectById(getUser().getId())).thenReturn(getUser());
when(tenantMapper.queryById(getUser().getTenantId())).thenReturn(getTenant());
Result result = resourcesService.readResource(getUser(), "", "", 1, 10);
logger.info(result.toString());
assertEquals(Status.RESOURCE_FILE_NOT_EXIST.getCode(), (int) result.getCode());

// RESOURCE_SUFFIX_NOT_SUPPORT_VIEW
when(FileUtils.getResourceViewSuffixes()).thenReturn("class");
result = resourcesService.readResource(getUser(), "", "", 1, 10);
logger.info(result.toString());
assertEquals(Status.RESOURCE_SUFFIX_NOT_SUPPORT_VIEW.getMsg(), result.getMsg());

// USER_NOT_EXIST
when(userMapper.selectById(getUser().getId())).thenReturn(null);
when(FileUtils.getResourceViewSuffixes()).thenReturn("jar");
when(Files.getFileExtension("ResourcesServiceTest.jar")).thenReturn("jar");
result = resourcesService.readResource(getUser(), "", "", 1, 10);
logger.info(result.toString());
assertEquals(Status.USER_NOT_EXIST.getCode(), (int) result.getCode());

// TENANT_NOT_EXIST
Expand All @@ -449,17 +438,14 @@ public void testReadResource() {
Assertions.assertThrows(ServiceException.class, () -> resourcesService.readResource(getUser(), "", "", 1, 10));

// SUCCESS
when(FileUtils.getResourceViewSuffixes()).thenReturn("jar,sh");
when(storageOperate.getResDir(getTenant().getTenantCode())).thenReturn("/dolphinscheduler/123/resources/");
when(userMapper.selectById(getUser().getId())).thenReturn(getUser());
when(tenantMapper.queryById(getUser().getTenantId())).thenReturn(getTenant());
try {
when(storageOperate.exists(Mockito.any())).thenReturn(true);
when(storageOperate.vimFile(Mockito.any(), Mockito.any(), eq(1), eq(10))).thenReturn(getContent());
} catch (IOException e) {
logger.error("storage error", e);
}
when(Files.getFileExtension("test.jar")).thenReturn("jar");
result = resourcesService.readResource(getUser(), "test.jar", "", 1, 10);
logger.info(result.toString());
when(storageOperate.exists(Mockito.any())).thenReturn(true);
when(storageOperate.vimFile(Mockito.any(), Mockito.any(), eq(1), eq(10))).thenReturn(getContent());
when(Files.getFileExtension("/dolphinscheduler/123/resources/test.jar")).thenReturn("jar");
result = resourcesService.readResource(getUser(), "/dolphinscheduler/123/resources/test.jar", "123", 1, 10);
assertEquals(Status.SUCCESS.getMsg(), result.getMsg());
}

Expand Down Expand Up @@ -640,19 +626,21 @@ void testQueryBaseDir() {
assertEquals(Status.SUCCESS.getMsg(), result.getMsg());
}

private Set<Integer> getSetIds() {

Set<Integer> resources = new HashSet<>();
resources.add(1);
return resources;
}

private Tenant getTenant() {
Tenant tenant = new Tenant();
tenant.setTenantCode("123");
tenant.setTenantCode(tenantCode);
return tenant;
}

private User getUser() {
User user = new User();
user.setId(1);
user.setUserType(UserType.GENERAL_USER);
user.setTenantId(1);
user.setTenantCode(tenantCode);
return user;
}

private StorageEntity getStorageEntityResource() {
StorageEntity entity = new StorageEntity();
entity.setAlias("ResourcesServiceTest");
Expand All @@ -661,7 +649,6 @@ private StorageEntity getStorageEntityResource() {
entity.setUserName("123");
entity.setType(ResourceType.FILE);
entity.setFullName("/dolphinscheduler/123/resources/ResourcesServiceTest");

return entity;
}

Expand All @@ -677,42 +664,6 @@ private StorageEntity getStorageEntityUdfResource() {
return entity;
}

private UdfFunc getUdfFunc() {

UdfFunc udfFunc = new UdfFunc();
udfFunc.setId(1);
return udfFunc;
}

private UdfFunc getUdfFunc(int udfId) {

UdfFunc udfFunc = new UdfFunc();
udfFunc.setId(udfId);
return udfFunc;
}

private List<UdfFunc> getUdfFuncList() {

List<UdfFunc> udfFuncs = new ArrayList<>();
udfFuncs.add(getUdfFunc(1));
udfFuncs.add(getUdfFunc(2));
udfFuncs.add(getUdfFunc(3));
return udfFuncs;
}

private List<UdfFunc> getSingleUdfFuncList() {
return Collections.singletonList(getUdfFunc(3));
}

private User getUser() {
User user = new User();
user.setId(1);
user.setUserType(UserType.GENERAL_USER);
user.setTenantId(1);
user.setTenantCode("tenantCode");
return user;
}

private List<String> getContent() {
List<String> contentList = new ArrayList<>();
contentList.add("test");
Expand Down

0 comments on commit b87241a

Please sign in to comment.