Skip to content

Commit

Permalink
Improving test coverage (#54)
Browse files Browse the repository at this point in the history
* Started ScanossDetectService testing

Signed-off-by: Vasyl Kerimov <[email protected]>

* Revert "Reduce the number of warnings according to the test policy (#43)"

This reverts commit f92b77e.

* Changed a test to covers a more of ScanossService

Signed-of-by: Vasyl Kerimov <[email protected]>

* Fixed problem with incorrectly typed variable

Signed-off-by: Vasyl Kerimov <[email protected]>

* Added tests to cover 80% of code lines

Signed-off-by: Vasyl Kerimov <[email protected]>

* Fixed highlights, incl. making some fields final

Signed-off-by: Vasyl Kerimov <[email protected]>

* Added assertions to the tests

Signed-off-by: Vasyl Kerimov <[email protected]>

* Update LicenseServiceTest.java

* Update pom.xml

* Update ResponseWrapper.java

* Update LicenseServiceTest.java

* Update ScanossDetectServiceTest.java

* Update FileUtilTest.java

Signed-off-by: Vasyl Kerimov <[email protected]>
Co-authored-by: Oleg Kopysov <[email protected]>
  • Loading branch information
v-kerimov and o-kopysov authored Sep 26, 2022
1 parent f01b675 commit bac3f49
Show file tree
Hide file tree
Showing 17 changed files with 420 additions and 22 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
<artifactId>bridge-method-injector</artifactId>
<version>1.23</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/LicensePreValidationSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@EnableAsync
public class LicensePreValidationSystem {

private int corePoolSize;
private final int corePoolSize;

public LicensePreValidationSystem(@Value("${lpvs.cores:8}") int corePoolSize) {
this.corePoolSize = corePoolSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

@RestController
public class GitHubWebhooksController {

private String GITHUB_SECRET;

@Autowired
Expand All @@ -56,7 +57,7 @@ public void setProps() {

private GitHubService gitHubService;

private static Logger LOG = LoggerFactory.getLogger(GitHubWebhooksController.class);
private static final Logger LOG = LoggerFactory.getLogger(GitHubWebhooksController.class);

private static final String SIGNATURE = "X-Hub-Signature-256";
private static final String SUCCESS = "Success";
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/lpvs/entity/ResponseWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package com.lpvs.entity;

public class ResponseWrapper {

private String message;

public ResponseWrapper(String message) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/lpvs/entity/enums/PullRequestAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public String getPullRequestAction() {
return type;
}

private static Logger LOG = LoggerFactory.getLogger(PullRequestAction.class);

public static PullRequestAction convertFrom(String action) {
if (action.equals(OPEN.getPullRequestAction())) {
return OPEN;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/service/DetectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public DetectService(@Value("${scanner:scanoss}") String scannerType,
this.scanossDetectService = scanossDetectService;
}

private static Logger LOG = LoggerFactory.getLogger(DetectService.class);
private static final Logger LOG = LoggerFactory.getLogger(DetectService.class);

@PostConstruct
private void init() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/service/GitHubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void checks() throws Exception {
}
}

private static Logger LOG = LoggerFactory.getLogger(GitHubService.class);
private static final Logger LOG = LoggerFactory.getLogger(GitHubService.class);

private static GitHub gitHub;

Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/lpvs/service/LicenseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class LicenseService {
public String licenseFilePath;
public String licenseConflictsSource;

private static Logger LOG = LoggerFactory.getLogger(LicenseService.class);
private static final Logger LOG = LoggerFactory.getLogger(LicenseService.class);

private List<LPVSLicense> licenses;

Expand Down Expand Up @@ -184,8 +184,11 @@ public List<Conflict<String, String>> findConflicts(WebhookConfig webhookConfig,
for (int i = 0; i < detectedLicensesUnique.size(); i++) {
for (int j = i + 1; j < detectedLicensesUnique.size(); j++) {
for (Conflict<String, String> licenseConflict : licenseConflicts) {
Conflict<String, String> possibleConflict = new Conflict<>(detectedLicensesUnique.toArray()[i].toString(),
detectedLicensesUnique.toArray()[j].toString());
Conflict<String, String> possibleConflict =
new Conflict<>(
(String) detectedLicensesUnique.toArray()[i],
(String) detectedLicensesUnique.toArray()[j]
);
if (licenseConflict.equals(possibleConflict)) {
foundConflicts.add(possibleConflict);
}
Expand All @@ -197,8 +200,8 @@ public List<Conflict<String, String>> findConflicts(WebhookConfig webhookConfig,
}

public static class Conflict<License1, License2> {
License1 l1;
License2 l2;
public License1 l1;
public License2 l2;
Conflict(License1 l1, License2 l2) {
this.l1 = l1;
this.l2 = l2;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/service/QueueProcessorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class QueueProcessorService {

private QueueService queueService;

private static Logger LOG = LoggerFactory.getLogger(QueueProcessorService.class);
private static final Logger LOG = LoggerFactory.getLogger(QueueProcessorService.class);

@Autowired
QueueProcessorService(QueueService queueService) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/service/QueueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public QueueService(GitHubService gitHubService,
this.licenseService = licenseService;
}

private static Logger LOG = LoggerFactory.getLogger(QueueService.class);
private static final Logger LOG = LoggerFactory.getLogger(QueueService.class);

private static final BlockingDeque<WebhookConfig> QUEUE = new LinkedBlockingDeque<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.*;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
Expand All @@ -33,7 +32,7 @@ public class ScanossDetectService {
@Autowired
private GitHubService gitHubService;

private static Logger LOG = LoggerFactory.getLogger(ScanossDetectService.class);
private static final Logger LOG = LoggerFactory.getLogger(ScanossDetectService.class);

public void runScan(WebhookConfig webhookConfig, String path) throws Exception {
LOG.info("Starting Scanoss scanning");
Expand Down Expand Up @@ -81,12 +80,12 @@ public List<LPVSFile> checkLicenses(WebhookConfig webhookConfig) {
Gson gson = new Gson();
Reader reader = Files.newBufferedReader(Paths.get("RESULTS/" + webhookConfig.getRepositoryName() + "_" + webhookConfig.getHeadCommitSHA() + ".json"));
// convert JSON file to map
Type mapType = new TypeToken<Map<ArrayList<String>, String>>() {}.getType();
Map<ArrayList<String>, String> map = gson.fromJson(reader, mapType);
Map<String, ArrayList<Object>> map = gson.fromJson(reader,
new TypeToken<Map<String, ArrayList<Object>>>() {}.getType());

// parse map entries
long ind = 0L;
for (Map.Entry<ArrayList<String>, String> entry : map.entrySet()) {
for (Map.Entry<String, ArrayList<Object>> entry : map.entrySet()) {
LPVSFile file = new LPVSFile();
file.setId(ind++);
file.setFilePath(entry.getKey().toString());
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/lpvs/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package com.lpvs.util;

import org.kohsuke.github.GHPullRequestFileDetail;
import org.kohsuke.github.PagedIterable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileSystemUtils;
Expand All @@ -19,9 +18,9 @@
import java.io.IOException;

public class FileUtil {
private static Logger LOG = LoggerFactory.getLogger(FileUtil.class);
private static final Logger LOG = LoggerFactory.getLogger(FileUtil.class);

public static String saveFiles(PagedIterable<GHPullRequestFileDetail> files, String folder, String headCommitSHA, int deletions) {
public static String saveFiles(Iterable<GHPullRequestFileDetail> files, String folder, String headCommitSHA, int deletions) {

String directoryPath = "Projects/" + folder + "/" + headCommitSHA;
String directoryDeletionPath = "";
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/com/lpvs/service/GitHubServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@
import org.junit.jupiter.api.Test;
import org.kohsuke.github.*;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.util.ReflectionTestUtils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand Down Expand Up @@ -1986,4 +1990,60 @@ public void testGetMatchedLinesAsLink_All() {
assertEquals(expected_result, gh_service.getMatchedLinesAsLink(webhookConfig, lpvs_file_1));
}
}

@Nested
class TestCommentResults {

final String GH_LOGIN = "test_login";
final String GH_AUTH_TOKEN = "test_auth_token";
final String GH_API_URL = "test_api_url";
GitHubService gh_service = new GitHubService(GH_LOGIN, GH_AUTH_TOKEN, GH_API_URL);
WebhookConfig webhookConfig;

@BeforeEach
void setUp() {
webhookConfig = new WebhookConfig();
webhookConfig.setRepositoryName("LPVS");
webhookConfig.setRepositoryOrganization("Samsung");
webhookConfig.setPullRequestAPIUrl("http://url.com");
}

@Test
public void testCommentResults() throws IOException {
GitHub gitHub = Mockito.mock(GitHub.class);
GHRepository repository = Mockito.mock(GHRepository.class);
ReflectionTestUtils.setField(gh_service, "gitHub", gitHub);
Mockito.when(gitHub.getRepository(
webhookConfig.getRepositoryOrganization() + "/" + webhookConfig.getRepositoryName()))
.thenReturn(repository);
LPVSFile file = new LPVSFile();
LPVSLicense license = new LPVSLicense(){{
setChecklist_url("");
setAccess("unrviewed");
}};
file.setLicenses(new HashSet<LPVSLicense>(){{
add(license);
}});
file.setFilePath("");
file.setComponent("");
file.setSnippetMatch("");
file.setMatchedLines("");
List<LPVSFile> fileList = new ArrayList<LPVSFile>(){{
add(file);
}};
List<LicenseService.Conflict<String, String>> conflictList = new ArrayList<>();
conflictList.add(new LicenseService.Conflict<>("1", "2"));
GHPullRequest pullRequest = new GHPullRequest();
ReflectionTestUtils.setField(pullRequest, "url", "http://url.com");
List<GHPullRequest> pullRequestList = new ArrayList<GHPullRequest>(){{
add(pullRequest);
}};
Mockito.when(repository.getPullRequests(GHIssueState.OPEN))
.thenReturn(pullRequestList);
gh_service.commentResults(webhookConfig, fileList, conflictList);
license.setAccess("");
gh_service.commentResults(webhookConfig, fileList, conflictList);
Mockito.verify(gitHub, times(2)).getRepository(Mockito.anyString());
}
}
}
82 changes: 82 additions & 0 deletions src/test/java/com/lpvs/service/LicenseServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Copyright (c) 2022, 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.entity.LPVSFile;
import com.lpvs.entity.LPVSLicense;
import com.lpvs.entity.config.WebhookConfig;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class LicenseServiceTest {
@Test
public void testFindConflicts() {
WebhookConfig webhookConfig = new WebhookConfig();
webhookConfig.setRepositoryName("LPVS");
webhookConfig.setRepositoryOrganization("Samsung");
webhookConfig.setRepositoryLicense("Apache-2.0");
webhookConfig.setPullRequestAPIUrl("http://url.com");
LPVSFile file = new LPVSFile();
LPVSLicense license = new LPVSLicense(){{
setChecklist_url("");
setAccess("unrviewed");
setSpdxId("LPGL-2.1-or-later");
}};
file.setLicenses(new HashSet<LPVSLicense>(){{
add(license);
}});
file.setFilePath("");
file.setComponent("");
file.setSnippetMatch("");
file.setMatchedLines("");

LPVSFile file1 = new LPVSFile();
LPVSLicense license1 = new LPVSLicense(){{
setChecklist_url("");
setAccess("unrviewed");
setSpdxId("Apache-2.0");
}};
file1.setLicenses(new HashSet<LPVSLicense>(){{
add(license1);
}});
file1.setFilePath("");
file1.setComponent("");
file1.setSnippetMatch("");
file1.setMatchedLines("");

LPVSFile file2 = new LPVSFile();
LPVSLicense license2 = new LPVSLicense(){{
setChecklist_url("");
setAccess("unrviewed");
setSpdxId("MIT");
}};
file2.setLicenses(new HashSet<LPVSLicense>(){{
add(license2);
}});
file2.setFilePath("");
file2.setComponent("");
file2.setSnippetMatch("");
file2.setMatchedLines("");

List<LPVSFile> fileList = new ArrayList<LPVSFile>(){{
add(file);
add(file1);
add(file2);
}};
LicenseService licenseService = new LicenseService("", "");
ReflectionTestUtils.setField(licenseService, "licenseConflicts",
new ArrayList<LicenseService.Conflict<String, String>>()
{{ add(new LicenseService.Conflict<>("", "")); }});
Assertions.assertNotNull(licenseService.findConflicts(webhookConfig, fileList));
}
}
Loading

0 comments on commit bac3f49

Please sign in to comment.