From 3c26d44cd591c57f0052b141be2cc8c6ed154062 Mon Sep 17 00:00:00 2001 From: Oleg Kopysov Date: Wed, 30 Oct 2024 18:36:15 +0200 Subject: [PATCH] test: Add unit tests for uncovered function (#645) Signed-off-by: Oleg Kopysov --- .../webhook/LPVSWebhookServiceImpl.java | 3 - .../webhook/LPVSWebhookServiceImplTest.java | 2 + .../com/lpvs/util/LPVSPayloadUtilTest.java | 73 +++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/lpvs/service/webhook/LPVSWebhookServiceImpl.java b/src/main/java/com/lpvs/service/webhook/LPVSWebhookServiceImpl.java index 0aca24e0..f9fb061b 100644 --- a/src/main/java/com/lpvs/service/webhook/LPVSWebhookServiceImpl.java +++ b/src/main/java/com/lpvs/service/webhook/LPVSWebhookServiceImpl.java @@ -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); diff --git a/src/test/java/com/lpvs/service/webhook/LPVSWebhookServiceImplTest.java b/src/test/java/com/lpvs/service/webhook/LPVSWebhookServiceImplTest.java index 45038835..3aedf90a 100644 --- a/src/test/java/com/lpvs/service/webhook/LPVSWebhookServiceImplTest.java +++ b/src/test/java/com/lpvs/service/webhook/LPVSWebhookServiceImplTest.java @@ -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"); @@ -811,6 +812,7 @@ void setUp() { 4); when(mocked_webhookServiceFactory.createWebhookService(false)) .thenReturn(webhookService); + when(mocked_lpvsPullRequestRepository.findByQueueId(1L)).thenReturn(lpvsPullRequest); } @Test diff --git a/src/test/java/com/lpvs/util/LPVSPayloadUtilTest.java b/src/test/java/com/lpvs/util/LPVSPayloadUtilTest.java index e2c1b836..d3005ac3 100644 --- a/src/test/java/com/lpvs/util/LPVSPayloadUtilTest.java +++ b/src/test/java/com/lpvs/util/LPVSPayloadUtilTest.java @@ -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 { @@ -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;