Skip to content

Commit

Permalink
fix: Fix unit test for LPVSScanossDetectService (#258)
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov authored Oct 25, 2023
1 parent 9158e76 commit 36b0eae
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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));
}
}

0 comments on commit 36b0eae

Please sign in to comment.