package org.lockss.plugin.clockss.elifesciences;

import org.apache.commons.io.FileUtils;
import org.lockss.daemon.ShouldNotHappenException;
import org.lockss.extractor.ArticleMetadata;
import org.lockss.extractor.FileMetadataExtractor;
import org.lockss.extractor.FileMetadataListExtractor;
import org.lockss.extractor.MetadataField;
import org.lockss.plugin.CachedUrl;
import org.lockss.plugin.clockss.JatsPublishingSchemaHelper;
import org.lockss.plugin.clockss.SourceXmlMetadataExtractorFactory;
import org.lockss.plugin.clockss.SourceXmlMetadataExtractorTest;
import org.lockss.plugin.clockss.SourceXmlSchemaHelper;
import org.lockss.util.Constants;
import org.lockss.util.Logger;
import org.w3c.dom.Document;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestElifeSciencesMetadataExtractor extends SourceXmlMetadataExtractorTest {

    private static final Logger log = Logger.getLogger(TestElifeSciencesMetadataExtractor.class);

    private static String BaseUrl = "http://source.host.org/sourcefiles/elifesciences/";
    private static String Directory = "2020";

    private static String getXmlFileContent(String fname) {
        String xmlContent = "";
        try {
            String currentDirectory = System.getProperty("user.dir");
            String pathname = currentDirectory +
                    "/plugins/test/src/org/lockss/plugin/clockss/elifesciences/" + fname;
            xmlContent = FileUtils.readFileToString(new File(pathname), Constants.DEFAULT_ENCODING);
        } catch(IOException e) {
            log.error(e.getMessage(), e);
        }
        return xmlContent;
    }


    public void testExtractArticleXmlSchema() throws Exception {

        String fname = "sample_test_jats.xml";
        String journalXml = getXmlFileContent(fname);
        assertNotNull(journalXml);

        String xml_url = BaseUrl + Directory + "/" + fname;

        FileMetadataExtractor me = new MyJatsPublishingSourceXmlMetadataExtractor();
        FileMetadataListExtractor mle =
                new FileMetadataListExtractor(me);
        List<ArticleMetadata> mdlist = extractFromContent(xml_url, "text/xml", journalXml, mle);
        assertNotEmpty(mdlist);
        ArticleMetadata md = mdlist.get(0);
        assertNotNull(md);
        assertEquals("10.7554/eLife.47946", md.get(MetadataField.FIELD_DOI));
        assertEquals("2020", md.get(MetadataField.FIELD_DATE));
        assertEquals(null, md.get(MetadataField.FIELD_END_PAGE));
        assertEquals(null, md.get(MetadataField.FIELD_START_PAGE));
        assertEquals(null, md.get(MetadataField.FIELD_ISSN));
        assertEquals("2050-084X", md.get(MetadataField.FIELD_EISSN));
        assertEquals("9", md.get(MetadataField.FIELD_VOLUME));
        assertEquals(null, md.get(MetadataField.FIELD_ISSUE));
        assertEquals("Tuerkova, Alzbeta", md.get(MetadataField.FIELD_AUTHOR));
        assertEquals("Effect of helical kink in antimicrobial peptides on membrane pore formation", md.get(MetadataField.FIELD_ARTICLE_TITLE));

    }

    private class MyJatsPublishingSourceXmlMetadataExtractor extends SourceXmlMetadataExtractorFactory.SourceXmlMetadataExtractor {

        private SourceXmlSchemaHelper JatsPublishingHelper = null;

        @Override
        protected SourceXmlSchemaHelper setUpSchema(CachedUrl cu) {
            throw new ShouldNotHappenException("This version of the schema setup cannot be used for this plugin");
        }

        @Override
        protected SourceXmlSchemaHelper setUpSchema(CachedUrl cu, Document xmlDoc) {
            if (JatsPublishingHelper == null) {
                JatsPublishingHelper = new JatsPublishingSchemaHelper();
            }
            return JatsPublishingHelper;
        }

        @Override
        protected List<String> getFilenamesAssociatedWithRecord(SourceXmlSchemaHelper helper, CachedUrl cu,
                                                                ArticleMetadata oneAM) {

            String url_string = cu.getUrl();
            List<String> returnList = new ArrayList<String>();
            returnList.add(url_string);
            return returnList;
        }

        @Override
        protected void postCookProcess(SourceXmlSchemaHelper schemaHelper,
                                       CachedUrl cu, ArticleMetadata thisAM) {

            //If we didn't get a valid date value, use the copyright year if it's there
            if (thisAM.get(MetadataField.FIELD_DATE) == null) {
                if (thisAM.getRaw(JatsPublishingSchemaHelper.JATS_date) != null) {
                    thisAM.put(MetadataField.FIELD_DATE, thisAM.getRaw(JatsPublishingSchemaHelper.JATS_date));
                } else {// last chance
                    thisAM.put(MetadataField.FIELD_DATE, thisAM.getRaw(JatsPublishingSchemaHelper.JATS_edate));
                }
            }
        }

    }

}


@@ -47,48 +47,8 @@
 s;%0A%0A
-import org.apache.commons.io.FileUtils;%0A
 impo
@@ -597,25 +597,22 @@
 ss.util.
-Constants
+IOUtil
 ;%0Aimport
@@ -632,24 +632,59 @@
 til.Logger;%0A
+import org.lockss.util.StringUtil;%0A
 import org.w
@@ -703,30 +703,8 @@
 ent;
-%0A%0Aimport java.io.File;
 %0Aimp
@@ -724,24 +724,52 @@
 OException;%0A
+import java.io.InputStream;%0A
 import java.
@@ -1141,31 +1141,24 @@
     private 
-static 
 String getXm
@@ -1230,93 +1230,48 @@
     
-try %7B%0A            String currentDirectory = System.getProperty(%22user.dir%22);%0A 
+InputStream file_input = null;%0A%0A
         
    S
@@ -1270,47 +1270,13 @@
     
-   String pathname = currentDirectory +
+try %7B
 %0A   
@@ -1288,84 +1288,47 @@
     
-        %22/plugins/test/src/org/lockss/plugin/clockss/elifesciences/%22 + 
+file_input = getResourceAsStream(
 fname
+)
 ;%0A  
@@ -1354,81 +1354,87 @@
 t = 
-FileUtils.readFileToString(new File(pathname), Constants.DEFAULT_ENCODING
+StringUtil.fromInputStream(file_input);%0A            IOUtil.safeClose(file_input
 );%0A 
@@ -1447,16 +1447,17 @@
  %7D catch
+ 
 (IOExcep
@@ -1504,24 +1504,86 @@
 sage(), e);%0A
+        %7D finally %7B%0A            IOUtil.safeClose(file_input);%0A
         %7D%0A  
@@ -1614,17 +1614,16 @@
 %0A    %7D%0A%0A
-%0A
     publ

update test to use getResourceAsStream function