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);