From 8a95ff723d0fe23fea9360166fab13647ce1caba Mon Sep 17 00:00:00 2001 From: nntthuy-axonivy Date: Tue, 24 Dec 2024 10:22:23 +0700 Subject: [PATCH 1/5] Prevent set to DB if no index file --- .../service/impl/FileDownloadServiceImpl.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) 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 78072bb28..c788cf314 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 @@ -20,15 +20,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; @@ -82,7 +74,8 @@ 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) { + if (cacheFolder.exists() && cacheFolder.isDirectory() && Objects.requireNonNull( + cacheFolder.listFiles()).length > 0 && !isForce) { log.warn("Data is already in {}", location); return null; } else { From 5684a44c92451929d7a6a69f5eac5c7a88e9a27d Mon Sep 17 00:00:00 2001 From: nntthuy-axonivy Date: Wed, 25 Dec 2024 16:13:49 +0700 Subject: [PATCH 2/5] Add unit test --- .../service/impl/FileDownloadServiceImpl.java | 2 +- .../impl/FileDownloadServiceImplTest.java | 25 ++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) 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 c788cf314..a747a97d8 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 @@ -71,7 +71,7 @@ public String downloadAndUnzipFile(String url, boolean isForce) throws IOExcepti return location; } - private Path createTempFileFromUrlAndExtractToLocation(String url, String location, + public Path createTempFileFromUrlAndExtractToLocation(String url, String location, boolean isForce) throws IOException { File cacheFolder = new File(location); if (cacheFolder.exists() && cacheFolder.isDirectory() && Objects.requireNonNull( 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 4f02e3198..2b593a348 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 @@ -3,6 +3,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; +import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; @@ -16,6 +17,7 @@ import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) class FileDownloadServiceImplTest { @@ -37,6 +39,23 @@ void testDownloadAndUnzipFileWithIssue() { assertThrows(ResourceAccessException.class, () -> fileDownloadService.downloadAndUnzipFile(DOWNLOAD_URL, true)); } + + @Test + void testCreateTempFileFromUrlAndExtractToLocation() throws IOException { + Path result = fileDownloadService.createTempFileFromUrlAndExtractToLocation(DOWNLOAD_URL, EXTRACT_DIR_LOCATION, + false); + + assertNull(result); + } + + @Test + void testCreateTempFileFromUrlAndExtractToLocation_ReturnsNull() throws IOException { + Path result = fileDownloadService.createTempFileFromUrlAndExtractToLocation(DOWNLOAD_URL, EXTRACT_DIR_LOCATION, + false); + + assertNull(result); + } + @Test void testSupportFunctions() throws IOException { var mockFile = fileDownloadService.createFolder("unzip"); @@ -82,9 +101,9 @@ void testCreateFolderShouldNotThrowExceptionWhenIOExceptionOccurs() { @Test void deleteDirectory_shouldDeleteAllFilesAndDirectories() { // Arrange - Path mockPath = Mockito.mock(Path.class); - Path file1 = Mockito.mock(Path.class); - Path file2 = Mockito.mock(Path.class); + Path mockPath = mock(Path.class); + Path file1 = mock(Path.class); + Path file2 = mock(Path.class); Stream mockStream = Stream.of(file1, file2); try (MockedStatic mockedFiles = Mockito.mockStatic(Files.class)) { From 259cc9f3f5cb1a97a4fd36587be611aa726fbe1b Mon Sep 17 00:00:00 2001 From: nntthuy-axonivy Date: Wed, 25 Dec 2024 16:18:01 +0700 Subject: [PATCH 3/5] Clear unused code --- .../service/impl/FileDownloadServiceImplTest.java | 10 ---------- 1 file changed, 10 deletions(-) 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 2b593a348..7d35b55c7 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 @@ -3,7 +3,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; -import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; @@ -39,15 +38,6 @@ void testDownloadAndUnzipFileWithIssue() { assertThrows(ResourceAccessException.class, () -> fileDownloadService.downloadAndUnzipFile(DOWNLOAD_URL, true)); } - - @Test - void testCreateTempFileFromUrlAndExtractToLocation() throws IOException { - Path result = fileDownloadService.createTempFileFromUrlAndExtractToLocation(DOWNLOAD_URL, EXTRACT_DIR_LOCATION, - false); - - assertNull(result); - } - @Test void testCreateTempFileFromUrlAndExtractToLocation_ReturnsNull() throws IOException { Path result = fileDownloadService.createTempFileFromUrlAndExtractToLocation(DOWNLOAD_URL, EXTRACT_DIR_LOCATION, From 797c55d30e4a561de5e58abea22c2850482c5c7a Mon Sep 17 00:00:00 2001 From: nntthuy-axonivy Date: Thu, 26 Dec 2024 10:59:46 +0700 Subject: [PATCH 4/5] Update test --- .../service/impl/FileDownloadServiceImpl.java | 7 +++--- .../impl/FileDownloadServiceImplTest.java | 23 +++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) 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 a747a97d8..0909b69f0 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 @@ -3,6 +3,7 @@ import com.axonivy.market.bo.Artifact; import com.axonivy.market.service.FileDownloadService; 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; @@ -71,11 +72,11 @@ public String downloadAndUnzipFile(String url, boolean isForce) throws IOExcepti return location; } - public Path createTempFileFromUrlAndExtractToLocation(String url, String location, + private Path createTempFileFromUrlAndExtractToLocation(String url, String location, boolean isForce) throws IOException { File cacheFolder = new File(location); - if (cacheFolder.exists() && cacheFolder.isDirectory() && Objects.requireNonNull( - cacheFolder.listFiles()).length > 0 && !isForce) { + 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/test/java/com/axonivy/market/service/impl/FileDownloadServiceImplTest.java b/marketplace-service/src/test/java/com/axonivy/market/service/impl/FileDownloadServiceImplTest.java index 7d35b55c7..b6c54a08c 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 org.apache.commons.lang3.ObjectUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -16,7 +17,7 @@ import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; +import static org.mockito.ArgumentMatchers.any; @ExtendWith(MockitoExtension.class) class FileDownloadServiceImplTest { @@ -39,11 +40,15 @@ void testDownloadAndUnzipFileWithIssue() { } @Test - void testCreateTempFileFromUrlAndExtractToLocation_ReturnsNull() throws IOException { - Path result = fileDownloadService.createTempFileFromUrlAndExtractToLocation(DOWNLOAD_URL, EXTRACT_DIR_LOCATION, - false); - - assertNull(result); + void testDownloadAndUnzipFileWithNullTempZipPath() throws IOException { + try (MockedStatic mockedFiles = Mockito.mockStatic(Files.class); + MockedStatic mockedObjectUtils = Mockito.mockStatic(ObjectUtils.class)) { + mockedObjectUtils.when(() -> ObjectUtils.isNotEmpty(any())).thenReturn(true); + + var result = fileDownloadService.downloadAndUnzipFile(DOWNLOAD_URL, false); + assertFalse(result.isEmpty()); + mockedFiles.verify(() -> Files.delete(any()), Mockito.times(0)); + } } @Test @@ -91,9 +96,9 @@ void testCreateFolderShouldNotThrowExceptionWhenIOExceptionOccurs() { @Test void deleteDirectory_shouldDeleteAllFilesAndDirectories() { // Arrange - Path mockPath = mock(Path.class); - Path file1 = mock(Path.class); - Path file2 = mock(Path.class); + Path mockPath = Mockito.mock(Path.class); + Path file1 = Mockito.mock(Path.class); + Path file2 = Mockito.mock(Path.class); Stream mockStream = Stream.of(file1, file2); try (MockedStatic mockedFiles = Mockito.mockStatic(Files.class)) { From b9c5fb8f97d55746402fdc9931ae877ad600d23d Mon Sep 17 00:00:00 2001 From: nntthuy-axonivy Date: Thu, 26 Dec 2024 13:37:36 +0700 Subject: [PATCH 5/5] Update unit test --- .../service/impl/FileDownloadServiceImpl.java | 3 ++- .../java/com/axonivy/market/util/FileUtils.java | 3 +++ .../service/impl/FileDownloadServiceImplTest.java | 14 +++++++++++--- 3 files changed, 16 insertions(+), 4 deletions(-) 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 0909b69f0..9da2a808c 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 6eeb35248..77b0481f3 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 b6c54a08c..219fa2269 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());