Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dinky-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-core</artifactId>
<version>${flink.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ public TreeNodeDTO createFolderOrGet(Integer pid, String fileName, String desc)
@Transactional(rollbackFor = Exception.class)
public void rename(Integer id, String fileName, String desc) {
Resources byId = getById(id);
String sourceFullName = byId.getFullName();
DinkyAssert.checkNull(byId, Status.RESOURCE_DIR_OR_FILE_NOT_EXIST);
String sourceFullName = byId.getFullName();
long count = count(new LambdaQueryWrapper<Resources>()
.eq(Resources::getPid, byId.getPid())
.eq(Resources::getFileName, fileName)
Expand All @@ -160,25 +160,10 @@ public void rename(Integer id, String fileName, String desc) {
byId.setFileName(fileName);
byId.setFullName(fullName);
updateById(byId);
boolean isRunStorageMove = false;
if (!byId.getIsDirectory()) {
List<Resources> list = list(new LambdaQueryWrapper<Resources>().eq(Resources::getPid, byId.getId()));
if (CollUtil.isNotEmpty(list)) {
for (Resources resources : list) {
resources.setFullName(fullName + "/" + resources.getFileName());
isRunStorageMove = !resources.getIsDirectory() && !isRunStorageMove;
}
updateBatchById(list);
}
} else {
isRunStorageMove = true;
if (!isExistsChildren(id)) {
return;
}
}
if (isRunStorageMove) {
getBaseResourceManager().rename(sourceFullName, fullName);
if (sourceFullName.equals(fullName) || (byId.getIsDirectory() && !isExistsChildren(id))) {
return;
}
getBaseResourceManager().rename(sourceFullName, fullName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.service.resource.impl;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

import org.dinky.data.model.Resources;
import org.dinky.data.model.ResourcesModelEnum;
import org.dinky.data.model.SystemConfiguration;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import com.baomidou.mybatisplus.core.conditions.Wrapper;

class ResourceServiceImplTest {

private final SystemConfiguration configuration = SystemConfiguration.getInstances();
private final String originalBasePath =
configuration.getResourcesUploadBasePath().getValue();
private final ResourcesModelEnum originalModel =
configuration.getResourcesModel().getValue();

@TempDir
Path tempDir;

@AfterEach
void restoreConfiguration() {
configuration.getResourcesUploadBasePath().setValue(originalBasePath);
configuration.getResourcesModel().setValue(originalModel);
}

@Test
void renameFileMovesPhysicalResource() throws Exception {
configuration.getResourcesUploadBasePath().setValue(tempDir.toString());
configuration.getResourcesModel().setValue(ResourcesModelEnum.LOCAL);

Path oldFile = tempDir.resolve("jars/old.jar");
Files.createDirectories(oldFile.getParent());
Files.write(oldFile, new byte[] {1});

Resources resource = Resources.builder()
.id(7)
.pid(1)
.fileName("old.jar")
.fullName("jars/old.jar")
.description("old")
.isDirectory(false)
.build();
ResourceServiceImpl service = spy(new ResourceServiceImpl());
doReturn(resource).when(service).getById(7);
doReturn(0L).when(service).count(any(Wrapper.class));
doReturn(Collections.emptyList()).when(service).list(any(Wrapper.class));
doReturn(true).when(service).updateById(any(Resources.class));

service.rename(7, "new.jar", "updated");

assertThat(oldFile).doesNotExist();
assertThat(tempDir.resolve("jars/new.jar")).exists();
assertThat(resource.getFileName()).isEqualTo("new.jar");
assertThat(resource.getFullName()).isEqualTo("jars/new.jar");
assertThat(resource.getDescription()).isEqualTo("updated");
}

@Test
void updateDescriptionKeepsPhysicalResourceInPlace() throws Exception {
configuration.getResourcesUploadBasePath().setValue(tempDir.toString());
configuration.getResourcesModel().setValue(ResourcesModelEnum.LOCAL);

Path file = tempDir.resolve("jars/current.jar");
Files.createDirectories(file.getParent());
Files.write(file, new byte[] {1});

Resources resource = Resources.builder()
.id(8)
.pid(1)
.fileName("current.jar")
.fullName("jars/current.jar")
.description("old")
.isDirectory(false)
.build();
ResourceServiceImpl service = spy(new ResourceServiceImpl());
doReturn(resource).when(service).getById(8);
doReturn(0L).when(service).count(any(Wrapper.class));
doReturn(true).when(service).updateById(any(Resources.class));

service.rename(8, "current.jar", "updated");

assertThat(file).exists();
assertThat(resource.getFullName()).isEqualTo("jars/current.jar");
assertThat(resource.getDescription()).isEqualTo("updated");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ public void remove(String path) {
@Override
public void rename(String path, String newPath) {
try {
getHdfs().rename(new Path(getFilePath(path)), new Path(getFilePath(newPath)));
boolean renamed = getHdfs().rename(new Path(getFilePath(path)), new Path(getFilePath(newPath)));
if (!renamed) {
throw new BusException(Status.RESOURCE_FILE_RENAME_FAILED);
}
} catch (IOException e) {
throw new BusException(Status.RESOURCE_FILE_RENAME_FAILED, e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.resource.impl;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.dinky.data.enums.Status;
import org.dinky.data.exception.BusException;

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import org.junit.jupiter.api.Test;

class HdfsResourceManagerTest {

@Test
void renameThrowsWhenHdfsRejectsMove() throws Exception {
FileSystem fileSystem = mock(FileSystem.class);
when(fileSystem.rename(any(Path.class), any(Path.class))).thenReturn(false);
HdfsResourceManager resourceManager = new HdfsResourceManager() {
@Override
public String getFilePath(String path) {
return path;
}
};
resourceManager.setHdfs(fileSystem);

assertThatThrownBy(() -> resourceManager.rename("jars/old.jar", "jars/new.jar"))
.isInstanceOf(BusException.class)
.extracting("code")
.isEqualTo(Status.RESOURCE_FILE_RENAME_FAILED);
}
}
Loading