From 56ca0411825a46d718951a690bc6f69f1d32ff2a Mon Sep 17 00:00:00 2001 From: Jay Chung Date: Tue, 29 Nov 2022 20:19:12 +0800 Subject: [PATCH] [fix] python api upload resource center failed branch dev already exists in apache/dolphinscheduler#13042 --- .../dolphinscheduler/api/python/PythonGateway.java | 5 ++--- .../api/service/ResourcesService.java | 3 +-- .../api/service/impl/ResourcesServiceImpl.java | 13 ++++++------- .../api/python/PythonGatewayTest.java | 10 ++-------- 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/python/PythonGateway.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/python/PythonGateway.java index a7ffbbaec8..4ad807bb1b 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/python/PythonGateway.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/python/PythonGateway.java @@ -637,11 +637,10 @@ public class PythonGateway { * @param fullName The fullname of resource.Includes path and suffix. * @param description description of resource * @param resourceContent content of resource - * @return id of resource */ - public Integer createOrUpdateResource( + public void createOrUpdateResource( String userName, String fullName, String description, String resourceContent) { - return resourceService.createOrUpdateResource(userName, fullName, description, resourceContent); + resourceService.createOrUpdateResource(userName, fullName, description, resourceContent); } @PostConstruct diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ResourcesService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ResourcesService.java index 00c6fa151b..a396a7f3e4 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ResourcesService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ResourcesService.java @@ -189,9 +189,8 @@ public interface ResourcesService { * @param fullName The fullname of resource.Includes path and suffix. * @param description description of resource * @param resourceContent content of resource - * @return id of resource */ - Integer createOrUpdateResource(String userName, String fullName, String description, String resourceContent); + void createOrUpdateResource(String userName, String fullName, String description, String resourceContent); /** * updateProcessInstance resource diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java index 377f90b54b..5cabd39734 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java @@ -1233,7 +1233,7 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe @Override @Transactional - public Integer createOrUpdateResource(String userName, String fullName, String description, + public void createOrUpdateResource(String userName, String fullName, String description, String resourceContent) { User user = userMapper.queryByUserNameAccurately(userName); int suffixLabelIndex = fullName.indexOf(PERIOD); @@ -1247,13 +1247,9 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe } Result createResult = onlineCreateOrUpdateResourceWithDir( user, fullName, description, resourceContent); - if (createResult.getCode() == Status.SUCCESS.getCode()) { - Map resultMap = (Map) createResult.getData(); - return (int) resultMap.get("id"); + if (createResult.getCode() != Status.SUCCESS.getCode()) { + throw new IllegalArgumentException(String.format("Can not create or update resource : %s", fullName)); } - String msg = String.format("Can not create or update resource : %s", fullName); - logger.error(msg); - throw new IllegalArgumentException(msg); } private int queryOrCreateDirId(User user, int pid, String currentDir, String dirName) { @@ -1532,6 +1528,9 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe @Override public Resource queryResourcesFileInfo(String userName, String fullName) { User user = userMapper.queryByUserNameAccurately(userName); + if (!fullName.startsWith(FOLDER_SEPARATOR)) { + fullName = FOLDER_SEPARATOR + fullName; + } Result resourceResponse = this.queryResource(user, fullName, null, ResourceType.FILE); if (resourceResponse.getCode() != Status.SUCCESS.getCode()) { String msg = String.format("Can not find valid resource by name %s", fullName); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/python/PythonGatewayTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/python/PythonGatewayTest.java index 2a49ed307b..f48b53986f 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/python/PythonGatewayTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/python/PythonGatewayTest.java @@ -30,6 +30,7 @@ import org.apache.dolphinscheduler.dao.mapper.TaskDefinitionMapper; import org.apache.dolphinscheduler.spi.enums.ResourceType; import org.junit.Assert; import org.junit.Test; +import org.junit.jupiter.api.Assertions; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; @@ -100,14 +101,7 @@ public class PythonGatewayTest { String content = "content"; String resourceFullName = resourceDir + resourceName + "." + resourceSuffix; - int resourceId = 3; - - Mockito.when(resourcesService.createOrUpdateResource(user.getUserName(), resourceFullName, desc, content)) - .thenReturn(resourceId); - - int id = pythonGateway.createOrUpdateResource( - user.getUserName(), resourceFullName, desc, content); - Assert.assertEquals(id, resourceId); + Assertions.assertDoesNotThrow(() -> resourcesService.createOrUpdateResource(user.getUserName(), resourceFullName, desc, content)); }