/*
 * Copyright 2012-2014 Johns Hopkins University HLTCOE. All rights reserved.
 * See LICENSE in the project root directory.
 */
package edu.jhu.hlt.concrete.gigaword;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import edu.jhu.hlt.concrete.Communication;
import edu.jhu.hlt.concrete.Section;
import edu.jhu.hlt.concrete.communications.SuperCommunication;
import edu.jhu.hlt.concrete.util.ConcreteException;
import edu.jhu.hlt.concrete.util.SuperTextSpan;
import gigaword.api.GigawordDocumentConverter;
import gigaword.interfaces.GigawordDocument;

/**
 * @author max
 * 
 */
public class IngesterTest {

  private static final Logger logger = LoggerFactory.getLogger(IngesterTest.class);
  
  Path p;

  /**
   * @throws java.lang.Exception
   */
  @Before
  public void setUp() throws Exception {
    p = Paths.get("src/test/resources/serif_dog-bites-man.sgml");
    if (!Files.exists(p))
      fail("Need to have: " + p.toString() + " to run this test.");
  }

  /**
   * @throws java.lang.Exception
   */
  @After
  public void tearDown() throws Exception {
  }
  
  @Rule
  public TemporaryFolder tmpFolder = new TemporaryFolder();

  /**
   * Test method for {@link edu.jhu.hlt.gigaword.Ingester#convert(java.io.InputStream, edu.jhu.hlt.concrete.CommunicationType)}.
   * 
   * @throws IOException 
   * @throws JsonMappingException 
   * @throws JsonParseException 
   * @throws ConcreteException 
   */
  @Test
  public void concreteMapper() throws IOException, ConcreteException {
    GigawordDocument pdc = new GigawordDocumentConverter().fromPathString(this.p.toString());
    Communication c = new ConcreteGigawordDocumentFactory().convert(pdc);
    
    assertEquals("dog-bites-man_20141009.sgml", c.getId());
    assertEquals("other", c.getType().toLowerCase());
    
    List<Section> sectionList = c.getSectionList();
    Section title = sectionList.get(0);
    assertEquals("Dog Bites Man\n", new SuperTextSpan(title.getTextSpan(), c).getText());
    assertEquals("Title", title.getKind());

    for (Section s : sectionList)
      logger.info(new SuperTextSpan(s.getTextSpan(), c).getText());
    
    assertEquals(
        "John Smith, manager of ACME INC, was bit by a dog on March 10th, 2013.\n", new SuperTextSpan(sectionList.get(1).getTextSpan(), c).getText());
    assertEquals("Passage", sectionList.get(1).getKind());
    
    // new SuperCommunication(c).writeToFile(Files.createTempFile("target", "test-out.concrete").getFileName(), true);
    new SuperCommunication(c).writeToFile(tmpFolder.getRoot().toPath().resolve("test-out.concrete"), true);
  }
}

@@ -261,27 +261,133 @@
 java.io.
-IOException
+BufferedInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.nio.charset.StandardCharsets
 ;%0Aimport
@@ -487,16 +487,54 @@
 .List;%0A%0A
+import org.apache.commons.io.IOUtils;%0A
 import o
@@ -1190,17 +1190,16 @@
 r max%0A *
- 
 %0A */%0Apub
@@ -1228,95 +1228,8 @@
  %7B%0A%0A
-  private static final Logger logger = LoggerFactory.getLogger(IngesterTest.class);%0A  %0A
   Pa
@@ -1598,18 +1598,16 @@
 n %7B%0A  %7D%0A
-  
 %0A  @Rule
@@ -1807,17 +1807,16 @@
 )%7D.%0A   *
- 
 %0A   * @t
@@ -1824,33 +1824,32 @@
 rows IOException
- 
 %0A   * @throws Js
@@ -1858,33 +1858,32 @@
 MappingException
- 
 %0A   * @throws Js
@@ -1894,25 +1894,24 @@
 rseException
- 
 %0A   * @throw
@@ -1929,17 +1929,16 @@
 xception
- 
 %0A   */%0A 
@@ -1962,22 +1962,22 @@
 oid 
-concreteMapper
+pathStringTest
 () t
@@ -2183,24 +2183,136 @@
 t(pdc);%0A    
+this.testAgainstDogVsMan(c);%0A  %7D%0A%0A  private void testAgainstDogVsMan(Communication c) throws ConcreteException %7B
 %0A    assertE
@@ -2410,28 +2410,24 @@
 werCase());%0A
-    
 %0A    List%3CSe
@@ -2646,115 +2646,8 @@
 );%0A%0A
-    for (Section s : sectionList)%0A      logger.info(new SuperTextSpan(s.getTextSpan(), c).getText());%0A    %0A
     
@@ -2874,19 +2874,12 @@
 ));%0A
-    
 %0A   
- //
  new
@@ -2917,189 +2917,540 @@
 ile(
-Files.createTempFile(%22target%22, %22test-out.concrete%22).getFileName(), true
+tmpFolder.getRoot().toPath().resolve(%22test-out.concrete%22), true);%0A  %7D%0A%0A  @Test%0A  public void sgmlStringTest() throws IOException, ConcreteException %7B%0A    try (InputStream is = Files.newInputStream(p
 );%0A    
+ 
-new SuperCommunication(c).writeToFile(tmpFolder.getRoot().toPath().resolve(%22test-out.concrete%22), true);
+   BufferedInputStream bis = new BufferedInputStream(is)) %7B%0A      String sgml = IOUtils.toString(bis, StandardCharsets.UTF_8);%0A      GigawordDocument pdc = new GigawordDocumentConverter().fromSGMLString(sgml);%0A      Communication c = new ConcreteGigawordDocumentFactory().convert(pdc);%0A      this.testAgainstDogVsMan(c);%0A    %7D
 %0A  %7D

Add another test to cover another method.