Skip to content

Commit

Permalink
Update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
nntthuy-axonivy committed Dec 26, 2024
1 parent 797c55d commit b9c5fb8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ public static void writeToFile(File file, String content) throws IOException {
}
}

public static File createNewFile(String location) {
return new File(location);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -42,8 +42,16 @@ void testDownloadAndUnzipFileWithIssue() {
@Test
void testDownloadAndUnzipFileWithNullTempZipPath() throws IOException {
try (MockedStatic<Files> mockedFiles = Mockito.mockStatic(Files.class);
MockedStatic<ObjectUtils> mockedObjectUtils = Mockito.mockStatic(ObjectUtils.class)) {
mockedObjectUtils.when(() -> ObjectUtils.isNotEmpty(any())).thenReturn(true);
MockedStatic<FileUtils> 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());
Expand Down

0 comments on commit b9c5fb8

Please sign in to comment.