Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix svace issue Redundant null check #518

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/com/lpvs/service/LPVSGitHubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public void commentResults(
* Retrieves the license of the GitHub repository associated with the pull request.
*
* @param webhookConfig LPVSQueue configuration for the pull request.
* @return License key of the GitHub repository or "Proprietary" if not available.
* @return License key of the GitHub repository or null if not available.
*/
public String getRepositoryLicense(LPVSQueue webhookConfig) {
try {
Expand All @@ -378,14 +378,14 @@ public String getRepositoryLicense(LPVSQueue webhookConfig) {
gitHub.getRepository(repositoryOrganization + "/" + repositoryName);
GHLicense license = repository.getLicense();
if (license == null) {
return "Proprietary";
return null;
} else {
return license.getKey();
}
} catch (IOException | IllegalArgumentException e) {
log.error("Can't authorize getRepositoryLicense(): " + e.getMessage());
}
return "Proprietary";
return null;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/lpvs/service/LPVSLicenseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public void reloadFromTables() {
* @param name SPDX identifier of the license.
* @return LPVSLicense object if found, otherwise null.
*/
public LPVSLicense findLicenseBySPDX(String name) {
protected LPVSLicense findLicenseBySPDX(String name) {
for (LPVSLicense license : licenses) {
if (license.getSpdxId().equalsIgnoreCase(name)) {
return license;
Expand All @@ -214,7 +214,7 @@ public LPVSLicense findLicenseBySPDX(String name) {
*
* @param license The license to add.
*/
public void addLicenseToList(LPVSLicense license) {
protected void addLicenseToList(LPVSLicense license) {
if (!licenses.contains(license)) {
licenses.add(license);
}
Expand All @@ -226,7 +226,7 @@ public void addLicenseToList(LPVSLicense license) {
* @param name The name of the license.
* @return LPVSLicense object if found, otherwise null.
*/
public LPVSLicense findLicenseByName(String name) {
protected LPVSLicense findLicenseByName(String name) {
for (LPVSLicense license : licenses) {
if (license.getLicenseName().equalsIgnoreCase(name)) {
return license;
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/com/lpvs/service/LPVSQueueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,12 @@ public void processWebHook(LPVSQueue webhookConfig) {
}
// check repository license
String repositoryLicense = gitHubService.getRepositoryLicense(webhookConfig);
LPVSLicense repoLicense =
licenseService.getLicenseBySpdxIdAndName(
repositoryLicense, Optional.empty());
if (repoLicense != null) {

if (repositoryLicense != null) {
LPVSLicense repoLicense =
licenseService.getLicenseBySpdxIdAndName(
repositoryLicense, Optional.empty());
webhookConfig.setRepositoryLicense(repoLicense.getSpdxId());
} else if (licenseService.findLicenseByName(repositoryLicense) != null) {
webhookConfig.setRepositoryLicense(
licenseService.findLicenseByName(repositoryLicense).getSpdxId());
} else {
webhookConfig.setRepositoryLicense(null);
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3757,7 +3757,7 @@ public void testGetRepositoryLicense__ApiUrlAbsentLisenceAbsent() {
.thenReturn(mocked_instance_gh);

// main test
assertEquals("Proprietary", gh_service.getRepositoryLicense(webhookConfig));
assertNull(gh_service.getRepositoryLicense(webhookConfig));

// verification of calling methods on `Mock`s
// `mocked_static_gh` verify
Expand Down Expand Up @@ -3955,7 +3955,7 @@ public void testGetRepositoryLicense__ApiUrlPresentLisenceAbsent() {
.thenReturn(mocked_instance_gh);

// main test
assertEquals("Proprietary", gh_service.getRepositoryLicense(webhookConfig));
assertNull(gh_service.getRepositoryLicense(webhookConfig));

// verification of calling methods on `Mock`s
// `mocked_static_gh` verify
Expand Down Expand Up @@ -4033,7 +4033,7 @@ public void testGetRepositoryLicense__ApiUrlAbsentCantAuthorize() {
.thenThrow(new IOException("test cant authorize"));

// main test
assertEquals("Proprietary", gh_service.getRepositoryLicense(webhookConfig));
assertNull(gh_service.getRepositoryLicense(webhookConfig));

// verification of calling methods on `Mock`s
// `mocked_static_gh` verify
Expand Down Expand Up @@ -4086,7 +4086,7 @@ public void testGetRepositoryLicense__ApiUrlPresentCantAuthorize() {
.thenThrow(new IOException("test cant authorize"));

// main test
assertEquals("Proprietary", gh_service.getRepositoryLicense(webhookConfig));
assertNull(gh_service.getRepositoryLicense(webhookConfig));

// verification of calling methods on `Mock`s
// `mocked_static_gh` verify
Expand Down
34 changes: 9 additions & 25 deletions src/test/java/com/lpvs/service/LPVSQueueServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,7 @@ void setUp() {

mockLicenseService = mock(LPVSLicenseService.class);
when(mockLicenseService.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty()))
.thenReturn(null);
when(mockLicenseService.findLicenseByName(licenseNameTest)).thenReturn(lpvsLicenseTest);
.thenReturn(lpvsLicenseTest);

mockDetectService = mock(LPVSDetectService.class);
try {
Expand Down Expand Up @@ -553,7 +552,8 @@ public void testProcessWebHook__DeletionAbsentLicenseFound() throws Exception {
verify(mockGitHubService, times(1)).getRepositoryLicense(webhookConfigMain);
verify(mockLicenseService, times(1))
.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty());
verify(mockLicenseService, times(2)).findLicenseByName(licenseNameTest);
verify(mockLicenseService, times(1))
.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty());
try {
verify(mockDetectService, times(1))
.runScan(webhookConfigMain, filePathTestNoDeletion);
Expand Down Expand Up @@ -616,8 +616,7 @@ void setUp() {

mockLicenseService = mock(LPVSLicenseService.class);
when(mockLicenseService.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty()))
.thenReturn(null);
when(mockLicenseService.findLicenseByName(licenseNameTest)).thenReturn(lpvsLicenseTest);
.thenReturn(lpvsLicenseTest);

mockDetectService = mock(LPVSDetectService.class);
try {
Expand Down Expand Up @@ -651,12 +650,13 @@ public void testProcessWebHook__DeletionPresentLicenseFound() throws Exception {
verify(mockGitHubService, times(1)).getRepositoryLicense(webhookConfigMain);
verify(mockLicenseService, times(1))
.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty());
verify(mockLicenseService, times(2)).findLicenseByName(licenseNameTest);
verify(mockLicenseService, times(1))
.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty());
try {
verify(mockDetectService, times(1))
.runScan(webhookConfigMain, filePathTestWithDeletionTruncated);
} catch (Exception e) {
log.error("TestProcessWebHook__DeletionAbsentLicensePresent: Exception: " + e);
log.error("TestProcessWebHook__DeletionPresentLicenseFound: Exception: " + e);
fail();
}
verify(mockLicenseService, times(1)).findConflicts(webhookConfigMain, LPVSFilesTest);
Expand Down Expand Up @@ -709,14 +709,9 @@ void setUp() {
mockGitHubService = mock(LPVSGitHubService.class);
when(mockGitHubService.getPullRequestFiles(webhookConfigMain))
.thenReturn(filePathTestNoDeletion);
when(mockGitHubService.getRepositoryLicense(webhookConfigMain))
.thenReturn(licenseNameTest);
when(mockGitHubService.getRepositoryLicense(webhookConfigMain)).thenReturn(null);

mockLicenseService = mock(LPVSLicenseService.class);
when(mockLicenseService.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty()))
.thenReturn(null);
when(mockLicenseService.findLicenseByName(licenseNameTest)).thenReturn(null);

mockDetectService = mock(LPVSDetectService.class);
try {
when(mockDetectService.runScan(webhookConfigMain, filePathTestNoDeletion))
Expand Down Expand Up @@ -746,9 +741,6 @@ public void testProcessWebHook__DeletionAbsentLicenseNull() throws Exception {

verify(mockGitHubService, times(1)).getPullRequestFiles(webhookConfigMain);
verify(mockGitHubService, times(1)).getRepositoryLicense(webhookConfigMain);
verify(mockLicenseService, times(1))
.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty());
verify(mockLicenseService, times(1)).findLicenseByName(licenseNameTest);
try {
verify(mockDetectService, times(1))
.runScan(webhookConfigMain, filePathTestNoDeletion);
Expand Down Expand Up @@ -806,14 +798,9 @@ void setUp() {
mockGitHubService = mock(LPVSGitHubService.class);
when(mockGitHubService.getPullRequestFiles(webhookConfigMain))
.thenReturn(filePathTestWithDeletion);
when(mockGitHubService.getRepositoryLicense(webhookConfigMain))
.thenReturn(licenseNameTest);
when(mockGitHubService.getRepositoryLicense(webhookConfigMain)).thenReturn(null);

mockLicenseService = mock(LPVSLicenseService.class);
when(mockLicenseService.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty()))
.thenReturn(null);
when(mockLicenseService.findLicenseByName(licenseNameTest)).thenReturn(null);

mockDetectService = mock(LPVSDetectService.class);
try {
when(mockDetectService.runScan(
Expand Down Expand Up @@ -844,9 +831,6 @@ public void testProcessWebHook__DeletionAbsentLicenseNull() throws Exception {

verify(mockGitHubService, times(1)).getPullRequestFiles(webhookConfigMain);
verify(mockGitHubService, times(1)).getRepositoryLicense(webhookConfigMain);
verify(mockLicenseService, times(1))
.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty());
verify(mockLicenseService, times(1)).findLicenseByName(licenseNameTest);
try {
verify(mockDetectService, times(1))
.runScan(webhookConfigMain, filePathTestWithDeletionTruncated);
Expand Down