diff --git a/src/main/java/com/lpvs/repository/LPVSLicenseRepository.java b/src/main/java/com/lpvs/repository/LPVSLicenseRepository.java index 623adc5a..e15a1286 100644 --- a/src/main/java/com/lpvs/repository/LPVSLicenseRepository.java +++ b/src/main/java/com/lpvs/repository/LPVSLicenseRepository.java @@ -58,6 +58,6 @@ public interface LPVSLicenseRepository extends JpaRepository * * @return List of SPDX identifiers as Strings. */ - @Query(value = "select license_list.spdxId from LPVSLicense license_list") + @Query(value = "select licenses.spdxId from LPVSLicense licenses") List takeAllSpdxId(); } diff --git a/src/main/java/com/lpvs/service/LPVSGitHubService.java b/src/main/java/com/lpvs/service/LPVSGitHubService.java index bb7b929f..fffe114a 100644 --- a/src/main/java/com/lpvs/service/LPVSGitHubService.java +++ b/src/main/java/com/lpvs/service/LPVSGitHubService.java @@ -213,178 +213,152 @@ public void setErrorCheck(LPVSQueue webhookConfig) { * @param scanResults List of detected files and licenses. * @param conflicts List of license conflicts. * @param lpvsPullRequest LPVS entity representing the pull request. - * @throws IOException if an error occurs during GitHub interaction. */ public void commentResults( LPVSQueue webhookConfig, List scanResults, List> conflicts, LPVSPullRequest lpvsPullRequest) - throws IOException { + throws Exception { - try { - GHRepository repository = - gitHub.getRepository( - LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) - + "/" - + LPVSWebhookUtil.getRepositoryName(webhookConfig)); - GHPullRequest pullRequest = getPullRequest(webhookConfig, repository); + GHRepository repository = + gitHub.getRepository( + LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) + + "/" + + LPVSWebhookUtil.getRepositoryName(webhookConfig)); + GHPullRequest pullRequest = getPullRequest(webhookConfig, repository); - if (pullRequest == null) { - log.error("Can't find pull request " + webhookConfig.getPullRequestUrl()); - lpvsPullRequest.setStatus(LPVSPullRequestStatus.NO_ACCESS.toString()); - pullRequestRepository.saveAndFlush(lpvsPullRequest); - return; - } - if (scanResults == null && conflicts == null) { - log.error( - "Files are not found in pull request " + webhookConfig.getPullRequestUrl()); - lpvsPullRequest.setStatus(LPVSPullRequestStatus.COMPLETED.toString()); - pullRequestRepository.saveAndFlush(lpvsPullRequest); - repository.createCommitStatus( - webhookConfig.getHeadCommitSHA(), - GHCommitState.SUCCESS, - null, - "Files are not found", - "[License Pre-Validation Service]"); - return; - } + if (pullRequest == null) { + log.error("Can't find pull request " + webhookConfig.getPullRequestUrl()); + lpvsPullRequest.setStatus(LPVSPullRequestStatus.NO_ACCESS.toString()); + pullRequestRepository.saveAndFlush(lpvsPullRequest); + return; + } + if (scanResults == null && conflicts == null) { + log.error("Files are not found in pull request " + webhookConfig.getPullRequestUrl()); + lpvsPullRequest.setStatus(LPVSPullRequestStatus.COMPLETED.toString()); + pullRequestRepository.saveAndFlush(lpvsPullRequest); + repository.createCommitStatus( + webhookConfig.getHeadCommitSHA(), + GHCommitState.SUCCESS, + null, + "Files are not found", + "[License Pre-Validation Service]"); + return; + } - boolean hasProhibitedOrRestricted = false; - boolean hasConflicts = false; - String commitComment = ""; + boolean hasProhibitedOrRestricted = false; + boolean hasConflicts = false; + String commitComment = ""; - if (scanResults != null && scanResults.size() != 0) { - commitComment = "**Detected licenses:**\n\n\n"; - for (LPVSFile file : scanResults) { - commitComment += "**File:** " + file.getFilePath() + "\n"; - commitComment += - "**License(s):** " - + file.convertLicensesToString(LPVSVcs.GITHUB) - + "\n"; - commitComment += - "**Component:** " - + file.getComponentName() - + " (" - + file.getComponentFilePath() - + ")\n"; - commitComment += - "**Matched Lines:** " - + LPVSCommentUtil.getMatchedLinesAsLink( - webhookConfig, file, LPVSVcs.GITHUB) - + "\n"; - commitComment += "**Snippet Match:** " + file.getSnippetMatch() + "\n\n\n\n"; - for (LPVSLicense license : file.getLicenses()) { - LPVSDetectedLicense detectedIssue = new LPVSDetectedLicense(); - detectedIssue.setPullRequest(lpvsPullRequest); - detectedIssue.setLicense(license); - detectedIssue.setFilePath(file.getFilePath()); - detectedIssue.setType(file.getSnippetType()); - detectedIssue.setMatch(file.getSnippetMatch()); - detectedIssue.setLines(file.getMatchedLines()); - detectedIssue.setComponentFilePath(file.getComponentFilePath()); - detectedIssue.setComponentFileUrl(file.getComponentFileUrl()); - detectedIssue.setComponentName(file.getComponentName()); - detectedIssue.setComponentLines(file.getComponentLines()); - detectedIssue.setComponentUrl(file.getComponentUrl()); - detectedIssue.setComponentVersion(file.getComponentVersion()); - detectedIssue.setComponentVendor(file.getComponentVendor()); - if (license.getAccess().isEmpty() - || license.getAccess().equalsIgnoreCase("prohibited") - || license.getAccess().equalsIgnoreCase("restricted") - || license.getAccess().equalsIgnoreCase("unreviewed")) { - hasProhibitedOrRestricted = true; - detectedIssue.setIssue(true); - } else { - detectedIssue.setIssue(false); - } - lpvsDetectedLicenseRepository.saveAndFlush(detectedIssue); - } - } - } - - if (conflicts != null && conflicts.size() > 0) { - hasConflicts = true; - StringBuilder commitCommentBuilder = new StringBuilder(); - commitCommentBuilder.append("**Detected license conflicts:**\n\n\n"); - commitCommentBuilder.append("
    "); - for (LPVSLicenseService.Conflict conflict : conflicts) { - commitCommentBuilder.append( - "
  • " + conflict.l1 + " and " + conflict.l2 + "
  • "); + if (scanResults != null && scanResults.size() != 0) { + commitComment = "**Detected licenses:**\n\n\n"; + for (LPVSFile file : scanResults) { + commitComment += "**File:** " + file.getFilePath() + "\n"; + commitComment += + "**License(s):** " + file.convertLicensesToString(LPVSVcs.GITHUB) + "\n"; + commitComment += + "**Component:** " + + file.getComponentName() + + " (" + + file.getComponentFilePath() + + ")\n"; + commitComment += + "**Matched Lines:** " + + LPVSCommentUtil.getMatchedLinesAsLink( + webhookConfig, file, LPVSVcs.GITHUB) + + "\n"; + commitComment += "**Snippet Match:** " + file.getSnippetMatch() + "\n\n\n\n"; + for (LPVSLicense license : file.getLicenses()) { LPVSDetectedLicense detectedIssue = new LPVSDetectedLicense(); detectedIssue.setPullRequest(lpvsPullRequest); - Long l1 = lpvsLicenseRepository.searchBySpdxId(conflict.l1).getLicenseId(); - Long l2 = lpvsLicenseRepository.searchBySpdxId(conflict.l2).getLicenseId(); - detectedIssue.setLicenseConflict( - lpvsLicenseConflictRepository.findLicenseConflict(l1, l2)); - if (webhookConfig.getRepositoryLicense() != null) { - LPVSLicense repoLicense = - lpvsLicenseRepository.searchBySpdxId( - webhookConfig.getRepositoryLicense()); - if (repoLicense == null) { - repoLicense = - lpvsLicenseRepository.searchByAlternativeLicenseNames( - webhookConfig.getRepositoryLicense()); - } - detectedIssue.setRepositoryLicense(repoLicense); + detectedIssue.setLicense(license); + detectedIssue.setFilePath(file.getFilePath()); + detectedIssue.setType(file.getSnippetType()); + detectedIssue.setMatch(file.getSnippetMatch()); + detectedIssue.setLines(file.getMatchedLines()); + detectedIssue.setComponentFilePath(file.getComponentFilePath()); + detectedIssue.setComponentFileUrl(file.getComponentFileUrl()); + detectedIssue.setComponentName(file.getComponentName()); + detectedIssue.setComponentLines(file.getComponentLines()); + detectedIssue.setComponentUrl(file.getComponentUrl()); + detectedIssue.setComponentVersion(file.getComponentVersion()); + detectedIssue.setComponentVendor(file.getComponentVendor()); + if (license.getAccess().isEmpty() + || license.getAccess().equalsIgnoreCase("prohibited") + || license.getAccess().equalsIgnoreCase("restricted") + || license.getAccess().equalsIgnoreCase("unreviewed")) { + hasProhibitedOrRestricted = true; + detectedIssue.setIssue(true); + } else { + detectedIssue.setIssue(false); } - detectedIssue.setIssue(true); lpvsDetectedLicenseRepository.saveAndFlush(detectedIssue); } - commitCommentBuilder.append("
"); - if (null != webhookConfig.getHubLink()) { - commitCommentBuilder.append( - "\n\n######

Check the validation details at the link("); - commitCommentBuilder.append(webhookConfig.getHubLink()); - commitCommentBuilder.append(")

"); - } - commitComment += commitCommentBuilder.toString(); } + } - if (hasProhibitedOrRestricted || hasConflicts) { - lpvsPullRequest.setStatus(LPVSPullRequestStatus.ISSUES_DETECTED.toString()); - pullRequestRepository.save(lpvsPullRequest); - pullRequest.comment( - "**\\[License Pre-Validation Service\\]** Potential license problem(s) detected \n\n" - + commitComment); - repository.createCommitStatus( - webhookConfig.getHeadCommitSHA(), - GHCommitState.FAILURE, - null, - "Potential license problem(s) detected", - "[License Pre-Validation Service]"); - } else { - lpvsPullRequest.setStatus(LPVSPullRequestStatus.COMPLETED.toString()); - pullRequestRepository.save(lpvsPullRequest); - pullRequest.comment( - "**\\[License Pre-Validation Service\\]** No license issue detected \n\n" - + commitComment); - repository.createCommitStatus( - webhookConfig.getHeadCommitSHA(), - GHCommitState.SUCCESS, - null, - "No license issue detected", - "[License Pre-Validation Service]"); + if (conflicts != null && conflicts.size() > 0) { + hasConflicts = true; + StringBuilder commitCommentBuilder = new StringBuilder(); + commitCommentBuilder.append("**Detected license conflicts:**\n\n\n"); + commitCommentBuilder.append("
    "); + for (LPVSLicenseService.Conflict conflict : conflicts) { + commitCommentBuilder.append("
  • " + conflict.l1 + " and " + conflict.l2 + "
  • "); + LPVSDetectedLicense detectedIssue = new LPVSDetectedLicense(); + detectedIssue.setPullRequest(lpvsPullRequest); + Long l1 = lpvsLicenseRepository.searchBySpdxId(conflict.l1).getLicenseId(); + Long l2 = lpvsLicenseRepository.searchBySpdxId(conflict.l2).getLicenseId(); + detectedIssue.setLicenseConflict( + lpvsLicenseConflictRepository.findLicenseConflict(l1, l2)); + if (webhookConfig.getRepositoryLicense() != null) { + LPVSLicense repoLicense = + lpvsLicenseRepository.searchBySpdxId( + webhookConfig.getRepositoryLicense()); + if (repoLicense == null) { + repoLicense = + lpvsLicenseRepository.searchByAlternativeLicenseNames( + webhookConfig.getRepositoryLicense()); + } + detectedIssue.setRepositoryLicense(repoLicense); + } + detectedIssue.setIssue(true); + lpvsDetectedLicenseRepository.saveAndFlush(detectedIssue); } - } catch (IOException | NullPointerException | IllegalArgumentException e) { - lpvsPullRequest.setStatus(LPVSPullRequestStatus.INTERNAL_ERROR.toString()); - pullRequestRepository.saveAndFlush(lpvsPullRequest); - log.error("Can't authorize commentResults(): " + e.getMessage()); - try { - GHRepository repository = - gitHub.getRepository( - LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) - + "/" - + LPVSWebhookUtil.getRepositoryName(webhookConfig)); - repository.createCommitStatus( - webhookConfig.getHeadCommitSHA(), - GHCommitState.ERROR, - null, - "Internal scan error occurs", - "[License Pre-Validation Service]"); - } catch (Exception ex) { - log.error("Can't authorize " + ex); + commitCommentBuilder.append("
"); + if (null != webhookConfig.getHubLink()) { + commitCommentBuilder.append( + "\n\n######

Check the validation details at the link("); + commitCommentBuilder.append(webhookConfig.getHubLink()); + commitCommentBuilder.append(")

"); } + commitComment += commitCommentBuilder.toString(); + } + + if (hasProhibitedOrRestricted || hasConflicts) { + lpvsPullRequest.setStatus(LPVSPullRequestStatus.ISSUES_DETECTED.toString()); + pullRequestRepository.save(lpvsPullRequest); + pullRequest.comment( + "**\\[License Pre-Validation Service\\]** Potential license problem(s) detected \n\n" + + commitComment); + repository.createCommitStatus( + webhookConfig.getHeadCommitSHA(), + GHCommitState.FAILURE, + null, + "Potential license problem(s) detected", + "[License Pre-Validation Service]"); + } else { + lpvsPullRequest.setStatus(LPVSPullRequestStatus.COMPLETED.toString()); + pullRequestRepository.save(lpvsPullRequest); + pullRequest.comment( + "**\\[License Pre-Validation Service\\]** No license issue detected \n\n" + + commitComment); + repository.createCommitStatus( + webhookConfig.getHeadCommitSHA(), + GHCommitState.SUCCESS, + null, + "No license issue detected", + "[License Pre-Validation Service]"); } } diff --git a/src/main/java/com/lpvs/service/LPVSQueueService.java b/src/main/java/com/lpvs/service/LPVSQueueService.java index b442cf70..7da3fd32 100644 --- a/src/main/java/com/lpvs/service/LPVSQueueService.java +++ b/src/main/java/com/lpvs/service/LPVSQueueService.java @@ -18,7 +18,6 @@ import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; -import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.*; @@ -275,8 +274,8 @@ public void processWebHook(LPVSQueue webhookConfig) { + pullRequest.getPullRequestUrl()); try { gitHubService.commentResults(webhookConfig, null, null, pullRequest); - } catch (IOException e1) { - log.warn("Failed to post FAIL result " + e.getMessage()); + } catch (Exception ex) { + log.warn("Failed to post FAIL result " + ex.getMessage()); } delete(webhookConfig); log.info( diff --git a/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java b/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java index c1db362e..967e98ec 100644 --- a/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java +++ b/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java @@ -1881,7 +1881,7 @@ void setUp() { } @Test - public void testCommentResults__PrAbsent() throws IOException { + public void testCommentResults__PrAbsent() throws Exception { // main test gh_service.commentResults(webhookConfig, null, null, lpvsPullRequest); @@ -1966,24 +1966,22 @@ void setUp() { } @Test - public void testCommentResults__CantAuthorize() throws IOException { - // main test - gh_service.commentResults(webhookConfig, null, null, lpvsPullRequest); + public void testCommentResults__CantAuthorize() throws Exception { // `mocked_instance_gh` verify try { + gh_service.commentResults(webhookConfig, null, null, lpvsPullRequest); + verify(mocked_instance_gh, times(2)) .getRepository( LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) + "/" + LPVSWebhookUtil.getRepositoryName(webhookConfig)); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__CantAuthorize.testCommentResults__CantAuthorize() error " + e); - fail(); } - verifyNoMoreInteractions(mocked_instance_gh); } } @@ -2057,7 +2055,7 @@ void setUp() { } @Test - public void testCommentResults__ScanResultsEmpty() throws IOException { + public void testCommentResults__ScanResultsEmpty() throws Exception { // main test gh_service.commentResults(webhookConfig, List.of(), null, lpvsPullRequest); @@ -2261,7 +2259,7 @@ void setUp() { } @Test - public void testCommentResults__ProhibitedPresentConflictsPresent() throws IOException { + public void testCommentResults__ProhibitedPresentConflictsPresent() throws Exception { // main test gh_service.commentResults( webhookConfig, List.of(lpvs_file_1), List.of(conflict_1), lpvsPullRequest); @@ -2306,7 +2304,7 @@ public void testCommentResults__ProhibitedPresentConflictsPresent() throws IOExc @Test public void testCommentResults__ProhibitedPresentConflictsPresentLicensePresent() - throws IOException { + throws Exception { // main test webhookConfig.setRepositoryLicense(spdx_id_1); when(mocked_lpvsLicenseRepository.searchBySpdxId(spdx_id_1)) @@ -2365,7 +2363,7 @@ public void testCommentResults__ProhibitedPresentConflictsPresentLicensePresent( @Test public void testCommentResults__ProhibitedPresentConflictsPresentLicensePresentAlt() - throws IOException { + throws Exception { // main test webhookConfig.setRepositoryLicense(spdx_id_2); when(mocked_lpvsLicenseRepository.searchBySpdxId(spdx_id_2)).thenReturn(null); @@ -2588,7 +2586,7 @@ void setUp() { } @Test - public void testCommentResults__EmptyPresentConflictsPresent() throws IOException { + public void testCommentResults__EmptyPresentConflictsPresent() throws Exception { // main test gh_service.commentResults( webhookConfig, List.of(lpvs_file_1), List.of(conflict_1), lpvsPullRequest); @@ -2633,7 +2631,7 @@ public void testCommentResults__EmptyPresentConflictsPresent() throws IOExceptio @Test public void testCommentResults__EmptyPresentConflictsPresentLicensePresent() - throws IOException { + throws Exception { // main test webhookConfig.setRepositoryLicense(spdx_id_1); when(mocked_lpvsLicenseRepository.searchBySpdxId(spdx_id_1)) @@ -2692,7 +2690,7 @@ public void testCommentResults__EmptyPresentConflictsPresentLicensePresent() @Test public void testCommentResults__EmptyPresentConflictsPresentLicensePresentAlt() - throws IOException { + throws Exception { // main test webhookConfig.setRepositoryLicense(spdx_id_2); when(mocked_lpvsLicenseRepository.searchBySpdxId(spdx_id_2)).thenReturn(null); @@ -2918,7 +2916,7 @@ void setUp() { } @Test - public void testCommentResults__RestrictedPresentConflictsPresent() throws IOException { + public void testCommentResults__RestrictedPresentConflictsPresent() throws Exception { // main test gh_service.commentResults( webhookConfig, List.of(lpvs_file_1), List.of(conflict_1), lpvsPullRequest); @@ -2930,7 +2928,7 @@ public void testCommentResults__RestrictedPresentConflictsPresent() throws IOExc LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) + "/" + LPVSWebhookUtil.getRepositoryName(webhookConfig)); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__UnreviewedPresentConflictsPresent.testCommentResults__RestrictedPresentConflictsPresent() error " + e); @@ -2948,7 +2946,7 @@ public void testCommentResults__RestrictedPresentConflictsPresent() throws IOExc null, "Potential license problem(s) detected", "[License Pre-Validation Service]"); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__UnreviewedPresentConflictsPresent.testCommentResults__RestrictedPresentConflictsPresent() error " + e); @@ -2963,7 +2961,7 @@ public void testCommentResults__RestrictedPresentConflictsPresent() throws IOExc @Test public void testCommentResults__RestrictedPresentConflictsPresentLicensePresent() - throws IOException { + throws Exception { // main test webhookConfig.setRepositoryLicense(spdx_id_1); when(mocked_lpvsLicenseRepository.searchBySpdxId(spdx_id_1)) @@ -2989,7 +2987,7 @@ public void testCommentResults__RestrictedPresentConflictsPresentLicensePresent( LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) + "/" + LPVSWebhookUtil.getRepositoryName(webhookConfig)); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__UnreviewedPresentConflictsPresent.testCommentResults__RestrictedPresentConflictsPresentLicensePresent() error " + e); @@ -3007,7 +3005,7 @@ public void testCommentResults__RestrictedPresentConflictsPresentLicensePresent( null, "Potential license problem(s) detected", "[License Pre-Validation Service]"); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__UnreviewedPresentConflictsPresent.testCommentResults__RestrictedPresentConflictsPresentLicensePresent() error " + e); @@ -3022,7 +3020,7 @@ public void testCommentResults__RestrictedPresentConflictsPresentLicensePresent( @Test public void testCommentResults__RestrictedPresentConflictsPresentLicensePresentAlt() - throws IOException { + throws Exception { // main test webhookConfig.setRepositoryLicense(spdx_id_2); when(mocked_lpvsLicenseRepository.searchBySpdxId(spdx_id_2)).thenReturn(null); @@ -3049,7 +3047,7 @@ public void testCommentResults__RestrictedPresentConflictsPresentLicensePresentA LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) + "/" + LPVSWebhookUtil.getRepositoryName(webhookConfig)); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__UnreviewedPresentConflictsPresent.testCommentResults__RestrictedPresentConflictsPresentLicensePresentAlt() error " + e); @@ -3067,7 +3065,7 @@ public void testCommentResults__RestrictedPresentConflictsPresentLicensePresentA null, "Potential license problem(s) detected", "[License Pre-Validation Service]"); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__UnreviewedPresentConflictsPresent.testCommentResults__RestrictedPresentConflictsPresentLicensePresentAlt() error " + e); @@ -3198,7 +3196,7 @@ void setUp() { + "/" + LPVSWebhookUtil.getRepositoryName(webhookConfig))) .thenReturn(mocked_repo); - } catch (IOException e) { + } catch (Exception e) { log.error("mocked_repo.getRepository error " + e); } try { @@ -3212,7 +3210,7 @@ void setUp() { try { when(mocked_repo.getPullRequests(GHIssueState.OPEN)) .thenReturn(Arrays.asList(mocked_pr_1, mocked_pr_2)); - } catch (IOException e) { + } catch (Exception e) { log.error("mocked_repo.getPullRequests error " + e); } @@ -3248,7 +3246,7 @@ void setUp() { } @Test - public void testCommentResults__RestrictedPresentConflictsPresent() throws IOException { + public void testCommentResults__RestrictedPresentConflictsPresent() throws Exception { // main test gh_service.commentResults( webhookConfig, List.of(lpvs_file_1), List.of(conflict_1), lpvsPullRequest); @@ -3260,7 +3258,7 @@ public void testCommentResults__RestrictedPresentConflictsPresent() throws IOExc LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) + "/" + LPVSWebhookUtil.getRepositoryName(webhookConfig)); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__RestrictedPresentConflictsPresent.testCommentResults__RestrictedPresentConflictsPresent() error " + e); @@ -3278,7 +3276,7 @@ public void testCommentResults__RestrictedPresentConflictsPresent() throws IOExc null, "Potential license problem(s) detected", "[License Pre-Validation Service]"); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__RestrictedPresentConflictsPresent.testCommentResults__RestrictedPresentConflictsPresent() error " + e); @@ -3293,7 +3291,7 @@ public void testCommentResults__RestrictedPresentConflictsPresent() throws IOExc @Test public void testCommentResults__RestrictedPresentConflictsPresentLicensePresent() - throws IOException { + throws Exception { // main test webhookConfig.setRepositoryLicense(spdx_id_1); when(mocked_lpvsLicenseRepository.searchBySpdxId(spdx_id_1)) @@ -3319,7 +3317,7 @@ public void testCommentResults__RestrictedPresentConflictsPresentLicensePresent( LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) + "/" + LPVSWebhookUtil.getRepositoryName(webhookConfig)); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__RestrictedPresentConflictsPresent.testCommentResults__RestrictedPresentConflictsPresentLicensePresent() error " + e); @@ -3337,7 +3335,7 @@ public void testCommentResults__RestrictedPresentConflictsPresentLicensePresent( null, "Potential license problem(s) detected", "[License Pre-Validation Service]"); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__RestrictedPresentConflictsPresent.testCommentResults__RestrictedPresentConflictsPresentLicensePresent() error " + e); @@ -3352,7 +3350,7 @@ public void testCommentResults__RestrictedPresentConflictsPresentLicensePresent( @Test public void testCommentResults__RestrictedPresentConflictsPresentLicensePresentAlt() - throws IOException { + throws Exception { // main test webhookConfig.setRepositoryLicense(spdx_id_2); when(mocked_lpvsLicenseRepository.searchBySpdxId(spdx_id_2)).thenReturn(null); @@ -3379,7 +3377,7 @@ public void testCommentResults__RestrictedPresentConflictsPresentLicensePresentA LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) + "/" + LPVSWebhookUtil.getRepositoryName(webhookConfig)); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__RestrictedPresentConflictsPresent.testCommentResults__RestrictedPresentConflictsPresentLicensePresentAlt() error " + e); @@ -3397,7 +3395,7 @@ public void testCommentResults__RestrictedPresentConflictsPresentLicensePresentA null, "Potential license problem(s) detected", "[License Pre-Validation Service]"); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__RestrictedPresentConflictsPresent.testCommentResults__RestrictedPresentConflictsPresentLicensePresentAlt() error " + e); @@ -3550,7 +3548,7 @@ void setUp() { } @Test - public void testCommentResults__ProhibitedAbsentConflictsAbsent() throws IOException { + public void testCommentResults__ProhibitedAbsentConflictsAbsent() throws Exception { // main test gh_service.commentResults( webhookConfig, List.of(lpvs_file_1), List.of(), lpvsPullRequest); @@ -3562,7 +3560,7 @@ public void testCommentResults__ProhibitedAbsentConflictsAbsent() throws IOExcep LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) + "/" + LPVSWebhookUtil.getRepositoryName(webhookConfig)); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__ProhibitedAbsentConflictsAbsent.testCommentResults__ProhibitedAbsentConflictsAbsent() error " + e); @@ -3580,7 +3578,7 @@ public void testCommentResults__ProhibitedAbsentConflictsAbsent() throws IOExcep null, "No license issue detected", "[License Pre-Validation Service]"); - } catch (IOException e) { + } catch (Exception e) { log.error( "TestCommentResults__ProhibitedAbsentConflictsAbsent.testCommentResults__ProhibitedAbsentConflictsAbsent() error " + e); @@ -4226,7 +4224,7 @@ void setUp() { } @Test - public void testCommentResults() throws IOException { + public void testCommentResults() throws Exception { GitHub gitHub = Mockito.mock(GitHub.class); GHRepository repository = Mockito.mock(GHRepository.class); ReflectionTestUtils.setField(gh_service, "gitHub", gitHub); @@ -4242,7 +4240,10 @@ public void testCommentResults() throws IOException { LPVSLicense license = new LPVSLicense() { { + setLicenseId(0L); setChecklistUrl(""); + setSpdxId("MIT"); + setLicenseName("MIT"); setAccess("unrviewed"); } }; @@ -4270,7 +4271,7 @@ public void testCommentResults() throws IOException { }; List> conflictList = new ArrayList<>(); conflictList.add(new LPVSLicenseService.Conflict<>("1", "2")); - GHPullRequest pullRequest = new GHPullRequest(); + GHPullRequest pullRequest = mock(GHPullRequest.class); ReflectionTestUtils.setField(pullRequest, "url", "http://url.com"); List pullRequestList = new ArrayList() { @@ -4279,10 +4280,13 @@ public void testCommentResults() throws IOException { } }; Mockito.when(repository.getPullRequests(GHIssueState.OPEN)).thenReturn(pullRequestList); + Mockito.when(mocked_lpvsLicenseRepository.searchBySpdxId(anyString())) + .thenReturn(license); + gh_service.commentResults(webhookConfig, fileList, conflictList, lpvsPullRequest); license.setAccess(""); gh_service.commentResults(webhookConfig, fileList, conflictList, lpvsPullRequest); - Mockito.verify(gitHub, times(4)).getRepository(Mockito.anyString()); + Mockito.verify(gitHub, times(2)).getRepository(Mockito.anyString()); } @Test diff --git a/src/test/java/com/lpvs/service/LPVSQueueServiceTest.java b/src/test/java/com/lpvs/service/LPVSQueueServiceTest.java index a2170deb..39be1a0a 100644 --- a/src/test/java/com/lpvs/service/LPVSQueueServiceTest.java +++ b/src/test/java/com/lpvs/service/LPVSQueueServiceTest.java @@ -24,7 +24,6 @@ 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; @@ -151,7 +150,7 @@ void setUp() { } @Test - public void testProcessWebHook__NoPRDownloaded() throws IOException { + public void testProcessWebHook__NoPRDownloaded() throws Exception { // main test queueService.processWebHook(webhookConfig); @@ -269,13 +268,13 @@ void setUp() { if (!Files.exists(emptyFilePath)) { Files.createFile(emptyFilePath); } - } catch (IOException e) { + } catch (Exception e) { log.warn("error creating temp folder/file " + e.getMessage()); } } @AfterEach - public void tearDown() throws IOException { + public void tearDown() throws Exception { // Clean up the temporary folder after each test Files.walk(tempFolderPath) .sorted(Comparator.reverseOrder()) @@ -348,7 +347,7 @@ void setUp() { } @Test - public void testProcessWebHook____DeletionAbsentLicensePresent() throws IOException { + public void testProcessWebHook____DeletionAbsentLicensePresent() throws Exception { // main test queueService.processWebHook(webhookConfigMain); @@ -442,7 +441,7 @@ void setUp() { } @Test - public void testProcessWebHook____DeletionPresentLicensePresent() throws IOException { + public void testProcessWebHook____DeletionPresentLicensePresent() throws Exception { // main test queueService.processWebHook(webhookConfigMain); @@ -536,7 +535,7 @@ void setUp() { } @Test - public void testProcessWebHook__DeletionAbsentLicenseFound() throws IOException { + public void testProcessWebHook__DeletionAbsentLicenseFound() throws Exception { // main test queueService.processWebHook(webhookConfigMain); @@ -632,7 +631,7 @@ void setUp() { } @Test - public void testProcessWebHook__DeletionPresentLicenseFound() throws IOException { + public void testProcessWebHook__DeletionPresentLicenseFound() throws Exception { // main test queueService.processWebHook(webhookConfigMain); @@ -727,7 +726,7 @@ void setUp() { } @Test - public void testProcessWebHook__DeletionAbsentLicenseNull() throws IOException { + public void testProcessWebHook__DeletionAbsentLicenseNull() throws Exception { // main test queueService.processWebHook(webhookConfigMain); @@ -823,7 +822,7 @@ void setUp() { } @Test - public void testProcessWebHook__DeletionAbsentLicenseNull() throws IOException { + public void testProcessWebHook__DeletionAbsentLicenseNull() throws Exception { // main test queueService.processWebHook(webhookConfigMain);