diff --git a/src/main/java/com/lpvs/service/LPVSQueueService.java b/src/main/java/com/lpvs/service/LPVSQueueService.java index 1a834b54..f76780bd 100644 --- a/src/main/java/com/lpvs/service/LPVSQueueService.java +++ b/src/main/java/com/lpvs/service/LPVSQueueService.java @@ -19,6 +19,8 @@ import org.springframework.stereotype.Service; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.*; import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; @@ -203,7 +205,7 @@ public void processWebHook(LPVSQueue webhookConfig) { pullRequest = lpvsPullRequestRepository.saveAndFlush(pullRequest); log.debug("ID: " + pullRequest.getId() + " " + pullRequest.toString()); - if (filePath != null) { + if (filePath != null && Files.list(Paths.get(filePath)).count() != 0) { log.debug("Successfully downloaded files"); if (filePath.contains(":::::")) { diff --git a/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java b/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java index f2b49396..942104ea 100644 --- a/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java +++ b/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java @@ -47,8 +47,6 @@ import java.util.Set; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; @Slf4j diff --git a/src/test/java/com/lpvs/service/LPVSQueueServiceTest.java b/src/test/java/com/lpvs/service/LPVSQueueServiceTest.java index a895cbbe..a2170deb 100644 --- a/src/test/java/com/lpvs/service/LPVSQueueServiceTest.java +++ b/src/test/java/com/lpvs/service/LPVSQueueServiceTest.java @@ -15,13 +15,19 @@ import com.lpvs.repository.LPVSPullRequestRepository; import com.lpvs.repository.LPVSQueueRepository; import lombok.extern.slf4j.Slf4j; + +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.*; import static org.junit.jupiter.api.Assertions.*; @@ -168,14 +174,19 @@ public void testProcessWebHook__NoPRDownloaded() throws IOException { // ==== constants common for next 6 tests ==== // case DeletionAbsent - static final String filePathTestNoDeletion = - "Projects/Samsung/LPVS/3c688f5caa42b936cd6bea04c1dc3f732364250b"; + static String filePathTestNoDeletion = + System.getProperty("user.dir") + + "/Projects/Samsung/LPVS/3c688f5caa42b936cd6bea04c1dc3f732364250b"; // case DeletionPresent - static final String filePathTestWithDeletion = - "Projects/Samsung/LPVS/3c688f5caa42b936cd6bea04c1dc3f732364250b"; - static final String filePathTestWithDeletionTruncated = - "Projects/Samsung/LPVS/3c688f5caa42b936cd6bea04c1dc3f732364250b"; + static String filePathTestWithDeletion = + System.getProperty("user.dir") + + "/Projects/Samsung/LPVS/3c688f5caa42b936cd6bea04c1dc3f732364250b"; + static String filePathTestWithDeletionTruncated = + System.getProperty("user.dir") + + "/Projects/Samsung/LPVS/3c688f5caa42b936cd6bea04c1dc3f732364250b"; + + private Path tempFolderPath; // LPVSLicense static final String licenseNameTest = "test_license_name"; @@ -246,6 +257,32 @@ public void testProcessWebHook__NoPRDownloaded() throws IOException { static final List LPVSFilesTest = Arrays.asList(lpvsFileTest_1, lpvsFileTest_2); + @BeforeEach + void setUp() { + // for next tests, create temp dir with temp file + tempFolderPath = Paths.get(filePathTestNoDeletion); + try { + if (!Files.exists(tempFolderPath)) { + Files.createDirectories(tempFolderPath); + } + Path emptyFilePath = tempFolderPath.resolve("dummyFile"); + if (!Files.exists(emptyFilePath)) { + Files.createFile(emptyFilePath); + } + } catch (IOException e) { + log.warn("error creating temp folder/file " + e.getMessage()); + } + } + + @AfterEach + public void tearDown() throws IOException { + // Clean up the temporary folder after each test + Files.walk(tempFolderPath) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + } + @Nested class TestProcessWebHook__DeletionAbsentLicensePresent { LPVSQueueService queueService;