// Dstl (c) Crown Copyright 2017
package uk.gov.dstl.baleen.annotators.grammatical;

import org.apache.uima.UIMAException;
import org.apache.uima.analysis_engine.AnalysisEngine;
import org.apache.uima.analysis_engine.AnalysisEngineDescription;
import org.apache.uima.fit.factory.AnalysisEngineFactory;
import org.apache.uima.fit.factory.ExternalResourceFactory;
import org.apache.uima.resource.ExternalResourceDescription;
import org.junit.Before;
import org.junit.Test;

import uk.gov.dstl.baleen.annotators.language.OpenNLP;
import uk.gov.dstl.baleen.annotators.testing.AbstractAnnotatorTest;
import uk.gov.dstl.baleen.annotators.testing.TestEntity;
import uk.gov.dstl.baleen.resources.SharedOpenNLPModel;
import uk.gov.dstl.baleen.types.semantic.Location;

/** Tests for {@link uk.gov.dstl.baleen.annotators.grammatical.TOLocationEntity}. */
public class TOLocationEntityTest extends AbstractAnnotatorTest {

  public TOLocationEntityTest() {
    super(TOLocationEntity.class);
  }

  private AnalysisEngine languageAE;

  @Before
  public void before() throws UIMAException {
    ExternalResourceDescription tokensDesc =
        ExternalResourceFactory.createNamedResourceDescription("tokens", SharedOpenNLPModel.class);
    ExternalResourceDescription sentencesDesc =
        ExternalResourceFactory.createNamedResourceDescription(
            "sentences", SharedOpenNLPModel.class);
    ExternalResourceDescription posDesc =
        ExternalResourceFactory.createNamedResourceDescription("posTags", SharedOpenNLPModel.class);
    ExternalResourceDescription chunksDesc =
        ExternalResourceFactory.createNamedResourceDescription(
            "phraseChunks", SharedOpenNLPModel.class);

    AnalysisEngineDescription desc =
        AnalysisEngineFactory.createEngineDescription(
            OpenNLP.class,
            "tokens",
            tokensDesc,
            "sentences",
            sentencesDesc,
            "posTags",
            posDesc,
            "phraseChunks",
            chunksDesc);

    languageAE = AnalysisEngineFactory.createEngine(desc);
  }

  @Test
  public void testSingleNNP() throws UIMAException {
    jCas.setDocumentText("James went to London.");
    process();

    assertAnnotations(1, Location.class, new TestEntity<>(0, "London"));
  }

  @Test
  public void testMultipleNNP() throws UIMAException {
    jCas.setDocumentText("James went to East London.");
    process();

    assertAnnotations(1, Location.class, new TestEntity<>(0, "East London"));
  }

  @Test
  public void testFollowingWords() throws UIMAException {
    jCas.setDocumentText("James went to South London on the 13th of last month.");
    process();

    assertAnnotations(1, Location.class, new TestEntity<>(0, "South London"));
  }

  @Test
  public void testEndOfSentence() throws UIMAException {
    jCas.setDocumentText("James went to London");
    process();

    assertAnnotations(1, Location.class, new TestEntity<>(0, "London"));
  }

  @Test
  public void testIncorrect() throws UIMAException {
    jCas.setDocumentText("James sent a letter to Sam");
    process();

    assertAnnotations(0, Location.class);
  }

  private void process() throws UIMAException {
    languageAE.process(jCas);
    processJCas();
  }
}
