Implement UserManager with CRUD operations and authentication
parent
b643d2d687
commit
5dd01944e6
|
@ -5,11 +5,13 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
|
||||
import domain.exception.UserAlreadyExistsException;
|
||||
import domain.exception.UserNotFoundException;
|
||||
|
||||
public class UserManager {
|
||||
|
||||
private ArrayList<User> users;
|
||||
|
||||
private User currentUser;
|
||||
|
||||
public UserManager() {
|
||||
this.users = new ArrayList<>();
|
||||
}
|
||||
|
@ -31,10 +33,8 @@ public class UserManager {
|
|||
throw new IllegalArgumentException("Passwords do not match!");
|
||||
|
||||
String email = username + "@easymail.de";
|
||||
|
||||
for (User tempUser : users)
|
||||
if (tempUser.getUsermail().getUsername().equalsIgnoreCase(email))
|
||||
throw new UserAlreadyExistsException("This email address is already taken!");
|
||||
if (findUserByUsername(email) != null)
|
||||
throw new UserAlreadyExistsException("This email address is already taken!");
|
||||
|
||||
int month = getMonthNumber(monthName);
|
||||
if (month == 0)
|
||||
|
@ -63,8 +63,64 @@ public class UserManager {
|
|||
Arrays.fill(password, ' ');
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean removeUser(String username) throws UserNotFoundException {
|
||||
if (username == null || username.trim().isEmpty())
|
||||
throw new IllegalArgumentException("Username cannot be null or empty!");
|
||||
|
||||
User userToBeRemoved = findUserByUsername(username);
|
||||
if (userToBeRemoved == null)
|
||||
throw new UserNotFoundException("This email address is not found!");
|
||||
|
||||
users.remove(userToBeRemoved);
|
||||
return true;
|
||||
}
|
||||
|
||||
public int getNumberOfUsers() {
|
||||
return users.size();
|
||||
}
|
||||
public boolean updateUser(String username, String firstName, String lastName, char[] password, char[] confirm) throws Exception {
|
||||
User userToBeUpdated = findUserByUsername(username);
|
||||
if (userToBeUpdated == null)
|
||||
throw new UserNotFoundException("This email address is not found!");
|
||||
|
||||
if (firstName == null || lastName == null || password == null || confirm == null)
|
||||
throw new IllegalArgumentException("Fields cannot be null!");
|
||||
|
||||
if (firstName.trim().isEmpty() || lastName.trim().isEmpty())
|
||||
throw new IllegalArgumentException("First name and last name are required!");
|
||||
|
||||
if (!Arrays.equals(password, confirm))
|
||||
throw new IllegalArgumentException("Passwords do not match!");
|
||||
|
||||
userToBeUpdated.setFirstname(firstName);
|
||||
userToBeUpdated.setLastname(lastName);
|
||||
|
||||
char[] passwordCopy = Arrays.copyOf(password, password.length);
|
||||
userToBeUpdated.getUsermail().setPassword(passwordCopy);
|
||||
Arrays.fill(password, ' ');
|
||||
Arrays.fill(confirm, ' ');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public User getUserByUsername(String username) {
|
||||
this.currentUser = findUserByUsername(username);
|
||||
if (this.currentUser == null)
|
||||
return null;
|
||||
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
private User findUserByUsername(String username) {
|
||||
for (User tempUser : users)
|
||||
if (tempUser.getUsermail().getUsername().equalsIgnoreCase(username))
|
||||
return tempUser;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private int getMonthNumber(String txtMonth) {
|
||||
switch (txtMonth.toLowerCase()) {
|
||||
case "januar": return 1;
|
||||
|
@ -82,5 +138,7 @@ public class UserManager {
|
|||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package domain.exception;
|
||||
|
||||
public class UserNotFoundException extends Exception {
|
||||
public UserNotFoundException(String error) {
|
||||
super(error);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package domain.unitTest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import domain.UserManager;
|
||||
import domain.exception.UserNotFoundException;
|
||||
|
||||
class RemoveUserTest {
|
||||
|
||||
private UserManager userManager;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
userManager = new UserManager();
|
||||
userManager.addUser("Jane", "Doe", "janedoe", 1992, 20, "April", "securePass1".toCharArray(), "securePass1".toCharArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveUserSuccessfully() throws Exception {
|
||||
boolean removed = userManager.removeUser("janedoe@easymail.de");
|
||||
assertTrue(removed);
|
||||
|
||||
assertNull(userManager.getUserByUsername("janedoe@easymail.de"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveUserNotFound() {
|
||||
assertThrows(UserNotFoundException.class, () -> {
|
||||
userManager.removeUser("nonexistent@easymail.de");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveUserNullUsername() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
userManager.removeUser(null);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveUserEmptyUsername() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
userManager.removeUser(" ");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,130 @@
|
|||
package domain.unitTest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import domain.User;
|
||||
import domain.UserManager;
|
||||
import domain.exception.UserNotFoundException;
|
||||
|
||||
class UpdateUserTest {
|
||||
|
||||
private UserManager userManager;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
userManager = new UserManager();
|
||||
userManager.addUser("John", "Doe", "johndoe", 1990, 15, "Mai", "password123".toCharArray(), "password123".toCharArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateUserSuccessfully() throws Exception {
|
||||
boolean updated = userManager.updateUser(
|
||||
"johndoe@easymail.de",
|
||||
"Johnny",
|
||||
"Doeman",
|
||||
"newpass123".toCharArray(),
|
||||
"newpass123".toCharArray()
|
||||
);
|
||||
assertTrue(updated);
|
||||
|
||||
User updatedUser = userManager.getUserByUsername("johndoe@easymail.de");
|
||||
assertEquals("Johnny", updatedUser.getFirstname());
|
||||
assertEquals("Doeman", updatedUser.getLastname());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateUserNotFound() {
|
||||
assertThrows(UserNotFoundException.class, () -> {
|
||||
userManager.updateUser(
|
||||
"unknown@easymail.de",
|
||||
"Johnny",
|
||||
"Doeman",
|
||||
"newpass123".toCharArray(),
|
||||
"newpass123".toCharArray()
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateUserNullFields() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
userManager.updateUser(
|
||||
"johndoe@easymail.de",
|
||||
null,
|
||||
"Doeman",
|
||||
"newpass123".toCharArray(),
|
||||
"newpass123".toCharArray()
|
||||
);
|
||||
});
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
userManager.updateUser(
|
||||
"johndoe@easymail.de",
|
||||
"Johnny",
|
||||
null,
|
||||
"newpass123".toCharArray(),
|
||||
"newpass123".toCharArray()
|
||||
);
|
||||
});
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
userManager.updateUser(
|
||||
"johndoe@easymail.de",
|
||||
"Johnny",
|
||||
"Doeman",
|
||||
null,
|
||||
"newpass123".toCharArray()
|
||||
);
|
||||
});
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
userManager.updateUser(
|
||||
"johndoe@easymail.de",
|
||||
"Johnny",
|
||||
"Doeman",
|
||||
"newpass123".toCharArray(),
|
||||
null
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateUserEmptyFirstnameOrLastname() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
userManager.updateUser(
|
||||
"johndoe@easymail.de",
|
||||
" ",
|
||||
"Doeman",
|
||||
"newpass123".toCharArray(),
|
||||
"newpass123".toCharArray()
|
||||
);
|
||||
});
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
userManager.updateUser(
|
||||
"johndoe@easymail.de",
|
||||
"Johnny",
|
||||
" ",
|
||||
"newpass123".toCharArray(),
|
||||
"newpass123".toCharArray()
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateUserPasswordMismatch() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
userManager.updateUser(
|
||||
"johndoe@easymail.de",
|
||||
"Johnny",
|
||||
"Doeman",
|
||||
"newpass123".toCharArray(),
|
||||
"differentpass".toCharArray()
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue