|
| 1 | +package org.jabref.logic.search.indexing; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.PreparedStatement; |
| 5 | +import java.sql.ResultSet; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.jabref.logic.search.PostgreServer; |
| 9 | +import org.jabref.logic.util.BackgroundTask; |
| 10 | +import org.jabref.model.database.BibDatabaseContext; |
| 11 | +import org.jabref.model.entry.BibEntry; |
| 12 | +import org.jabref.model.entry.BibEntryPreferences; |
| 13 | +import org.jabref.model.entry.field.StandardField; |
| 14 | +import org.jabref.model.entry.types.StandardEntryType; |
| 15 | +import org.jabref.model.search.PostgreConstants; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +public class BibFieldsIndexerTest { |
| 26 | + |
| 27 | + private PostgreServer postgreServer; |
| 28 | + |
| 29 | + private final BibEntryPreferences bibEntryPreferences = mock(BibEntryPreferences.class); |
| 30 | + |
| 31 | + @BeforeEach |
| 32 | + void setUp() { |
| 33 | + when(bibEntryPreferences.getKeywordSeparator()).thenReturn(','); |
| 34 | + postgreServer = new PostgreServer(); |
| 35 | + } |
| 36 | + |
| 37 | + @AfterEach |
| 38 | + void tearDown() { |
| 39 | + if (postgreServer != null) { |
| 40 | + postgreServer.shutdown(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void addToIndexIsIdempotentForSameEntry() throws Exception { |
| 46 | + BibDatabaseContext databaseContext = new BibDatabaseContext(); |
| 47 | + Connection connection = postgreServer.getConnection(); |
| 48 | + BibFieldsIndexer indexer = new BibFieldsIndexer(bibEntryPreferences, databaseContext, connection); |
| 49 | + |
| 50 | + BibEntry entry = new BibEntry(StandardEntryType.Article); |
| 51 | + entry.withCitationKey("https://doi.org/10.48550/arxiv.2405.02318"); |
| 52 | + entry.withField(StandardField.TITLE, "Cool Paper"); |
| 53 | + entry.withField(StandardField.AUTHOR, "Doe, John"); |
| 54 | + entry.withField(StandardField.GROUPS, "Imported entries, Other"); |
| 55 | + |
| 56 | + BackgroundTask<?> dummyTask = new BackgroundTask<>() { |
| 57 | + @Override |
| 58 | + public Object call() { |
| 59 | + return null; |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + // Index the same entry twice - this used to throw due to a duplicate key on (entryid, field_name) |
| 64 | + indexer.addToIndex(List.of(entry), dummyTask); |
| 65 | + indexer.addToIndex(List.of(entry), dummyTask); |
| 66 | + |
| 67 | + // Verify that there's exactly one row for (entryid, 'citationkey') |
| 68 | + String tableRef = PostgreConstants.getMainTableSchemaReference(indexer.getTable()); |
| 69 | + String sql = "SELECT COUNT(*) FROM " + tableRef + " WHERE \"" + PostgreConstants.ENTRY_ID + "\" = ? AND \"" + PostgreConstants.FIELD_NAME + "\" = ?"; |
| 70 | + try (PreparedStatement ps = connection.prepareStatement(sql)) { |
| 71 | + ps.setString(1, entry.getId()); |
| 72 | + ps.setString(2, "citationkey"); |
| 73 | + try (ResultSet rs = ps.executeQuery()) { |
| 74 | + rs.next(); |
| 75 | + int count = rs.getInt(1); |
| 76 | + assertEquals(1, count); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Verify that there's exactly one row for (entryid, 'groups') in the main table as well |
| 81 | + try (PreparedStatement ps = connection.prepareStatement(sql)) { |
| 82 | + ps.setString(1, entry.getId()); |
| 83 | + ps.setString(2, "groups"); |
| 84 | + try (ResultSet rs = ps.executeQuery()) { |
| 85 | + rs.next(); |
| 86 | + int count = rs.getInt(1); |
| 87 | + assertEquals(1, count); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Cleanup resources gracefully |
| 92 | + indexer.closeAndWait(); |
| 93 | + } |
| 94 | +} |
0 commit comments