diff --git a/README.md b/README.md index cd8e7d40..867df936 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,7 @@ You can now create a new pull request or update an existing one with commits. LP ## How to Build and Run _LPVS_ from Source Code -#### 1. Build Prerequisites +### 1. Build Prerequisites Before building LPVS from source code, ensure that you have the following prerequisites installed: @@ -160,7 +160,7 @@ Before building LPVS from source code, ensure that you have the following prereq sudo apt install mysql-server ``` -#### 2. Create Necessary MySQL Database and User (optional if not using a database) +### 2. Create Necessary MySQL Database and User (optional if not using a database) 2.1 Start the MySQL server: ```bash @@ -194,7 +194,7 @@ Before building LPVS from source code, ensure that you have the following prereq spring.datasource.password=password ``` -#### 3. Setting up _LPVS_ `application.properties` +### 3. Setting up _LPVS_ `application.properties` Fill in the following lines in the [`src/main/resources/application.properties`](src/main/resources/application.properties) file: @@ -226,20 +226,25 @@ spring.datasource.password= Alternatively, you can provide the necessary values using the following environment variables: `LPVS_GITHUB_LOGIN`, `LPVS_GITHUB_TOKEN`, `LPVS_GITHUB_API_URL`, `LPVS_GITHUB_SECRET`, and `LPVS_LICENSE_CONFLICT`. -#### 4. Build LPVS Application with Maven and Run it -To build LPVS from source code and run it, follow these steps: +### 4. Build LPVS Application with Maven and Run it -4.1 Build the LPVS application using Maven: +#### 4.a Service mode (default) + +To build LPVS from source code and run it in the default service mode, follow these steps: + +4.a.1 Build the LPVS application using Maven: ```bash mvn clean install ``` -4.2 Navigate to the target directory: +4.a.2 Navigate to the target directory: ```bash cd target/ ``` -4.3 Run the LPVS application using the following command: +4.a.3. Run the LPVS application. + + Service is run using the following command: ```bash java -jar lpvs-*.jar ``` @@ -252,6 +257,30 @@ To build LPVS from source code and run it, follow these steps: LPVS is now built and running. You can create a new pull request or update an existing one with commits, and LPVS will automatically start scanning and provide comments about the licenses found in the project. +#### 4.b Single scan mode + +Alternatively, you can perform a one-time scan on a specific pull request using the single scan mode. Follow these steps: + +4.b.1. Begin by running the installation and navigating to the target directory, similar to the process in service mode (refer to steps 4.a.1 and 4.a.2): + + ```bash + mvn clean install + cd target/ + ``` + +4.b.2. Execute the single scan with the following command: + + ```bash + java -jar -Dgithub.token= lpvs-*.jar --github.pull.request= + ``` + +4.b.3. By default, the above command requires a pre-configured MySQL database. To avoid setting up the database, use the "singlescan" profile: + ```bash + java -jar -Dspring.profiles.active=singlescan -Dgithub.token= lpvs-*.jar --github.pull-request= + ``` + +These steps streamline the process, allowing you to run a scan on a single pull request without the need for a preconfigured database. + --- ## Frontend Source Code (React) diff --git a/pom.xml b/pom.xml index f0b8e07a..f4dfc408 100644 --- a/pom.xml +++ b/pom.xml @@ -131,6 +131,11 @@ 1.18.20 provided + + com.h2database + h2 + 2.1.214 + diff --git a/src/main/java/com/lpvs/entity/LPVSQueue.java b/src/main/java/com/lpvs/entity/LPVSQueue.java index 1b970ead..25685cdc 100644 --- a/src/main/java/com/lpvs/entity/LPVSQueue.java +++ b/src/main/java/com/lpvs/entity/LPVSQueue.java @@ -33,7 +33,7 @@ public class LPVSQueue implements Serializable { @Column(name = "scan_date") private Date date; - @Column(name = "user") + @Column(name = "user_id") private String userId; @Column(name = "review_system_type") diff --git a/src/main/java/com/lpvs/service/LPVSDetectService.java b/src/main/java/com/lpvs/service/LPVSDetectService.java index ea6a7953..4cc5e092 100644 --- a/src/main/java/com/lpvs/service/LPVSDetectService.java +++ b/src/main/java/com/lpvs/service/LPVSDetectService.java @@ -9,12 +9,29 @@ import com.lpvs.entity.LPVSFile; import com.lpvs.entity.LPVSQueue; import com.lpvs.service.scanner.scanoss.LPVSScanossDetectService; +import com.lpvs.util.LPVSFileUtil; +import com.nimbusds.jose.util.IOUtils; + import lombok.extern.slf4j.Slf4j; +import org.kohsuke.github.GHPullRequest; +import org.kohsuke.github.GHRepository; +import org.kohsuke.github.GitHub; 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; + import javax.annotation.PostConstruct; + +import java.io.File; +import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; @Service @@ -25,11 +42,22 @@ public class LPVSDetectService { private LPVSScanossDetectService scanossDetectService; + private LPVSGitHubConnectionService gitHubConnectionService; + + @Autowired private ApplicationEventPublisher eventPublisher; + + @Value("${github.pull.request:}") + private String trigger; + + @Autowired ApplicationContext ctx; + @Autowired public LPVSDetectService( @Value("${scanner:scanoss}") String scannerType, + LPVSGitHubConnectionService gitHubConnectionService, LPVSScanossDetectService scanossDetectService) { this.scannerType = scannerType; + this.gitHubConnectionService = gitHubConnectionService; this.scanossDetectService = scanossDetectService; } @@ -38,6 +66,77 @@ private void init() { log.info("License detection scanner: " + scannerType); } + @EventListener(ApplicationReadyEvent.class) + public void runOneScan() { + if (trigger != null && !HtmlUtils.htmlEscape(trigger).equals("")) { + try { + LPVSQueue webhookConfig = + this.getInternalQueueByPullRequest(HtmlUtils.htmlEscape(trigger)); + this.runScan(webhookConfig, LPVSDetectService.getPathByPullRequest(webhookConfig)); + File scanResult = new File(LPVSFileUtil.getScanResultsJsonFilePath(webhookConfig)); + if (scanResult.exists()) { + String jsonTxt = IOUtils.readFileToString(scanResult); + // ToDo: form html report and console output + log.info(jsonTxt); + log.info("\n\n\n Single scan finished successfully \n\n\n"); + } + } catch (Exception ex) { + log.info("\n\n\n Single scan finished with errors \n\n\n"); + log.error("Can't triger single scan " + ex); + } + // exiting application + eventPublisher.publishEvent(new ExitCodeEvent(new Object(), 0)); + } + } + + private static LPVSQueue getGitHubWebhookConfig(GHRepository repo, GHPullRequest pR) { + LPVSQueue webhookConfig = new LPVSQueue(); + webhookConfig.setPullRequestUrl( + pR.getHtmlUrl() != null ? pR.getHtmlUrl().toString() : null); + if (pR.getHead() != null + && pR.getHead().getRepository() != null + && pR.getHead().getRepository().getHtmlUrl() != null) { + webhookConfig.setPullRequestFilesUrl( + pR.getHead().getRepository().getHtmlUrl().toString()); + } else { + webhookConfig.setPullRequestFilesUrl(webhookConfig.getPullRequestUrl()); + } + webhookConfig.setPullRequestAPIUrl(pR.getUrl() != null ? pR.getUrl().toString() : null); + webhookConfig.setRepositoryUrl( + repo.getHtmlUrl() != null ? repo.getHtmlUrl().toString() : null); + webhookConfig.setUserId("Single scan run"); + webhookConfig.setHeadCommitSHA(pR.getHead() != null ? pR.getHead().getSha() : null); + return webhookConfig; + } + + public LPVSQueue getInternalQueueByPullRequest(String pullRequest) { + try { + if (pullRequest == null) return null; + String[] pullRequestSplit = pullRequest.split("/"); + if (pullRequestSplit.length < 5) return null; + String pullRequestRepo = + String.join( + "/", + Arrays.asList(pullRequestSplit) + .subList( + pullRequestSplit.length - 4, + pullRequestSplit.length - 2)); + int pullRequestNum = Integer.parseInt(pullRequestSplit[pullRequestSplit.length - 1]); + GitHub gitHub = gitHubConnectionService.connectToGitHubApi(); + GHRepository repo = gitHub.getRepository(pullRequestRepo); + GHPullRequest pR = repo.getPullRequest(pullRequestNum); + return LPVSDetectService.getGitHubWebhookConfig(repo, pR); + } catch (IOException e) { + log.error("Can't set up github client: " + e); + } + return null; + } + + public static String getPathByPullRequest(LPVSQueue webhookConfig) { + if (webhookConfig == null) return null; + return LPVSFileUtil.getLocalDirectoryPath(webhookConfig); + } + public List runScan(LPVSQueue webhookConfig, String path) throws Exception { if (scannerType.equals("scanoss")) { scanossDetectService.runScan(webhookConfig, path); diff --git a/src/main/java/com/lpvs/service/LPVSGitHubConnectionService.java b/src/main/java/com/lpvs/service/LPVSGitHubConnectionService.java new file mode 100644 index 00000000..27a60ccc --- /dev/null +++ b/src/main/java/com/lpvs/service/LPVSGitHubConnectionService.java @@ -0,0 +1,92 @@ +/** + * Copyright (c) 2023, Samsung Electronics Co., Ltd. All rights reserved. + * + * Use of this source code is governed by a MIT license that can be + * found in the LICENSE file. + */ +package com.lpvs.service; + +import com.lpvs.util.LPVSExitHandler; +import lombok.extern.slf4j.Slf4j; +import org.kohsuke.github.GitHub; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.util.Optional; + +@Service +@Slf4j +public class LPVSGitHubConnectionService { + + private String GITHUB_LOGIN; + private String GITHUB_AUTH_TOKEN; + private String GITHUB_API_URL; + + private static final String GITHUB_LOGIN_PROP_NAME = "github.login"; + private static final String GITHUB_AUTH_TOKEN_PROP_NAME = "github.token"; + private static final String GITHUB_API_URL_PROP_NAME = "github.api.url"; + + private static final String GITHUB_LOGIN_ENV_VAR_NAME = "LPVS_GITHUB_LOGIN"; + private static final String GITHUB_AUTH_TOKEN_ENV_VAR_NAME = "LPVS_GITHUB_TOKEN"; + private static final String GITHUB_API_URL_ENV_VAR_NAME = "LPVS_GITHUB_API_URL"; + + private LPVSExitHandler exitHandler; + + @Autowired + public LPVSGitHubConnectionService( + @Value("${" + GITHUB_LOGIN_PROP_NAME + "}") String GITHUB_LOGIN, + @Value("${" + GITHUB_AUTH_TOKEN_PROP_NAME + "}") String GITHUB_AUTH_TOKEN, + @Value("${" + GITHUB_API_URL_PROP_NAME + "}") String GITHUB_API_URL, + LPVSExitHandler exitHandler) { + this.GITHUB_LOGIN = + Optional.ofNullable(GITHUB_LOGIN) + .filter(s -> !s.isEmpty()) + .orElse( + Optional.ofNullable(System.getenv(GITHUB_LOGIN_ENV_VAR_NAME)) + .orElse("")); + this.GITHUB_AUTH_TOKEN = + Optional.ofNullable(GITHUB_AUTH_TOKEN) + .filter(s -> !s.isEmpty()) + .orElse( + Optional.ofNullable(System.getenv(GITHUB_AUTH_TOKEN_ENV_VAR_NAME)) + .orElse("")); + this.GITHUB_API_URL = + Optional.ofNullable(GITHUB_API_URL) + .filter(s -> !s.isEmpty()) + .orElse( + Optional.ofNullable(System.getenv(GITHUB_API_URL_ENV_VAR_NAME)) + .orElse("")); + this.exitHandler = exitHandler; + } + + @PostConstruct + private void checks() { + if (this.GITHUB_AUTH_TOKEN.isEmpty()) { + log.error( + GITHUB_AUTH_TOKEN_ENV_VAR_NAME + + "(" + + GITHUB_AUTH_TOKEN_PROP_NAME + + ") is not set."); + exitHandler.exit(-1); + } + } + + public GitHub connectToGitHubApi() throws IOException { + GitHub gH; + if (GITHUB_AUTH_TOKEN.isEmpty()) setGithubTokenFromEnv(); + if (GITHUB_API_URL.isEmpty()) gH = GitHub.connect(GITHUB_LOGIN, GITHUB_AUTH_TOKEN); + else + gH = + GitHub.connectToEnterpriseWithOAuth( + GITHUB_API_URL, GITHUB_LOGIN, GITHUB_AUTH_TOKEN); + return gH; + } + + public void setGithubTokenFromEnv() { + if (System.getenv("LPVS_GITHUB_TOKEN") != null) + GITHUB_AUTH_TOKEN = System.getenv("LPVS_GITHUB_TOKEN"); + } +} diff --git a/src/main/java/com/lpvs/service/LPVSGitHubService.java b/src/main/java/com/lpvs/service/LPVSGitHubService.java index c7bf1d33..e34109cf 100644 --- a/src/main/java/com/lpvs/service/LPVSGitHubService.java +++ b/src/main/java/com/lpvs/service/LPVSGitHubService.java @@ -15,7 +15,6 @@ import com.lpvs.repository.LPVSLicenseRepository; import com.lpvs.repository.LPVSPullRequestRepository; import com.lpvs.util.LPVSCommentUtil; -import com.lpvs.util.LPVSExitHandler; import com.lpvs.util.LPVSFileUtil; import com.lpvs.util.LPVSWebhookUtil; import lombok.extern.slf4j.Slf4j; @@ -26,30 +25,15 @@ import org.kohsuke.github.GHIssueState; import org.kohsuke.github.GHCommitState; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import javax.annotation.PostConstruct; import java.io.IOException; import java.util.List; -import java.util.Optional; @Service @Slf4j public class LPVSGitHubService { - private String GITHUB_LOGIN; - private String GITHUB_AUTH_TOKEN; - private String GITHUB_API_URL; - - private static final String GITHUB_LOGIN_PROP_NAME = "github.login"; - private static final String GITHUB_AUTH_TOKEN_PROP_NAME = "github.token"; - private static final String GITHUB_API_URL_PROP_NAME = "github.api.url"; - - private static final String GITHUB_LOGIN_ENV_VAR_NAME = "LPVS_GITHUB_LOGIN"; - private static final String GITHUB_AUTH_TOKEN_ENV_VAR_NAME = "LPVS_GITHUB_TOKEN"; - private static final String GITHUB_API_URL_ENV_VAR_NAME = "LPVS_GITHUB_API_URL"; - private LPVSPullRequestRepository pullRequestRepository; private LPVSDetectedLicenseRepository lpvsDetectedLicenseRepository; @@ -58,65 +42,27 @@ public class LPVSGitHubService { private LPVSLicenseConflictRepository lpvsLicenseConflictRepository; - private LPVSExitHandler exitHandler; + private LPVSGitHubConnectionService gitHubConnectionService; @Autowired public LPVSGitHubService( - @Value("${" + GITHUB_LOGIN_PROP_NAME + "}") String GITHUB_LOGIN, - @Value("${" + GITHUB_AUTH_TOKEN_PROP_NAME + "}") String GITHUB_AUTH_TOKEN, - @Value("${" + GITHUB_API_URL_PROP_NAME + "}") String GITHUB_API_URL, LPVSPullRequestRepository pullRequestRepository, LPVSDetectedLicenseRepository lpvsDetectedLicenseRepository, LPVSLicenseRepository lpvsLicenseRepository, LPVSLicenseConflictRepository lpvsLicenseConflictRepository, - LPVSExitHandler exitHandler) { - this.GITHUB_LOGIN = - Optional.ofNullable(GITHUB_LOGIN) - .filter(s -> !s.isEmpty()) - .orElse( - Optional.ofNullable(System.getenv(GITHUB_LOGIN_ENV_VAR_NAME)) - .orElse("")); - this.GITHUB_AUTH_TOKEN = - Optional.ofNullable(GITHUB_AUTH_TOKEN) - .filter(s -> !s.isEmpty()) - .orElse( - Optional.ofNullable(System.getenv(GITHUB_AUTH_TOKEN_ENV_VAR_NAME)) - .orElse("")); - this.GITHUB_API_URL = - Optional.ofNullable(GITHUB_API_URL) - .filter(s -> !s.isEmpty()) - .orElse( - Optional.ofNullable(System.getenv(GITHUB_API_URL_ENV_VAR_NAME)) - .orElse("")); - this.exitHandler = exitHandler; + LPVSGitHubConnectionService gitHubConnectionService) { this.pullRequestRepository = pullRequestRepository; this.lpvsDetectedLicenseRepository = lpvsDetectedLicenseRepository; this.lpvsLicenseRepository = lpvsLicenseRepository; this.lpvsLicenseConflictRepository = lpvsLicenseConflictRepository; - } - - @PostConstruct - private void checks() { - if (this.GITHUB_AUTH_TOKEN.isEmpty()) { - log.error( - GITHUB_AUTH_TOKEN_ENV_VAR_NAME - + "(" - + GITHUB_AUTH_TOKEN_PROP_NAME - + ") is not set."); - exitHandler.exit(-1); - } + this.gitHubConnectionService = gitHubConnectionService; } private static GitHub gitHub; public String getPullRequestFiles(LPVSQueue webhookConfig) { try { - if (GITHUB_AUTH_TOKEN.isEmpty()) setGithubTokenFromEnv(); - if (GITHUB_API_URL.isEmpty()) gitHub = GitHub.connect(GITHUB_LOGIN, GITHUB_AUTH_TOKEN); - else - gitHub = - GitHub.connectToEnterpriseWithOAuth( - GITHUB_API_URL, GITHUB_LOGIN, GITHUB_AUTH_TOKEN); + gitHub = gitHubConnectionService.connectToGitHubApi(); log.debug( "Repository Info: " + LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) @@ -170,12 +116,7 @@ private GHPullRequest getPullRequest(LPVSQueue webhookConfig, GHRepository repos public void setPendingCheck(LPVSQueue webhookConfig) { try { - if (GITHUB_AUTH_TOKEN.isEmpty()) setGithubTokenFromEnv(); - if (GITHUB_API_URL.isEmpty()) gitHub = GitHub.connect(GITHUB_LOGIN, GITHUB_AUTH_TOKEN); - else - gitHub = - GitHub.connectToEnterpriseWithOAuth( - GITHUB_API_URL, GITHUB_LOGIN, GITHUB_AUTH_TOKEN); + gitHub = gitHubConnectionService.connectToGitHubApi(); GHRepository repository = gitHub.getRepository( LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) @@ -194,12 +135,7 @@ public void setPendingCheck(LPVSQueue webhookConfig) { public void setErrorCheck(LPVSQueue webhookConfig) { try { - if (GITHUB_AUTH_TOKEN.isEmpty()) setGithubTokenFromEnv(); - if (GITHUB_API_URL.isEmpty()) gitHub = GitHub.connect(GITHUB_LOGIN, GITHUB_AUTH_TOKEN); - else - gitHub = - GitHub.connectToEnterpriseWithOAuth( - GITHUB_API_URL, GITHUB_LOGIN, GITHUB_AUTH_TOKEN); + gitHub = gitHubConnectionService.connectToGitHubApi(); GHRepository repository = gitHub.getRepository( LPVSWebhookUtil.getRepositoryOrganization(webhookConfig) @@ -395,12 +331,7 @@ public String getRepositoryLicense(LPVSQueue webhookConfig) { String repositoryName = LPVSWebhookUtil.getRepositoryName(webhookConfig); String repositoryOrganization = LPVSWebhookUtil.getRepositoryOrganization(webhookConfig); - if (GITHUB_AUTH_TOKEN.isEmpty()) setGithubTokenFromEnv(); - if (GITHUB_API_URL.isEmpty()) gitHub = GitHub.connect(GITHUB_LOGIN, GITHUB_AUTH_TOKEN); - else - gitHub = - GitHub.connectToEnterpriseWithOAuth( - GITHUB_API_URL, GITHUB_LOGIN, GITHUB_AUTH_TOKEN); + gitHub = gitHubConnectionService.connectToGitHubApi(); GHRepository repository = gitHub.getRepository(repositoryOrganization + "/" + repositoryName); GHLicense license = repository.getLicense(); @@ -414,9 +345,4 @@ public String getRepositoryLicense(LPVSQueue webhookConfig) { } return "Proprietary"; } - - public void setGithubTokenFromEnv() { - if (System.getenv("LPVS_GITHUB_TOKEN") != null) - GITHUB_AUTH_TOKEN = System.getenv("LPVS_GITHUB_TOKEN"); - } } diff --git a/src/main/java/com/lpvs/service/LPVSQueueProcessorService.java b/src/main/java/com/lpvs/service/LPVSQueueProcessorService.java index 586d6c73..c05b1282 100644 --- a/src/main/java/com/lpvs/service/LPVSQueueProcessorService.java +++ b/src/main/java/com/lpvs/service/LPVSQueueProcessorService.java @@ -9,6 +9,7 @@ import com.lpvs.entity.LPVSQueue; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; @@ -21,6 +22,9 @@ public class LPVSQueueProcessorService { private LPVSQueueService queueService; + @Value("${github.pull.request:}") + private String trigger; + @Autowired LPVSQueueProcessorService(LPVSQueueService queueService) { this.queueService = queueService; @@ -29,7 +33,7 @@ public class LPVSQueueProcessorService { @EventListener(ApplicationReadyEvent.class) private void queueProcessor() throws Exception { queueService.checkForQueue(); - while (true) { + while (trigger == null || trigger.equals("")) { LPVSQueue webhookConfig = queueService.getQueueFirstElement(); log.info("PROCESS Webhook id = " + webhookConfig.getId()); webhookConfig.setDate(new Date()); diff --git a/src/main/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectService.java b/src/main/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectService.java index f5995fbe..e7b305c9 100644 --- a/src/main/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectService.java +++ b/src/main/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectService.java @@ -12,7 +12,6 @@ import com.lpvs.entity.LPVSLicense; import com.lpvs.entity.LPVSQueue; import com.lpvs.repository.LPVSLicenseRepository; -import com.lpvs.service.LPVSGitHubService; import com.lpvs.service.LPVSLicenseService; import com.lpvs.util.LPVSWebhookUtil; @@ -35,8 +34,6 @@ public class LPVSScanossDetectService { @Autowired private LPVSLicenseService licenseService; - @Autowired private LPVSGitHubService gitHubService; - @Autowired private LPVSLicenseRepository lpvsLicenseRepository; private Boolean debug; @@ -45,11 +42,9 @@ public class LPVSScanossDetectService { public LPVSScanossDetectService( @Value("${debug:false}") Boolean debug, LPVSLicenseService licenseService, - LPVSGitHubService gitHubService, LPVSLicenseRepository lpvsLicenseRepository) { this.debug = debug; this.licenseService = licenseService; - this.gitHubService = gitHubService; this.lpvsLicenseRepository = lpvsLicenseRepository; } @@ -93,19 +88,19 @@ public void runScan(LPVSQueue webhookConfig, String path) throws Exception { int status = process.waitFor(); if (status == 1) { - log.error("Scanoss scanner terminated with none-zero code. Terminating."); + log.error("Scanoss scanner terminated with non-zero code. Terminating."); BufferedReader output = null; try { output = createBufferReader(createInputStreamReader(process)); log.error(output.readLine()); throw new Exception( - "Scanoss scanner terminated with none-zero code. Terminating."); + "Scanoss scanner terminated with non-zero code. Terminating."); } finally { if (output != null) output.close(); } } } catch (IOException | InterruptedException ex) { - log.error("Scanoss scanner terminated with none-zero code. Terminating."); + log.error("Scanoss scanner terminated with non-zero code. Terminating."); throw ex; } diff --git a/src/main/resources/application-singlescan.properties b/src/main/resources/application-singlescan.properties new file mode 100644 index 00000000..56def825 --- /dev/null +++ b/src/main/resources/application-singlescan.properties @@ -0,0 +1,7 @@ +# DB Configuration +# Whether to enable logging of SQL statements. +spring.jpa.show-sql=true + +spring.datasource.url=jdbc:h2:mem:test;DATABASE_TO_UPPER=false;MODE=MySQL;NON_KEYWORDS=USER +spring.datasource.username= +spring.datasource.password= diff --git a/src/main/resources/data-h2.sql b/src/main/resources/data-h2.sql new file mode 100644 index 00000000..72d42aab --- /dev/null +++ b/src/main/resources/data-h2.sql @@ -0,0 +1 @@ +INSERT INTO licenses (id, license_alternative_names, license_name, license_spdx, license_usage) VALUES (531, NULL, "MIT License", "MIT", "PERMITTED"), (529, NULL, "Apache License 2.0", "Apache-2.0", "PERMITTED"), (257, NULL, "GNU General Public License v2.0 only", "GPL-2.0-only", "RESTRICTED"), (289, NULL, "GNU Lesser General Public License v3.0 or later", "LGPL-3.0-or-later", "PROHIBITED"); \ No newline at end of file diff --git a/src/main/resources/schema-h2.sql b/src/main/resources/schema-h2.sql new file mode 100644 index 00000000..1ceea440 --- /dev/null +++ b/src/main/resources/schema-h2.sql @@ -0,0 +1,86 @@ +CREATE SCHEMA IF NOT EXISTS lpvs; +USE lpvs; + +DROP TABLE IF EXISTS licenses; +DROP TABLE IF EXISTS license_conflicts; +DROP TABLE IF EXISTS pull_requests; +DROP TABLE IF EXISTS detected_license; +DROP TABLE IF EXISTS queue; +DROP TABLE IF EXISTS member; + +CREATE TABLE licenses ( + id bigint NOT NULL AUTO_INCREMENT, + license_usage varchar DEFAULT NULL, + license_name varchar NOT NULL, + license_spdx varchar NOT NULL, + license_alternative_names longtext DEFAULT NULL, + PRIMARY KEY (id) +); + +CREATE TABLE license_conflicts ( + id bigint NOT NULL AUTO_INCREMENT, + conflict_license_id bigint NOT NULL, + repository_license_id bigint NOT NULL, + PRIMARY KEY (id) +); + +CREATE TABLE pull_requests ( + id bigint NOT NULL AUTO_INCREMENT, + scan_date datetime NOT NULL, + user varchar DEFAULT NULL, + repository_name varchar NOT NULL, + url longtext NOT NULL, + diff_url longtext, + status varchar DEFAULT NULL, + pull_request_head varchar NOT NULL, + pull_request_base varchar NOT NULL, + sender varchar NOT NULL, + PRIMARY KEY (id) +); + +CREATE TABLE detected_license ( + id bigint NOT NULL AUTO_INCREMENT, + pull_request_id bigint DEFAULT NULL, + license_id bigint DEFAULT NULL, + conflict_id bigint DEFAULT NULL, + repository_license_id bigint DEFAULT NULL, + file_path longtext, + match_type varchar DEFAULT NULL, + match_value varchar DEFAULT NULL, + match_lines varchar DEFAULT NULL, + component_file_path longtext, + component_file_url longtext, + component_name varchar DEFAULT NULL, + component_lines varchar DEFAULT NULL, + component_url longtext, + component_version varchar DEFAULT NULL, + component_vendor varchar DEFAULT NULL, + issue bit DEFAULT NULL, + PRIMARY KEY (id) +); + +CREATE TABLE queue ( + id bigint NOT NULL AUTO_INCREMENT, + action bigint NOT NULL, + attempts int DEFAULT '0', + scan_date datetime DEFAULT NULL, + user_id varchar DEFAULT NULL, + review_system_type varchar DEFAULT NULL, + repository_url longtext, + pull_request_url longtext, + pull_request_api_url longtext, + pull_request_diff_url longtext, + status_callback_url longtext, + commit_sha varchar DEFAULT NULL, + PRIMARY KEY (id) +); + +CREATE TABLE member ( + id bigint PRIMARY KEY NOT NULL AUTO_INCREMENT, + email varchar NOT NULL, + name varchar NOT NULL, + nickname varchar DEFAULT NULL, + provider varchar NOT NULL, + organization varchar DEFAULT NULL, + UNIQUE (email,provider) +); diff --git a/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java b/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java index 66ac6f58..a6dc197c 100644 --- a/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java +++ b/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java @@ -9,26 +9,66 @@ import com.lpvs.entity.LPVSFile; import com.lpvs.entity.LPVSQueue; import com.lpvs.service.scanner.scanoss.LPVSScanossDetectService; + import lombok.extern.slf4j.Slf4j; + 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.kohsuke.github.GHCommitPointer; +import org.kohsuke.github.GHPullRequest; +import org.kohsuke.github.GHRepository; +import org.kohsuke.github.GitHub; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationEventPublisher; +import java.io.IOException; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.net.URL; import java.util.ArrayList; import java.util.List; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; @Slf4j +@ExtendWith(MockitoExtension.class) public class LPVSDetectServiceTest { + @Mock private ApplicationEventPublisher mockEventPublisher; + + @Mock private LPVSGitHubConnectionService gitHubConnectionService; + + @Mock private GitHub gitHub; + + @Mock private GHRepository ghRepository; + + @Mock private GHPullRequest ghPullRequest; + + @Mock private LPVSScanossDetectService scanossDetectService; + + @Mock private ApplicationContext applicationContext; + + @Mock private ApplicationReadyEvent applicationReadyEvent; + + @InjectMocks private LPVSDetectService lpvsDetectService; + @Nested class TestInit { - final LPVSDetectService detectService = new LPVSDetectService("scanoss", null); + final LPVSDetectService detectService = new LPVSDetectService("scanoss", null, null); @Test public void testInit() { @@ -46,6 +86,7 @@ public void testInit() { @Nested class TestRunScan__Scanoss { LPVSDetectService detectService; + LPVSGitHubConnectionService github_mock = mock(LPVSGitHubConnectionService.class); LPVSScanossDetectService scanoss_mock = mock(LPVSScanossDetectService.class); LPVSQueue webhookConfig; final String test_path = "test_path"; @@ -54,7 +95,7 @@ class TestRunScan__Scanoss { @BeforeEach void setUp() { - detectService = new LPVSDetectService("scanoss", scanoss_mock); + detectService = new LPVSDetectService("scanoss", github_mock, scanoss_mock); webhookConfig = new LPVSQueue(); webhookConfig.setId(1L); @@ -72,6 +113,88 @@ void setUp() { .thenReturn(List.of(lpvs_file_1, lpvs_file_2)); } + @Test + void testRunOneScan_Default() throws NoSuchFieldException, IllegalAccessException { + + lpvsDetectService = spy(new LPVSDetectService("scanoss", null, scanossDetectService)); + + setPrivateField(lpvsDetectService, "trigger", "fake-trigger-value"); + setPrivateField(lpvsDetectService, "eventPublisher", mockEventPublisher); + doNothing().when(mockEventPublisher).publishEvent(any()); + + assertDoesNotThrow(() -> lpvsDetectService.runOneScan()); + } + + @Test + void testRunOneScan_trigerInternalQueueException() + throws NoSuchFieldException, IllegalAccessException { + + setPrivateField(lpvsDetectService, "trigger", "fake-trigger-value"); + setPrivateField(lpvsDetectService, "eventPublisher", mockEventPublisher); + doNothing().when(mockEventPublisher).publishEvent(any()); + + assertDoesNotThrow(() -> lpvsDetectService.runOneScan()); + } + + @Test + void testRunOneScan_TriggerNotNull() throws Exception { + + // Arrange + GitHub mockGitHub = mock(GitHub.class); + GHCommitPointer mockCommitPointer = mock(GHCommitPointer.class); + when(gitHubConnectionService.connectToGitHubApi()).thenReturn(mockGitHub); + + setPrivateField(lpvsDetectService, "trigger", "github/owner/repo/branch/123"); + setPrivateField(lpvsDetectService, "scannerType", "scanoss"); + setPrivateField(lpvsDetectService, "eventPublisher", mockEventPublisher); + + // Mock the necessary GitHub objects for LPVSQueue + GHRepository mockRepository = mock(GHRepository.class); + GHPullRequest mockPullRequest = mock(GHPullRequest.class); + when(mockGitHub.getRepository(any())).thenReturn(mockRepository); + when(mockRepository.getPullRequest(anyInt())).thenReturn(mockPullRequest); + when(mockRepository.getPullRequest(anyInt())).thenReturn(mockPullRequest); + when(mockPullRequest.getHead()).thenReturn(mockCommitPointer); + + GHRepository mockHeadRepository = mock(GHRepository.class); + when(mockCommitPointer.getRepository()).thenReturn(mockHeadRepository); + when(mockHeadRepository.getHtmlUrl()) + .thenReturn(new URL("https://example.com/repo/files")); + + // Set up expected values + String expectedPullRequestUrl = "https://example.com/pull/1"; + when(mockPullRequest.getHtmlUrl()).thenReturn(new URL(expectedPullRequestUrl)); + + doNothing().when(scanossDetectService).runScan(any(), anyString()); + + lpvsDetectService.runOneScan(); + + assertDoesNotThrow(() -> lpvsDetectService.runOneScan()); + } + + @Test + void testGetInternalQueueByPullRequest() throws IOException { + String pullRequest = "github/owner/repo/branch/123"; + when(gitHubConnectionService.connectToGitHubApi()).thenReturn(gitHub); + when(gitHub.getRepository("owner/repo")).thenReturn(ghRepository); + when(ghRepository.getPullRequest(123)).thenReturn(ghPullRequest); + + LPVSQueue result = lpvsDetectService.getInternalQueueByPullRequest(pullRequest); + + assertNotNull(result); + assertEquals(result.getUserId(), "Single scan run"); + } + + @Test + public void testGetPathByPullRequest() { + + LPVSQueue mockWebhookConfig = mock(LPVSQueue.class); + + String result = LPVSDetectService.getPathByPullRequest(mockWebhookConfig); + + assertNotNull(result); + } + @Test public void testRunScan__Scanoss() { try { @@ -99,6 +222,7 @@ public void testRunScan__Scanoss() { @Nested class TestRunScan__ScanossException { LPVSDetectService detectService; + LPVSGitHubConnectionService github_mock = mock(LPVSGitHubConnectionService.class); LPVSScanossDetectService scanoss_mock = mock(LPVSScanossDetectService.class); LPVSQueue webhookConfig; final String test_path = "test_path"; @@ -106,7 +230,7 @@ class TestRunScan__ScanossException { @BeforeEach void setUp() { - detectService = new LPVSDetectService("scanoss", scanoss_mock); + detectService = new LPVSDetectService("scanoss", github_mock, scanoss_mock); webhookConfig = new LPVSQueue(); webhookConfig.setId(1L); @@ -151,7 +275,7 @@ class TestRunScan__NotScanoss { @BeforeEach void setUp() { - detectService = new LPVSDetectService("not_scanoss", null); + detectService = new LPVSDetectService("not_scanoss", null, null); } @Test @@ -165,4 +289,12 @@ public void testRunScan__NotScanoss() { } } } + + private void setPrivateField(Object target, String fieldName, Object value) + throws NoSuchFieldException, IllegalAccessException { + Field field = target.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + field.set(target, value); + field.setAccessible(false); + } } diff --git a/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java b/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java index 69298608..1f62098b 100644 --- a/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java +++ b/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java @@ -159,14 +159,12 @@ class TestGetPullRequestFiles__ApiUrlAbsentPullPresentNoRescan { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -294,14 +292,12 @@ class TestGetPullRequestFiles__ApiUrlPresentPullPresentNoRescan { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -430,7 +426,7 @@ public void testGetPullRequestFiles__ApiUrlPresentPullPresentNoRescan() { @Nested class TestGetPullRequestFiles__ApiUrlPresentPullPresentNoRescanEmptyAuthToken { final String GH_LOGIN = "test_login"; - final String GH_AUTH_TOKEN = ""; + final String GH_AUTH_TOKEN = "test_auth_token"; final String GH_API_URL = "test_api_url"; LPVSPullRequestRepository mocked_pullRequestRepository = mock(LPVSPullRequestRepository.class); @@ -439,16 +435,16 @@ class TestGetPullRequestFiles__ApiUrlPresentPullPresentNoRescanEmptyAuthToken { LPVSLicenseRepository mocked_lpvsLicenseRepository = mock(LPVSLicenseRepository.class); LPVSLicenseConflictRepository mocked_lpvsLicenseConflictRepository = mock(LPVSLicenseConflictRepository.class); + LPVSGitHubConnectionService lpvsGitHubConnectionService = + new LPVSGitHubConnectionService(GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler); + final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + lpvsGitHubConnectionService); LPVSQueue webhookConfig; GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -588,14 +584,12 @@ class TestGetPullRequestFiles__ApiUrlAbsentPullAbsentNoRescan { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -698,14 +692,12 @@ class TestGetPullRequestFiles__ApiUrlPresentPullAbsentNoRescan { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -815,14 +807,12 @@ class TestGetPullRequestFiles__ApiUrlAbsentPullExceptionNoRescan { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -914,14 +904,12 @@ class TestGetPullRequestFiles__ApiUrlPresentPullExceptionNoRescan { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -1019,14 +1007,12 @@ class TestGetPullRequestFiles__ApiUrlPresentPullPresentRescanPresent { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -1171,14 +1157,12 @@ class TestSetPendingCheck__ApiUrlAbsentNormalExecution { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); LPVSQueue webhookConfig; @@ -1280,14 +1264,12 @@ class TestSetPendingCheck__ApiUrlPresentNormalExecution { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); LPVSQueue webhookConfig; @@ -1396,14 +1378,12 @@ class TestSetPendingCheck__ApiUrlAbsentCantAuthorize { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; @BeforeEach @@ -1445,14 +1425,12 @@ class TestSetPendingCheck__ApiUrlPresentCantAuthorize { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; @BeforeEach @@ -1501,14 +1479,12 @@ class TestSetErrorCheck__ApiUrlAbsentNormalExecution { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); LPVSQueue webhookConfig; @@ -1610,14 +1586,12 @@ class TestSetErrorCheck__ApiUrlPresentNormalExecution { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); LPVSQueue webhookConfig; @@ -1726,14 +1700,12 @@ class TestSetErrorCheck__ApiUrlAbsentCantAuthorize { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; @BeforeEach @@ -1775,14 +1747,12 @@ class TestSetErrorCheck__ApiUrlPresentCantAuthorize { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; @BeforeEach @@ -1828,14 +1798,11 @@ class TestCommentResults__PrAbsent { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - null, - null, - null, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + null); GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); GHPullRequest mocked_pr_1; @@ -1927,14 +1894,11 @@ class TestCommentResults__CantAuthorize { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - null, - null, - null, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + null); GitHub mocked_instance_gh = mock(GitHub.class); LPVSQueue webhookConfig; LPVSPullRequest lpvsPullRequest; @@ -2008,14 +1972,11 @@ class TestCommentResults__ScanResultsEmpty { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - null, - null, - null, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + null); GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); GHPullRequest mocked_pr_1; @@ -2123,14 +2084,12 @@ class TestCommentResults__ProhibitedPresentConflictsPresent { final String GH_API_URL = ""; final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -2446,20 +2405,19 @@ class TestCommentResults__EmptyPresentConflictsPresent { LPVSLicenseRepository mocked_lpvsLicenseRepository = mock(LPVSLicenseRepository.class); LPVSLicenseConflictRepository mocked_lpvsLicenseConflictRepository = mock(LPVSLicenseConflictRepository.class); + LPVSGitHubConnectionService lpvsGitHubConnectionService = + mock(LPVSGitHubConnectionService.class); final String GH_LOGIN = "test_login"; final String GH_AUTH_TOKEN = "test_auth_token"; final String GH_API_URL = ""; final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + lpvsGitHubConnectionService); GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -2773,20 +2731,19 @@ class TestCommentResults__UnreviewedPresentConflictsPresent { LPVSLicenseRepository mocked_lpvsLicenseRepository = mock(LPVSLicenseRepository.class); LPVSLicenseConflictRepository mocked_lpvsLicenseConflictRepository = mock(LPVSLicenseConflictRepository.class); + LPVSGitHubConnectionService lpvsGitHubConnectionService = + mock(LPVSGitHubConnectionService.class); final String GH_LOGIN = "test_login"; final String GH_AUTH_TOKEN = "test_auth_token"; final String GH_API_URL = ""; final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + lpvsGitHubConnectionService); GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -3103,20 +3060,19 @@ class TestCommentResults__RestrictedPresentConflictsPresent { LPVSLicenseRepository mocked_lpvsLicenseRepository = mock(LPVSLicenseRepository.class); LPVSLicenseConflictRepository mocked_lpvsLicenseConflictRepository = mock(LPVSLicenseConflictRepository.class); + LPVSGitHubConnectionService lpvsGitHubConnectionService = + mock(LPVSGitHubConnectionService.class); final String GH_LOGIN = "test_login"; final String GH_AUTH_TOKEN = "test_auth_token"; final String GH_API_URL = ""; final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + lpvsGitHubConnectionService); GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -3435,14 +3391,11 @@ class TestCommentResults__ProhibitedAbsentConflictsAbsent { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - null, - null, - null, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + null); GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); GHPullRequest mocked_pr_1; @@ -3626,14 +3579,12 @@ class TestGetRepositoryLicense__ApiUrlAbsentLisencePresent { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -3721,14 +3672,12 @@ class TestGetRepositoryLicense__ApiUrlAbsentLisenceAbsent { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -3816,14 +3765,12 @@ class TestGetRepositoryLicense__ApiUrlPresentLisencePresent { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -3918,14 +3865,12 @@ class TestGetRepositoryLicense__ApiUrlPresentLisenceAbsent { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); LPVSQueue webhookConfig; GitHub mocked_instance_gh = mock(GitHub.class); GHRepository mocked_repo = mock(GHRepository.class); @@ -4020,14 +3965,12 @@ class TestGetRepositoryLicense__ApiUrlAbsentCantAuthorize { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); final String url_pr_1 = "https://github.com/Samsung/LPVS/pull/18"; LPVSQueue webhookConfig; @@ -4071,14 +4014,12 @@ class TestGetRepositoryLicense__ApiUrlPresentCantAuthorize { mock(LPVSLicenseConflictRepository.class); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler)); final String url_pr_1 = "https://github.com/Samsung/LPVS/pull/18"; LPVSQueue webhookConfig; @@ -4217,7 +4158,7 @@ public void testGetMatchedLinesAsLink_All() { class TestCommentResults { final String GH_LOGIN = "test_login"; - final String GH_AUTH_TOKEN = "test_auth_token"; + final String GH_AUTH_TOKEN = "my-token"; final String GH_API_URL = "test_api_url"; LPVSPullRequestRepository mocked_pullRequestRepository = mock(LPVSPullRequestRepository.class); @@ -4226,16 +4167,15 @@ class TestCommentResults { LPVSLicenseRepository mocked_lpvsLicenseRepository = mock(LPVSLicenseRepository.class); LPVSLicenseConflictRepository mocked_lpvsLicenseConflictRepository = mock(LPVSLicenseConflictRepository.class); + LPVSGitHubConnectionService lpvsGitHubConnectionService = + new LPVSGitHubConnectionService(GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler); final LPVSGitHubService gh_service = new LPVSGitHubService( - GH_LOGIN, - GH_AUTH_TOKEN, - GH_API_URL, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); + lpvsGitHubConnectionService); final String url_pr_1 = "https://github.com/Samsung/LPVS/pull/18"; LPVSQueue webhookConfig; LPVSPullRequest lpvsPullRequest; @@ -4313,20 +4253,22 @@ public void testCommentResults() throws IOException { public void testSetGithubTokenFromEnv_WhenEnvVariableIsSet() throws IllegalAccessException, NoSuchFieldException { String githubTokenValue = "GitHubTokenValue"; - gh_service.setGithubTokenFromEnv(); - Field field = gh_service.getClass().getDeclaredField("GITHUB_AUTH_TOKEN"); + lpvsGitHubConnectionService.setGithubTokenFromEnv(); + Field field = + lpvsGitHubConnectionService.getClass().getDeclaredField("GITHUB_AUTH_TOKEN"); field.setAccessible(true); - String githubRealTokenValue = (String) field.get(gh_service); + String githubRealTokenValue = (String) field.get(lpvsGitHubConnectionService); assertEquals(githubTokenValue, githubRealTokenValue); } @Test public void testSetGithubTokenFromEnv_WhenEnvVariableIsNotSet() throws NoSuchFieldException, IllegalAccessException { - gh_service.setGithubTokenFromEnv(); - Field field = gh_service.getClass().getDeclaredField("GITHUB_AUTH_TOKEN"); + lpvsGitHubConnectionService.setGithubTokenFromEnv(); + Field field = + lpvsGitHubConnectionService.getClass().getDeclaredField("GITHUB_AUTH_TOKEN"); field.setAccessible(true); - String githubRealTokenValue = (String) field.get(gh_service); + String githubRealTokenValue = (String) field.get(lpvsGitHubConnectionService); assertEquals(GH_AUTH_TOKEN, githubRealTokenValue); } } @@ -4340,6 +4282,7 @@ public void setUp() { } @Test + @SetEnvironmentVariable(key = "LPVS_GITHUB_TOKEN", value = "") public void testCheckEmpty() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { LPVSPullRequestRepository mocked_pullRequestRepository = @@ -4350,20 +4293,19 @@ public void testCheckEmpty() LPVSLicenseConflictRepository mocked_lpvsLicenseConflictRepository = mock(LPVSLicenseConflictRepository.class); LPVSExitHandler exitHandler = mock(LPVSExitHandler.class); + LPVSGitHubConnectionService lpvsGitHubConnectionService = + new LPVSGitHubConnectionService("", "", "", exitHandler); final LPVSGitHubService gh_service = new LPVSGitHubService( - null, - "", - null, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); - Method method = gh_service.getClass().getDeclaredMethod("checks"); + lpvsGitHubConnectionService); + Method method = lpvsGitHubConnectionService.getClass().getDeclaredMethod("checks"); method.setAccessible(true); - method.invoke(gh_service); + method.invoke(lpvsGitHubConnectionService); verify(exitHandler).exit(-1); } @@ -4371,6 +4313,9 @@ public void testCheckEmpty() @Test public void testCheckNotEmpty() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + String GH_LOGIN = ""; + String GH_AUTH_TOKEN = "non-empty"; + String GH_API_URL = ""; LPVSPullRequestRepository mocked_pullRequestRepository = mock(LPVSPullRequestRepository.class); LPVSDetectedLicenseRepository mocked_lpvsDetectedLicenseRepository = @@ -4379,20 +4324,20 @@ public void testCheckNotEmpty() LPVSLicenseConflictRepository mocked_lpvsLicenseConflictRepository = mock(LPVSLicenseConflictRepository.class); LPVSExitHandler exitHandler = mock(LPVSExitHandler.class); + LPVSGitHubConnectionService lpvsGitHubConnectionService = + new LPVSGitHubConnectionService( + GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL, exitHandler); final LPVSGitHubService gh_service = new LPVSGitHubService( - null, - "some_token", - null, mocked_pullRequestRepository, mocked_lpvsDetectedLicenseRepository, mocked_lpvsLicenseRepository, mocked_lpvsLicenseConflictRepository, - exitHandler); - Method method = gh_service.getClass().getDeclaredMethod("checks"); + lpvsGitHubConnectionService); + Method method = lpvsGitHubConnectionService.getClass().getDeclaredMethod("checks"); method.setAccessible(true); - method.invoke(gh_service); + method.invoke(lpvsGitHubConnectionService); verify(exitHandler, never()).exit(anyInt()); } diff --git a/src/test/java/com/lpvs/service/LPVSLoginCheckServiceTest.java b/src/test/java/com/lpvs/service/LPVSLoginCheckServiceTest.java index b8de63ab..00c70a3d 100644 --- a/src/test/java/com/lpvs/service/LPVSLoginCheckServiceTest.java +++ b/src/test/java/com/lpvs/service/LPVSLoginCheckServiceTest.java @@ -9,6 +9,7 @@ import com.lpvs.entity.LPVSMember; import com.lpvs.entity.history.HistoryPageEntity; import com.lpvs.exception.LoginFailedException; +import com.lpvs.exception.WrongAccessException; import com.lpvs.repository.LPVSMemberRepository; import com.lpvs.repository.LPVSPullRequestRepository; import org.junit.jupiter.api.BeforeEach; @@ -104,7 +105,7 @@ public void testGetMemberFromMemberMap() { } @Test - public void testPathCheckOwnType() { + public void testPathCheckAllTypes() { LPVSMember member = new LPVSMember(); member.setNickname("testNickname"); Map attributes = new HashMap<>(); @@ -120,7 +121,15 @@ public void testPathCheckOwnType() { HistoryPageEntity result = loginCheckService.pathCheck("own", "testNickname", null, authentication); + assertNotNull(result); + result = loginCheckService.pathCheck("send", "testNickname", null, authentication); assertNotNull(result); + + assertThrows( + WrongAccessException.class, + () -> { + loginCheckService.pathCheck("test", "testNickname", null, authentication); + }); } } diff --git a/src/test/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectServiceTest.java b/src/test/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectServiceTest.java index df71b20c..ccc84571 100644 --- a/src/test/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectServiceTest.java +++ b/src/test/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectServiceTest.java @@ -71,8 +71,7 @@ public void setUp() throws URISyntaxException, IOException { } }); scanossDetectService = - new LPVSScanossDetectService( - false, licenseService, gitHubService, lpvsLicenseRepository); + new LPVSScanossDetectService(false, licenseService, lpvsLicenseRepository); } @AfterEach @@ -84,6 +83,10 @@ public void tearDown() { @Test public void testCheckLicense() { + LPVSLicenseService licenseService = Mockito.mock(LPVSLicenseService.class); + LPVSLicenseRepository lpvsLicenseRepository = Mockito.mock(LPVSLicenseRepository.class); + LPVSScanossDetectService scanossDetectService = + new LPVSScanossDetectService(false, licenseService, lpvsLicenseRepository); String licenseConflictsSource = "scanner"; Mockito.when(LPVSWebhookUtil.getRepositoryName(lpvsQueue)).thenReturn("C"); Mockito.when(lpvsQueue.getHeadCommitSHA()).thenReturn("A_B"); @@ -97,6 +100,10 @@ public void testCheckLicense() { @Test public void testCheckLicenseHeadCommitSHA() { + LPVSLicenseService licenseService = Mockito.mock(LPVSLicenseService.class); + LPVSLicenseRepository lpvsLicenseRepository = Mockito.mock(LPVSLicenseRepository.class); + LPVSScanossDetectService scanossDetectService = + new LPVSScanossDetectService(false, licenseService, lpvsLicenseRepository); String licenseConflictsSource = "scanner"; LPVSQueue webhookConfig = Mockito.mock(LPVSQueue.class); Mockito.when(LPVSWebhookUtil.getRepositoryName(webhookConfig)).thenReturn("C"); @@ -112,6 +119,10 @@ public void testCheckLicenseHeadCommitSHA() { @Test public void testWithNullHeadCommitSHA() { + LPVSLicenseService licenseService = Mockito.mock(LPVSLicenseService.class); + LPVSLicenseRepository lpvsLicenseRepository = Mockito.mock(LPVSLicenseRepository.class); + LPVSScanossDetectService scanossDetectService = + new LPVSScanossDetectService(false, licenseService, lpvsLicenseRepository); String licenseConflictsSource = "scanner"; LPVSQueue webhookConfig = Mockito.mock(LPVSQueue.class); Mockito.when(LPVSWebhookUtil.getRepositoryName(webhookConfig)).thenReturn("A"); @@ -156,7 +167,7 @@ public void testRunScan_StatusEqualsOne() throws Exception { // Verify that the method throws an exception when the status is 1 assertEquals( - "Scanoss scanner terminated with none-zero code. Terminating.", + "Scanoss scanner terminated with non-zero code. Terminating.", exception.getMessage()); verify(mockedPb.constructed().get(0)).start();