package org.lockss.plugin.clockss.igpublishing;

import org.apache.commons.io.FileUtils;
import org.lockss.extractor.*;
import org.lockss.plugin.CachedUrl;
import org.lockss.plugin.clockss.SourceXmlMetadataExtractorTest;
import org.lockss.test.MockArchivalUnit;
import org.lockss.test.MockCachedUrl;
import org.lockss.util.CIProperties;
import org.lockss.util.Constants;
import org.lockss.util.Logger;

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

public class TestIGPublishingMetadataExtractorFactory extends SourceXmlMetadataExtractorTest {

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

    private static String BaseUrl = "http://source.host.org/sourcefiles/igroupnet-released/";
    private static String Directory = "2019";

    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/igpublishing/" + 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.xml";
        String journalXml = getXmlFileContent(fname);

        assertNotNull(journalXml);

        String xml_url = BaseUrl + Directory + "/" + fname;
        CIProperties xmlHeader = new CIProperties();
        xmlHeader.put(CachedUrl.PROPERTY_CONTENT_TYPE, "text/xml");

        MockArchivalUnit mau = new MockArchivalUnit();
        MockCachedUrl mcu = mau.addUrl(xml_url, true, true, xmlHeader);
        mcu.setContent(journalXml);
        mcu.setContentSize(journalXml.length());
        mcu.setProperty(CachedUrl.PROPERTY_CONTENT_TYPE, "text/xml");

        FileMetadataExtractor me =  new IGPublishingMetadataExtractorFactory().
                createFileMetadataExtractor(MetadataTarget.Any(), "text/xml");
        FileMetadataListExtractor mle =
                new FileMetadataListExtractor(me);
        List<ArticleMetadata> mdlist = mle.extract(MetadataTarget.Any(), mcu);
        assertNotEmpty(mdlist);
        assertEquals(1, mdlist.size());

        ArticleMetadata md = mdlist.get(0);
        assertNotNull(md);

        assertEquals("9781788398299", md.get(MetadataField.FIELD_ISBN));
        assertEquals("9781788479028", md.get(MetadataField.FIELD_EISBN));
        assertEquals("Soni, Mitesh", md.get(MetadataField.FIELD_AUTHOR));
        assertEquals("Practical AWS: networking build and manage complex networks using services such as Amazon VPC, Elastic Load Balancing, Direct Connect, and Amazon Route 53", md.get(MetadataField.FIELD_PUBLICATION_TITLE));
        assertEquals("IG Publishing", md.get(MetadataField.FIELD_PUBLISHER));
        assertEquals("2018", md.get(MetadataField.FIELD_DATE));
    }
}



@@ -46,48 +46,8 @@
 g;%0A%0A
-import org.apache.commons.io.FileUtils;%0A
 impo
@@ -280,107 +280,37 @@
 til.
-CIProperties;%0Aimport org.lockss.util.Constants;%0Aimport org.lockss.util.Logger;%0A%0Aimport java.io.File
+*;%0Aimport java.io.IOException
 ;%0Aim
@@ -319,34 +319,34 @@
 rt java.io.I
-OException
+nputStream
 ;%0Aimport jav
@@ -709,31 +709,24 @@
     private 
-static 
 String getXm
@@ -798,132 +798,53 @@
     
-try %7B%0A            String currentDirectory = System.getProperty(%22user.dir%22);%0A            String pathname = currentDirectory +
+InputStream file_input = null;%0A%0A        try %7B
 %0A   
@@ -856,83 +856,47 @@
     
-        %22/plugins/test/src/org/lockss/plugin/clockss/igpublishing/%22 + 
+file_input = getResourceAsStream(
 fname
+)
 ;%0A  
@@ -922,81 +922,87 @@
 t = 
-FileUtils.readFileToString(new File(pathname), Constants.DEFAULT_ENCODING
+StringUtil.fromInputStream(file_input);%0A            IOUtil.safeClose(file_input
 );%0A 
@@ -1072,24 +1072,86 @@
 sage(), e);%0A
+        %7D finally %7B%0A            IOUtil.safeClose(file_input);%0A
         %7D%0A  

update test to use getResourceAsStream function