Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test to four test classes #7651

Merged
merged 23 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c67512d
Added unit tests with mocks for DuplicationSearch
BShaq Apr 19, 2021
35c486b
Added unit tests with mocks for ManageKeywordsViewModel
BShaq Apr 19, 2021
54dbb0d
Added unit tests with mocks for GroupDiff
BShaq Apr 19, 2021
d74f0a7
organized imports in DuplicateSearch
BShaq Apr 19, 2021
6953da2
Merge remote-tracking branch 'upstream/main' into a3-bs
BShaq Apr 19, 2021
54a6e3b
Added unit test to GroupTreeNodeTest.java
BShaq Apr 20, 2021
16e8b59
remove outcommeted code
BShaq Apr 20, 2021
b71be0d
fixed checkstyle
BShaq Apr 23, 2021
4b7f78e
Merge remote-tracking branch 'upstream/main' into a3-bs
BShaq Apr 23, 2021
81e79ee
switched to comparing content of lists in ManageKeywordsViewModelTest
BShaq Apr 26, 2021
3ecb4f8
removed outcommented code
BShaq Apr 26, 2021
97b8217
Merge remote-tracking branch 'upstream/main' into a3-bs
BShaq Apr 26, 2021
bd1d218
Merge remote-tracking branch 'upstream/main' into a3-bs
BShaq May 2, 2021
540e915
compare content in GroupDiffTest.java
BShaq May 15, 2021
a867e3a
created BeforeAll method
BShaq May 15, 2021
b8c996e
fixed checkstyle issue
BShaq May 15, 2021
42fc053
made entries creation more concise
BShaq May 15, 2021
6cd9986
Merge remote-tracking branch 'upstream/main' into a3-bs
BShaq May 15, 2021
cd37a64
Merge branch 'a3-bs' of https://github.com/ningxie1991/jabref into a3-bs
BShaq May 15, 2021
948f6c0
Fix checkstyle
koppor May 17, 2021
17c5b31
Merge branch 'main' into ningxie1991-a3-bs
koppor Jun 29, 2021
29718c5
Remove Globals dependency
koppor Jun 29, 2021
d4d8170
Fix Codacy
koppor Jun 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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..."));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi :)
I saw on the discussion #7636 that you would prefer using any() over a localized argument for the notify() method. The rationale for using this localized argument was to verify if dialogService shows the correct message for each condition. As @ningxie1991 pointed out, would the dialogService somehow loose its purpose when using any() as the argument?

Moreover, in this case, if I use the localized argument, both very() methods work, but by using any() as argument I receive the feedback that the dialogService got invoked only once. So, what did I miss on here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's okay to use the correct localization argument. I think we thought that it just verifies the numbers of calls, but not the content

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);
koppor marked this conversation as resolved.
Show resolved Hide resolved

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