Skip to content

Commit

Permalink
refactor: Change table names, replace native queries with a non-native
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 17, 2024
1 parent e989b32 commit ef280cc
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 72 deletions.
4 changes: 2 additions & 2 deletions doc/quick-start-guide-and-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Before building _LPVS_ from source code, ensure that you have the following prer
mysql -u[username] -p[password] < src/main/resources/database_dump.sql
```

2.5 Fill in the `license_list` and `license_conflicts` tables with the information about permitted, restricted, and prohibited licenses, as well as their compatibility specifics. You can find an example database dump file in the repository at [`src/main/resources/database_dump.sql`](../src/main/resources/database_dump.sql).
2.5 Fill in the `lpvs_license_list` and `lpvs_license_conflicts` tables with the information about permitted, restricted, and prohibited licenses, as well as their compatibility specifics. You can find an example database dump file in the repository at [`src/main/resources/database_dump.sql`](../src/main/resources/database_dump.sql).

2.6 Update the following lines in the [`src/main/resources/application.properties`](../src/main/resources/application.properties) file:
```properties
Expand All @@ -198,7 +198,7 @@ github.secret=LPVS
scanner=scanoss
# Used license conflicts source:
# > option "db": take conflicts from MySQL database - 'license_conflicts' table (should be filled manually
# > option "db": take conflicts from MySQL database - 'lpvs_license_conflicts' table (should be filled manually
# according to the example at 'src/main/resources/database_dump.sql')
# > option "scanner": take conflicts from the scanner response
license_conflict=db
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/lpvs/entity/LPVSDetectedLicense.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

/**
* Entity class representing detected licenses in the LPVS system.
* This class is mapped to the "detected_license" table in the "lpvs" schema.
* This class is mapped to the "lpvs_detected_license" table in the "lpvs" schema.
*/
@Entity
@Table(name = "detected_license")
@Table(name = "lpvs_detected_license")
@Getter
@Setter
public class LPVSDetectedLicense implements Serializable {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/lpvs/entity/LPVSLicense.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

/**
* Entity class representing licenses in the LPVS system.
* This class is mapped to the "license_list" table in the "lpvs" schema.
* This class is mapped to the "lpvs_license_list" table in the "lpvs" schema.
*/
@Entity
@Table(
name = "license_list",
name = "lpvs_license_list",
indexes = {@Index(name = "spdx_id", columnList = "license_spdx", unique = true)})
@Getter
@Setter
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/lpvs/entity/LPVSLicenseConflict.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

/**
* Represents a license conflict in the LPVS system.
* This class is mapped to the "license_conflicts" table in the "lpvs" schema.
* This class is mapped to the "lpvs_license_conflicts" table in the "lpvs" schema.
*/
@Entity
@Table(name = "license_conflicts")
@Table(name = "lpvs_license_conflicts")
@Getter
@Setter
@NoArgsConstructor
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/lpvs/entity/LPVSPullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

/**
* Represents a pull request in the LPVS system.
* This class is mapped to the "pull_requests" table in the "lpvs" schema.
* This class is mapped to the "lpvs_pull_requests" table in the "lpvs" schema.
*/
@Entity
@Table(name = "pull_requests")
@Table(name = "lpvs_pull_requests")
@Getter
@Setter
@AllArgsConstructor
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/lpvs/entity/LPVSQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

/**
* Represents an item in the LPVS queue for processing pull requests.
* This class is mapped to the "queue" table in the "lpvs" schema.
* This class is mapped to the "lpvs_queue" table in the "lpvs" schema.
*/
@Entity
@Table(name = "queue")
@Table(name = "lpvs_queue")
@Getter
@Setter
public class LPVSQueue implements Serializable {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/lpvs/entity/auth/LPVSMember.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

/**
* Represents a member in the LPVS system.
* This class is mapped to the "member" table in the "lpvs" schema.
* This class is mapped to the "lpvs_member" table in the "lpvs" schema.
*/
@Getter
@NoArgsConstructor
@DynamicUpdate
@Entity
@Table(
name = "member",
name = "lpvs_member",
indexes = {@Index(name = "unq_member", columnList = "email, provider", unique = true)})
public class LPVSMember {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface LPVSLicenseConflictRepository extends JpaRepository<LPVSLicense
*
* @return List of {@link LPVSLicenseConflict} entities representing license conflicts.
*/
@Query(value = "SELECT * FROM license_conflicts", nativeQuery = true)
@Query("SELECT lc FROM LPVSLicenseConflict lc")
List<LPVSLicenseConflict> takeAllLicenseConflicts();

/**
Expand All @@ -37,10 +37,11 @@ public interface LPVSLicenseConflictRepository extends JpaRepository<LPVSLicense
* @return The latest {@link LPVSLicenseConflict} entity representing the conflict between the two licenses.
*/
@Query(
value =
"SELECT * FROM license_conflicts WHERE (license_conflicts.repository_license_id = :license1 AND license_conflicts.conflict_license_id = :license2) "
+ "OR (license_conflicts.repository_license_id = :license2 AND license_conflicts.conflict_license_id = :license1) ORDER BY id DESC LIMIT 1",
nativeQuery = true)
"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 "
+ "LIMIT 1")
LPVSLicenseConflict findLicenseConflict(
@Param("license1") Long license1, @Param("license2") Long license2);
}
12 changes: 4 additions & 8 deletions src/main/java/com/lpvs/repository/LPVSLicenseRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface LPVSLicenseRepository extends JpaRepository<LPVSLicense, Long>
*
* @return List of {@link LPVSLicense} entities representing licenses.
*/
@Query(value = "SELECT * FROM license_list", nativeQuery = true)
@Query("SELECT l FROM LPVSLicense l")
List<LPVSLicense> takeAllLicenses();

/**
Expand All @@ -35,10 +35,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.
*/
@Query(
value =
"SELECT * FROM license_list WHERE license_list.license_spdx = :spdxId ORDER BY id DESC LIMIT 1",
nativeQuery = true)
@Query("SELECT l FROM LPVSLicense l WHERE l.spdxId = :spdxId ORDER BY l.id DESC LIMIT 1")
LPVSLicense searchBySpdxId(@Param("spdxId") String spdxId);

/**
Expand All @@ -48,9 +45,8 @@ public interface LPVSLicenseRepository extends JpaRepository<LPVSLicense, Long>
* @return The latest {@link LPVSLicense} entity with the specified alternative license name.
*/
@Query(
value =
"SELECT * FROM license_list WHERE FIND_IN_SET(:licenseName, license_alternative_names) > 0 ORDER BY id DESC LIMIT 1",
nativeQuery = true)
"SELECT l FROM LPVSLicense l WHERE (CONCAT(',', l.alternativeNames, ',') LIKE CONCAT('%,', :licenseName, ',%')) "
+ "ORDER BY l.id DESC LIMIT 1")
LPVSLicense searchByAlternativeLicenseNames(@Param("licenseName") String licenseName);

/**
Expand Down
22 changes: 6 additions & 16 deletions src/main/java/com/lpvs/repository/LPVSPullRequestRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,13 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;

/**
* Repository interface for managing {@link LPVSPullRequest} entities.
* Extends {@link org.springframework.data.jpa.repository.JpaRepository} for basic CRUD operations.
*/
public interface LPVSPullRequestRepository extends JpaRepository<LPVSPullRequest, Long> {
/**
* Get the current date and time from the database.
*
* @return The current date and time.
*/
@Query(value = "SELECT now();", nativeQuery = true)
Date getNow();

/**
* Retrieves the latest LPVSPullRequest entity based on the provided criteria.
*
Expand All @@ -44,16 +35,15 @@ public interface LPVSPullRequestRepository extends JpaRepository<LPVSPullRequest
*/
@Query(
value =
"SELECT * FROM pull_requests pr "
"SELECT pr FROM LPVSPullRequest pr "
+ "WHERE (:user IS NULL OR pr.user = :user) "
+ "AND (:repositoryName IS NULL OR pr.repository_name = :repositoryName) "
+ "AND (:pullRequestFilesUrl IS NULL OR pr.diff_url = :pullRequestFilesUrl) "
+ "AND (:pullRequestHead IS NULL OR pr.pull_request_head = :pullRequestHead) "
+ "AND (:pullRequestBase IS NULL OR pr.pull_request_base = :pullRequestBase) "
+ "AND (:repositoryName IS NULL OR pr.repositoryName = :repositoryName) "
+ "AND (:pullRequestFilesUrl IS NULL OR pr.pullRequestFilesUrl = :pullRequestFilesUrl) "
+ "AND (:pullRequestHead IS NULL OR pr.pullRequestHead = :pullRequestHead) "
+ "AND (:pullRequestBase IS NULL OR pr.pullRequestBase = :pullRequestBase) "
+ "AND (:sender IS NULL OR pr.sender = :sender) "
+ "AND (:status IS NULL OR pr.status = :status) "
+ "ORDER BY pr.scan_date DESC LIMIT 1",
nativeQuery = true)
+ "ORDER BY pr.date DESC LIMIT 1")
LPVSPullRequest findLatestByPullRequestInfo(
@Param("user") String user,
@Param("repositoryName") String repositoryName,
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/lpvs/repository/LPVSQueueRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
public interface LPVSQueueRepository extends JpaRepository<LPVSQueue, Long> {

/**
* Retrieve the list of all entities from the "queue" table.
* Retrieve the list of all entities from the "lpvs_queue" table.
*
* @return List of {@link LPVSQueue} entities representing the queue.
*/
@Query(value = "SELECT * FROM queue", nativeQuery = true)
@Query("SELECT q FROM LPVSQueue q")
List<LPVSQueue> getQueueList();
}
3 changes: 1 addition & 2 deletions src/main/java/com/lpvs/service/LPVSLicenseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ protected LPVSLicense findLicenseByName(String name) {
if (license.getLicenseName().equalsIgnoreCase(name)) {
return license;
}
if (license.getAlternativeNames() != null
&& !license.getAlternativeNames().trim().isEmpty()) {
if (license.getAlternativeNames() != null && !license.getAlternativeNames().isBlank()) {
String[] names = license.getAlternativeNames().split(",");
for (String n : names) {
if (n.trim().equalsIgnoreCase(name)) {
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/lpvs/util/LPVSPayloadUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.lpvs.entity.LPVSLicense;
import com.lpvs.entity.LPVSQueue;
Expand Down Expand Up @@ -72,12 +73,12 @@ public static LPVSLicense convertOsoriDbResponseToLicense(String payload) {
lic.setAccess("UNREVIEWED");

List<String> nicknameList = new ArrayList<>();
detailInfoArray
.get(0)
.getAsJsonObject()
.get("nicknameList")
.getAsJsonArray()
.forEach(element -> nicknameList.add(element.getAsString()));
JsonElement nicknameListArray =
detailInfoArray.get(0).getAsJsonObject().get("nicknameList");
if (nicknameListArray != null && nicknameListArray.isJsonArray()) {
JsonArray nicknameArray = nicknameListArray.getAsJsonArray();
nicknameArray.forEach(element -> nicknameList.add(element.getAsString()));
}
lic.setAlternativeNames(String.join(",", nicknameList));

return lic;
Expand Down
28 changes: 14 additions & 14 deletions src/main/resources/database_dump.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CREATE SCHEMA IF NOT EXISTS lpvs;
USE lpvs;

CREATE TABLE IF NOT EXISTS license_list (
CREATE TABLE IF NOT EXISTS lpvs_license_list (
id bigint NOT NULL AUTO_INCREMENT,
license_usage varchar(255) DEFAULT NULL,
license_name varchar(255) NOT NULL,
Expand All @@ -11,18 +11,18 @@ CREATE TABLE IF NOT EXISTS license_list (
UNIQUE (license_spdx)
);

CREATE TABLE IF NOT EXISTS license_conflicts (
CREATE TABLE IF NOT EXISTS lpvs_license_conflicts (
id bigint NOT NULL AUTO_INCREMENT,
conflict_license_id bigint NOT NULL,
repository_license_id bigint NOT NULL,
PRIMARY KEY (id),
KEY (conflict_license_id),
KEY (repository_license_id),
FOREIGN KEY (conflict_license_id) REFERENCES license_list (id),
FOREIGN KEY (repository_license_id) REFERENCES license_list (id)
FOREIGN KEY (conflict_license_id) REFERENCES lpvs_license_list (id),
FOREIGN KEY (repository_license_id) REFERENCES lpvs_license_list (id)
);

CREATE TABLE IF NOT EXISTS pull_requests (
CREATE TABLE IF NOT EXISTS lpvs_pull_requests (
id bigint NOT NULL AUTO_INCREMENT,
scan_date datetime NOT NULL,
user varchar(255) DEFAULT NULL,
Expand All @@ -36,7 +36,7 @@ CREATE TABLE IF NOT EXISTS pull_requests (
PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS detected_license (
CREATE TABLE IF NOT EXISTS lpvs_detected_license (
id bigint NOT NULL AUTO_INCREMENT,
pull_request_id bigint DEFAULT NULL,
license_id bigint DEFAULT NULL,
Expand All @@ -59,13 +59,13 @@ CREATE TABLE IF NOT EXISTS detected_license (
KEY (license_id),
KEY (repository_license_id),
KEY (conflict_id),
FOREIGN KEY (conflict_id) REFERENCES license_conflicts (id),
FOREIGN KEY (license_id) REFERENCES license_list (id),
FOREIGN KEY (pull_request_id) REFERENCES pull_requests (id),
FOREIGN KEY (repository_license_id) REFERENCES license_list (id)
FOREIGN KEY (conflict_id) REFERENCES lpvs_license_conflicts (id),
FOREIGN KEY (license_id) REFERENCES lpvs_license_list (id),
FOREIGN KEY (pull_request_id) REFERENCES lpvs_pull_requests (id),
FOREIGN KEY (repository_license_id) REFERENCES lpvs_license_list (id)
);

CREATE TABLE IF NOT EXISTS queue (
CREATE TABLE IF NOT EXISTS lpvs_queue (
id bigint NOT NULL AUTO_INCREMENT,
action bigint NOT NULL,
attempts int DEFAULT '0',
Expand All @@ -84,7 +84,7 @@ CREATE TABLE IF NOT EXISTS queue (
PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS member (
CREATE TABLE IF NOT EXISTS lpvs_member (
id bigint PRIMARY KEY NOT NULL AUTO_INCREMENT,
email varchar(255) NOT NULL,
name varchar(255) NOT NULL,
Expand All @@ -94,7 +94,7 @@ CREATE TABLE IF NOT EXISTS member (
UNIQUE (email,provider)
);

INSERT INTO license_list (id, license_name, license_spdx, license_alternative_names, license_usage) VALUES
INSERT INTO lpvs_license_list (id, license_name, license_spdx, license_alternative_names, license_usage) VALUES
(1, 'GNU General Public License v3.0 only','GPL-3.0-only','','PROHIBITED'),
(2, 'OpenSSL License','OpenSSL','OPENSSL_LICENSE,SSLeay license and OpenSSL License','PERMITTED'),
(3, 'GNU Lesser General Public License v2.0 or later','LGPL-2.0-or-later','','RESTRICTED'),
Expand All @@ -103,5 +103,5 @@ INSERT INTO license_list (id, license_name, license_spdx, license_alternative_na
(6, 'GNU General Public License v2.0 only','GPL-2.0-only','','RESTRICTED'),
(7, 'GNU Lesser General Public License v3.0 or later','LGPL-3.0-or-later','GNU Lesser General Public License v3 or later (LGPLv3+),Lesser General Public License version 3 or greater,LGPLv3+','PROHIBITED');

INSERT INTO license_conflicts (conflict_license_id, repository_license_id) VALUES
INSERT INTO lpvs_license_conflicts (conflict_license_id, repository_license_id) VALUES
(1, 3), (1, 6), (2, 6), (2, 3), (3, 5), (3, 7), (5, 6), (6, 7);
8 changes: 4 additions & 4 deletions src/test/java/com/lpvs/entity/LPVSPullRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public class LPVSPullRequestTest {
final String pullRequestUrl = "pulRequestUrl";
final String pullRequestFilesUrl = "pullRequestFileUrl";
final String status = "status";
final String pullRequestBase = "user";
final String pullRequestHead = "user";
final String sender = "user";
final String pullRequestBase = "base";
final String pullRequestHead = "head";
final String sender = "sender";

@BeforeEach
void setUp() {
Expand All @@ -39,8 +39,8 @@ void setUp() {
pullRequestUrl,
pullRequestFilesUrl,
status,
pullRequestBase,
pullRequestHead,
pullRequestBase,
sender);
}

Expand Down
22 changes: 21 additions & 1 deletion src/test/java/com/lpvs/util/LPVSPayloadUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,30 @@ public void testConvertOsoriDbResponseToLicense() {
expectedLicense.getAlternativeNames(), actualLicense.getAlternativeNames());
}

@Test
public void testConvertOsoriDbResponseToLicense_nullNickname() {
String payload =
"{\"code\":\"200\",\"messageList\":{\"detailInfo\":[{\"name\":\"Apache License 2.0\",\"spdx_identifier\":\"Apache-2.0\",\"webpage\":\"https://spdx.org/licenses/Apache-2.0.html\",\"nicknameList\":null,\"webpageList\":null,\"restrictionList\":null}]},\"success\":true}";
LPVSLicense expectedLicense = new LPVSLicense();
expectedLicense.setLicenseName("Apache License 2.0");
expectedLicense.setSpdxId("Apache-2.0");
expectedLicense.setAccess("UNREVIEWED");
expectedLicense.setAlternativeNames("");

LPVSLicense actualLicense = LPVSPayloadUtil.convertOsoriDbResponseToLicense(payload);

assertNotNull(actualLicense);
assertEquals(expectedLicense.getLicenseName(), actualLicense.getLicenseName());
assertEquals(expectedLicense.getSpdxId(), actualLicense.getSpdxId());
assertEquals(expectedLicense.getAccess(), actualLicense.getAccess());
assertEquals(
expectedLicense.getAlternativeNames(), actualLicense.getAlternativeNames());
}

@Test
public void testConvertOsoriDbResponseToLicense_withInvalidPayload_N() {
String payload =
"{\"code\":\"200\",\"messageList\":{\"detailInfo\":[{\"name\":\"Apache License 2.0\",\"spdx_identifier\":\"Apache-2.0\",\"webpage\":\"https://spdx.org/licenses/Apache-2.0.html\",\"webpageList\":null,\"restrictionList\":null}]},\"success\":true}";
"{\"code\":\"200\",\"messageList\":{\"detailInfo\":[{\"name\":\"Apache License 2.0\",\"webpage\":\"https://spdx.org/licenses/Apache-2.0.html\",\"webpageList\":null,\"restrictionList\":null}]},\"success\":true}";
LPVSLicense actualLicense = LPVSPayloadUtil.convertOsoriDbResponseToLicense(payload);
assertNull(actualLicense);
}
Expand Down

0 comments on commit ef280cc

Please sign in to comment.