package net.straininfo2.grs.idloader;

import net.straininfo2.grs.idloader.bioproject.eutils.EutilsDownloader;
import net.straininfo2.grs.idloader.bioproject.xmlparsing.DocumentChunker;
import net.straininfo2.grs.idloader.bioproject.xmlparsing.DocumentChunkerTest;
import net.straininfo2.grs.idloader.bioproject.xmlparsing.DomainConverter;
import net.straininfo2.grs.idloader.bioproject.xmlparsing.PackageProcessor;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xml.sax.SAXException;

import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import static org.junit.Assert.assertTrue;

public class LoaderTest {

    @Test
    @Category(IntegrationTest.class)
    public void testConstruction() {
        Loader l = new ClassPathXmlApplicationContext("classpath:applicationContext.xml").getBean(Loader.class);
    }

    @Test
    @Category(IntegrationTest.class)
    public void loadIntoDb() throws ParserConfigurationException, IOException, SAXException, JAXBException {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Loader l = ctx.getBean(Loader.class);
        PackageProcessor dbloader = ctx.getBean(DomainConverter.class);
        DocumentChunker.parseXmlFile(DocumentChunkerTest.getBioProjectUrl(), dbloader);
        ctx.close();
    }

    @Test(expected = RuntimeException.class)
    public void badEmail() {
        Loader l = new Loader();
        l.checkEmailWasSet();
    }

    @Test
    public void goodEmail() {
        Loader l = new Loader();
        l.setDownloader(new EutilsDownloader());
        l.getDownloader().setEmail("blah@somethingelse");
        l.checkEmailWasSet();
    }

}

@@ -409,16 +409,69 @@
 cessor;%0A
+import org.junit.After;%0Aimport org.junit.AfterClass;%0A
 import o
@@ -481,24 +481,24 @@
 junit.Test;%0A
-
 import org.j
@@ -757,32 +757,88 @@
 io.IOException;%0A
+import java.nio.file.Files;%0Aimport java.nio.file.Paths;%0A
 import java.util
@@ -1033,32 +1033,58 @@
 ) %7B%0A        
-Loader l
+ClassPathXmlApplicationContext ctx
  = new Class
@@ -1144,37 +1144,117 @@
 xt.xml%22)
-.getBean(Loader.class
+;%0A        Loader l = ctx.getBean(Loader.class);%0A        ctx.close();%0A        removeTemporaryDatabase(
 );%0A    %7D
@@ -1734,16 +1734,16 @@
 oader);%0A
-
         
@@ -1751,24 +1751,261 @@
 tx.close();%0A
+        removeTemporaryDatabase();%0A    %7D%0A%0A    public static void removeTemporaryDatabase() %7B%0A        try %7B%0A            Files.delete(Paths.get(%22grsdb2.h2.db%22));%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A
     %7D%0A%0A    @

Remove temporary files after loader test