Skip to content

Commit

Permalink
fix: add check for empty number of files in dir before scan (#431)
Browse files Browse the repository at this point in the history
Signed-off-by: Mykola Rudyk <m.rudyk@samsung.com>
  • Loading branch information
m-rudyk committed Feb 16, 2024
1 parent c44618e commit f6fc79e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/lpvs/service/LPVSQueueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(":::::")) {
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 43 additions & 6 deletions src/test/java/com/lpvs/service/LPVSQueueServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -246,6 +257,32 @@ public void testProcessWebHook__NoPRDownloaded() throws IOException {

static final List<LPVSFile> 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;
Expand Down

0 comments on commit f6fc79e

Please sign in to comment.