package com.hida.dao;

import com.hida.model.AutoId;
import com.hida.model.CustomId;
import com.hida.model.Pid;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.hibernate.NonUniqueObjectException;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
 * Tests the functionality of PidDao and ensures that it properly interacts with
 * Hibernate.
 *
 * @author lruffin
 */
public class PidDaoImplTest extends EntityDaoImplTest {

    @Autowired
    PidDao PidDao;

    /**
     * Retrieves data from an xml file sheet to mock Pids.
     *
     * @return a data set
     * @throws Exception
     */
    @Override
    protected IDataSet getDataSet() throws Exception {
        IDataSet dataSet = new FlatXmlDataSet(this.getClass().getClassLoader().
                getResourceAsStream("Pid.xml"));
        return dataSet;
    }

    /**
     * Tests to see if PidDao can find an Pid by it's name.
     */
    @Test
    public void testFindByName() {
        Pid entity1 = PidDao.findByName("1");
        Pid entity2 = PidDao.findByName("2");
        Assert.assertNotNull(entity1);
        Assert.assertNotNull(entity2);
    }

    /**
     * Tests to see if Pid can be saved.
     */
    @Test
    public void testSavePid() {
        Pid autoSample = getSampleAutoId();
        Pid customSample = getSampleCustomId();

        PidDao.savePid(autoSample);
        PidDao.savePid(customSample);

        Assert.assertEquals(PidDao.findAllPids().size(), 4);
    }

    /**
     * Tests to see if all the Pids can be listed and returned.
     */
    @Test
    public void testFindAllPids() {
        int size = PidDao.findAllPids().size();

        Assert.assertEquals(size, 2);
    }

    /**
     * Tests to see if AutoIds with the same name can be added to the database.
     */
    @Test(expectedExceptions = NonUniqueObjectException.class)
    public void testNonUniqueObjectExceptionWithAutoId() {
        Pid autoSample1 = getSampleAutoId();
        Pid autoSample2 = getSampleAutoId();

        PidDao.savePid(autoSample1);
        PidDao.savePid(autoSample2);
    }

    /**
     * Tests to see if CustomIds with the same name can be added to the
     * database.
     */
    @Test(expectedExceptions = NonUniqueObjectException.class)
    public void testNonUniqueObjectExceptionWithCustomId() {
        Pid customSample1 = getSampleCustomId();
        Pid customSample2 = getSampleCustomId();

        PidDao.savePid(customSample1);
        PidDao.savePid(customSample2);
    }

    /**
     * returns a sample AutoId.
     *
     * @return
     */
    private Pid getSampleAutoId() {
        Pid sample = new AutoId("", new int[1], "a");
        sample.getName();

        return sample;
    }

    /**
     * Returns a sample CustomId.
     *
     * @return
     */
    private Pid getSampleCustomId() {
        Pid sample = new CustomId("", new int[1], new String[]{"b"});
        sample.getName();

        return sample;
    }

}

@@ -105,16 +105,43 @@
 el.Pid;%0A
+import java.util.Iterator;%0A
 import o
@@ -584,24 +584,32 @@
 towired%0A    
+private 
 PidDao PidDa
@@ -610,16 +610,71 @@
  PidDao;
+%0A    %0A    @Autowired%0A    private PidRepository PidRepo;
 %0A%0A    /*
@@ -1175,29 +1175,27 @@
 y1 = Pid
-Da
+Rep
 o.find
-ByNam
+On
 e(%221%22);%0A
@@ -1223,21 +1223,19 @@
  Pid
-Da
+Rep
 o.find
-ByNam
+On
 e(%222
@@ -1526,32 +1526,66 @@
      Pid
-Da
+Rep
 o.save
-Pid
 (au
+toSample);%0A        PidRepo.save(cus
 to
+m
 Sample);
@@ -1589,38 +1589,131 @@
 e);%0A        
-PidDao.savePid
+%0A        Pid autoEntity = PidRepo.findOne(autoSample.getName());%0A        Pid customEntity = PidRepo.findOne
 (customSampl
@@ -1713,16 +1713,26 @@
 omSample
+.getName()
 );%0A%0A    
@@ -1759,38 +1759,127 @@
 als(
-PidDao.findAllPids().size(), 4
+autoSample.getName(), autoEntity.getName());%0A        Assert.assertEquals(customSample.getName(), customEntity.getName()
 );%0A 
@@ -2022,24 +2022,39 @@
     
-int size
+Iterator%3CPid%3E iterator
  = Pid
-Da
+Rep
 o.fi
@@ -2062,22 +2062,147 @@
 dAll
-Pids().size();
+().iterator();%0A        %0A        int size = 0;%0A        while(iterator.hasNext())%7B%0A            iterator.next();%0A            size++;%0A        %7D
 %0A%0A  
@@ -2560,35 +2560,33 @@
 %0A        Pid
-Da
+Rep
 o.save
-Pid
 (autoSample1
@@ -2599,27 +2599,25 @@
      Pid
-Da
+Rep
 o.save
-Pid
 (autoSam
@@ -2965,35 +2965,33 @@
 %0A        Pid
-Da
+Rep
 o.save
-Pid
 (customSampl
@@ -3010,19 +3010,17 @@
  Pid
-Da
+Rep
 o.save
-Pid
 (cus

Replace Dao with Repo equivalent in PidDaoTest