package org.zanata.adapter.xliff;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.testng.annotations.Test;
import org.xml.sax.InputSource;
import org.zanata.common.LocaleId;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TextFlow;
import org.zanata.rest.dto.resource.TextFlowTarget;
import org.zanata.rest.dto.resource.TranslationsResource;

@Test(groups = { "unit-tests" })
public class XliffReaderTest
{
   private String testDir = "src/test/resources/";
   private XliffReader reader;

   @Test
   public void extractTemplateSizeTest() throws FileNotFoundException
   {
      Resource doc = getTemplateDoc();

      assertThat(doc.getName(), equalTo("StringResource_en_US.xml"));
      assertThat(doc.getTextFlows().size(), is(3));
   }

   @Test
   public void templateFirstAndLastTextFlowTest() throws FileNotFoundException
   {
      Resource doc = getTemplateDoc();

      TextFlow firstTextFlow = doc.getTextFlows().get(0);
      TextFlow lastTextFlow = doc.getTextFlows().get(doc.getTextFlows().size() - 1);

      assertThat(firstTextFlow.getContent(), equalTo("Translation Unit 1"));
      assertThat(lastTextFlow.getContent(), equalTo("Translation Unit 3"));
   }

   @Test
   public void extractTargetSizeTest() throws FileNotFoundException
   {
      reader = new XliffReader();
      
      File fileTarget = new File(testDir, "/StringResource_de.xml");
      InputSource inputSource = new InputSource(new FileInputStream(fileTarget));
      TranslationsResource tr = reader.extractTarget(inputSource);
      assertThat(tr.getTextFlowTargets().size(), is(2));
   }

   @Test
   public void targetFirstAndLastTextFlowTest() throws FileNotFoundException
   {
      reader = new XliffReader();
      
      File fileTarget = new File(testDir, "/StringResource_de.xml");
      InputSource inputSource = new InputSource(new FileInputStream(fileTarget));
      TranslationsResource tr = reader.extractTarget(inputSource);

      TextFlowTarget firstTextFlow = tr.getTextFlowTargets().get(0);
      TextFlowTarget lastTextFlow = tr.getTextFlowTargets().get(tr.getTextFlowTargets().size() - 1);
     
      assertThat(firstTextFlow.getContent(), equalTo("Translation 1"));
      assertThat(lastTextFlow.getContent(), equalTo("Translation 3"));
   }


   private Resource getTemplateDoc() throws FileNotFoundException
   {
      reader = new XliffReader();
      String docName = "StringResource_en_US.xml";

      File file = new File(testDir, "/" + docName);
      InputSource inputSource = new InputSource(new FileInputStream(file));
      return reader.extractTemplate(inputSource, LocaleId.EN_US, docName);
   }
}

@@ -259,16 +259,58 @@
 ption;%0A%0A
+import org.testng.annotations.BeforeTest;%0A
 import o
@@ -686,22 +686,36 @@
 ate 
-String testDir
+static final String TEST_DIR
  = %22
@@ -751,27 +751,186 @@
 ate 
-XliffReader reader;
+static final String DOC_NAME = %22StringResource_en_US.xml%22;%0A   private XliffReader reader;%0A%0A   @BeforeTest%0A   public void resetReader()%0A   %7B%0A      reader = new XliffReader();%0A   %7D
 %0A%0A  
@@ -1095,34 +1095,16 @@
 lTo(
-%22StringResource_en_US.xml%22
+DOC_NAME
 ));%0A
@@ -1683,49 +1683,8 @@
   %7B%0A
-      reader = new XliffReader();%0A      %0A
     
@@ -1704,39 +1704,40 @@
 rget = new File(
-testDir
+TEST_DIR
 , %22/StringResour
@@ -2056,49 +2056,8 @@
   %7B%0A
-      reader = new XliffReader();%0A      %0A
     
@@ -2081,31 +2081,32 @@
  = new File(
-testDir
+TEST_DIR
 , %22/StringRe
@@ -2679,137 +2679,64 @@
     
-reader = new XliffReader();%0A      String docName = %22StringResource_en_US.xml%22;%0A%0A      File file = new File(testDir, %22/%22 + docName
+File file = new File(TEST_DIR, File.separator + DOC_NAME
 );%0A 
@@ -2879,15 +2879,16 @@
 US, 
-docName
+DOC_NAME
 );%0A 

refactor xliff reader tests