package org.zanata.model;

import java.util.Arrays;
import java.util.List;

import org.dbunit.operation.DatabaseOperation;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.ZanataDbunitJpaTest;
import org.zanata.common.ContentType;
import org.zanata.common.LocaleId;
import org.zanata.dao.LocaleDAO;
import org.zanata.dao.TextFlowTargetHistoryDAO;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class HTextFlowTargetHistoryJPATest extends ZanataDbunitJpaTest {
    private LocaleDAO localeDAO;
    private TextFlowTargetHistoryDAO historyDAO;
    HLocale en_US;
    HLocale de_DE;

    @BeforeMethod(firstTimeOnly = true)
    public void beforeMethod() {
        localeDAO = new LocaleDAO((Session) em.getDelegate());
        historyDAO = new TextFlowTargetHistoryDAO((Session) em.getDelegate());
        en_US = localeDAO.findByLocaleId(LocaleId.EN_US);
        de_DE = localeDAO.findByLocaleId(new LocaleId("de"));
    }

    @Override
    protected void prepareDBUnitOperations() {
        beforeTestOperations.add(new DataSetOperation(
            "org/zanata/test/model/ProjectsData.dbunit.xml",
            DatabaseOperation.CLEAN_INSERT));
        beforeTestOperations.add(new DataSetOperation(
            "org/zanata/test/model/LocalesData.dbunit.xml",
            DatabaseOperation.CLEAN_INSERT));
    }

    @Test
    public void ensureHistoryIsRecorded() {
        Session session = getSession();
        HDocument d =
            new HDocument("/path/to/document.txt", ContentType.TextPlain,
                en_US);
        d.setProjectIteration((HProjectIteration) session.load(
            HProjectIteration.class, 1L));
        session.save(d);
        session.flush();

        HTextFlow tf = new HTextFlow(d, "mytf", "hello world");
        d.getTextFlows().add(tf);
        session.flush();

        HTextFlowTarget target = new HTextFlowTarget(tf, de_DE);
        target.setContents("helleu world");
        session.save(target);
        session.flush();

        List<HTextFlowTargetHistory> historyElems = getHistory(target);
        assertThat("Incorrect History size on persist", historyElems.size(),
            is(0));

        target.setContents("blah!");
        session.flush();

        historyElems = getHistory(target);

        assertThat("Incorrect History size on first update",
            historyElems.size(), is(1));

        target.setContents("hola mundo!");
        session.flush();

        historyElems = getHistory(target);

        assertThat("Incorrect History size on second update",
            historyElems.size(), is(2));
        assertThat(historyElems.size(), is(2));
        HTextFlowTargetHistory hist = historyElems.get(0);
        assertThat(hist.getContents(), is(Arrays.asList("helleu world")));
    }

    @Test
    public void ensureHistoryIsRecordedPlural() {
        Session session = getSession();
        HDocument d =
            new HDocument("/path/to/document.txt", ContentType.TextPlain,
                en_US);
        d.setProjectIteration((HProjectIteration) session.load(
            HProjectIteration.class, 1L));
        session.save(d);
        session.flush();

        HTextFlow tf = new HTextFlow(d, "mytf", "hello world");
        d.getTextFlows().add(tf);
        session.flush();

        HTextFlowTarget target = new HTextFlowTarget(tf, de_DE);
        target.setContents("helleu world", "helleu worlds");
        session.save(target);
        session.flush();

        List<HTextFlowTargetHistory> historyElems = getHistory(target);
        assertThat(historyElems.size(), is(0));

        target.setContents("blah", "blah!");
        session.flush();

        historyElems = getHistory(target);

        assertThat(historyElems.size(), is(1));
        HTextFlowTargetHistory hist = historyElems.get(0);
        assertThat(hist.getContents(),
            is(Arrays.asList("helleu world", "helleu worlds")));

        assert historyDAO.findContentInHistory(target,
            Arrays.asList("helleu world", "helleu worlds"));
        assert !historyDAO.findContentInHistory(target,
            Arrays.asList("helleu world"));
        assert !historyDAO.findContentInHistory(target,
            Arrays.asList("helleu worlds"));
        assert !historyDAO.findContentInHistory(target,
            Arrays.asList("blah", "blah!"));
    }

    @SuppressWarnings("unchecked")
    private List<HTextFlowTargetHistory> getHistory(HTextFlowTarget tft) {
        return getSession().createCriteria(HTextFlowTargetHistory.class)
            .add(Restrictions.eq("textFlowTarget", tft)).list();
    }
}

@@ -1219,32 +1219,36 @@
 on(%0A            
+    
 %22org/zanata/test
@@ -1272,32 +1272,36 @@
 ta.dbunit.xml%22,%0A
+    
             Data
@@ -1389,32 +1389,36 @@
 on(%0A            
+    
 %22org/zanata/test
@@ -1441,32 +1441,36 @@
 ta.dbunit.xml%22,%0A
+    
             Data
@@ -1626,32 +1626,36 @@
 d =%0A            
+    
 new HDocument(%22/
@@ -1692,32 +1692,40 @@
 Type.TextPlain,%0A
+        
                 
@@ -1800,32 +1800,36 @@
 ad(%0A            
+    
 HProjectIteratio
@@ -2344,16 +2344,20 @@
         
+    
 is(0));%0A
@@ -2529,32 +2529,36 @@
 e%22,%0A            
+    
 historyElems.siz
@@ -2750,32 +2750,36 @@
 e%22,%0A            
+    
 historyElems.siz
@@ -3106,32 +3106,36 @@
 d =%0A            
+    
 new HDocument(%22/
@@ -3172,32 +3172,40 @@
 Type.TextPlain,%0A
+        
                 
@@ -3280,32 +3280,36 @@
 ad(%0A            
+    
 HProjectIteratio
@@ -4074,16 +4074,20 @@
         
+    
 is(Array
@@ -4179,32 +4179,36 @@
 History(target,%0A
+    
             Arra
@@ -4312,32 +4312,36 @@
 et,%0A            
+    
 Arrays.asList(%22h
@@ -4416,32 +4416,36 @@
 et,%0A            
+    
 Arrays.asList(%22h
@@ -4521,32 +4521,36 @@
 et,%0A            
+    
 Arrays.asList(%22b
@@ -4756,16 +4756,20 @@
 .class)%0A
+    
         

Reformat file: HTextFlowTargetHistoryJPATest