package ae3.servlet;

import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.SolrCore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
import ae3.util.AtlasProperties;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.io.FilenameFilter;

import static org.junit.Assert.*;

/**
 * @author ostolop
 */
public class GoogleSitemapXmlServletTest {
    final private Logger log = LoggerFactory.getLogger(getClass());

    private SolrCore core;

    @Before
    public void setUp() throws IOException, ParserConfigurationException, SAXException {
        final String solrIndexLocation = AtlasProperties.getProperty("atlas.solrIndexLocation");
        final CoreContainer multiCore = new CoreContainer(solrIndexLocation, new File(solrIndexLocation, "solr.xml"));

        core = multiCore.getCore("atlas");
    }

    @After
    public void tearDown() {
        core.close();
        core = null;

        // cleanup
        String[] filesToDelete = new File(System.getProperty("java.io.tmpdir")).list(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                if(name.startsWith("geneSitemap"))
                    return true;

                return false;
            }
        });

        for (String f : filesToDelete) {
            if(!(new File(f).delete()))
                log.error("Couldn't delete temporary file " + f + " in " + System.getProperty("java.io.tmpdir"));
        }
    }

    @Test
    public void testWriteGeneSitemap() {
        GoogleSitemapXmlServlet svt = new GoogleSitemapXmlServlet();

        svt.setBasePath(System.getProperty("java.io.tmpdir"));
        svt.writeGeneSitemap(core);

        File geneSitemapIndex = new File(svt.getBasePath() + File.separator + "geneSitemapIndex.xml");
        assertTrue(geneSitemapIndex.exists());

        File geneSitemap0 = new File(svt.getBasePath() + File.separator + "geneSitemap0.xml.gz");
        assertTrue(geneSitemap0.exists());
    }
}

@@ -36,61 +36,9 @@
 nit.
-Before;%0Aimport org.junit.After;%0Aimport org.junit.Test
+*
 ;%0Aim
@@ -238,16 +238,80 @@
 perties;
+%0Aimport ae3.service.structuredquery.AtlasStructuredQueryService;
 %0A%0Aimport
@@ -473,16 +473,73 @@
 ssert.*;
+%0Aimport uk.ac.ebi.ae3.indexbuilder.AbstractOnceIndexTest;
 %0A%0A/**%0A *
@@ -600,16 +600,46 @@
 letTest 
+extends AbstractOnceIndexTest 
 %7B%0A    fi
@@ -717,21 +717,38 @@
 ate 
-SolrCore core
+static CoreContainer container
 ;%0A%0A 
@@ -757,16 +757,21 @@
  @Before
+Class
 %0A    pub
@@ -778,18 +778,34 @@
 lic 
-void setUp
+static  void initContainer
 () t
@@ -810,18 +810,16 @@
  throws 
-IO
 Exceptio
@@ -823,271 +823,363 @@
 tion
-, ParserConfigurationException, SAXException %7B
+ %7B%0A        container = new CoreContainer(getSolrPath().toString(), new File(getSolrPath(), %22solr.xml%22));
 %0A    
+%7D%0A%0A
     
-final String solrIndexLocation = AtlasProperties.getProperty(%22atlas.solrIndexLocation%22
+@AfterClass%0A    public static void shutdownContainer() throws Exception %7B%0A        container.shutdown(
 );%0A    
+%7D%0A%0A
     
-final CoreContainer multiCore = new CoreContainer(solrIndexLocation, new File(solrIndexLocation, %22solr.xml%22));%0A
+private SolrCore core;%0A%0A    @Before%0A    public void setUp() throws IOException, ParserConfigurationException, SAXException %7B
 %0A   
@@ -1194,17 +1194,17 @@
 e = 
-multiCore
+container
 .get

Migrate sitemap servlet test to new stub index