/*
 * Copyright 2012 SFB 632.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package annis;

import annis.dao.AnnisDao;
import annis.dao.SpringAnnisDao;
import annis.provider.SDocumentGraphProvider;
import annis.test.TestHelper;
import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
import com.carrotsearch.junitbenchmarks.BenchmarkRule;
import com.carrotsearch.junitbenchmarks.annotation.AxisRange;
import com.carrotsearch.junitbenchmarks.annotation.BenchmarkHistoryChart;
import com.carrotsearch.junitbenchmarks.annotation.BenchmarkMethodChart;
import com.carrotsearch.junitbenchmarks.annotation.LabelType;
import com.google.common.io.ByteStreams;
import com.sun.jersey.core.util.StringKeyIgnoreCaseMultivaluedMap;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.Resource;
import javax.ws.rs.core.MediaType;
import org.corpus_tools.salt.common.SaltProject;
import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeNoException;
import static org.junit.Assume.assumeNotNull;
import static org.junit.Assume.assumeTrue;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
import org.springframework.dao.DataAccessException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

;

/**
 * This will execute tests on a real database and check if the counts are OK.
 *
 * @author Thomas Krause <krauseto@hu-berlin.de>
 */
@RunWith(SpringJUnit4ClassRunner.class)
// TODO: do not test context only for annopool
@ContextConfiguration(locations =
{
  "file:src/main/distribution/conf/spring/Common.xml",
  "file:src/main/distribution/conf/spring/SqlGenerator.xml",
  "file:src/main/distribution/conf/spring/Dao.xml"
  
}, loader = AnnisXmlContextLoader.class)
@BenchmarkOptions(callgc = false, benchmarkRounds = 5, warmupRounds = 5)
@BenchmarkMethodChart(filePrefix = "annis-benchmark")
@BenchmarkHistoryChart(labelWith = LabelType.RUN_ID, maxRuns = 20)
@AxisRange(min = 0.0)
@Ignore
public class BenchmarkTest
{

  @Rule
  public TestRule benchmarkRun = new BenchmarkRule();

  @Resource(name = "annisDao")
  AnnisDao annisDao;

  private List<Long> pcc2CorpusID;

  private List<Long> ridgesCorpusID;

  private final SDocumentGraphProvider provider = new SDocumentGraphProvider();

  private final OutputStream nullStream = ByteStreams.nullOutputStream();
  private final MediaType typeXMI = new MediaType("application", "xmi+xml");
  private final MediaType typeBinary = new MediaType("application", "xmi+binary");

  @Before
  public void setup()
  {
    SpringAnnisDao springAnnisDao = (SpringAnnisDao) TestHelper.proxyTarget(
      annisDao);

    try
    {
      assumeNotNull(springAnnisDao.getSimpleJdbcTemplate());

      springAnnisDao.getSimpleJdbcTemplate().queryForInt("SELECT 1");

    }
    catch (DataAccessException ex)
    {
      assumeNoException(ex);
    }

    // get the id of the "pcc2" corpus
    pcc2CorpusID = getCorpusIDs("pcc2");

    // get the id of the "Ridges_Herbology_Version_2.0" corpus
    ridgesCorpusID = getCorpusIDs("Ridges_Herbology_Version_2.0");


  }

  private List<Long> getCorpusIDs(String corpus)
  {
    // (and check if it's there, otherwise ignore these tests)
    List<String> corpusNames = new LinkedList<>();
    corpusNames.add(corpus);
    List<Long> corpusIDs = annisDao.mapCorpusNamesToIds(corpusNames);
    return corpusIDs;
  }

  @Test
  public void mapSalt_Pcc4282()
  {
    assumeTrue(pcc2CorpusID.size() > 0);

    SaltProject p = annisDao.retrieveAnnotationGraph("pcc2",
        "4282", null);

    assertEquals(1, p.getCorpusGraphs().size());
  }

  @Test
  public void mapSaltAndSaveXMI_Pcc4282() throws IOException
  {
    assumeTrue(ridgesCorpusID.size() > 0);

    SaltProject p = annisDao.retrieveAnnotationGraph("pcc2",
        "4282", null);
    provider.writeTo(p.getCorpusGraphs().get(0).getDocuments().get(0).getDocumentGraph(), SaltProject.class, null, null,
      typeXMI, new StringKeyIgnoreCaseMultivaluedMap<>(),
      nullStream);
  }


  @Test
  public void mapSalt_SonderbaresKraeuterBuch()
  {
    assumeTrue(ridgesCorpusID.size() > 0);

    SaltProject p = annisDao.retrieveAnnotationGraph("Ridges_Herbology_Version_2.0",
        "sonderbares.kraeuterbuch.16175.11-21", null);

    assertEquals(1, p.getCorpusGraphs().size());
  }

  @Test
  public void mapSaltAndSaveXMI_SonderbaresKraeuterBuch() throws IOException
  {
    assumeTrue(ridgesCorpusID.size() > 0);

    SaltProject p = annisDao.retrieveAnnotationGraph("Ridges_Herbology_Version_2.0",
        "sonderbares.kraeuterbuch.16175.11-21", null);
    provider.writeTo(p.getCorpusGraphs().get(0).getDocuments().get(0).getDocumentGraph(), 
      SaltProject.class, null, null,
      typeXMI, null,
      nullStream);
  }
}

@@ -685,29 +685,26 @@
 ovider.S
-DocumentGraph
+altProject
 Provider
@@ -2923,29 +2923,26 @@
  final S
-DocumentGraph
+altProject
 Provider
@@ -2958,29 +2958,26 @@
  = new S
-DocumentGraph
+altProject
 Provider
@@ -4540,74 +4540,8 @@
 To(p
-.getCorpusGraphs().get(0).getDocuments().get(0).getDocumentGraph()
 , Sa
@@ -5216,32 +5216,32 @@
 .11-21%22, null);%0A
+
     provider.wri
@@ -5250,74 +5250,8 @@
 To(p
-.getCorpusGraphs().get(0).getDocuments().get(0).getDocumentGraph()
 , %0A 

test should use saltproject provider