cz.PA165.vozovyPark.dao.UserDAOTest.java Source code

Java tutorial

Introduction

Here is the source code for cz.PA165.vozovyPark.dao.UserDAOTest.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.PA165.vozovyPark.dao;

import cz.PA165.vozovyPark.dao.impl.UserDAOImpl;
import cz.PA165.vozovyPark.entities.User;
import cz.PA165.vozovyPark.enums.UserClassEnum;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import static junit.framework.Assert.fail;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import junit.framework.Assert;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author Juraj Pleko, 359530
 */
public class UserDAOTest {

    private static final String PERSISTENCE_UNIT_NAME = "VozovnaPU";

    @Autowired
    private UserDAO userDao;

    EntityManagerFactory emf;
    EntityManager em;

    /**
     *
     */
    @Before
    public void setUp() {
        emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        em = emf.createEntityManager();
        UserDAOImpl userDaoImpl = new UserDAOImpl(em);
        this.userDao = userDaoImpl;

        //cleaning
        List<User> allUsers = userDao.findAll();
        for (User u : allUsers) {
            em.getTransaction().begin();
            userDao.removeUser(u);
            em.getTransaction().commit();
        }
    }

    /**
     *
     */
    @After
    public void tearDown() {
        List<User> allUsers = userDao.findAll();
        for (User u : allUsers) {
            em.getTransaction().begin();
            userDao.removeUser(u);
            em.getTransaction().commit();
        }
        if (emf.isOpen()) {
            emf.close();
        }
    }

    /**
     *
        
    /**
     *
     */
    @Test
    public void testGetUserByIdWithNullArgument() {
        // UserDAO without Entity Manager Factory
        // UserDAO dao = createUserDAOFactory();

        try {
            userDao.getById(null);
            fail("Exception for null argument was not threw.");
        } catch (IllegalArgumentException e) {
        } catch (Exception e) {
            fail("Wrong Exception was threw: " + e + " " + e.getMessage());
        }
    }

    /**
     *
     */
    @Test
    public void testValidCreateAndGetUserById() {
        //UserDAO dao = createUserDAOFactory();
        User user = newDefaultUser();
        User result = null;
        try {
            userDao.createUser(user);
            result = userDao.getById(user.getId());
        } catch (Exception e) {
            fail("Unexpected exception was threw: " + e + " " + e.getMessage());
        }
        Assert.assertEquals(user, result);
        assertDeepEquals(user, result);
    }

    @Test
    public void testGetByUsername() {
        //UserDAO dao = createUserDAOFactory();
        User user = newDefaultUser();
        List<User> result = null;
        try {
            userDao.createUser(user);
            em.getTransaction().begin();
            result = userDao.getByUsername(user.getUsername());
            em.getTransaction().commit();
        } catch (Exception e) {
            fail("Unexpected exception was threw: " + e + " " + e.getMessage());
        }
        Assert.assertEquals(user, result.get(0));
        assertDeepEquals(user, result.get(0));
    }

    /**
     *
     */
    @Test
    public void testCreateWithNullArguments() {
        //UserDAO dao = createUserDAOFactory();
        try {
            userDao.createUser(null);
            fail("Exception for null argument was not threw.");
        } catch (IllegalArgumentException e) {
        } catch (Exception e) {
            fail("Wrong Exception was threw: " + e + " " + e.getMessage());
        }
    }

    /**
     *
     */
    /**
     *
     */

    /**
     *
     */
    @Test
    public void testUpdateWithNullArgument() {
        try {
            userDao.updateUser(null);
            fail("Exception for null argument was not threw.");
        } catch (IllegalArgumentException e) {
        } catch (Exception e) {
            fail("Wrong Exception was threw: " + e + " " + e.getMessage());
        }

    }

    /**
     *
     */
    @Test
    public void testRemoveWithNullArgument() {
        // Remove user that is null 
        //UserDAO dao = createUserDAOFactory();
        try {
            userDao.removeUser(null);
            fail("Exception for null argument was not threw.");
        } catch (IllegalArgumentException e) {
        } catch (Exception e) {
            fail("Wrong Exception was threw: " + e + " " + e.getMessage());
        }
    }

    /**
     *
     */
    @Test
    public void testValidRemove() {
        //UserDAO dao = createUserDAOFactory();
        User user = newDefaultUser();
        User result = null;
        try {
            userDao.createUser(user);
            Long id = user.getId();
            em.getTransaction().begin();
            userDao.removeUser(user);
            em.getTransaction().commit();
            result = userDao.getById(id);

        } catch (Exception e) {
            fail("Unexpected exception was threw: " + e + " " + e.getMessage());
        }
        Assert.assertFalse(em.contains(user));

    }

    /**
     *
     */
    @Test
    public void testValidFindAll() {
        //UserDAO dao = createUserDAOFactory();
        User user1 = newUser("Jan", "Novak", UserClassEnum.EMPLOYEE, false);
        User user2 = newUser("Petr", "Novak", UserClassEnum.EMPLOYEE, false);
        List<User> results = null;
        try {
            userDao.createUser(user1);
            userDao.createUser(user2);
            em.getTransaction().begin();
            results = userDao.findAll();
            em.getTransaction().commit();
        } catch (Exception e) {
            fail("Unexpected exception was threw: " + e + " " + e.getMessage());
        }
        Assert.assertEquals("Unexpected number of results.", 2, results.size());

        List<User> expected = Arrays.asList(user1, user2);

        Collections.sort(results, idComparator);
        Collections.sort(expected, idComparator);

        Assert.assertEquals(expected, results);
        assertDeepEquals(expected, results);
    }

    /**
     *
     */
    @Test
    public void testFindByNameWithWrongArgument() {
        //UserDAO dao = createUserDAOFactory();

        try {
            userDao.findByLastName(null);
            fail("Exception for null argument was not threw.");
        } catch (IllegalArgumentException e) {
        } catch (Exception e) {
            fail("Wrong Exception was threw: " + e + " " + e.getMessage());
        }

        try {
            userDao.findByLastName("");
            fail("Exception for empty string as argument was not threw.");
        } catch (IllegalArgumentException e) {
        } catch (Exception e) {
            fail("Wrong Exception was threw: " + e + " " + e.getMessage());
        }
    }

    /**
     *
     */
    @Test
    public void testValidFindByName() {
        User user1 = newUser("Jan", "Novak", UserClassEnum.EMPLOYEE, false);
        User user2 = newUser("Petr", "Stary", UserClassEnum.EMPLOYEE, false);
        List<User> results = null;
        try {
            userDao.createUser(user1);
            userDao.createUser(user2);
            em.getTransaction().begin();
            results = userDao.findByLastName(user1.getLastName());
            em.getTransaction().commit();
        } catch (Exception e) {
            fail("Unexpected exception was thrown: " + e + " " + e.getMessage());
        }
        Assert.assertEquals("Unexpected number of results.", 1, results.size());

        Assert.assertEquals(user1, results.get(0));
        assertDeepEquals(user1, results.get(0));
    }

    // Helping methods
    private void assertDeepEquals(User expected, User actual) {
        Assert.assertEquals(expected.getId(), actual.getId());
        Assert.assertEquals(expected.getLastName(), actual.getLastName());
        Assert.assertEquals(expected.getFirstName(), actual.getFirstName());
        Assert.assertEquals(expected.getIsAdmin(), actual.getIsAdmin());
        Assert.assertEquals(expected.getUserClass(), actual.getUserClass());
    }

    private void assertDeepEquals(List<User> expectedList, List<User> actualList) {
        for (int i = 0; i < expectedList.size(); i++) {
            User expected = expectedList.get(i);
            User actual = actualList.get(i);
            assertDeepEquals(expected, actual);
        }
    }

    // Factories
    private User newDefaultUser() {
        return newUser("Jan", "Novak", UserClassEnum.EMPLOYEE, false);
    }

    private User newUser(String fname, String lname, UserClassEnum userClass, Boolean isAdmin) {
        User user = new User();
        user.setFirstName(fname);
        user.setLastName(lname);
        user.setUserClass(userClass);
        user.setIsAdmin(isAdmin);
        user.setEnabled(true);
        user.setUsername(lname);
        user.setPassword("123heslo");
        return user;

    }

    private static Comparator<User> idComparator = new Comparator<User>() {

        @Override
        public int compare(User o1, User o2) {
            return Long.valueOf(o1.getId()).compareTo(Long.valueOf(o2.getId()));
        }
    };
}