package org.jabref.logic.importer.fetcher;

import java.io.IOException;
import java.net.URL;
import java.util.Optional;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.testutils.category.FetcherTest;

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

import static org.junit.jupiter.api.Assertions.assertEquals;

@FetcherTest
class DoiResolutionTest {

    private DoiResolution finder;
    private BibEntry entry;

    @BeforeEach
    void setUp() {
        finder = new DoiResolution();
        entry = new BibEntry();
    }

    @Test
    void linkWithPdfInTitleTag() throws IOException {
        entry.setField(StandardField.DOI, "10.1051/0004-6361/201527330");

        assertEquals(
                Optional.of(new URL("https://www.aanda.org/articles/aa/pdf/2016/01/aa27330-15.pdf")),
                finder.findFullText(entry)
        );
    }

    @Test
    void linkWithPdfStringLeadsToFulltext() throws IOException {
        entry.setField(StandardField.DOI, "10.1002/acr2.11101");
        assertEquals(Optional.of(new URL("https://onlinelibrary.wiley.com/doi/pdf/10.1002/acr2.11101")), finder.findFullText(entry));
    }

    @Test
    void citationMetaTagLeadsToFulltext() throws IOException {
        entry.setField(StandardField.DOI, "10.1007/978-3-319-89963-3_28");
        assertEquals(Optional.of(new URL("https://link.springer.com/content/pdf/10.1007%2F978-3-319-89963-3_28.pdf")), finder.findFullText(entry));
    }

    @Test
    void notReturnAnythingWhenMultipleLinksAreFound() throws IOException {
        entry.setField(StandardField.DOI, "10.1109/JXCDC.2019.2911135");
        assertEquals(Optional.empty(), finder.findFullText(entry));
    }

    @Test
    void notFoundByDOI() throws IOException {
        entry.setField(StandardField.DOI, "10.1186/unknown-doi");

        assertEquals(Optional.empty(), finder.findFullText(entry));
    }

    @Test
    void entityWithoutDoi() throws IOException {
        assertEquals(Optional.empty(), finder.findFullText(entry));
    }

    @Test
    void trustLevel() {
        assertEquals(TrustLevel.SOURCE, finder.getTrustLevel());
    }
}

@@ -260,49 +260,8 @@
 t;%0A%0A
-import org.junit.jupiter.api.BeforeEach;%0A
 impo
@@ -424,88 +424,8 @@
 ion 
-finder;%0A    private BibEntry entry;%0A%0A    @BeforeEach%0A    void setUp() %7B%0A        
 find
@@ -450,27 +450,40 @@
 tion();%0A    
-   
+private BibEntry
  entry = new
@@ -494,22 +494,16 @@
 Entry();
-%0A    %7D
 %0A%0A    @T
@@ -630,25 +630,24 @@
 01527330%22);%0A
-%0A
         asse
@@ -1628,32 +1628,797 @@
 entry));%0A    %7D%0A%0A
+    @Test%0A    void returnAnythingWhenBehindSpringerPayWall() throws IOException %7B%0A        // Springer returns a HTML page instead of an empty page,%0A        // even if the user does not have access%0A        // We cannot easily handle this case, because other publisher return the wrong media type.%0A        entry.setField(StandardField.DOI, %2210.1007/978-3-319-62594-2_12%22);%0A        assertEquals(Optional.of(new URL(%22https://link.springer.com/content/pdf/10.1007%252F978-3-319-62594-2_12.pdf%22)), finder.findFullText(entry));%0A    %7D%0A%0A    @Test%0A    void notReturnAnythingWhenBehindElsevierPayWall() throws IOException %7B%0A        entry.setField(StandardField.DOI, %2210.1016/j.pquantelec.2021.100316%22);%0A        assertEquals(Optional.empty(), finder.findFullText(entry));%0A    %7D%0A%0A
     @Test%0A    vo
@@ -2523,17 +2523,16 @@
 -doi%22);%0A
-%0A
         

Add test for paywalled articles