package com.atlassian.stash.internal.group;

import com.atlassian.bitbucket.annotation.Dao;
import com.atlassian.bitbucket.annotation.DataSet;
import com.atlassian.stash.internal.AbstractDaoTest;
import com.atlassian.stash.internal.DataSets;
import com.atlassian.bitbucket.util.Page;
import com.atlassian.bitbucket.util.PageRequest;
import com.atlassian.bitbucket.util.PageUtils;
import org.joda.time.Instant;
import org.junit.Test;

@DataSet({DataSets.DELETED_GROUPS})
public class HibernateDeletedGroupDaoTest extends AbstractDaoTest {

    @Dao(HibernateDeletedGroupDao.class)
    private DeletedGroupDao dao;

    @Test
    public void testFindDeletedGroupByName() throws Exception {
        InternalDeletedGroup deletedGroup = dao.findByName("test-group-to-delete");

        assertNotNull(deletedGroup);
        assertEquals(1, deletedGroup.getId());
        assertEquals("test-group-to-delete", deletedGroup.getName());
        assertEquals(Instant.parse("2015-04-07T12:00:00.000").toDate(), deletedGroup.getDeletedDate());
    }

    @Test
    public void testFindDeletedGroupByNameCaseInsensitive() throws Exception {
        InternalDeletedGroup deletedGroup = dao.findByName("TeSt-GrOuP-tO-dElEtE");

        assertNotNull(deletedGroup);
        assertEquals(1, deletedGroup.getId());
        assertEquals("test-group-to-delete", deletedGroup.getName());
        assertEquals(Instant.parse("2015-04-07T12:00:00.000").toDate(), deletedGroup.getDeletedDate());
    }

    @Test
    public void testFindByDeletedDateEarlierThan() throws Exception {
        final PageRequest request = PageUtils.newRequest(0, 100);

        Page<InternalDeletedGroup> allDeletedGroups = dao.findAll(request);
        assertEquals(2, allDeletedGroups.getSize());

        Page<InternalDeletedGroup> deletedGroups =
                dao.findByDeletedDateEarlierThan(Instant.parse("2015-04-08T12:00:00").toDate(), request);

        assertEquals(1, deletedGroups.getSize());

        InternalDeletedGroup deletedGroup = deletedGroups.getValues().iterator().next();
        assertNotNull(deletedGroup);
        assertEquals("test-group-to-delete", deletedGroup.getName());
    }
}
