cz.muni.fi.dndtroops.test.TroopDaoImplTest.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.dndtroops.test.TroopDaoImplTest.java

Source

package cz.muni.fi.dndtroops.test;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import cz.muni.fi.dndtroops.Main;
import cz.muni.fi.dndtroops.PersistenceSampleApplicationContext;
import cz.muni.fi.dndtroops.dao.TroopDao;
import cz.muni.fi.dndtroops.dao.TroopDaoImpl;
import cz.muni.fi.dndtroops.entity.Troop;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;
import org.testng.annotations.Test;

import org.testng.Assert;
import org.testng.annotations.BeforeClass;

/**
 *
 * @author Karel Auf
 */
@ContextConfiguration(classes = PersistenceSampleApplicationContext.class)
@TestExecutionListeners(TransactionalTestExecutionListener.class)
@Transactional
public class TroopDaoImplTest extends AbstractTestNGSpringContextTests {

    @PersistenceUnit
    private EntityManagerFactory emf;

    @Autowired
    private TroopDao troopDao;

    private Troop troopA;
    private Troop troopB;

    @BeforeClass
    public void setup() {

        troopA = new Troop();
        troopA.setName("Angels");
        troopA.setMoney(BigDecimal.ZERO);
        troopA.setMission("mise A");

        troopB = new Troop();
        troopB.setName("Bulls");
        troopB.setMission("mise B");
        troopB.setMoney(new BigDecimal("5"));

    }

    @Test
    public void testFindAllTroops() {
        List<Troop> list = troopDao.findAllTroops();

        for (Iterator<Troop> i = list.iterator(); i.hasNext();) {
            Troop t = i.next();
            troopDao.deleteTroop(t);
        }

        troopDao.createTroop(troopA);
        troopDao.createTroop(troopB);

        list = troopDao.findAllTroops();

        Assert.assertEquals(list.size(), 2);

        Assert.assertTrue(list.contains(troopA));
        Assert.assertTrue(list.contains(troopB));

    }

    @Test
    public void testCreateTroop() {
        EntityManager entityManager = emf.createEntityManager();
        entityManager.getTransaction().begin();
        Troop troopC = new Troop();
        troopC.setName("Testers");
        troopC.setMoney(new BigDecimal("30"));
        troopC.setMission("mise C");
        entityManager.getTransaction().commit();
        entityManager.close();

        troopDao.createTroop(troopC);

        Troop t1 = troopDao.findTroopById(troopC.getId());
        Assert.assertEquals(t1.getName(), "Testers");
        Assert.assertEquals(t1.getMoney().compareTo(new BigDecimal("30")), 0);
        Assert.assertEquals(t1.getMission(), "mise C");

    }

    @Test
    public void testDeleteTroop() {
        EntityManager entityManager = emf.createEntityManager();
        entityManager.getTransaction().begin();
        Troop troopC = new Troop();
        troopC.setName("Testers");
        troopC.setMoney(new BigDecimal("30"));
        troopC.setMission("mise C");
        entityManager.persist(troopC);
        entityManager.getTransaction().commit();
        entityManager.close();

        Troop t1 = troopDao.findTroopById(troopC.getId());
        troopDao.deleteTroop(t1);

        Troop t2 = troopDao.findTroopById(troopC.getId());
        Assert.assertEquals(t2, null);

    }

    @Test
    public void testFindTroopById() {
        Troop troopD = new Troop();
        troopD.setName("Devils");
        troopD.setMoney(BigDecimal.ZERO);
        troopD.setMission("mise D");
        troopDao.createTroop(troopD);
        Troop t1 = troopDao.findTroopById(troopD.getId());

        Assert.assertEquals(t1.getName(), "Devils");
        Assert.assertEquals(t1.getMission(), "mise D");

    }

    @Test
    public void testFindTroopByName() {
        Troop troopE = new Troop();
        troopE.setName("E");
        troopE.setMoney(BigDecimal.ZERO);
        troopE.setMission("mise E");
        troopDao.createTroop(troopE);
        Troop t1 = troopDao.findTroopById(troopE.getId());

        Assert.assertEquals(t1.getName(), "E");
        Assert.assertEquals(t1.getMission(), "mise E");

    }

    @Test
    public void testAssignMissionToTroop() {
        EntityManager entityManager = emf.createEntityManager();
        entityManager.getTransaction().begin();
        Troop troopC = new Troop();
        troopC.setName("Testers");
        troopC.setMoney(new BigDecimal("30"));
        entityManager.persist(troopC);
        entityManager.getTransaction().commit();
        entityManager.close();

        troopDao.assignMissionToTroop(troopC.getId(), "mise C");

        Troop t1 = troopDao.findTroopById(troopC.getId());
        Assert.assertEquals(t1.getMission(), "mise C");

        troopDao.assignMissionToTroop(troopC.getId(), null);

        Troop t2 = troopDao.findTroopById(troopC.getId());
        Assert.assertEquals(t2.getMission(), null);

    }

    @Test
    public void testUpdateGoldForTroop() {
        EntityManager entityManager = emf.createEntityManager();
        entityManager.getTransaction().begin();
        Troop troopC = new Troop();
        troopC.setName("Testers");
        troopC.setMoney(new BigDecimal("30"));
        entityManager.persist(troopC);
        entityManager.getTransaction().commit();
        entityManager.close();

        troopDao.updateGoldForTroop(troopC.getId(), new BigDecimal("5"));

        Troop t1 = troopDao.findTroopById(troopC.getId());
        Assert.assertEquals(t1.getMoney().compareTo(new BigDecimal("5")), 0);

    }

    //Tests for wrong parameters
    @Test
    public void testCreateTroopWithWrongParameters() {

        Troop troopC = new Troop();
        Troop troopD = null;

        try {
            troopDao.createTroop(troopC); // no name assigned
            troopDao.createTroop(troopD); //null argument
            Assert.fail();
        } catch (IllegalArgumentException ex) {
            //OK
        }

    }

    @Test
    public void testDeleteTroopWithWrongParameters() {

        Troop troopC = new Troop();
        Troop troopD = null;

        try {
            troopDao.deleteTroop(troopC); // detached entity
            troopDao.deleteTroop(troopD); //null argument
            Assert.fail();
        } catch (IllegalArgumentException ex) {
            //OK
        }

    }

    @Test
    public void testFindTroopByIdWithWrongParameters() {

        try {
            Troop t1 = troopDao.findTroopById(5l); //this id is not used
            Troop t2 = troopDao.findTroopById(null); //null parameter
            Assert.fail();
        } catch (IllegalArgumentException ex) {
            //OK
        }

    }

    @Test
    public void testFindTroopByNameWithWrongParameters() {

        try {
            Troop t1 = troopDao.findTroopByName("Rats"); //this name is not used
            Troop t2 = troopDao.findTroopByName(null); //null parameter
            Assert.fail();
        } catch (IllegalArgumentException ex) {
            //OK
        }

    }
}