From 37ce449824d18c8233d9d413d924e60cebb1240d Mon Sep 17 00:00:00 2001 From: Oleg Kopysov Date: Tue, 26 Mar 2024 19:39:17 +0200 Subject: [PATCH 1/3] feat: Load service dynamically based on configuration property Signed-off-by: Oleg Kopysov --- .../com/lpvs/service/LPVSQueueService.java | 1 + .../service/{ => scan}/LPVSDetectService.java | 49 +++++++----------- .../lpvs/service/scan/LPVSScanService.java | 35 +++++++++++++ .../service/scan/LPVSScanServiceFactory.java | 50 +++++++++++++++++++ .../scanner}/LPVSScanossDetectService.java | 25 +++++----- .../scanner}/package-info.java | 4 +- .../lpvs/service/LPVSDetectServiceTest.java | 46 +++++++---------- .../lpvs/service/LPVSQueueServiceTest.java | 1 + .../LPVSScanossDetectServiceTest.java | 2 +- 9 files changed, 138 insertions(+), 75 deletions(-) rename src/main/java/com/lpvs/service/{ => scan}/LPVSDetectService.java (83%) create mode 100644 src/main/java/com/lpvs/service/scan/LPVSScanService.java create mode 100644 src/main/java/com/lpvs/service/scan/LPVSScanServiceFactory.java rename src/main/java/com/lpvs/service/{scanner/scanoss => scan/scanner}/LPVSScanossDetectService.java (96%) rename src/main/java/com/lpvs/service/{scanner/scanoss => scan/scanner}/package-info.java (88%) rename src/test/java/com/lpvs/service/{scanner/scanoss => scan/scanner}/LPVSScanossDetectServiceTest.java (99%) diff --git a/src/main/java/com/lpvs/service/LPVSQueueService.java b/src/main/java/com/lpvs/service/LPVSQueueService.java index 7da3fd32..58fa547c 100644 --- a/src/main/java/com/lpvs/service/LPVSQueueService.java +++ b/src/main/java/com/lpvs/service/LPVSQueueService.java @@ -12,6 +12,7 @@ import com.lpvs.entity.enums.LPVSPullRequestStatus; import com.lpvs.repository.LPVSPullRequestRepository; import com.lpvs.repository.LPVSQueueRepository; +import com.lpvs.service.scan.LPVSDetectService; import com.lpvs.util.LPVSWebhookUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; diff --git a/src/main/java/com/lpvs/service/LPVSDetectService.java b/src/main/java/com/lpvs/service/scan/LPVSDetectService.java similarity index 83% rename from src/main/java/com/lpvs/service/LPVSDetectService.java rename to src/main/java/com/lpvs/service/scan/LPVSDetectService.java index 119fc187..7b71783f 100644 --- a/src/main/java/com/lpvs/service/LPVSDetectService.java +++ b/src/main/java/com/lpvs/service/scan/LPVSDetectService.java @@ -4,14 +4,16 @@ * Use of this source code is governed by a MIT license that can be * found in the LICENSE file. */ -package com.lpvs.service; +package com.lpvs.service.scan; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; import java.util.List; +import com.lpvs.service.LPVSGitHubConnectionService; +import com.lpvs.service.LPVSGitHubService; +import com.lpvs.service.LPVSLicenseService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; @@ -23,10 +25,8 @@ 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 lombok.extern.slf4j.Slf4j; /** @@ -36,16 +36,6 @@ @Slf4j public class LPVSDetectService { - /** - * The type of license detection scanner. - */ - private String scannerType; - - /** - * Service responsible for performing license detection using the ScanOSS scanner. - */ - private LPVSScanossDetectService scanossDetectService; - /** * Service responsible for establishing and managing connections to the GitHub API. */ @@ -61,6 +51,11 @@ public class LPVSDetectService { */ private LPVSGitHubService gitHubService; + /** + * Service responsible for initialization of the scanner. + */ + private LPVSScanService scanService; + /** * GitHub pull request used to trigger a single license scan (optional). */ @@ -82,29 +77,24 @@ public class LPVSDetectService { * Constructs an instance of LPVSDetectService with the specified parameters. * * @param scannerType The type of license detection scanner. + * @param isInternal * @param gitHubConnectionService Service for connecting to the GitHub API. - * @param scanossDetectService Service for license detection using ScanOSS. * @param licenseService Service for license conflict analysis. * @param gitHubService Service for GitHub connection and operation. + * @param scanServiceFactory */ + @Autowired public LPVSDetectService( @Value("${scanner:scanoss}") String scannerType, + @Value("${internal:false}") boolean isInternal, LPVSGitHubConnectionService gitHubConnectionService, - LPVSScanossDetectService scanossDetectService, LPVSLicenseService licenseService, - LPVSGitHubService gitHubService) { - this.scannerType = scannerType; + LPVSGitHubService gitHubService, + LPVSScanServiceFactory scanServiceFactory) { this.gitHubConnectionService = gitHubConnectionService; - this.scanossDetectService = scanossDetectService; this.licenseService = licenseService; this.gitHubService = gitHubService; - } - - /** - * Initializes the LPVSDetectService bean and logs the selected license detection scanner. - */ - @PostConstruct - private void init() { + this.scanService = scanServiceFactory.createScanService(scannerType, isInternal); log.info("License detection scanner: " + scannerType); } @@ -166,10 +156,7 @@ public void runOneScan() { * @throws Exception if an error occurs during the scan. */ public List runScan(LPVSQueue webhookConfig, String path) throws Exception { - if (scannerType.equals("scanoss")) { - scanossDetectService.runScan(webhookConfig, path); - return scanossDetectService.checkLicenses(webhookConfig); - } - return new ArrayList<>(); + scanService.runScan(webhookConfig, path); + return scanService.checkLicenses(webhookConfig); } } diff --git a/src/main/java/com/lpvs/service/scan/LPVSScanService.java b/src/main/java/com/lpvs/service/scan/LPVSScanService.java new file mode 100644 index 00000000..8091c96a --- /dev/null +++ b/src/main/java/com/lpvs/service/scan/LPVSScanService.java @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2024, 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.scan; + +import com.lpvs.entity.LPVSFile; +import com.lpvs.entity.LPVSQueue; + +import java.util.List; + +/** + * Interface representing a service for scanning files and checking licenses. + */ +public interface LPVSScanService { + + /** + * Runs a scan on the specified path using the provided webhook configuration. + * + * @param webhookConfig The webhook configuration to use for the scan. + * @param path The path to the file or directory to scan. + * @throws Exception if an error occurs during the scan process. + */ + void runScan(LPVSQueue webhookConfig, String path) throws Exception; + + /** + * Checks licenses for files using the provided webhook configuration. + * + * @param webhookConfig The webhook configuration to use for checking licenses. + * @return A list of LPVSFile objects representing files with detected licenses. + */ + List checkLicenses(LPVSQueue webhookConfig); +} diff --git a/src/main/java/com/lpvs/service/scan/LPVSScanServiceFactory.java b/src/main/java/com/lpvs/service/scan/LPVSScanServiceFactory.java new file mode 100644 index 00000000..ca1d64f3 --- /dev/null +++ b/src/main/java/com/lpvs/service/scan/LPVSScanServiceFactory.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2024, 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.scan; + +import java.lang.reflect.Constructor; + +/** + * Factory class for creating instances of {@link LPVSScanService}. + */ +public class LPVSScanServiceFactory { + + /** + * Creates a scan service based on the specified scanner type and configuration. + * + * @param scannerType The type of scanner to create. + * @param isInternal Flag indicating whether the scanner is internal or not. + * @return An instance of {@link LPVSScanService} corresponding to the specified scanner type. + * @throws IllegalArgumentException if the specified scanner type is not supported or if an error occurs during + * the creation process. + */ + public LPVSScanService createScanService(String scannerType, boolean isInternal) { + try { + Class serviceClass = Class.forName(getServiceClassName(scannerType, isInternal)); + Constructor constructor = serviceClass.getDeclaredConstructor(); + return (LPVSScanService) constructor.newInstance(); + } catch (Exception e) { + throw new IllegalArgumentException("Error creating scan service for type: " + scannerType, e); + } + } + + /** + * Gets the fully qualified class name of the scan service based on the specified scanner type and configuration. + * + * @param scannerType The type of scanner. + * @param isInternal Flag indicating whether the scanner is internal or not. + * @return The fully qualified class name of the scan service. + * @throws IllegalArgumentException if the specified scanner type is null or empty string. + */ + private String getServiceClassName(String scannerType, boolean isInternal) { + if (scannerType != null && !scannerType.isEmpty()) { + return "com.lpvs." + (isInternal ? "internal." : "") + "service.scan.scanner.LPVS" + scannerType.substring(0, 1).toUpperCase() + scannerType.substring(1) + "DetectService"; + } else { + throw new IllegalArgumentException("Scanner type cannot be null or empty."); + } + } +} diff --git a/src/main/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectService.java b/src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java similarity index 96% rename from src/main/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectService.java rename to src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java index dfdbc2f1..2121d247 100644 --- a/src/main/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectService.java +++ b/src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java @@ -4,7 +4,7 @@ * Use of this source code is governed by a MIT license that can be * found in the LICENSE file. */ -package com.lpvs.service.scanner.scanoss; +package com.lpvs.service.scan.scanner; import com.google.gson.*; import com.google.gson.reflect.TypeToken; @@ -13,6 +13,7 @@ import com.lpvs.entity.LPVSQueue; import com.lpvs.repository.LPVSLicenseRepository; import com.lpvs.service.LPVSLicenseService; +import com.lpvs.service.scan.LPVSScanService; import com.lpvs.util.LPVSWebhookUtil; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -29,12 +30,12 @@ import static com.lpvs.util.LPVSFileUtil.getScanResultsJsonFilePath; /** - * Service class responsible for interacting with the Scanoss scanner to detect licenses in files. + * Service class responsible for interacting with the Scanoss scanner to scan licenses in files. * It handles the initiation of the scan, processing scan results, and checking for license conflicts. */ @Service @Slf4j -public class LPVSScanossDetectService { +public class LPVSScanossDetectService implements LPVSScanService { /** * The service for managing licenses, providing operations related to licenses. @@ -301,16 +302,16 @@ private class ScanossJsonStructure { private String matched; private String oss_lines; private ArrayList purl; - private String release_date; - private ScanossServer server; - private String source_hash; - private String status; - private String url; - private String url_hash; - private String vendor; - private String version; + private String release_date; + private ScanossServer server; + private String source_hash; + private String status; + private String url; + private String url_hash; + private String vendor; + private String version; - private class ScanossLicense { + private class ScanossLicense { private String checklist_url; private String copyleft; private ArrayList incompatible_with; diff --git a/src/main/java/com/lpvs/service/scanner/scanoss/package-info.java b/src/main/java/com/lpvs/service/scan/scanner/package-info.java similarity index 88% rename from src/main/java/com/lpvs/service/scanner/scanoss/package-info.java rename to src/main/java/com/lpvs/service/scan/scanner/package-info.java index e312517a..849e9e16 100644 --- a/src/main/java/com/lpvs/service/scanner/scanoss/package-info.java +++ b/src/main/java/com/lpvs/service/scan/scanner/package-info.java @@ -7,9 +7,9 @@ /** * The {@code scanoss} package contains services and utilities related to the Scanoss scanner integration. - * Scanoss is a tool used for scanning and analyzing open source software components to detect licenses, + * Scanoss is a tool used for scanning and analyzing open source software components to scan licenses, * vulnerabilities, and other relevant information. This package encapsulates functionality for running * Scanoss scans, processing scan results, and handling license-related operations. * The package also contains utility classes for handling file paths, reading scan results, and managing licenses. */ -package com.lpvs.service.scanner.scanoss; +package com.lpvs.service.scan.scanner; diff --git a/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java b/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java index d1196c58..ef814bbd 100644 --- a/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java +++ b/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java @@ -8,7 +8,9 @@ import com.lpvs.entity.LPVSFile; import com.lpvs.entity.LPVSQueue; -import com.lpvs.service.scanner.scanoss.LPVSScanossDetectService; +import com.lpvs.service.scan.LPVSDetectService; +import com.lpvs.service.scan.LPVSScanServiceFactory; +import com.lpvs.service.scan.scanner.LPVSScanossDetectService; import com.lpvs.util.LPVSCommentUtil; import com.lpvs.util.LPVSFileUtil; @@ -51,24 +53,6 @@ public class LPVSDetectServiceTest { @InjectMocks private LPVSDetectService lpvsDetectService; - @Nested - class TestInit { - final LPVSDetectService detectService = - new LPVSDetectService("scanoss", null, null, null, null); - - @Test - public void testInit() { - try { - Method init_method = detectService.getClass().getDeclaredMethod("init"); - init_method.setAccessible(true); - init_method.invoke(detectService); - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { - log.error("LPVSDetectServiceTest::TestInit exception: " + e); - fail(); - } - } - } - @Nested class TestRunScan__Scanoss { LPVSDetectService detectService; @@ -76,6 +60,7 @@ class TestRunScan__Scanoss { LPVSScanossDetectService scanoss_mock = mock(LPVSScanossDetectService.class); LPVSLicenseService licenseservice_mock = mock(LPVSLicenseService.class); LPVSGitHubService githubservice_mock = mock(LPVSGitHubService.class); + LPVSScanServiceFactory scanServiceFactory_mock = mock(LPVSScanServiceFactory.class); GitHub mockGitHub = mock(GitHub.class); GHCommitPointer mockCommitPointer = mock(GHCommitPointer.class); GHRepository mockRepository = mock(GHRepository.class); @@ -93,10 +78,11 @@ void setUp() throws IOException { detectService = new LPVSDetectService( "scanoss", + false, github_mock, - scanoss_mock, licenseservice_mock, - githubservice_mock); + githubservice_mock, + scanServiceFactory_mock); webhookConfig = new LPVSQueue(); webhookConfig.setId(1L); @@ -117,9 +103,9 @@ void setUp() throws IOException { } @Test - void testRunOneScanWithNullTriger() throws NoSuchFieldException, IllegalAccessException { + void testRunOneScanWithNullTrigger() throws NoSuchFieldException, IllegalAccessException { lpvsDetectService = - spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null)); + spy(new LPVSDetectService("scanoss", false,null, null, null, null)); setPrivateField(lpvsDetectService, "trigger", null); @@ -134,7 +120,7 @@ void testRunOneScanWithNullTriger() throws NoSuchFieldException, IllegalAccessEx void testRunOneScan_Default() throws NoSuchFieldException, IllegalAccessException { lpvsDetectService = - spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null)); + spy(new LPVSDetectService("scanoss", false, null, null, null, null)); setPrivateField(lpvsDetectService, "trigger", "fake-trigger-value"); setPrivateField(lpvsDetectService, "ctx", mockApplicationContext); @@ -152,7 +138,7 @@ void testRunOneScan_Branch2() List.of(conflict_1, conflict_1); lpvsDetectService = - spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null)); + spy(new LPVSDetectService("scanoss", false, null, null, null, null)); // Mock the necessary GitHub objects for LPVSQueue when(mockGitHub.getRepository(any())).thenReturn(mockRepository); @@ -190,7 +176,7 @@ void testRunOneScan_Branch3() List.of(conflict_1, conflict_1); lpvsDetectService = - spy(new LPVSDetectService("scanoss", null, scanossDetectService, null, null)); + spy(new LPVSDetectService("scanoss", false, null, null, null, null)); setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); setPrivateField(detectService, "scannerType", "scanoss"); @@ -392,6 +378,7 @@ class TestRunScan__ScanossException { LPVSScanossDetectService scanoss_mock = mock(LPVSScanossDetectService.class); LPVSLicenseService licenseservice_mock = mock(LPVSLicenseService.class); LPVSGitHubService githubservice_mock = mock(LPVSGitHubService.class); + LPVSScanServiceFactory scanServiceFactory_mock = mock(LPVSScanServiceFactory.class); LPVSQueue webhookConfig; final String test_path = "test_path"; @@ -402,10 +389,11 @@ void setUp() { detectService = new LPVSDetectService( "scanoss", + false, github_mock, - scanoss_mock, licenseservice_mock, - githubservice_mock); + githubservice_mock, + scanServiceFactory_mock); webhookConfig = new LPVSQueue(); webhookConfig.setId(1L); @@ -450,7 +438,7 @@ class TestRunScan__NotScanoss { @BeforeEach void setUp() { - detectService = new LPVSDetectService("not_scanoss", null, null, null, null); + detectService = new LPVSDetectService("not_scanoss", false, null, null, null, null); } @Test diff --git a/src/test/java/com/lpvs/service/LPVSQueueServiceTest.java b/src/test/java/com/lpvs/service/LPVSQueueServiceTest.java index 39be1a0a..e71ea211 100644 --- a/src/test/java/com/lpvs/service/LPVSQueueServiceTest.java +++ b/src/test/java/com/lpvs/service/LPVSQueueServiceTest.java @@ -14,6 +14,7 @@ import com.lpvs.entity.enums.LPVSPullRequestStatus; import com.lpvs.repository.LPVSPullRequestRepository; import com.lpvs.repository.LPVSQueueRepository; +import com.lpvs.service.scan.LPVSDetectService; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.AfterEach; diff --git a/src/test/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectServiceTest.java b/src/test/java/com/lpvs/service/scan/scanner/LPVSScanossDetectServiceTest.java similarity index 99% rename from src/test/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectServiceTest.java rename to src/test/java/com/lpvs/service/scan/scanner/LPVSScanossDetectServiceTest.java index f097bea5..ede7cf9e 100644 --- a/src/test/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectServiceTest.java +++ b/src/test/java/com/lpvs/service/scan/scanner/LPVSScanossDetectServiceTest.java @@ -4,7 +4,7 @@ * Use of this source code is governed by a MIT license that can be * found in the LICENSE file. */ -package com.lpvs.service.scanner.scanoss; +package com.lpvs.service.scan.scanner; import com.lpvs.entity.LPVSLicense; import com.lpvs.entity.LPVSQueue; From 5f72a43d93312d074d825cfe0f066af92a5f4085 Mon Sep 17 00:00:00 2001 From: Oleg Kopysov Date: Wed, 27 Mar 2024 08:23:28 +0200 Subject: [PATCH 2/3] test: Add unit tests for new classes Signed-off-by: Oleg Kopysov --- .../lpvs/service/scan/LPVSDetectService.java | 10 ++- .../service/scan/LPVSScanServiceFactory.java | 42 +++++++++-- .../com/lpvs/service/scan/package-info.java | 15 ++++ .../scanner/LPVSScanossDetectService.java | 18 ++--- .../service/scan/scanner/package-info.java | 3 +- .../{ => scan}/LPVSDetectServiceTest.java | 51 +++++++------- .../scan/LPVSScanServiceFactoryTest.java | 70 +++++++++++++++++++ 7 files changed, 167 insertions(+), 42 deletions(-) create mode 100644 src/main/java/com/lpvs/service/scan/package-info.java rename src/test/java/com/lpvs/service/{ => scan}/LPVSDetectServiceTest.java (92%) create mode 100644 src/test/java/com/lpvs/service/scan/LPVSScanServiceFactoryTest.java diff --git a/src/main/java/com/lpvs/service/scan/LPVSDetectService.java b/src/main/java/com/lpvs/service/scan/LPVSDetectService.java index 7b71783f..a060fca3 100644 --- a/src/main/java/com/lpvs/service/scan/LPVSDetectService.java +++ b/src/main/java/com/lpvs/service/scan/LPVSDetectService.java @@ -9,6 +9,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; import java.util.List; import com.lpvs.service.LPVSGitHubConnectionService; @@ -156,7 +157,12 @@ public void runOneScan() { * @throws Exception if an error occurs during the scan. */ public List runScan(LPVSQueue webhookConfig, String path) throws Exception { - scanService.runScan(webhookConfig, path); - return scanService.checkLicenses(webhookConfig); + try { + scanService.runScan(webhookConfig, path); + return scanService.checkLicenses(webhookConfig); + } catch (IllegalArgumentException | NullPointerException ex) { + log.error(ex.getMessage()); + return new ArrayList<>(); + } } } diff --git a/src/main/java/com/lpvs/service/scan/LPVSScanServiceFactory.java b/src/main/java/com/lpvs/service/scan/LPVSScanServiceFactory.java index ca1d64f3..17f0bcac 100644 --- a/src/main/java/com/lpvs/service/scan/LPVSScanServiceFactory.java +++ b/src/main/java/com/lpvs/service/scan/LPVSScanServiceFactory.java @@ -6,13 +6,36 @@ */ package com.lpvs.service.scan; +import com.lpvs.repository.LPVSLicenseRepository; +import com.lpvs.service.LPVSLicenseService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + import java.lang.reflect.Constructor; /** * Factory class for creating instances of {@link LPVSScanService}. */ +@Service public class LPVSScanServiceFactory { + /** + * Flag indicating whether the application is in debug mode. + */ + @Value("${debug:false}") + Boolean debug; + + /** + * The service for managing licenses, providing operations related to licenses. + */ + @Autowired LPVSLicenseService licenseService; + + /** + * The repository for LPVSLicense entities, allowing database interactions for licenses. + */ + @Autowired LPVSLicenseRepository lpvsLicenseRepository; + /** * Creates a scan service based on the specified scanner type and configuration. * @@ -25,10 +48,14 @@ public class LPVSScanServiceFactory { public LPVSScanService createScanService(String scannerType, boolean isInternal) { try { Class serviceClass = Class.forName(getServiceClassName(scannerType, isInternal)); - Constructor constructor = serviceClass.getDeclaredConstructor(); - return (LPVSScanService) constructor.newInstance(); + Constructor constructor = + serviceClass.getDeclaredConstructor( + Boolean.class, LPVSLicenseService.class, LPVSLicenseRepository.class); + return (LPVSScanService) + constructor.newInstance(debug, licenseService, lpvsLicenseRepository); } catch (Exception e) { - throw new IllegalArgumentException("Error creating scan service for type: " + scannerType, e); + throw new IllegalArgumentException( + "Error creating scan service for type: " + scannerType, e); } } @@ -40,9 +67,14 @@ public LPVSScanService createScanService(String scannerType, boolean isInternal) * @return The fully qualified class name of the scan service. * @throws IllegalArgumentException if the specified scanner type is null or empty string. */ - private String getServiceClassName(String scannerType, boolean isInternal) { + protected String getServiceClassName(String scannerType, boolean isInternal) { if (scannerType != null && !scannerType.isEmpty()) { - return "com.lpvs." + (isInternal ? "internal." : "") + "service.scan.scanner.LPVS" + scannerType.substring(0, 1).toUpperCase() + scannerType.substring(1) + "DetectService"; + return "com.lpvs." + + (isInternal ? "internal." : "") + + "service.scan.scanner.LPVS" + + scannerType.substring(0, 1).toUpperCase() + + scannerType.substring(1) + + "DetectService"; } else { throw new IllegalArgumentException("Scanner type cannot be null or empty."); } diff --git a/src/main/java/com/lpvs/service/scan/package-info.java b/src/main/java/com/lpvs/service/scan/package-info.java new file mode 100644 index 00000000..a1aade38 --- /dev/null +++ b/src/main/java/com/lpvs/service/scan/package-info.java @@ -0,0 +1,15 @@ +/** + * Copyright (c) 2024, 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. + */ + +/** + * Provides services related to scanning operations. + *

+ * This package contains classes and interfaces that define services for performing scanning operations, + * such as checking licenses, and managing scanning configurations. + *

+ */ +package com.lpvs.service.scan; diff --git a/src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java b/src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java index 2121d247..c63b9346 100644 --- a/src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java +++ b/src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java @@ -302,16 +302,16 @@ private class ScanossJsonStructure { private String matched; private String oss_lines; private ArrayList purl; - private String release_date; - private ScanossServer server; - private String source_hash; - private String status; - private String url; - private String url_hash; - private String vendor; - private String version; + private String release_date; + private ScanossServer server; + private String source_hash; + private String status; + private String url; + private String url_hash; + private String vendor; + private String version; - private class ScanossLicense { + private class ScanossLicense { private String checklist_url; private String copyleft; private ArrayList incompatible_with; diff --git a/src/main/java/com/lpvs/service/scan/scanner/package-info.java b/src/main/java/com/lpvs/service/scan/scanner/package-info.java index 849e9e16..0a5f6c91 100644 --- a/src/main/java/com/lpvs/service/scan/scanner/package-info.java +++ b/src/main/java/com/lpvs/service/scan/scanner/package-info.java @@ -6,10 +6,9 @@ */ /** - * The {@code scanoss} package contains services and utilities related to the Scanoss scanner integration. + * The package contains service related to the Scanoss scanner integration. * Scanoss is a tool used for scanning and analyzing open source software components to scan licenses, * vulnerabilities, and other relevant information. This package encapsulates functionality for running * Scanoss scans, processing scan results, and handling license-related operations. - * The package also contains utility classes for handling file paths, reading scan results, and managing licenses. */ package com.lpvs.service.scan.scanner; diff --git a/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java b/src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java similarity index 92% rename from src/test/java/com/lpvs/service/LPVSDetectServiceTest.java rename to src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java index ef814bbd..092c7e92 100644 --- a/src/test/java/com/lpvs/service/LPVSDetectServiceTest.java +++ b/src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java @@ -4,12 +4,13 @@ * Use of this source code is governed by a MIT license that can be * found in the LICENSE file. */ -package com.lpvs.service; +package com.lpvs.service.scan; import com.lpvs.entity.LPVSFile; import com.lpvs.entity.LPVSQueue; -import com.lpvs.service.scan.LPVSDetectService; -import com.lpvs.service.scan.LPVSScanServiceFactory; +import com.lpvs.service.LPVSGitHubConnectionService; +import com.lpvs.service.LPVSGitHubService; +import com.lpvs.service.LPVSLicenseService; import com.lpvs.service.scan.scanner.LPVSScanossDetectService; import com.lpvs.util.LPVSCommentUtil; @@ -25,14 +26,10 @@ 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 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; @@ -49,9 +46,7 @@ @ExtendWith(MockitoExtension.class) public class LPVSDetectServiceTest { - @Mock private LPVSScanossDetectService scanossDetectService; - - @InjectMocks private LPVSDetectService lpvsDetectService; + private LPVSDetectService lpvsDetectService; @Nested class TestRunScan__Scanoss { @@ -105,7 +100,9 @@ void setUp() throws IOException { @Test void testRunOneScanWithNullTrigger() throws NoSuchFieldException, IllegalAccessException { lpvsDetectService = - spy(new LPVSDetectService("scanoss", false,null, null, null, null)); + spy( + new LPVSDetectService( + "scanoss", false, null, null, null, scanServiceFactory_mock)); setPrivateField(lpvsDetectService, "trigger", null); @@ -120,7 +117,9 @@ void testRunOneScanWithNullTrigger() throws NoSuchFieldException, IllegalAccessE void testRunOneScan_Default() throws NoSuchFieldException, IllegalAccessException { lpvsDetectService = - spy(new LPVSDetectService("scanoss", false, null, null, null, null)); + spy( + new LPVSDetectService( + "scanoss", false, null, null, null, scanServiceFactory_mock)); setPrivateField(lpvsDetectService, "trigger", "fake-trigger-value"); setPrivateField(lpvsDetectService, "ctx", mockApplicationContext); @@ -138,7 +137,9 @@ void testRunOneScan_Branch2() List.of(conflict_1, conflict_1); lpvsDetectService = - spy(new LPVSDetectService("scanoss", false, null, null, null, null)); + spy( + new LPVSDetectService( + "scanoss", false, null, null, null, scanServiceFactory_mock)); // Mock the necessary GitHub objects for LPVSQueue when(mockGitHub.getRepository(any())).thenReturn(mockRepository); @@ -155,7 +156,6 @@ void testRunOneScan_Branch2() when(mockPullRequest.getHtmlUrl()).thenReturn(new URL(expectedPullRequestUrl)); setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); - setPrivateField(detectService, "scannerType", "scanoss"); setPrivateField(detectService, "htmlReport", null); setPrivateField(detectService, "ctx", mockApplicationContext); @@ -176,10 +176,11 @@ void testRunOneScan_Branch3() List.of(conflict_1, conflict_1); lpvsDetectService = - spy(new LPVSDetectService("scanoss", false, null, null, null, null)); + spy( + new LPVSDetectService( + "scanoss", false, null, null, null, scanServiceFactory_mock)); setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); - setPrivateField(detectService, "scannerType", "scanoss"); setPrivateField(detectService, "htmlReport", "build"); setPrivateField(detectService, "ctx", mockApplicationContext); @@ -206,10 +207,10 @@ void testRunOneScan_Branch3() void testRunOneScan_trigerInternalQueueException() throws NoSuchFieldException, IllegalAccessException { - setPrivateField(lpvsDetectService, "trigger", "fake-trigger-value"); - setPrivateField(lpvsDetectService, "ctx", mockApplicationContext); + setPrivateField(detectService, "trigger", "fake-trigger-value"); + setPrivateField(detectService, "ctx", mockApplicationContext); - assertDoesNotThrow(() -> lpvsDetectService.runOneScan()); + assertDoesNotThrow(() -> detectService.runOneScan()); } @Test @@ -222,7 +223,6 @@ void testRunOneScan_TriggerNotNull() throws Exception { List.of(conflict_1, conflict_1); setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); - setPrivateField(detectService, "scannerType", "scanoss"); setPrivateField(detectService, "htmlReport", "build/report/test.html"); setPrivateField(detectService, "ctx", mockApplicationContext); @@ -255,7 +255,6 @@ void testRunOneScan_TriggerNotNull_Branch2() throws Exception { List.of(conflict_1, conflict_1); setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); - setPrivateField(detectService, "scannerType", "scanoss"); setPrivateField(detectService, "htmlReport", "build/report/test.html"); setPrivateField(detectService, "ctx", mockApplicationContext); @@ -289,7 +288,6 @@ void testRunOneScan_TriggerNotNull_Branch3() throws Exception { List.of(conflict_1, conflict_1); setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); - setPrivateField(detectService, "scannerType", "scanoss"); setPrivateField(detectService, "htmlReport", "build/report/test.html"); setPrivateField(detectService, "ctx", mockApplicationContext); @@ -304,7 +302,6 @@ void testRunOneScan_TriggerNotNull_Branch3() throws Exception { // Set up expected values String expectedPullRequestUrl = "https://example.com/pull/1"; when(mockRepository.getHtmlUrl()).thenReturn(new URL(expectedPullRequestUrl)); - // setPrivateField(mockPullRequest, "url", expectedPullRequestUrl); detectService.runOneScan(); @@ -350,6 +347,7 @@ public void testGetPathByPullRequest() { @Test public void testRunScan__Scanoss() { try { + setPrivateField(detectService, "scanService", scanoss_mock); // main test assertEquals( List.of(lpvs_file_1, lpvs_file_2), @@ -399,6 +397,7 @@ void setUp() { webhookConfig.setId(1L); try { + setPrivateField(detectService, "scanService", scanoss_mock); doThrow(new Exception(exc_msg)) .when(scanoss_mock) .runScan(webhookConfig, test_path); @@ -436,9 +435,13 @@ public void testRunScan__ScanossException() { class TestRunScan__NotScanoss { LPVSDetectService detectService; + LPVSScanServiceFactory scanServiceFactory_mock = mock(LPVSScanServiceFactory.class); + @BeforeEach void setUp() { - detectService = new LPVSDetectService("not_scanoss", false, null, null, null, null); + detectService = + new LPVSDetectService( + "not_scanoss", false, null, null, null, scanServiceFactory_mock); } @Test diff --git a/src/test/java/com/lpvs/service/scan/LPVSScanServiceFactoryTest.java b/src/test/java/com/lpvs/service/scan/LPVSScanServiceFactoryTest.java new file mode 100644 index 00000000..946ef7cd --- /dev/null +++ b/src/test/java/com/lpvs/service/scan/LPVSScanServiceFactoryTest.java @@ -0,0 +1,70 @@ +/** + * Copyright (c) 2024, 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.scan; + +import com.lpvs.service.scan.scanner.LPVSScanossDetectService; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class LPVSScanServiceFactoryTest { + + private LPVSScanServiceFactory scanServiceFactory = new LPVSScanServiceFactory(); + + @Test + void testGetServiceClassName() { + String expected1 = "com.lpvs.internal.service.scan.scanner.LPVSFooDetectService"; + String actual1 = scanServiceFactory.getServiceClassName("foo", true); + assertEquals(expected1, actual1); + + String expected2 = "com.lpvs.service.scan.scanner.LPVSFooDetectService"; + String actual2 = scanServiceFactory.getServiceClassName("foo", false); + assertEquals(expected2, actual2); + } + + @Test + void testGetServiceClassName_NullScannerType_N() { + assertThrows( + IllegalArgumentException.class, + () -> scanServiceFactory.getServiceClassName(null, true)); + } + + @Test + void testGetServiceClassName_EmptyScannerType_N() { + assertThrows( + IllegalArgumentException.class, + () -> scanServiceFactory.getServiceClassName("", true)); + } + + @Test + void testCreateScanService() { + LPVSScanService service = scanServiceFactory.createScanService("scanoss", false); + assertNotNull(service); + assertTrue(service instanceof LPVSScanossDetectService); + } + + @Test + void testCreateScanService_NullScannerType_N() { + assertThrows( + IllegalArgumentException.class, + () -> scanServiceFactory.createScanService(null, true)); + } + + @Test + void testCreateScanService_EmptyScannerType_N() { + assertThrows( + IllegalArgumentException.class, + () -> scanServiceFactory.createScanService("", true)); + } + + @Test + void testCreateScanService_NoSuchScannerType_N() { + assertThrows( + IllegalArgumentException.class, + () -> scanServiceFactory.createScanService("baz", true)); + } +} From 57da4d6ce7bc4afde94740e32b46ff12f4a6d6f4 Mon Sep 17 00:00:00 2001 From: Oleg Kopysov Date: Wed, 27 Mar 2024 08:41:37 +0200 Subject: [PATCH 3/3] fix: Add missing comments Signed-off-by: Oleg Kopysov --- src/main/java/com/lpvs/service/scan/LPVSDetectService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/lpvs/service/scan/LPVSDetectService.java b/src/main/java/com/lpvs/service/scan/LPVSDetectService.java index a060fca3..a162f4b6 100644 --- a/src/main/java/com/lpvs/service/scan/LPVSDetectService.java +++ b/src/main/java/com/lpvs/service/scan/LPVSDetectService.java @@ -78,11 +78,11 @@ public class LPVSDetectService { * Constructs an instance of LPVSDetectService with the specified parameters. * * @param scannerType The type of license detection scanner. - * @param isInternal + * @param isInternal Flag indicating whether the scanner is internal or not. * @param gitHubConnectionService Service for connecting to the GitHub API. * @param licenseService Service for license conflict analysis. * @param gitHubService Service for GitHub connection and operation. - * @param scanServiceFactory + * @param scanServiceFactory Service for creating instance of the scanner. */ @Autowired public LPVSDetectService(