Skip to content

Commit

Permalink
Add unit test to four test classes (#7651)
Browse files Browse the repository at this point in the history
  • Loading branch information
BShaq authored Jun 29, 2021
1 parent c168fbd commit a01754c
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class GroupDiff {
private final GroupTreeNode originalGroupRoot;
private final GroupTreeNode newGroupRoot;

private GroupDiff(GroupTreeNode originalGroupRoot, GroupTreeNode newGroupRoot) {
GroupDiff(GroupTreeNode originalGroupRoot, GroupTreeNode newGroupRoot) {
this.originalGroupRoot = originalGroupRoot;
this.newGroupRoot = newGroupRoot;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.jabref.gui.duplicationFinder;

import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;

import org.jabref.gui.DialogService;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.gui.undo.CountingUndoManager;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.util.OptionalObjectProperty;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.testutils.category.GUITest;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testfx.framework.junit5.ApplicationExtension;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@GUITest
@ExtendWith(ApplicationExtension.class)
public class DuplicateSearchTest {

private final DialogService dialogService = spy(DialogService.class);
private final StateManager stateManager = mock(StateManager.class);
private final JabRefFrame jabRefFrame = mock(JabRefFrame.class);
private final LibraryTab libraryTab = mock(LibraryTab.class);
private final BibDatabaseContext bibDatabaseContext = mock(BibDatabaseContext.class);
private final CountingUndoManager undoManager = mock(CountingUndoManager.class);

private DuplicateSearch duplicateSearch;
private BibEntry entry1;

@BeforeEach
void setupDuplicateSearchInstance() {
entry1 = new BibEntry(StandardEntryType.InProceedings)
.withField(StandardField.AUTHOR, "Souti Chattopadhyay and Nicholas Nelson and Audrey Au and Natalia Morales and Christopher Sanchez and Rahul Pandita and Anita Sarma")
.withField(StandardField.TITLE, "A tale from the trenches")
.withField(StandardField.YEAR, "2020")
.withField(StandardField.DOI, "10.1145/3377811.3380330")
.withField(StandardField.SUBTITLE, "cognitive biases and software development")
.withCitationKey("Chattopadhyay2020");

when(jabRefFrame.getCurrentLibraryTab()).thenReturn(libraryTab);
when(stateManager.activeDatabaseProperty()).thenReturn(OptionalObjectProperty.empty());
duplicateSearch = new DuplicateSearch(jabRefFrame, dialogService, stateManager);
}

@Test
public void executeWithNoEntries() {
when(stateManager.getActiveDatabase()).thenReturn(Optional.of(bibDatabaseContext));
when(bibDatabaseContext.getEntries()).thenReturn(Collections.emptyList());

duplicateSearch.execute();
verify(dialogService, times(1)).notify(Localization.lang("Searching for duplicates..."));
}

@Test
public void executeWithOneEntry() {
when(stateManager.getActiveDatabase()).thenReturn(Optional.of(bibDatabaseContext));
when(bibDatabaseContext.getEntries()).thenReturn(Collections.singletonList(entry1));

duplicateSearch.execute();
verify(dialogService, times(1)).notify(Localization.lang("Searching for duplicates..."));
}

@Test
public void executeWithNoDuplicates() {
BibEntry entry2 = new BibEntry(StandardEntryType.InProceedings)
.withField(StandardField.AUTHOR, "Tale S Sastad and Karl Thomas Hjelmervik")
.withField(StandardField.TITLE, "Synthesizing Realistic, High-Resolution Anti-Submarine Sonar Data\n")
.withField(StandardField.YEAR, "2018")
.withField(StandardField.DOI, "10.1109/OCEANSKOBE.2018.8558837")
.withCitationKey("Sastad2018");

when(stateManager.getActiveDatabase()).thenReturn(Optional.of(bibDatabaseContext));
when(bibDatabaseContext.getEntries()).thenReturn(Arrays.asList(entry1, entry2));
when(bibDatabaseContext.getMode()).thenReturn(BibDatabaseMode.BIBTEX);
when(libraryTab.getBibDatabaseContext()).thenReturn(bibDatabaseContext);
when(libraryTab.getDatabase()).thenReturn(mock(BibDatabase.class));
when(libraryTab.getUndoManager()).thenReturn(undoManager);
when(undoManager.addEdit(mock(NamedCompound.class))).thenReturn(true);

duplicateSearch.execute();
verify(dialogService, times(1)).notify(Localization.lang("Searching for duplicates..."));
verify(dialogService, times(1)).notify(Localization.lang("Duplicates found") + ": " + String.valueOf(0) + ' '
+ Localization.lang("pairs processed") + ": " + String.valueOf(0));
}
}
83 changes: 83 additions & 0 deletions src/test/java/org/jabref/gui/edit/ManageKeywordsViewModelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.jabref.gui.edit;

import java.util.Arrays;
import java.util.List;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.preferences.PreferencesService;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ManageKeywordsViewModelTest {

private final PreferencesService preferences = mock(PreferencesService.class);
private ManageKeywordsViewModel keywordsViewModel;

@BeforeEach
void setUp() {
BibEntry entryOne = new BibEntry(StandardEntryType.Article)
.withField(StandardField.AUTHOR, "Prakhar Srivastava and Nishant Singh")
.withField(StandardField.YEAR, "2020")
.withField(StandardField.DOI, "10.1109/PARC49193.2020.236624")
.withField(StandardField.ISBN, "978-1-7281-6575-2")
.withField(StandardField.JOURNALTITLE, "2020 International Conference on Power Electronics & IoT Applications in Renewable Energy and its Control (PARC)")
.withField(StandardField.PAGES, "351--354")
.withField(StandardField.PUBLISHER, "IEEE")
.withField(StandardField.TITLE, "Automatized Medical Chatbot (Medibot)")
.withField(StandardField.KEYWORDS, "Human-machine interaction, Chatbot, Medical Chatbot, Natural Language Processing, Machine Learning, Bot");

BibEntry entryTwo = new BibEntry(StandardEntryType.Article)
.withField(StandardField.AUTHOR, "Mladjan Jovanovic and Marcos Baez and Fabio Casati")
.withField(StandardField.DATE, "November 2020")
.withField(StandardField.YEAR, "2020")
.withField(StandardField.DOI, "10.1109/MIC.2020.3037151")
.withField(StandardField.ISSN, "1941-0131")
.withField(StandardField.JOURNALTITLE, "IEEE Internet Computing")
.withField(StandardField.PAGES, "1--1")
.withField(StandardField.PUBLISHER, "IEEE")
.withField(StandardField.TITLE, "Chatbots as conversational healthcare services")
.withField(StandardField.KEYWORDS, "Chatbot, Medical services, Internet, Data collection, Medical diagnostic imaging, Automation, Vocabulary");

List<BibEntry> entries = List.of(entryOne, entryTwo);

char delimiter = ',';
when(preferences.getKeywordDelimiter()).thenReturn(delimiter);

keywordsViewModel = new ManageKeywordsViewModel(preferences, entries);
}

@Test
void keywordsFilledInCorrectly() {
ObservableList<String> addedKeywords = keywordsViewModel.getKeywords();
List<String> expectedKeywordsList = Arrays.asList("Human-machine interaction", "Chatbot", "Medical Chatbot",
"Natural Language Processing", "Machine Learning", "Bot", "Chatbot", "Medical services", "Internet",
"Data collection", "Medical diagnostic imaging", "Automation", "Vocabulary");

assertEquals(FXCollections.observableList(expectedKeywordsList), addedKeywords);
}

@Test
void removedKeywordNotIncludedInKeywordsList() {
ObservableList<String> modifiedKeywords = keywordsViewModel.getKeywords();
List<String> originalKeywordsList = Arrays.asList("Human-machine interaction", "Chatbot", "Medical Chatbot",
"Natural Language Processing", "Machine Learning", "Bot", "Chatbot", "Medical services", "Internet",
"Data collection", "Medical diagnostic imaging", "Automation", "Vocabulary");

assertEquals(FXCollections.observableList(originalKeywordsList), modifiedKeywords, "compared lists are not identical");

keywordsViewModel.removeKeyword("Human-machine interaction");

assertNotEquals(FXCollections.observableList(originalKeywordsList), modifiedKeywords, "compared lists are identical");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.jabref.logic.bibtex.comparator;

import java.util.Optional;

import org.jabref.model.groups.AllEntriesGroup;
import org.jabref.model.groups.ExplicitGroup;
import org.jabref.model.groups.GroupHierarchyType;
import org.jabref.model.groups.GroupTreeNode;
import org.jabref.model.metadata.MetaData;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class GroupDiffTest {

private final MetaData originalMetaData = mock(MetaData.class);
private final MetaData newMetaData = mock(MetaData.class);
private GroupTreeNode rootOriginal;

@BeforeEach
void setup() {
rootOriginal = GroupTreeNode.fromGroup(new AllEntriesGroup("All entries"));
rootOriginal.addSubgroup(new ExplicitGroup("ExplicitA", GroupHierarchyType.INCLUDING, ','));
GroupTreeNode parent = rootOriginal
.addSubgroup(new ExplicitGroup("ExplicitParent", GroupHierarchyType.INDEPENDENT, ','));
parent.addSubgroup(new ExplicitGroup("ExplicitNode", GroupHierarchyType.REFINING, ','));
}

@Test
void compareEmptyGroups() {
when(originalMetaData.getGroups()).thenReturn(Optional.empty());
when(newMetaData.getGroups()).thenReturn(Optional.empty());

assertEquals(Optional.empty(), GroupDiff.compare(originalMetaData, newMetaData));
}

@Test
void compareGroupWithItself() {
when(originalMetaData.getGroups()).thenReturn(Optional.of(rootOriginal));
when(newMetaData.getGroups()).thenReturn(Optional.of(rootOriginal));

assertEquals(Optional.empty(), GroupDiff.compare(originalMetaData, newMetaData));
}

@Test
void compareWithChangedGroup() {
GroupTreeNode rootModified = GroupTreeNode.fromGroup(new AllEntriesGroup("All entries"));
rootModified.addSubgroup(new ExplicitGroup("ExplicitA", GroupHierarchyType.INCLUDING, ','));

when(originalMetaData.getGroups()).thenReturn(Optional.of(rootOriginal));
when(newMetaData.getGroups()).thenReturn(Optional.of(rootModified));

Optional<GroupDiff> groupDiff = GroupDiff.compare(originalMetaData, newMetaData);

Optional<GroupDiff> expectedGroupDiff = Optional.of(new GroupDiff(newMetaData.getGroups().get(), originalMetaData.getGroups().get()));

assertEquals(expectedGroupDiff.get().getNewGroupRoot(), groupDiff.get().getNewGroupRoot());
assertEquals(expectedGroupDiff.get().getOriginalGroupRoot(), groupDiff.get().getOriginalGroupRoot());
}

}
Loading

0 comments on commit a01754c

Please sign in to comment.