package net.sf.jabref.logic.importer.fetcher;

import java.util.Optional;

import net.sf.jabref.Globals;
import net.sf.jabref.logic.importer.FetcherException;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.BibLatexEntryTypes;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class DoiFetcherTest {

    private DoiFetcher fetcher;
    private BibEntry bibEntryBurd2011, bibEntryDecker2007;

    @Before
    public void setUp() {
        fetcher = new DoiFetcher(Globals.prefs.getImportFormatPreferences());

        bibEntryBurd2011 = new BibEntry();
        bibEntryBurd2011.setType(BibLatexEntryTypes.BOOK);
        bibEntryBurd2011.setField("bibtexkey", "Burd_2011");
        bibEntryBurd2011.setField("title", "Java{\\textregistered} For Dummies{\\textregistered}");
        bibEntryBurd2011.setField("publisher", "Wiley-Blackwell");
        bibEntryBurd2011.setField("year", "2011");
        bibEntryBurd2011.setField("author", "Barry Burd");
        bibEntryBurd2011.setField("month", "jul");
        bibEntryBurd2011.setField("doi", "10.1002/9781118257517");
        bibEntryBurd2011.setField("url", "http://dx.doi.org/10.1002/9781118257517");

        bibEntryDecker2007 = new BibEntry();
        bibEntryDecker2007.setType(BibLatexEntryTypes.INPROCEEDINGS);
        bibEntryDecker2007.setField("bibtexkey", "Decker_2007");
        bibEntryDecker2007.setField("author", "Gero Decker and Oliver Kopp and Frank Leymann and Mathias Weske");
        bibEntryDecker2007.setField("booktitle", "{IEEE} International Conference on Web Services ({ICWS} 2007)");
        bibEntryDecker2007.setField("month", "jul");
        bibEntryDecker2007.setField("publisher", "Institute of Electrical {\\&} Electronics Engineers ({IEEE})");
        bibEntryDecker2007.setField("title", "{BPEL}4Chor: Extending {BPEL} for Modeling Choreographies");
        bibEntryDecker2007.setField("url", "http://dx.doi.org/10.1109/ICWS.2007.59");
        bibEntryDecker2007.setField("year", "2007");
        bibEntryDecker2007.setField("doi", "10.1109/icws.2007.59");
    }

    @Test
    public void testGetName() {
        assertEquals("DOI", fetcher.getName());
    }

    @Test
    public void testGetHelpPage() {
        assertEquals("DOItoBibTeXHelp", fetcher.getHelpPage().getPageName());
    }

    @Test
    public void testPerformSearchBurd2011() throws FetcherException {
        Optional<BibEntry> fetchedEntry = fetcher.performSearchById("10.1002/9781118257517");
        assertEquals(Optional.of(bibEntryBurd2011), fetchedEntry);
    }

    @Test
    public void testPerformSearchDecker2007() throws FetcherException {
        Optional<BibEntry> fetchedEntry = fetcher.performSearchById("10.1109/ICWS.2007.59");
        assertEquals(Optional.of(bibEntryDecker2007), fetchedEntry);
    }

    @Test(expected = FetcherException.class)
    public void testPerformSearchEmptyDOI() throws FetcherException {
        Optional<BibEntry> fetchedEntry = fetcher.performSearchById("");
        assertEquals(Optional.empty(), fetchedEntry);
    }

    @Test(expected = FetcherException.class)
    public void testPerformSearchInvalidDOI() throws FetcherException {
        fetcher.performSearchById("10.1002/9781118257517F");
        fail();
    }
}

@@ -72,38 +72,8 @@
 l;%0A%0A
-import net.sf.jabref.Globals;%0A
 impo
@@ -217,16 +217,68 @@
 ryTypes;
+%0Aimport net.sf.jabref.preferences.JabRefPreferences;
 %0A%0Aimport
@@ -601,21 +601,39 @@
 her(
-Globals.prefs
+JabRefPreferences.getInstance()
 .get

use JabRefPreferences.getInstance() to fix ci