/**
 * Multimud Helper.
 * 
 * Copyright (C) 2009 Benjamin Lerman<br>
 * Copyright (C) 2009 Jacques ???
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package net.ambre.multimud_helper.dao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import javax.annotation.Resource;

import net.ambre.multimud_helper.bean.Skill;
import net.ambre.multimud_helper.bean.SkillLevel;
import net.ambre.multimud_helper.bean.TrainedSkill;
import net.ambre.multimud_helper.bean.Trainer;
import net.ambre.multimud_helper.config.TestBase;

import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

/**
 * Testing Trainer.
 */
public class TrainerDaoTest extends TestBase {

    /**
     * a constant {@link String} for tests.
     */
    private static final String[] TEST_STRINGS = { "Hello1", "Hello2", "Hello3" };

    /**
     * the {@link TrainerDAO} to test.
     */
    @Resource
    private TrainerDAO trainerDAO;

    /**
     * the {@link SessionFactory} to use.
     */
    @Resource
    private SessionFactory hibernateSessionFactory;

    /**
     * Testing {@link Skill} creation.
     */
    @Test
    @Transactional
    public void testCreation() {
        HibernateTemplate template = new HibernateTemplate(hibernateSessionFactory);
        assertNull("No skill should be created prior to the test.", template.get(Skill.class, TEST_STRINGS[0]));
        hibernateSessionFactory.getCurrentSession().flush();
        hibernateSessionFactory.getCurrentSession().clear();
        Skill skill = trainerDAO.getSkill(TEST_STRINGS[0]);
        assertNotNull("the skill should have been created.", skill);
        assertEquals("an object should be unique during a hibernate session.", skill, trainerDAO.getSkill(TEST_STRINGS[0]));
    }

    @Test
    @Transactional
    public void testTrainer() {
        Trainer trainer = new Trainer();
        trainer.setZone(trainerDAO.getZone("TestZone"));
        trainer.setName("foo");
        hibernateSessionFactory.getCurrentSession().save(trainer);
        Integer idTrainer = trainer.getId();
        trainerDAO.addSkill(trainer, trainerDAO.getSkill(TEST_STRINGS[1]), SkillLevel.NOT_LEARNED, SkillLevel.VERY_GOOD);
        trainerDAO.addSkill(trainer, trainerDAO.getSkill(TEST_STRINGS[2]), SkillLevel.NOT_LEARNED, SkillLevel.INTERMEDIATE);
        trainerDAO.addSkill(trainer, trainerDAO.getSkill(TEST_STRINGS[0]), SkillLevel.NOT_LEARNED, SkillLevel.INTERMEDIATE);
        trainerDAO.addSkill(trainer, trainerDAO.getSkill(TEST_STRINGS[0]), SkillLevel.NOT_LEARNED, SkillLevel.GOOD);
        hibernateSessionFactory.getCurrentSession().flush();
        hibernateSessionFactory.getCurrentSession().clear();
        trainer = (Trainer) hibernateSessionFactory.getCurrentSession().get(Trainer.class, idTrainer);
        assertEquals("The trainer should have 3 skills", 3, trainer.getTrainedSkills().size());
        int i = 0;
        for (TrainedSkill trainedSkill : trainer.getTrainedSkills()) {
            assertEquals("Skills should be sorted", TEST_STRINGS[i], trainedSkill.getSkill().getName());
            i++;
        }
    }

}
