From 0b0b251d02feeb6d61277d2bb0c4985967c17c92 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Tue, 5 Mar 2024 15:09:58 +0100 Subject: [PATCH] Add BibEntryRelationsRepositoryTest (#10983) --- .../BibEntryRelationsRepositoryTest.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/test/java/org/jabref/gui/entryeditor/citationrelationtab/BibEntryRelationsRepositoryTest.java diff --git a/src/test/java/org/jabref/gui/entryeditor/citationrelationtab/BibEntryRelationsRepositoryTest.java b/src/test/java/org/jabref/gui/entryeditor/citationrelationtab/BibEntryRelationsRepositoryTest.java new file mode 100644 index 00000000000..41106d57a6b --- /dev/null +++ b/src/test/java/org/jabref/gui/entryeditor/citationrelationtab/BibEntryRelationsRepositoryTest.java @@ -0,0 +1,61 @@ +package org.jabref.gui.entryeditor.citationrelationtab; + +import java.util.List; + +import org.jabref.gui.entryeditor.citationrelationtab.semanticscholar.SemanticScholarFetcher; +import org.jabref.model.entry.BibEntry; +import org.jabref.model.entry.field.StandardField; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class BibEntryRelationsRepositoryTest { + + private List getCitedBy(BibEntry entry) { + return List.of(createCitingBibEntry(entry)); + } + + private BibEntry createBibEntry(int i) { + return new BibEntry() + .withCitationKey("entry" + i) + .withField(StandardField.DOI, "10.1234/5678" + i); + } + + private BibEntry createCitingBibEntry(Integer i) { + return new BibEntry() + .withCitationKey("citing_entry" + i) + .withField(StandardField.DOI, "10.2345/6789" + i); + } + + private BibEntry createCitingBibEntry(BibEntry citedEntry) { + return createCitingBibEntry(Integer.valueOf(citedEntry.getCitationKey().get().substring(5))); + } + + @Test + void getCitations() throws Exception { + SemanticScholarFetcher semanticScholarFetcher = mock(SemanticScholarFetcher.class); + when(semanticScholarFetcher.searchCitedBy(any(BibEntry.class))).thenAnswer(invocation -> { + BibEntry entry = invocation.getArgument(0); + return getCitedBy(entry); + }); + BibEntryRelationsCache bibEntryRelationsCache = new BibEntryRelationsCache(); + + BibEntryRelationsRepository bibEntryRelationsRepository = new BibEntryRelationsRepository(semanticScholarFetcher, bibEntryRelationsCache); + + for (int i = 0; i < 150; i++) { + BibEntry entry = createBibEntry(i); + List citations = bibEntryRelationsRepository.getCitations(entry); + assertEquals(getCitedBy(entry), citations); + } + + for (int i = 0; i < 150; i++) { + BibEntry entry = createBibEntry(i); + List citations = bibEntryRelationsRepository.getCitations(entry); + assertEquals(getCitedBy(entry), citations); + } + } +}