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 0909b69f..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,6 +2,7 @@ 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; @@ -74,7 +75,7 @@ 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); + File cacheFolder = FileUtils.createNewFile(location); if (cacheFolder.exists() && cacheFolder.isDirectory() && ObjectUtils.isNotEmpty( cacheFolder.listFiles()) && !isForce) { log.warn("Data is already in {}", location); 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 b6c54a08..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,6 +1,6 @@ package com.axonivy.market.service.impl; -import org.apache.commons.lang3.ObjectUtils; +import com.axonivy.market.util.FileUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -42,8 +42,16 @@ void testDownloadAndUnzipFileWithIssue() { @Test void testDownloadAndUnzipFileWithNullTempZipPath() throws IOException { try (MockedStatic mockedFiles = Mockito.mockStatic(Files.class); - MockedStatic mockedObjectUtils = Mockito.mockStatic(ObjectUtils.class)) { - mockedObjectUtils.when(() -> ObjectUtils.isNotEmpty(any())).thenReturn(true); + 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());