@Test public void testGetUserByUsername_OK() { User user = UserFactoryForTests.getDefaultTestUser(); String salt = "salt"; try { userDAO.createUser(dbConnection, user, user.getPassword(), salt); User retrievedUser = userDAO.getUserByUsername(dbConnection, user.getUsername()); assertEquals(0, user.compareTo(retrievedUser)); } catch (SQLException e) { ExceptionUtilsForTests.logAndFailOnUnexpectedException(e); } }
@Test public void testDeleteUser_NOK() { User defaultUser = UserFactoryForTests.getDefaultTestUser(); try { assertEquals(0, userDAO.countUsersTable(dbConnection)); boolean userWasDeleted = userDAO.deleteUser(dbConnection, defaultUser.getUsername()); assertEquals(false, userWasDeleted); assertEquals(0, userDAO.countUsersTable(dbConnection)); } catch (SQLException e) { ExceptionUtilsForTests.logAndFailOnUnexpectedException(e); } }
@Test public void testGetPasswordByUsername_NOK() { User user = UserFactoryForTests.getDefaultTestUser(); try { assertEquals(0, userDAO.countUsersTable(dbConnection)); String password = userDAO.getPasswordByUsername(dbConnection, user.getUsername()); assertNotNull(password); assertEquals("", password); } catch (SQLException e) { ExceptionUtilsForTests.logAndFailOnUnexpectedException(e); } }
@Test public void testCreateUser_NOK_emptyUser() { User user = new User(null, null, null, false, null, null); String salt = "salt"; try { assertEquals(0, userDAO.countUsersTable(dbConnection)); userDAO.createUser(dbConnection, user, user.getPassword(), salt); fail("The method shoud threw an exception"); } catch (SQLException e) { assertNotNull(e); } }
@Test public void testGetUserByUsername_NOK() { User user = UserFactoryForTests.getDefaultTestUser(); String salt = "salt"; try { userDAO.createUser(dbConnection, user, user.getPassword(), salt); String unknownUsername = "******"; User retrievedUser = userDAO.getUserByUsername(dbConnection, unknownUsername); assertNull(retrievedUser); } catch (SQLException e) { ExceptionUtilsForTests.logAndFailOnUnexpectedException(e); } }
@Test public void testGetPasswordByUsername_OK() { User user = UserFactoryForTests.getDefaultTestUser(); String salt = "salt"; try { assertEquals(0, userDAO.countUsersTable(dbConnection)); userDAO.createUser(dbConnection, user, user.getPassword(), salt); assertEquals(1, userDAO.countUsersTable(dbConnection)); String password = userDAO.getPasswordByUsername(dbConnection, user.getUsername()); assertEquals(user.getPassword(), password); } catch (SQLException e) { ExceptionUtilsForTests.logAndFailOnUnexpectedException(e); } }
@Test public void testCountUsersTable_NOK() { User user = UserFactoryForTests.getDefaultTestUser(); try { assertEquals(0, userDAO.countUsersTable(dbConnection)); int numberOfUsersIntoDb = userDAO.getAllUsers(dbConnection).size(); assertEquals(0, numberOfUsersIntoDb); assertEquals(numberOfUsersIntoDb, userDAO.countUsersTable(dbConnection)); User retrievedUser = userDAO.getUserByUsername(dbConnection, user.getUsername()); assertEquals(0, user.compareTo(retrievedUser)); } catch (SQLException e) { ExceptionUtilsForTests.logAndFailOnUnexpectedException(e); } }
@Test public void testDeleteUser_OK() { User defaultUser = UserFactoryForTests.getDefaultTestUser(); String salt = "salt"; try { assertEquals(0, userDAO.countUsersTable(dbConnection)); userDAO.createUser(dbConnection, defaultUser, defaultUser.getPassword(), salt); assertEquals(1, userDAO.countUsersTable(dbConnection)); boolean userWasDeleted = userDAO.deleteUser(dbConnection, defaultUser.getUsername()); assertEquals(true, userWasDeleted); assertEquals(0, userDAO.countUsersTable(dbConnection)); } catch (SQLException e) { ExceptionUtilsForTests.logAndFailOnUnexpectedException(e); } }
@Test public void testGetAllUsers_NOK() { int listSize = 0; List<User> userList = UserFactoryForTests.getUserList(listSize); String salt = "123"; try { for (User user : userList) { userDAO.createUser(dbConnection, user, user.getPassword(), salt); } assertEquals(listSize, userDAO.countUsersTable(dbConnection)); List<User> retrievedUsers = userDAO.getAllUsers(dbConnection); assertEquals(listSize, retrievedUsers.size()); } catch (SQLException e) { ExceptionUtilsForTests.logAndFailOnUnexpectedException(e); } }
@Test public void testUpdateUser_NOK() { User defaultUser = UserFactoryForTests.getDefaultTestUser(); String salt = "salt"; try { assertEquals(0, userDAO.countUsersTable(dbConnection)); userDAO.createUser(dbConnection, defaultUser, defaultUser.getPassword(), salt); assertEquals(1, userDAO.countUsersTable(dbConnection)); User updatedUser = UserFactoryForTests.getDefaultTestUser(1); String unknownUsername = "******"; boolean userWasUpdated = userDAO.updateUser( dbConnection, unknownUsername, updatedUser.getName(), updatedUser.getPassword()); assertEquals(false, userWasUpdated); assertEquals(1, userDAO.countUsersTable(dbConnection)); User unknownUser = userDAO.getUserByUsername(dbConnection, unknownUsername); assertNull(unknownUser); User retrievedDefaultUser = userDAO.getUserByUsername(dbConnection, defaultUser.getUsername()); assertEquals(defaultUser.getName(), retrievedDefaultUser.getName()); assertEquals(defaultUser.getUsername(), retrievedDefaultUser.getUsername()); assertEquals(defaultUser.getPassword(), retrievedDefaultUser.getPassword()); } catch (SQLException e) { ExceptionUtilsForTests.logAndFailOnUnexpectedException(e); } }