Skip to content

Commit

Permalink
fix: fix remarks after review, close application after one scan
Browse files Browse the repository at this point in the history
Signed-off-by: Mykola Rudyk <m.rudyk@samsung.com>
  • Loading branch information
m-rudyk committed Mar 5, 2024
1 parent bd874c3 commit 79c1481
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
31 changes: 21 additions & 10 deletions src/main/java/com/lpvs/service/LPVSDetectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-singlescan.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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=

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/lpvs/service/LPVSDetectServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand All @@ -216,6 +221,7 @@ void testRunOneScan_trigerInternalQueueException()
throws NoSuchFieldException, IllegalAccessException {

setPrivateField(lpvsDetectService, "trigger", "fake-trigger-value");
setPrivateField(lpvsDetectService, "ctx", mockApplicationContext);

assertDoesNotThrow(() -> lpvsDetectService.runOneScan());
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 79c1481

Please sign in to comment.