From 36b0eae9afdfde49d147c7b6e5e9d952c6e87439 Mon Sep 17 00:00:00 2001 From: Oleg Kopysov Date: Wed, 25 Oct 2023 08:46:09 +0300 Subject: [PATCH] fix: Fix unit test for LPVSScanossDetectService (#258) Signed-off-by: Oleg Kopysov --- .../scanoss/LPVSScanossDetectService.java | 12 ++++++- .../scanoss/LPVSScanossDetectServiceTest.java | 34 +++++++++++-------- 2 files changed, 31 insertions(+), 15 deletions(-) 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 e574cbc7..71a406a0 100644 --- a/src/main/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectService.java +++ b/src/main/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectService.java @@ -43,9 +43,19 @@ public class LPVSScanossDetectService { @Autowired private LPVSLicenseRepository lpvsLicenseRepository; - @Value("${debug:false}") private Boolean debug; + @Autowired + 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; + } + public void runScan(LPVSQueue webhookConfig, String path) throws Exception { log.debug("Starting Scanoss scanning"); 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 a32ccde0..ccafd7f8 100644 --- a/src/test/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectServiceTest.java +++ b/src/test/java/com/lpvs/service/scanner/scanoss/LPVSScanossDetectServiceTest.java @@ -9,6 +9,7 @@ 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; @@ -17,6 +18,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; import org.springframework.test.util.ReflectionTestUtils; import java.io.File; @@ -28,41 +30,45 @@ import java.util.Objects; public class LPVSScanossDetectServiceTest { + @BeforeEach public void setUp() throws URISyntaxException, IOException { - if (!(new File("RESULTS").exists())) { - new File("RESULTS").mkdir(); + MockitoAnnotations.openMocks(this); + String resourcePath = "A_B.json"; + String destinationPath = System.getProperty("user.home") + File.separator + "Results" + File.separator + "C" ; + if (!(new File(destinationPath).exists())) { + new File(destinationPath).mkdirs(); } - // File f = new File(Objects.requireNonNull(getClass().getClassLoader().getResource("A_B.txt")).toURI()); - Files.copy(Paths.get(Objects.requireNonNull(getClass().getClassLoader().getResource("A_B.json")).toURI()), - Paths.get("RESULTS/A_B.json"), StandardCopyOption.REPLACE_EXISTING); + Files.copy(Paths.get(Objects.requireNonNull(getClass().getClassLoader().getResource(resourcePath)).toURI()), + Paths.get(destinationPath + File.separator + resourcePath), StandardCopyOption.REPLACE_EXISTING); } @AfterEach public void tearDown() { - if ((new File("RESULTS")).exists()) { - new File("RESULTS").delete(); + if ((new File(System.getProperty("user.home") + File.separator + "Results")).exists()) { + new File(System.getProperty("user.home") + File.separator + "Results").delete(); } } @Test - public void test() { - LPVSScanossDetectService scanossDetectService = new LPVSScanossDetectService(); + public void testCheckLicense() { LPVSLicenseService licenseService = Mockito.mock(LPVSLicenseService.class); LPVSGitHubService gitHubService = Mockito.mock(LPVSGitHubService.class); + LPVSLicenseRepository lpvsLicenseRepository = Mockito.mock(LPVSLicenseRepository.class); + LPVSScanossDetectService scanossDetectService = new LPVSScanossDetectService(false, licenseService, gitHubService, lpvsLicenseRepository); String licenseConflictsSource = "scanner"; - ReflectionTestUtils.setField(scanossDetectService, "licenseService", licenseService); - ReflectionTestUtils.setField(scanossDetectService, "gitHubService", gitHubService); LPVSQueue webhookConfig = Mockito.mock(LPVSQueue.class); - Mockito.when(LPVSWebhookUtil.getRepositoryName(webhookConfig)).thenReturn("A"); - Mockito.when(webhookConfig.getHeadCommitSHA()).thenReturn("B"); - scanossDetectService.checkLicenses(webhookConfig); + Mockito.when(LPVSWebhookUtil.getRepositoryName(webhookConfig)).thenReturn("C"); + Mockito.when(webhookConfig.getHeadCommitSHA()).thenReturn("A_B"); + Mockito.when(lpvsLicenseRepository.save(Mockito.any(LPVSLicense.class))) + .thenAnswer(i -> i.getArguments()[0]); ReflectionTestUtils.setField(licenseService, "licenseConflictsSource", licenseConflictsSource); Mockito.when(licenseService.findLicenseBySPDX("MIT")).thenReturn(new LPVSLicense(){{ setLicenseName("MIT"); setLicenseId(1L); setSpdxId("MIT"); }}); + scanossDetectService.checkLicenses(webhookConfig); Assertions.assertNotNull(scanossDetectService.checkLicenses(webhookConfig)); } }