From ec860d0572ee7ee16a45d7a8e371e9645043b602 Mon Sep 17 00:00:00 2001 From: Mykola Rudyk Date: Tue, 5 Mar 2024 00:46:51 +0200 Subject: [PATCH 1/3] fix: fix signlescan option Signed-off-by: Mykola Rudyk --- .../com/lpvs/entity/LPVSDetectedLicense.java | 2 +- .../java/com/lpvs/entity/LPVSLicense.java | 1 - .../com/lpvs/entity/LPVSLicenseConflict.java | 2 +- .../java/com/lpvs/entity/LPVSPullRequest.java | 2 +- src/main/java/com/lpvs/entity/LPVSQueue.java | 2 +- .../java/com/lpvs/entity/auth/LPVSMember.java | 1 - .../com/lpvs/service/LPVSDetectService.java | 25 ++++--------------- .../com/lpvs/service/LPVSLicenseService.java | 22 +++++++++++++++- src/main/resources/application.properties | 5 ++-- .../lpvs/service/LPVSDetectServiceTest.java | 17 ------------- 10 files changed, 33 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/lpvs/entity/LPVSDetectedLicense.java b/src/main/java/com/lpvs/entity/LPVSDetectedLicense.java index e417d96d..d0fb2176 100644 --- a/src/main/java/com/lpvs/entity/LPVSDetectedLicense.java +++ b/src/main/java/com/lpvs/entity/LPVSDetectedLicense.java @@ -17,7 +17,7 @@ * This class is mapped to the "detected_license" table in the "lpvs" schema. */ @Entity -@Table(name = "detected_license", schema = "${lpvs.schema:lpvs}") +@Table(name = "detected_license") @Getter @Setter public class LPVSDetectedLicense implements Serializable { diff --git a/src/main/java/com/lpvs/entity/LPVSLicense.java b/src/main/java/com/lpvs/entity/LPVSLicense.java index 71db5929..bdccc600 100644 --- a/src/main/java/com/lpvs/entity/LPVSLicense.java +++ b/src/main/java/com/lpvs/entity/LPVSLicense.java @@ -21,7 +21,6 @@ @Entity @Table( name = "license_list", - schema = "${lpvs.schema:lpvs}", indexes = {@Index(name = "spdx_id", columnList = "license_spdx", unique = true)}) @Getter @Setter diff --git a/src/main/java/com/lpvs/entity/LPVSLicenseConflict.java b/src/main/java/com/lpvs/entity/LPVSLicenseConflict.java index 2e4561e8..0cc34051 100644 --- a/src/main/java/com/lpvs/entity/LPVSLicenseConflict.java +++ b/src/main/java/com/lpvs/entity/LPVSLicenseConflict.java @@ -18,7 +18,7 @@ * This class is mapped to the "license_conflicts" table in the "lpvs" schema. */ @Entity -@Table(name = "license_conflicts", schema = "${lpvs.schema:lpvs}") +@Table(name = "license_conflicts") @Getter @Setter @NoArgsConstructor diff --git a/src/main/java/com/lpvs/entity/LPVSPullRequest.java b/src/main/java/com/lpvs/entity/LPVSPullRequest.java index a874ca1b..34645435 100644 --- a/src/main/java/com/lpvs/entity/LPVSPullRequest.java +++ b/src/main/java/com/lpvs/entity/LPVSPullRequest.java @@ -21,7 +21,7 @@ * This class is mapped to the "pull_requests" table in the "lpvs" schema. */ @Entity -@Table(name = "pull_requests", schema = "${lpvs.schema:lpvs}") +@Table(name = "pull_requests") @Getter @Setter @AllArgsConstructor diff --git a/src/main/java/com/lpvs/entity/LPVSQueue.java b/src/main/java/com/lpvs/entity/LPVSQueue.java index 83c89d96..3e1e53bc 100644 --- a/src/main/java/com/lpvs/entity/LPVSQueue.java +++ b/src/main/java/com/lpvs/entity/LPVSQueue.java @@ -20,7 +20,7 @@ * This class is mapped to the "queue" table in the "lpvs" schema. */ @Entity -@Table(name = "queue", schema = "${lpvs.schema:lpvs}") +@Table(name = "queue") @Getter @Setter public class LPVSQueue implements Serializable { diff --git a/src/main/java/com/lpvs/entity/auth/LPVSMember.java b/src/main/java/com/lpvs/entity/auth/LPVSMember.java index 37502783..137d8f8e 100644 --- a/src/main/java/com/lpvs/entity/auth/LPVSMember.java +++ b/src/main/java/com/lpvs/entity/auth/LPVSMember.java @@ -22,7 +22,6 @@ @Entity @Table( name = "member", - schema = "${lpvs.schema:lpvs}", indexes = {@Index(name = "unq_member", columnList = "email, provider", unique = true)}) public class LPVSMember { diff --git a/src/main/java/com/lpvs/service/LPVSDetectService.java b/src/main/java/com/lpvs/service/LPVSDetectService.java index 9d97ded7..308438f9 100644 --- a/src/main/java/com/lpvs/service/LPVSDetectService.java +++ b/src/main/java/com/lpvs/service/LPVSDetectService.java @@ -10,15 +10,10 @@ import com.lpvs.entity.LPVSQueue; import com.lpvs.service.scanner.scanoss.LPVSScanossDetectService; import com.lpvs.util.LPVSCommentUtil; -import com.lpvs.util.LPVSFileUtil; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.ExitCodeEvent; import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; import org.springframework.web.util.HtmlUtils; @@ -61,11 +56,6 @@ public class LPVSDetectService { */ private LPVSGitHubService gitHubService; - /** - * Event publisher for triggering application events. - */ - @Autowired private ApplicationEventPublisher eventPublisher; - /** * GitHub pull request used to trigger a single license scan (optional). */ @@ -78,11 +68,6 @@ public class LPVSDetectService { @Value("${build.html.report:}") private String htmlReport; - /** - * Spring application context. - */ - @Autowired ApplicationContext ctx; - /** * Constructs an instance of LPVSDetectService with the specified parameters. * @@ -119,14 +104,15 @@ private void init() { @EventListener(ApplicationReadyEvent.class) public void runOneScan() { if (trigger != null && !HtmlUtils.htmlEscape(trigger).equals("")) { - log.info("Triggered signle scan operation"); + log.info("Triggered single scan operation"); try { + licenseService.reloadFromTables(); LPVSQueue webhookConfig = gitHubService.getInternalQueueByPullRequest(HtmlUtils.htmlEscape(trigger)); List scanResult = this.runScan( - webhookConfig, LPVSFileUtil.getPathByPullRequest(webhookConfig)); + webhookConfig, gitHubService.getPullRequestFiles(webhookConfig)); List> detectedConflicts = licenseService.findConflicts(webhookConfig, scanResult); @@ -152,12 +138,11 @@ public void runOneScan() { webhookConfig, scanResult, detectedConflicts); log.info(report); } + log.info("Single scan completed."); } catch (Exception ex) { log.error("\n\n\n Single scan finished with errors \n\n\n"); - log.error("Can't triger single scan " + ex); + log.error("Can't trigger single scan: " + ex.getMessage()); } - // exiting application - eventPublisher.publishEvent(new ExitCodeEvent(new Object(), 0)); } } diff --git a/src/main/java/com/lpvs/service/LPVSLicenseService.java b/src/main/java/com/lpvs/service/LPVSLicenseService.java index 1372dd67..a960b1b1 100644 --- a/src/main/java/com/lpvs/service/LPVSLicenseService.java +++ b/src/main/java/com/lpvs/service/LPVSLicenseService.java @@ -149,13 +149,33 @@ private void init() { } catch (Exception ex) { log.warn("LICENSES and LICENSE CONFLICTS are not loaded."); - log.error(ex.toString()); + log.error(ex.getMessage()); licenses = new ArrayList<>(); licenseConflicts = new ArrayList<>(); } } } + public void reloadFromTables() { + if (licenses.isEmpty()) { + licenses = lpvsLicenseRepository.takeAllLicenses(); + log.info("LOADED " + licenses.size() + " licenses from DB."); + + List conflicts = + lpvsLicenseConflictRepository.takeAllLicenseConflicts(); + for (LPVSLicenseConflict conflict : conflicts) { + Conflict conf = + new Conflict<>( + conflict.getConflictLicense().getSpdxId(), + conflict.getRepositoryLicense().getSpdxId()); + if (!licenseConflicts.contains(conf)) { + licenseConflicts.add(conf); + } + } + log.info("LOADED " + licenseConflicts.size() + " license conflicts from DB."); + } + } + /** * Finds a license by SPDX identifier. * diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b67f0719..725e2e19 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -14,6 +14,7 @@ license_conflict=db # Logger configuration logging.pattern.console=%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n +spring.profiles.active=singlescan # GitHub settings # Corresponding env. variable LPVS_GITHUB_LOGIN @@ -32,7 +33,7 @@ lpvs.attempts=4 # DB Configuration # The name of DB schema -lpvs.schema=lpvs +spring.jpa.properties.hibernate.default_schema=lpvs spring.jpa.open-in-view=true spring.jpa.properties.hibernate.connection.CharSet=utf8 @@ -43,6 +44,6 @@ spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.use_sql_comments=true spring.jpa.properties.hibernate.show_sql=false spring.jpa.hibernate.ddl-auto=update -spring.datasource.url=jdbc:mysql://localhost:3306/${lpvs.schema} +spring.datasource.url=jdbc:mysql://localhost:3306/lpvs spring.datasource.username= spring.datasource.password= diff --git a/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java b/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java index 75d07814..27a40a51 100644 --- a/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java +++ b/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java @@ -25,7 +25,6 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.context.ApplicationEventPublisher; import java.io.IOException; import java.lang.reflect.Field; @@ -47,8 +46,6 @@ @ExtendWith(MockitoExtension.class) public class LPVSDetectServiceTest { - @Mock private ApplicationEventPublisher mockEventPublisher; - @Mock private LPVSScanossDetectService scanossDetectService; @InjectMocks private LPVSDetectService lpvsDetectService; @@ -123,7 +120,6 @@ void testRunOneScanWithNullTriger() throws NoSuchFieldException, IllegalAccessEx spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null)); setPrivateField(lpvsDetectService, "trigger", null); - setPrivateField(lpvsDetectService, "eventPublisher", mockEventPublisher); assertDoesNotThrow(() -> lpvsDetectService.runOneScan()); @@ -139,8 +135,6 @@ void testRunOneScan_Default() throws NoSuchFieldException, IllegalAccessExceptio spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null)); setPrivateField(lpvsDetectService, "trigger", "fake-trigger-value"); - setPrivateField(lpvsDetectService, "eventPublisher", mockEventPublisher); - doNothing().when(mockEventPublisher).publishEvent(any()); assertDoesNotThrow(() -> lpvsDetectService.runOneScan()); } @@ -173,11 +167,8 @@ void testRunOneScan_Branch2() setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); setPrivateField(detectService, "scannerType", "scanoss"); - setPrivateField(detectService, "eventPublisher", mockEventPublisher); setPrivateField(detectService, "htmlReport", null); - doNothing().when(mockEventPublisher).publishEvent(any()); - detectService.runOneScan(); setPrivateField(detectService, "htmlReport", ""); @@ -194,15 +185,12 @@ void testRunOneScan_Branch3() List> expected = List.of(conflict_1, conflict_1); - doNothing().when(mockEventPublisher).publishEvent(any()); - lpvsDetectService = spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null)); setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); setPrivateField(detectService, "scannerType", "scanoss"); setPrivateField(detectService, "htmlReport", "build"); - setPrivateField(detectService, "eventPublisher", mockEventPublisher); // Mock the necessary GitHub objects for LPVSQueue when(mockGitHub.getRepository(any())).thenReturn(mockRepository); @@ -228,8 +216,6 @@ void testRunOneScan_trigerInternalQueueException() throws NoSuchFieldException, IllegalAccessException { setPrivateField(lpvsDetectService, "trigger", "fake-trigger-value"); - setPrivateField(lpvsDetectService, "eventPublisher", mockEventPublisher); - doNothing().when(mockEventPublisher).publishEvent(any()); assertDoesNotThrow(() -> lpvsDetectService.runOneScan()); } @@ -246,7 +232,6 @@ void testRunOneScan_TriggerNotNull() throws Exception { setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); setPrivateField(detectService, "scannerType", "scanoss"); setPrivateField(detectService, "htmlReport", "build/report/test.html"); - setPrivateField(detectService, "eventPublisher", mockEventPublisher); // Mock the necessary GitHub objects for LPVSQueue when(mockGitHub.getRepository(any())).thenReturn(mockRepository); @@ -279,7 +264,6 @@ void testRunOneScan_TriggerNotNull_Branch2() throws Exception { setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); setPrivateField(detectService, "scannerType", "scanoss"); setPrivateField(detectService, "htmlReport", "build/report/test.html"); - setPrivateField(detectService, "eventPublisher", mockEventPublisher); // Mock the necessary GitHub objects for LPVSQueue when(mockGitHub.getRepository(any())).thenReturn(mockRepository); @@ -313,7 +297,6 @@ void testRunOneScan_TriggerNotNull_Branch3() throws Exception { setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); setPrivateField(detectService, "scannerType", "scanoss"); setPrivateField(detectService, "htmlReport", "build/report/test.html"); - setPrivateField(detectService, "eventPublisher", mockEventPublisher); // Mock the necessary GitHub objects for LPVSQueue when(mockGitHub.getRepository(any())).thenReturn(mockRepository); From bd874c3588723e3a81e737c7867bfb9f2aa3bad8 Mon Sep 17 00:00:00 2001 From: Mykola Rudyk Date: Tue, 5 Mar 2024 09:44:10 +0200 Subject: [PATCH 2/3] test: add test for reloading tables from DB for singlescan Signed-off-by: Mykola Rudyk --- .../com/lpvs/service/LPVSLicenseService.java | 4 + .../lpvs/service/LPVSLicenseServiceTest.java | 105 ++++++++++++++---- 2 files changed, 89 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/lpvs/service/LPVSLicenseService.java b/src/main/java/com/lpvs/service/LPVSLicenseService.java index a960b1b1..9cfdfa5b 100644 --- a/src/main/java/com/lpvs/service/LPVSLicenseService.java +++ b/src/main/java/com/lpvs/service/LPVSLicenseService.java @@ -156,6 +156,10 @@ private void init() { } } + /** + * This method reloads licenses and license conflicts from database tables if the + * licenses list in case of single scan triggered and used in memory database. + */ public void reloadFromTables() { if (licenses.isEmpty()) { licenses = lpvsLicenseRepository.takeAllLicenses(); diff --git a/src/test/java/com/lpvs/service/LPVSLicenseServiceTest.java b/src/test/java/com/lpvs/service/LPVSLicenseServiceTest.java index b56f627e..10bcaa52 100644 --- a/src/test/java/com/lpvs/service/LPVSLicenseServiceTest.java +++ b/src/test/java/com/lpvs/service/LPVSLicenseServiceTest.java @@ -6,6 +6,31 @@ */ package com.lpvs.service; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.Mockito.when; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mockito; +import org.springframework.test.util.ReflectionTestUtils; + import com.lpvs.entity.LPVSFile; import com.lpvs.entity.LPVSLicense; import com.lpvs.entity.LPVSLicenseConflict; @@ -13,31 +38,12 @@ import com.lpvs.repository.LPVSLicenseConflictRepository; import com.lpvs.repository.LPVSLicenseRepository; import com.lpvs.util.LPVSExitHandler; + import lombok.extern.slf4j.Slf4j; import uk.org.webcompere.systemstubs.environment.EnvironmentVariables; import uk.org.webcompere.systemstubs.jupiter.SystemStub; import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mockito; -import org.springframework.test.util.ReflectionTestUtils; - -import java.lang.reflect.Field; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Objects; -import java.util.Set; - -import static org.junit.jupiter.api.Assertions.*; - @Slf4j @ExtendWith(SystemStubsExtension.class) public class LPVSLicenseServiceTest { @@ -229,6 +235,65 @@ public void testInitDB() { fail(); } } + + @Test + public void testReloadFromTables() + throws NoSuchFieldException, + SecurityException, + IllegalArgumentException, + IllegalAccessException { + + List licenseList = new ArrayList<>(); + + LPVSLicense license1 = + new LPVSLicense() { + { + setChecklistUrl(""); + setAccess("unrviewed"); + setSpdxId("Apache-2.0"); + } + }; + LPVSLicense license2 = + new LPVSLicense() { + { + setChecklistUrl(""); + setAccess("unrviewed"); + setSpdxId("MIT"); + } + }; + + licenseList.add(license1); + licenseList.add(license2); + + when(lpvsLicenseRepository.takeAllLicenses()).thenReturn(licenseList); + + LPVSLicenseConflict licenseConflict = new LPVSLicenseConflict(); + licenseConflict.setConflictId(1L); + licenseConflict.setConflictLicense(license1); + licenseConflict.setRepositoryLicense(license2); + List licenseConflictList = new ArrayList<>(); + licenseConflictList.add(licenseConflict); + + when(lpvsLicenseConflictRepository.takeAllLicenseConflicts()) + .thenReturn(licenseConflictList); + + Field lpvsLicenseRepositoryField = + LPVSLicenseService.class.getDeclaredField("lpvsLicenseRepository"); + lpvsLicenseRepositoryField.setAccessible(true); + lpvsLicenseRepositoryField.set(licenseService, lpvsLicenseRepository); + + Field lpvsLicenseConflictRepositoryField = + LPVSLicenseService.class.getDeclaredField("lpvsLicenseConflictRepository"); + lpvsLicenseConflictRepositoryField.setAccessible(true); + lpvsLicenseConflictRepositoryField.set(licenseService, lpvsLicenseConflictRepository); + + try { + licenseService.reloadFromTables(); + } catch (Exception e) { + e.printStackTrace(); + fail("Exception occurred: " + e.getMessage()); + } + } } @Nested From 79c14819cfdfd83fe2d802af713f8f89a923c6a5 Mon Sep 17 00:00:00 2001 From: Mykola Rudyk Date: Tue, 5 Mar 2024 12:07:26 +0200 Subject: [PATCH 3/3] fix: fix remarks after review, close application after one scan Signed-off-by: Mykola Rudyk --- .../com/lpvs/service/LPVSDetectService.java | 31 +++++++++++++------ .../application-singlescan.properties | 2 +- src/main/resources/application.properties | 2 +- .../lpvs/service/LPVSDetectServiceTest.java | 9 ++++++ 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/lpvs/service/LPVSDetectService.java b/src/main/java/com/lpvs/service/LPVSDetectService.java index 308438f9..119fc187 100644 --- a/src/main/java/com/lpvs/service/LPVSDetectService.java +++ b/src/main/java/com/lpvs/service/LPVSDetectService.java @@ -6,23 +6,28 @@ */ package com.lpvs.service; -import com.lpvs.entity.LPVSFile; -import com.lpvs.entity.LPVSQueue; -import com.lpvs.service.scanner.scanoss.LPVSScanossDetectService; -import com.lpvs.util.LPVSCommentUtil; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; -import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.ApplicationContext; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; import org.springframework.web.util.HtmlUtils; + +import com.lpvs.entity.LPVSFile; +import com.lpvs.entity.LPVSQueue; +import com.lpvs.service.scanner.scanoss.LPVSScanossDetectService; +import com.lpvs.util.LPVSCommentUtil; + import jakarta.annotation.PostConstruct; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; +import lombok.extern.slf4j.Slf4j; /** * Service class for detecting licenses in GitHub pull requests using a specified scanner. @@ -68,6 +73,11 @@ public class LPVSDetectService { @Value("${build.html.report:}") private String htmlReport; + /** + * Spring application context. + */ + @Autowired ApplicationContext ctx; + /** * Constructs an instance of LPVSDetectService with the specified parameters. * @@ -143,6 +153,7 @@ public void runOneScan() { log.error("\n\n\n Single scan finished with errors \n\n\n"); log.error("Can't trigger single scan: " + ex.getMessage()); } + SpringApplication.exit(ctx, () -> 0); } } diff --git a/src/main/resources/application-singlescan.properties b/src/main/resources/application-singlescan.properties index b829889f..09f54d54 100644 --- a/src/main/resources/application-singlescan.properties +++ b/src/main/resources/application-singlescan.properties @@ -7,7 +7,7 @@ spring.sql.init.platform=h2 # DB Configuration spring.sql.init.data-locations=classpath*:database_dump.sql spring.datasource.initialization-mode=always -spring.datasource.url=jdbc:h2:mem:singlescan;DATABASE_TO_UPPER=false;MODE=MySQL;NON_KEYWORDS=USER +spring.datasource.url=jdbc:h2:mem:singlescan;DATABASE_TO_UPPER=false;MODE=MySQL;NON_KEYWORDS=USER;DB_CLOSE_ON_EXIT=FALSE spring.datasource.username= spring.datasource.password= diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 725e2e19..ac0ba853 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -14,7 +14,7 @@ license_conflict=db # Logger configuration logging.pattern.console=%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n -spring.profiles.active=singlescan +spring.profiles.active= # GitHub settings # Corresponding env. variable LPVS_GITHUB_LOGIN diff --git a/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java b/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java index 27a40a51..d1196c58 100644 --- a/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java +++ b/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java @@ -14,6 +14,7 @@ import com.lpvs.util.LPVSFileUtil; import lombok.extern.slf4j.Slf4j; +import org.springframework.context.ApplicationContext; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -80,6 +81,7 @@ class TestRunScan__Scanoss { GHRepository mockRepository = mock(GHRepository.class); GHPullRequest mockPullRequest = mock(GHPullRequest.class); GHRepository mockHeadRepository = mock(GHRepository.class); + ApplicationContext mockApplicationContext = mock(ApplicationContext.class); LPVSQueue webhookConfig; final String test_path = "test_path"; @@ -135,6 +137,7 @@ void testRunOneScan_Default() throws NoSuchFieldException, IllegalAccessExceptio spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null)); setPrivateField(lpvsDetectService, "trigger", "fake-trigger-value"); + setPrivateField(lpvsDetectService, "ctx", mockApplicationContext); assertDoesNotThrow(() -> lpvsDetectService.runOneScan()); } @@ -168,6 +171,7 @@ void testRunOneScan_Branch2() setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); setPrivateField(detectService, "scannerType", "scanoss"); setPrivateField(detectService, "htmlReport", null); + setPrivateField(detectService, "ctx", mockApplicationContext); detectService.runOneScan(); @@ -191,6 +195,7 @@ void testRunOneScan_Branch3() setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); setPrivateField(detectService, "scannerType", "scanoss"); setPrivateField(detectService, "htmlReport", "build"); + setPrivateField(detectService, "ctx", mockApplicationContext); // Mock the necessary GitHub objects for LPVSQueue when(mockGitHub.getRepository(any())).thenReturn(mockRepository); @@ -216,6 +221,7 @@ void testRunOneScan_trigerInternalQueueException() throws NoSuchFieldException, IllegalAccessException { setPrivateField(lpvsDetectService, "trigger", "fake-trigger-value"); + setPrivateField(lpvsDetectService, "ctx", mockApplicationContext); assertDoesNotThrow(() -> lpvsDetectService.runOneScan()); } @@ -232,6 +238,7 @@ void testRunOneScan_TriggerNotNull() throws Exception { setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); setPrivateField(detectService, "scannerType", "scanoss"); setPrivateField(detectService, "htmlReport", "build/report/test.html"); + setPrivateField(detectService, "ctx", mockApplicationContext); // Mock the necessary GitHub objects for LPVSQueue when(mockGitHub.getRepository(any())).thenReturn(mockRepository); @@ -264,6 +271,7 @@ void testRunOneScan_TriggerNotNull_Branch2() throws Exception { setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); setPrivateField(detectService, "scannerType", "scanoss"); setPrivateField(detectService, "htmlReport", "build/report/test.html"); + setPrivateField(detectService, "ctx", mockApplicationContext); // Mock the necessary GitHub objects for LPVSQueue when(mockGitHub.getRepository(any())).thenReturn(mockRepository); @@ -297,6 +305,7 @@ void testRunOneScan_TriggerNotNull_Branch3() throws Exception { setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); setPrivateField(detectService, "scannerType", "scanoss"); setPrivateField(detectService, "htmlReport", "build/report/test.html"); + setPrivateField(detectService, "ctx", mockApplicationContext); // Mock the necessary GitHub objects for LPVSQueue when(mockGitHub.getRepository(any())).thenReturn(mockRepository);