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

Java tutorial

Introduction

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

Source

/*
 * 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.
 */
package cz.muni.fi.dndtroops.test;

import cz.muni.fi.dndtroops.PersistenceSampleApplicationContext;
import cz.muni.fi.dndtroops.dao.HeroDao;
import cz.muni.fi.dndtroops.dao.TroopDao;
import cz.muni.fi.dndtroops.entity.Hero;
import cz.muni.fi.dndtroops.entity.Troop;
import cz.muni.fi.dndtroops.enums.RoleName;
import cz.muni.fi.dndtroops.exception.ConnectionException;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

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

    @PersistenceUnit
    private EntityManagerFactory emf;

    @Autowired
    private TroopDao troopDao;

    @Autowired
    private HeroDao heroDao;

    @Test
    public void testFindAllHeroes() throws ConnectionException {

        Hero heroF = new Hero();
        heroF.setName("Hero");
        heroF.setLevel(1);
        heroF.setRole(RoleName.BARD);
        Troop troopF = new Troop();
        troopF.setName("Angels");
        troopF.setMoney(BigDecimal.ZERO);
        troopF.setMission("mise A");
        heroF.setTroop(troopF);

        troopDao.createTroop(troopF);
        heroDao.createHero(heroF);
        List<Hero> list = heroDao.findAll();

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

        Assert.assertTrue(list.contains(heroF));
    }

    @Test
    public void testCreateHero() throws ConnectionException {
        Hero heroC = new Hero();
        heroC.setName("Hero");
        heroC.setLevel(1);
        heroC.setRole(RoleName.BARD);
        Troop troopC = new Troop();
        troopC.setName("Angels");
        troopC.setMoney(BigDecimal.ZERO);
        troopC.setMission("mise A");
        heroC.setTroop(troopC);

        troopDao.createTroop(troopC);
        heroDao.createHero(heroC);

        Hero h1 = heroDao.findById(heroC.getId());
        Assert.assertEquals(h1.getName(), "Hero");
        Assert.assertEquals(h1.getRole(), RoleName.BARD);
        Assert.assertEquals(h1.getLevel(), 1);
        Assert.assertEquals(h1.getTroop(), troopC);

    }

    @Test
    public void testDeleteHero() throws ConnectionException {
        Hero heroD = new Hero();
        heroD.setName("Hero");
        heroD.setLevel(1);
        heroD.setRole(RoleName.BARD);
        Troop troopD = new Troop();
        troopD.setName("Angels");
        troopD.setMoney(BigDecimal.ZERO);
        troopD.setMission("mise A");
        heroD.setTroop(troopD);

        troopDao.createTroop(troopD);
        heroDao.createHero(heroD);

        heroDao.deleteById(heroD.getId());

        Hero h2 = heroDao.findById(heroD.getId());
        Assert.assertEquals(h2, null);

        Hero heroE = new Hero();
        heroE.setName("Nick");
        heroE.setLevel(1);
        heroE.setRole(RoleName.FIGHTER);
        heroE.setTroop(troopD);

        heroDao.createHero(heroE);

        Hero h3 = heroDao.findById(heroE.getId());
        heroDao.deleteHero(h3);

        Hero h4 = heroDao.findById(heroE.getId());
        Assert.assertEquals(h4, null);

    }

    @Test
    public void testFindHero() throws ConnectionException {
        Hero heroG = new Hero();
        heroG.setName("Hero");
        heroG.setLevel(1);
        heroG.setRole(RoleName.BARD);
        Troop troopG = new Troop();
        troopG.setName("Angels");
        troopG.setMoney(BigDecimal.ZERO);
        troopG.setMission("mise A");
        heroG.setTroop(troopG);

        troopDao.createTroop(troopG);
        heroDao.createHero(heroG);

        Hero h1 = heroDao.findById(heroG.getId());
        Hero h2 = heroDao.findByName("Hero");

        Assert.assertEquals(h1, heroG);
        Assert.assertEquals(h2, heroG);

    }

}