diff --git a/marketplace-service/src/main/java/com/axonivy/market/service/impl/FileDownloadServiceImpl.java b/marketplace-service/src/main/java/com/axonivy/market/service/impl/FileDownloadServiceImpl.java index 78072bb2..9da2a808 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/service/impl/FileDownloadServiceImpl.java +++ b/marketplace-service/src/main/java/com/axonivy/market/service/impl/FileDownloadServiceImpl.java @@ -2,7 +2,9 @@ import com.axonivy.market.bo.Artifact; import com.axonivy.market.service.FileDownloadService; +import com.axonivy.market.util.FileUtils; import lombok.extern.log4j.Log4j2; +import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.SystemUtils; import org.springframework.stereotype.Service; @@ -20,15 +22,7 @@ import java.nio.file.attribute.FileAttribute; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.EnumSet; -import java.util.Enumeration; -import java.util.List; -import java.util.Set; -import java.util.UUID; +import java.util.*; import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -81,8 +75,9 @@ public String downloadAndUnzipFile(String url, boolean isForce) throws IOExcepti private Path createTempFileFromUrlAndExtractToLocation(String url, String location, boolean isForce) throws IOException { - File cacheFolder = new File(location); - if (cacheFolder.exists() && cacheFolder.isDirectory() && !isForce) { + File cacheFolder = FileUtils.createNewFile(location); + if (cacheFolder.exists() && cacheFolder.isDirectory() && ObjectUtils.isNotEmpty( + cacheFolder.listFiles()) && !isForce) { log.warn("Data is already in {}", location); return null; } else { diff --git a/marketplace-service/src/main/java/com/axonivy/market/util/FileUtils.java b/marketplace-service/src/main/java/com/axonivy/market/util/FileUtils.java index 6eeb3524..77b0481f 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/util/FileUtils.java +++ b/marketplace-service/src/main/java/com/axonivy/market/util/FileUtils.java @@ -28,4 +28,7 @@ public static void writeToFile(File file, String content) throws IOException { } } + public static File createNewFile(String location) { + return new File(location); + } } diff --git a/marketplace-service/src/test/java/com/axonivy/market/service/impl/FileDownloadServiceImplTest.java b/marketplace-service/src/test/java/com/axonivy/market/service/impl/FileDownloadServiceImplTest.java index 4f02e319..219fa226 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/service/impl/FileDownloadServiceImplTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/service/impl/FileDownloadServiceImplTest.java @@ -1,5 +1,6 @@ package com.axonivy.market.service.impl; +import com.axonivy.market.util.FileUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -16,6 +17,7 @@ import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; @ExtendWith(MockitoExtension.class) class FileDownloadServiceImplTest { @@ -37,6 +39,26 @@ void testDownloadAndUnzipFileWithIssue() { assertThrows(ResourceAccessException.class, () -> fileDownloadService.downloadAndUnzipFile(DOWNLOAD_URL, true)); } + @Test + void testDownloadAndUnzipFileWithNullTempZipPath() throws IOException { + try (MockedStatic mockedFiles = Mockito.mockStatic(Files.class); + MockedStatic mockFileUtils = Mockito.mockStatic(FileUtils.class)) { + + File mockChildFile = Mockito.mock(File.class); + + File mockFile = Mockito.mock(File.class); + Mockito.when(mockFile.exists()).thenReturn(true); + Mockito.when(mockFile.isDirectory()).thenReturn(true); + Mockito.when(mockFile.listFiles()).thenReturn(new File[]{mockChildFile}); + + mockFileUtils.when(() -> FileUtils.createNewFile(Mockito.anyString())).thenReturn(mockFile); + + var result = fileDownloadService.downloadAndUnzipFile(DOWNLOAD_URL, false); + assertFalse(result.isEmpty()); + mockedFiles.verify(() -> Files.delete(any()), Mockito.times(0)); + } + } + @Test void testSupportFunctions() throws IOException { var mockFile = fileDownloadService.createFolder("unzip");