<commit_before>/**
 * Copyright (C) 2011  JTalks.org Team
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package org.jtalks.poulpe.model.dao.hibernate;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNotSame;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals;

import java.util.List;

import junit.framework.AssertionFailedError;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jtalks.poulpe.model.dao.BranchDao;
import org.jtalks.poulpe.model.entity.PoulpeBranch;
import org.jtalks.poulpe.model.entity.PoulpeSection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.validation.ConstraintViolationException;

/**
 * @author <NAME>
 * @author <NAME>
 */
@ContextConfiguration(locations = { "classpath:/org/jtalks/poulpe/model/entity/applicationContext-dao.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class BranchHibernateDaoTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    private SessionFactory sessionFactory;
    @Autowired
    private BranchDao dao;

    private Session session;
    private PoulpeBranch branch;

    @BeforeMethod
    public void setUp() throws Exception {
        session = sessionFactory.getCurrentSession();
    }

    @Test
    public void testSave() {
        givenParentSection();

        dao.saveOrUpdate(branch);

        assertBranchSaved();
    }

    private void givenParentSection() {
        branch = ObjectsFactory.createBranch();
        session.save(branch.getSection());
    }

    private void assertBranchSaved() throws AssertionFailedError {
        assertNotSame(branch.getId(), 0, "Id not created");
        PoulpeBranch actual = retrieveActualBranch();
        actual.setSection(branch.getSection());
        assertReflectionEquals(branch, actual);
    }

    private PoulpeBranch retrieveActualBranch() {
        return ObjectRetriever.retrieveUpdated(branch, session);
    }

    @Test(expectedExceptions = ConstraintViolationException.class)
    public void testSaveBranchWithNameNotNullViolation() {
        PoulpeBranch branch = new PoulpeBranch();
        dao.saveOrUpdate(branch);
    }

    @Test
    public void testGet() {
        givenBranch();

        PoulpeBranch result = dao.get(branch.getId());

        assertNotNull(result);
        assertEquals(result.getId(), branch.getId());
    }

    private void givenBranch() {
        givenParentSection();
        session.save(branch);
    }

    @Test
    public void testGetInvalidId() {
        PoulpeBranch result = dao.get(-567890L);

        assertNull(result);
    }

    @Test
    public void testUpdate() {
        givenBranch();

        String newName = "new name";
        branch.setName(newName);

        dao.saveOrUpdate(branch);

        assertNameChanged(newName);
    }

    private void assertNameChanged(String newName) {
        PoulpeBranch result = retrieveActualBranch();
        assertEquals(result.getName(), newName);
    }

    @Test(expectedExceptions = ConstraintViolationException.class)
    public void testUpdateNotNullViolation() {
        givenBranch();
        branch.setName(null);

        dao.saveOrUpdate(branch);
    }

    @Test
    public void testDelete() {
        givenBranch();

        boolean result = dao.delete(branch.getId());
        assertTrue(result, "Entity is not deleted");

        assertBranchDeleted();
    }

    private void assertBranchDeleted() {
        int branchCount = retrieveActualBranchesAmount();
        assertEquals(branchCount, 0);
    }

    private int retrieveActualBranchesAmount() {
        String countQuery = "select count(*) from PoulpeBranch";
        Number count = (Number) session.createQuery(countQuery).uniqueResult();
        return count.intValue();
    }

    @Test
    public void testDeleteInvalidId() {
        boolean result = dao.delete(-100500L);

        assertFalse(result, "Entity deleted");
    }

    @Test
    public void testGetAll() {
        givenTwoBranches();

        List<PoulpeBranch> branches = dao.getAll();

        assertEquals(branches.size(), 2);
    }

    private void givenTwoBranches() {
        addBranch();<commit_msg>BranchHibernateDaoTest was modified to test Branch-Group relationship. CRLF -> LF.<commit_after>/**
 * Copyright (C) 2011  JTalks.org Team
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package org.jtalks.poulpe.model.dao.hibernate;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNotSame;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals;

import java.util.List;

import javax.validation.ConstraintViolationException;

import junit.framework.AssertionFailedError;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jtalks.common.model.entity.Group;
import org.jtalks.poulpe.model.dao.BranchDao;
import org.jtalks.poulpe.model.entity.PoulpeBranch;
import org.jtalks.poulpe.model.entity.PoulpeSection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
 * @author <NAME>
 * @author <NAME>
 */
@ContextConfiguration(locations = { "classpath:/org/jtalks/poulpe/model/entity/applicationContext-dao.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class BranchHibernateDaoTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    private SessionFactory sessionFactory;
    @Autowired
    private BranchDao dao;

    private Session session;
    private PoulpeBranch branch;
    private Group group;

    @BeforeMethod
    public void setUp() throws Exception {
        session = sessionFactory.getCurrentSession();
    }

    @Test
    public void testSave() {
        givenParentSection();

        dao.saveOrUpdate(branch);

        assertBranchSaved();
        assertGroupSaved();
    }

    private void givenParentSection() {
        branch = ObjectsFactory.createBranch();
        group = ObjectsFactory.createGroup();
        branch.setModeratorsGroup(group);
        session.save(branch.getSection());
    }

    private void assertBranchSaved() throws AssertionFailedError {
        assertNotSame(branch.getId(), 0, "Id not created");
        PoulpeBranch actual = retrieveActualBranch();
        actual.setSection(branch.getSection());
        assertReflectionEquals(branch, actual);
    }
    
    private void assertGroupSaved() {
        assertNotSame(group.getId(), 0, "Id not created");
        Group actual = retrieveActualGroup();
        assertReflectionEquals(group, actual);
    }

    private PoulpeBranch retrieveActualBranch() {
        return ObjectRetriever.retrieveUpdated(branch, session);
    }

    private Group retrieveActualGroup() {
        return ObjectRetriever.retrieveUpdated(group, session);
    }

    @Test(expectedExceptions = ConstraintViolationException.class)
    public void testSaveBranchWithNameNotNullViolation() {
        PoulpeBranch branch = new PoulpeBranch();
        dao.saveOrUpdate(branch);
    }

    @Test
    public void testGet() {
        givenBranch();

        PoulpeBranch result = dao.get(branch.getId());

        assertNotNull(result);
        assertEquals(result.getId(), branch.getId());
    }

    private void givenBranch() {
        givenParentSection();
        session.save(branch);
    }

    @Test
    public void testGetInvalidId() {
        PoulpeBranch result = dao.get(-567890L);

        assertNull(result);
    }

    @Test
    public void testUpdate() {
        givenBranch();

        String newGroupName = "new group name";
        group.setName(newGroupName);
        String newName = "new name";
        branch.setName(newName);

        dao.saveOrUpdate(branch);

        assertBranchNameChanged(newName);
        assertGroupNameChanged(newGroupName);
    }

    private void assertBranchNameChanged(String newName) {
        PoulpeBranch result = retrieveActualBranch();
        assertEquals(result.getName(), newName);
    }


    private void assertGroupNameChanged(String newName) {
        Group result = retrieveActualGroup();
        assertEquals(result.getName(), newName);
    }

    @Test(expectedExceptions = ConstraintViolationException.class)
    public void testUpdateNotNullViolation() {
        givenBranch();
        branch.setName(null);

        dao.saveOrUpdate(branch);
    }

    @Test
    public void testDelete() {
        givenBranch();

        dao.delete(branch);

        assertBranchDeleted();
        assertGroupDeleted();
    }

    private void assertBranchDeleted() {
        int branchCount = retrieveActualBranchesAmount();
        assertEquals(branchCount, 0);
    }

    private void assertGroupDeleted() {
        int groupCount = retrieveActualGroupsAmount();
        assertEquals(groupCount, 0);
    }

    private int retrieveActualBranchesAmount() {
        String countQuery = "select count(*) from PoulpeBranch";
        Number count = (Number) session.createQuery(countQuery).uniqueResult();
        return count.intValue();
    }

    private int retrieveActualGroupsAmount() {
        String countQuery = "select count(*) from Group";
        Number count = (Number) session.createQuery(countQuery).uniqueResult();
        return count.intValue();
    }

    @Test
    public void testDeleteInvalidId() {
        boolean result = dao.delete(-100500L);

        assertFalse(result, "Entity deleted");
    }

    @Test
    public void testGetAll() {
        givenTwoBranches();

        List<PoulpeBranch> branches = dao.getAll();

        assertEquals(branches.size(), 2);
    }

    private void givenTwoBranches() {
        addBranch();
{'fraction_non_alphanumeric': 0.09183756180594958, 'fraction_numerical': 0.005349760881899976, 'mean_word_length': 3.53436236677692, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1699630', 'n_tokens_mistral': 3344, 'n_tokens_neox': 3311, 'n_words': 814}