package cz.muni.fi.pa165.ddtroops.persistence;
import cz.muni.fi.pa165.ddtroops.daointerfaces.SkillDAO;
import cz.muni.fi.pa165.ddtroops.entities.Skill;
import java.util.List;
import java.util.logging.Logger;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* Test class for SkillDAOImpl class.
* @author Jan Šťastný
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:testAppContext.xml" })
@Transactional
public class SkillDAOTest extends TestCase {
private static final Logger LOGGER = Logger.getLogger(SkillDAOTest.class.getName());
@Autowired
private SkillDAO skillDAO;
/**
* Test checks whether skill after creation has received id
*/
@Test
public void testHasIdAfterCreation(){
Skill newSkill = new Skill();
newSkill.setDescription("Brand new skill");
newSkill.setName("New skill");
newSkill.setMinXP(1000L);
newSkill.setProfession("Wizard");
skillDAO.create(newSkill);
assertNotNull(newSkill.getId());
}
/**
* Test checks whether the skill was created in db and can be retreived.
*/
@Test
public void testCreate(){
Skill newSkill = new Skill();
newSkill.setDescription("Brand new skill");
newSkill.setName("Created skill");
newSkill.setMinXP(1000L);
newSkill.setProfession("Wizard");
newSkill.setDescription("abc");
skillDAO.create(newSkill);
Skill fromDB = skillDAO.getById(newSkill.getId());
assertEquals(newSkill,fromDB);
}
/**
* Test checks whether a skill can be retreived by id.
*/
@Test
public void testGetById(){
Skill newSkill = new Skill();
newSkill.setDescription("Brand new skill");
newSkill.setName("New skill");
newSkill.setMinXP(1000L);
newSkill.setProfession("Wizard");
skillDAO.create(newSkill);
Skill skillFromDB = skillDAO.getById(newSkill.getId());
assertEquals(newSkill, skillFromDB);
}
/**
* Test checks update function on persisted object.
*/
@Test
public void testUpdate(){
Skill newSkill = new Skill();
newSkill.setDescription("Brand new skill");
newSkill.setName("New skill");
newSkill.setMinXP(1000L);
newSkill.setProfession("Wizard");
skillDAO.create(newSkill);
newSkill.setName("Edited");
skillDAO.update(newSkill);
Skill control = skillDAO.getById(newSkill.getId());
assertEquals("Edited", control.getName());
}
/**
* Test checks retreival of all the skills.
*/
@Test
public void testGetAll(){
Skill newSkill = new Skill();
newSkill.setDescription("Brand new skill");
newSkill.setName("New skill");
newSkill.setMinXP(1000L);
newSkill.setProfession("Wizard");
Skill anotherSkill = new Skill();
anotherSkill.setDescription("Brand new skill");
anotherSkill.setName("New skill");
anotherSkill.setMinXP(1000L);
anotherSkill.setProfession("Wizard");
skillDAO.create(newSkill);
skillDAO.create(anotherSkill);
List<Skill> skills = skillDAO.getAll();
assertTrue(skills.contains(newSkill)&&skills.contains(anotherSkill));
}
/**
* Test checks delete method by deleting all entries in db.
*/
@Test
public void testDelete(){
Skill newSkill = new Skill();
newSkill.setDescription("Brand new skill");
newSkill.setName("New skill");
newSkill.setMinXP(1000L);
newSkill.setProfession("Wizard");
skillDAO.create(newSkill);
List<Skill> skills = skillDAO.getAll();
for(Skill s:skills){
skillDAO.delete(s);
}
List<Skill> deletedSkills = skillDAO.getAll();
assertTrue(deletedSkills.isEmpty());
}
}