Skip to content

Commit

Permalink
fix: Fix runtime errors in queries
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 committed Jun 26, 2024
1 parent 3fc7d42 commit 8e3ae57
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/controller/LPVSWebController.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public ResponseEntity<LPVSResult> resultPage(
LPVSPullRequest pr = prOpt.get();

List<LPVSLicense> distinctByLicense =
detectedLicenseRepository.findDistinctByPullRequestAndLicense(pr);
detectedLicenseRepository.findDistinctLicenseByPullRequest(pr);
List<String> detectedLicenses = new ArrayList<>();
Map<String, Integer> licenseCountMap = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public interface LPVSDetectedLicenseRepository extends JpaRepository<LPVSDetecte
* @param pr The {@link LPVSPullRequest} to retrieve distinct licenses for.
* @return List of distinct {@link LPVSLicense} associated with the pull request.
*/
List<LPVSLicense> findDistinctByPullRequestAndLicense(@Param("pr") LPVSPullRequest pr);
List<LPVSLicense> findDistinctLicenseByPullRequest(@Param("pr") LPVSPullRequest pr);

/**
* Find detected licenses associated with a specific pull request where the license is not null.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface LPVSLicenseConflictRepository extends JpaRepository<LPVSLicense
"SELECT lc FROM LPVSLicenseConflict lc "
+ "WHERE (lc.repositoryLicense.licenseId = :license1 AND lc.conflictLicense.licenseId = :license2) "
+ "OR (lc.repositoryLicense.licenseId = :license2 AND lc.conflictLicense.licenseId = :license1) "
+ "ORDER BY lc.id DESC "
+ "ORDER BY lc.conflictId DESC "
+ "LIMIT 1")
LPVSLicenseConflict findLicenseConflict(
@Param("license1") Long license1, @Param("license2") Long license2);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/lpvs/repository/LPVSLicenseRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface LPVSLicenseRepository extends JpaRepository<LPVSLicense, Long>
* @param spdxId The SPDX identifier of the license.
* @return The latest {@link LPVSLicense} entity with the specified SPDX identifier.
*/
LPVSLicense findFirstBySpdxIdOrderByIdDesc(@Param("spdxId") String spdxId);
LPVSLicense findFirstBySpdxIdOrderByLicenseIdDesc(@Param("spdxId") String spdxId);

/**
* Search for a license by alternative license names.
Expand All @@ -37,13 +37,14 @@ public interface LPVSLicenseRepository extends JpaRepository<LPVSLicense, Long>
*/
@Query(
"SELECT l FROM LPVSLicense l WHERE (CONCAT(',', l.alternativeNames, ',') LIKE CONCAT('%,', :licenseName, ',%')) "
+ "ORDER BY l.id DESC LIMIT 1")
+ "ORDER BY l.licenseId DESC LIMIT 1")
LPVSLicense searchByAlternativeLicenseNames(@Param("licenseName") String licenseName);

/**
* Retrieve all SPDX identifiers of licenses from the database.
*
* @return List of SPDX identifiers as Strings.
*/
@Query(value = "select l.spdxId from LPVSLicense l")
List<String> findAllSpdxId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ LPVSPullRequest findLatestByPullRequestInfo(
* @param pageable The pagination information.
* @return Page of {@link LPVSPullRequest} entities with the specified sender or pull request head.
*/
@Query(
value =
"select pr from LPVSPullRequest pr where pr.sender = :name or pr.pullRequestHead = :name")
Page<LPVSPullRequest> findBySenderOrPullRequestHead(
@Param("name") String name, Pageable pageable);

Expand All @@ -94,6 +97,9 @@ Page<LPVSPullRequest> findBySenderOrPullRequestHead(
* @param name The name of the sender or pull request head.
* @return List of {@link LPVSPullRequest} entities with the specified sender or pull request head.
*/
@Query(
value =
"select pr from LPVSPullRequest pr where pr.sender = :name or pr.pullRequestHead = :name")
List<LPVSPullRequest> findBySenderOrPullRequestHead(@Param("name") String name);

/**
Expand All @@ -102,5 +108,8 @@ Page<LPVSPullRequest> findBySenderOrPullRequestHead(
* @param name The name of the sender or pull request head.
* @return The count of pull requests with the specified sender or pull request head.
*/
@Query(
value =
"select count(*) from LPVSPullRequest pr where pr.sender = :name or pr.pullRequestHead = :name")
Long countBySenderOrPullRequestHead(@Param("name") String name);
}
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 @@ -309,17 +309,17 @@ public void commentResults(
detectedIssue.setPullRequest(lpvsPullRequest);
Long l1 =
lpvsLicenseRepository
.findFirstBySpdxIdOrderByIdDesc(conflict.l1)
.findFirstBySpdxIdOrderByLicenseIdDesc(conflict.l1)
.getLicenseId();
Long l2 =
lpvsLicenseRepository
.findFirstBySpdxIdOrderByIdDesc(conflict.l2)
.findFirstBySpdxIdOrderByLicenseIdDesc(conflict.l2)
.getLicenseId();
detectedIssue.setLicenseConflict(
lpvsLicenseConflictRepository.findLicenseConflict(l1, l2));
if (webhookConfig.getRepositoryLicense() != null) {
LPVSLicense repoLicense =
lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(
lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(
webhookConfig.getRepositoryLicense());
if (repoLicense == null) {
repoLicense =
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/service/LPVSLicenseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public List<Conflict<String, String>> findConflicts(

if (repositoryLicense != null) {
LPVSLicense repoLicense =
lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(repositoryLicense);
lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(repositoryLicense);
if (repoLicense == null) {
repoLicense =
lpvsLicenseRepository.searchByAlternativeLicenseNames(repositoryLicense);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public void testResultPage() {

when(loginCheckService.getMemberFromMemberMap(authentication)).thenReturn(new LPVSMember());
when(lpvsPullRequestRepository.findById(prId)).thenReturn(Optional.of(pullRequest));
when(detectedLicenseRepository.findDistinctByPullRequestAndLicense(pullRequest))
when(detectedLicenseRepository.findDistinctLicenseByPullRequest(pullRequest))
.thenReturn(licenses);
when(licenseRepository.findAllSpdxId()).thenReturn(List.of("MIT", "Apache-2.0"));
when(detectedLicenseRepository.findByPullRequest(pullRequest, pageable))
Expand Down
28 changes: 15 additions & 13 deletions src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2255,7 +2255,7 @@ void setUp() {
null);
conflict_1 = new LPVSLicenseService.Conflict<>(conflict_1_l1, conflict_1_l2);

when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(anyString()))
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(anyString()))
.thenReturn(lpvs_license_1);
when(mocked_lpvsLicenseRepository.searchByAlternativeLicenseNames(anyString()))
.thenReturn(null);
Expand Down Expand Up @@ -2310,7 +2310,7 @@ public void testCommentResults__ProhibitedPresentConflictsPresentLicensePresent(
throws Exception {
// main test
webhookConfig.setRepositoryLicense(spdx_id_1);
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(spdx_id_1))
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(spdx_id_1))
.thenReturn(
new LPVSLicense(
1L,
Expand Down Expand Up @@ -2369,7 +2369,7 @@ public void testCommentResults__ProhibitedPresentConflictsPresentLicensePresentA
throws Exception {
// main test
webhookConfig.setRepositoryLicense(spdx_id_2);
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(spdx_id_2))
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(spdx_id_2))
.thenReturn(null);
when(mocked_lpvsLicenseRepository.searchByAlternativeLicenseNames(spdx_id_2))
.thenReturn(
Expand Down Expand Up @@ -2585,7 +2585,7 @@ void setUp() {
null);
conflict_1 = new LPVSLicenseService.Conflict<>(conflict_1_l1, conflict_1_l2);

when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(anyString()))
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(anyString()))
.thenReturn(lpvs_license_1);
when(mocked_lpvsLicenseRepository.searchByAlternativeLicenseNames(anyString()))
.thenReturn(null);
Expand Down Expand Up @@ -2640,7 +2640,7 @@ public void testCommentResults__EmptyPresentConflictsPresentLicensePresent()
throws Exception {
// main test
webhookConfig.setRepositoryLicense(spdx_id_1);
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(spdx_id_1))
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(spdx_id_1))
.thenReturn(
new LPVSLicense(
1L,
Expand Down Expand Up @@ -2699,7 +2699,7 @@ public void testCommentResults__EmptyPresentConflictsPresentLicensePresentAlt()
throws Exception {
// main test
webhookConfig.setRepositoryLicense(spdx_id_2);
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(spdx_id_2))
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(spdx_id_2))
.thenReturn(null);
when(mocked_lpvsLicenseRepository.searchByAlternativeLicenseNames(spdx_id_2))
.thenReturn(
Expand Down Expand Up @@ -2918,7 +2918,7 @@ void setUp() {
null);
conflict_1 = new LPVSLicenseService.Conflict<>(conflict_1_l1, conflict_1_l2);

when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(anyString()))
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(anyString()))
.thenReturn(lpvs_license_1);
when(mocked_lpvsLicenseRepository.searchByAlternativeLicenseNames(anyString()))
.thenReturn(null);
Expand Down Expand Up @@ -2973,7 +2973,7 @@ public void testCommentResults__RestrictedPresentConflictsPresentLicensePresent(
throws Exception {
// main test
webhookConfig.setRepositoryLicense(spdx_id_1);
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(spdx_id_1))
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(spdx_id_1))
.thenReturn(
new LPVSLicense(
1L,
Expand Down Expand Up @@ -3032,7 +3032,7 @@ public void testCommentResults__RestrictedPresentConflictsPresentLicensePresentA
throws Exception {
// main test
webhookConfig.setRepositoryLicense(spdx_id_2);
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(spdx_id_2))
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(spdx_id_2))
.thenReturn(null);
when(mocked_lpvsLicenseRepository.searchByAlternativeLicenseNames(spdx_id_2))
.thenReturn(
Expand Down Expand Up @@ -3251,7 +3251,7 @@ void setUp() {
null);
conflict_1 = new LPVSLicenseService.Conflict<>(conflict_1_l1, conflict_1_l2);

when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(anyString()))
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(anyString()))
.thenReturn(lpvs_license_1);
when(mocked_lpvsLicenseRepository.searchByAlternativeLicenseNames(anyString()))
.thenReturn(null);
Expand Down Expand Up @@ -3306,7 +3306,7 @@ public void testCommentResults__RestrictedPresentConflictsPresentLicensePresent(
throws Exception {
// main test
webhookConfig.setRepositoryLicense(spdx_id_1);
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(spdx_id_1))
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(spdx_id_1))
.thenReturn(
new LPVSLicense(
1L,
Expand Down Expand Up @@ -3365,7 +3365,7 @@ public void testCommentResults__RestrictedPresentConflictsPresentLicensePresentA
throws Exception {
// main test
webhookConfig.setRepositoryLicense(spdx_id_2);
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(spdx_id_2))
when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(spdx_id_2))
.thenReturn(null);
when(mocked_lpvsLicenseRepository.searchByAlternativeLicenseNames(spdx_id_2))
.thenReturn(
Expand Down Expand Up @@ -4298,7 +4298,9 @@ public void testCommentResults() throws Exception {
}
};
Mockito.when(repository.getPullRequests(GHIssueState.OPEN)).thenReturn(pullRequestList);
Mockito.when(mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(anyString()))
Mockito.when(
mocked_lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(
anyString()))
.thenReturn(license);

gh_service.commentResults(webhookConfig, fileList, conflictList, lpvsPullRequest);
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/com/lpvs/service/LPVSLicenseServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public void testFindConflicts() {
LPVSLicenseRepository lpvsLicenseRepository = Mockito.mock(LPVSLicenseRepository.class);
ReflectionTestUtils.setField(
licenseService, "lpvsLicenseRepository", lpvsLicenseRepository);
when(lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(anyString())).thenReturn(null);
when(lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(anyString()))
.thenReturn(null);
when(lpvsLicenseRepository.searchByAlternativeLicenseNames(anyString())).thenReturn(null);

Assertions.assertNotNull(licenseService.findConflicts(webhookConfig, fileList));
Expand Down Expand Up @@ -442,7 +443,7 @@ void setUp() throws NoSuchFieldException, IllegalAccessException {
null,
null);

when(lpvsLicenseRepository.findFirstBySpdxIdOrderByIdDesc(anyString()))
when(lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(anyString()))
.thenReturn(null);
when(lpvsLicenseRepository.searchByAlternativeLicenseNames(anyString()))
.thenReturn(lpvs_license_1);
Expand Down

0 comments on commit 8e3ae57

Please sign in to comment.