Skip to content

Commit

Permalink
fix: Fix svace issue Redundant null check (#518)
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kopysov <o.kopysov@samsung.com>
  • Loading branch information
o-kopysov authored May 13, 2024
1 parent c1db058 commit e08fe1b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 42 deletions.
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

0 comments on commit e08fe1b

Please sign in to comment.