Skip to content

Commit

Permalink
test: Add unit tests for uncovered function
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov committed Oct 30, 2024
1 parent 492db95 commit 2184d5c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,6 @@ public void processWebHook(LPVSQueue webhookConfig) {
if (filePath != null && Files.list(Paths.get(filePath)).count() != 0) {
log.debug("Successfully downloaded files");

if (filePath.contains(":::::")) {
filePath = filePath.split(":::::")[0];
}
// check repository license
String[] repositoryLicense = gitHubService.getRepositoryLicense(webhookConfig);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,7 @@ void setUp() {
lpvsPullRequest.setUser("user");

webhookConfigMain = new LPVSQueue();
webhookConfigMain.setId(1L);
webhookConfigMain.setPullRequestUrl("http://test_url/url/pull/1");
webhookConfigMain.setPullRequestFilesUrl("http://test_url/url");
webhookConfigMain.setRepositoryUrl("http://test_url/url");
Expand Down Expand Up @@ -811,6 +812,7 @@ void setUp() {
4);
when(mocked_webhookServiceFactory.createWebhookService(false))
.thenReturn(webhookService);
when(mocked_lpvsPullRequestRepository.findByQueueId(1L)).thenReturn(lpvsPullRequest);
}

@Test
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/com/lpvs/util/LPVSPayloadUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.kohsuke.github.GHCommitPointer;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHRepository;
import org.mockito.Mock;
import org.springframework.http.HttpHeaders;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

public class LPVSPayloadUtilTest {

Expand Down Expand Up @@ -87,6 +93,73 @@ public void testConvertOsoriDbResponseToLicense_emptyPayload_N() {
}
}

@Nested
class TestGetGitHubWebhookConfig {
GHRepository repo;
GHPullRequest pR;

@BeforeEach
void setUp() {
repo = mock(GHRepository.class);
pR = mock(GHPullRequest.class);
}

@Test
void testGetGitHubWebhookConfig_WithValidPRAndRepo() throws MalformedURLException {
when(repo.getHtmlUrl()).thenReturn(new URL("https://github.com/repo"));
when(pR.getHtmlUrl()).thenReturn(new URL("https://github.com/repo/pull/1"));
when(pR.getHead()).thenReturn(mock(GHCommitPointer.class));
when(pR.getHead().getRepository()).thenReturn(repo);
when(pR.getHead().getRepository().getHtmlUrl())
.thenReturn(new URL("https://github.com/repo"));
when(pR.getHead().getSha()).thenReturn("1234567890");

LPVSQueue webhookConfig = LPVSPayloadUtil.getGitHubWebhookConfig(repo, pR);

assertEquals("https://github.com/repo/pull/1", webhookConfig.getPullRequestUrl());
assertEquals("https://github.com/repo", webhookConfig.getPullRequestFilesUrl());
assertNull(webhookConfig.getPullRequestAPIUrl());
assertEquals("https://github.com/repo", webhookConfig.getRepositoryUrl());
assertEquals("Single scan of pull request run", webhookConfig.getUserId());
assertEquals("1234567890", webhookConfig.getHeadCommitSHA());
}

@Test
void testGetGitHubWebhookConfig_WithNullPRHtmlUrl() {
when(pR.getHtmlUrl()).thenReturn(null);
when(pR.getHead()).thenReturn(mock(GHCommitPointer.class));
when(pR.getHead().getRepository()).thenReturn(repo);
when(pR.getHead().getRepository().getHtmlUrl()).thenReturn(null);
when(pR.getHead().getSha()).thenReturn("1234567890");

LPVSQueue webhookConfig = LPVSPayloadUtil.getGitHubWebhookConfig(repo, pR);

assertNull(webhookConfig.getPullRequestUrl());
assertNull(webhookConfig.getPullRequestFilesUrl());
assertNull(webhookConfig.getPullRequestAPIUrl());
assertNull(webhookConfig.getRepositoryUrl());
assertEquals("Single scan of pull request run", webhookConfig.getUserId());
assertEquals("1234567890", webhookConfig.getHeadCommitSHA());
}

@Test
void testGetGitHubWebhookConfig_WithNullPRHead() {
when(pR.getHtmlUrl()).thenReturn(null);
when(pR.getHead()).thenReturn(mock(GHCommitPointer.class));
when(pR.getHead().getRepository()).thenReturn(null);
when(pR.getHead().getSha()).thenReturn("1234567890");

LPVSQueue webhookConfig = LPVSPayloadUtil.getGitHubWebhookConfig(repo, pR);

assertNull(webhookConfig.getPullRequestUrl());
assertNull(webhookConfig.getPullRequestFilesUrl());
assertNull(webhookConfig.getPullRequestAPIUrl());
assertNull(webhookConfig.getRepositoryUrl());
assertEquals("Single scan of pull request run", webhookConfig.getUserId());
assertEquals("1234567890", webhookConfig.getHeadCommitSHA());
}
}

@Nested
class TestGetGitHubWebhookConfig__ForkTrue {
String json_to_test;
Expand Down

0 comments on commit 2184d5c

Please sign in to comment.