implement easyMail without test
parent
cfd37a9a03
commit
46c2fb84b8
|
@ -1,8 +1,96 @@
|
|||
package domain;
|
||||
|
||||
import domain.user.UserManager;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import domain.email.Email;
|
||||
import domain.user.*;
|
||||
|
||||
public class EasyMail {
|
||||
private UserManager userManager;
|
||||
private User currentUser;
|
||||
|
||||
public EasyMail() {
|
||||
this.userManager = new UserManager();
|
||||
}
|
||||
|
||||
public void userRegister(String firstname, String lastName, String username, int year, int day, String monthName,
|
||||
char[] password, char[] passwordConfirmation) throws Exception {
|
||||
this.currentUser = userManager.addUser(firstname, lastName, username, year, day, monthName, password,
|
||||
passwordConfirmation);
|
||||
}
|
||||
|
||||
public void userSignin(String username, char[] password) throws Exception {
|
||||
this.currentUser = userManager.checkLogin(username, password);
|
||||
if (this.currentUser != null)
|
||||
this.currentUser.getUsermail().signIn();
|
||||
}
|
||||
|
||||
public boolean removeUser(String username) throws UserNotFoundException {
|
||||
return userManager.removeUser(username);
|
||||
}
|
||||
|
||||
public void updateUser(String username, String firstName, String lastName, char[] password, char[] confirm)
|
||||
throws Exception {
|
||||
this.currentUser = userManager.updateUser(username, firstName, lastName, password, confirm);
|
||||
}
|
||||
|
||||
public int getNumberOfUsers() {
|
||||
return userManager.getNumberOfUsers();
|
||||
}
|
||||
|
||||
public boolean sendEmail(String receiverEmail, String subject, String content) throws Exception {
|
||||
if (receiverEmail.trim().isEmpty() || subject.trim().isEmpty() || content.trim().isEmpty())
|
||||
throw new IllegalArgumentException("All fields are required!");
|
||||
|
||||
if (!currentUser.getUsermail().getStatus())
|
||||
throw new IllegalStateException("No user is currently logged in!");
|
||||
|
||||
User sender = this.currentUser;
|
||||
User receiver = userManager.findUserByUsername(receiverEmail);
|
||||
if (receiver == null)
|
||||
throw new UserNotFoundException("The receiver is not found!");
|
||||
|
||||
LocalDateTime timestamp = LocalDateTime.now();
|
||||
Email newEmail = new Email(sender, receiver, subject, content, timestamp);
|
||||
sender.getUsermail().getSentFolder().addEmail(newEmail);
|
||||
receiver.getUsermail().getInbox().addEmail(newEmail);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean removeEmailFromInbox(String subject) throws Exception {
|
||||
if (subject.trim().isEmpty())
|
||||
throw new IllegalArgumentException("Subject field is required!");
|
||||
|
||||
if (!this.currentUser.getUsermail().getStatus())
|
||||
throw new IllegalStateException("No user is currently logged in!");
|
||||
|
||||
Email removedEmail = this.currentUser.getUsermail().getInbox().removeEmail(subject);
|
||||
return this.currentUser.getUsermail().getTrashFolder().addEmail(removedEmail);
|
||||
|
||||
}
|
||||
|
||||
public boolean removeEmailFromSentFolder(String subject) throws Exception {
|
||||
if (subject.trim().isEmpty())
|
||||
throw new IllegalArgumentException("Subject field is required!");
|
||||
|
||||
if (!this.currentUser.getUsermail().getStatus())
|
||||
throw new IllegalStateException("No user is currently logged in!");
|
||||
|
||||
Email removedEmail= this.currentUser.getUsermail().getInbox().removeEmail(subject);
|
||||
return this.currentUser.getUsermail().getTrashFolder().addEmail(removedEmail);
|
||||
}
|
||||
|
||||
public void removeEmailFromTrash(String subject) throws Exception {
|
||||
if (subject.trim().isEmpty())
|
||||
throw new IllegalArgumentException("Subject field is required!");
|
||||
|
||||
if (!this.currentUser.getUsermail().getStatus())
|
||||
throw new IllegalStateException("No user is currently logged in!");
|
||||
|
||||
this.currentUser.getUsermail().getTrashFolder().removeEmail(subject);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package domain.email;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import domain.user.*;
|
||||
|
||||
|
@ -9,9 +8,9 @@ public class Email {
|
|||
private User receiver;
|
||||
private String subject;
|
||||
private String content;
|
||||
private LocalDate date;
|
||||
private LocalDateTime date;
|
||||
|
||||
public Email(User sender, User receiver, String subject, String content, LocalDate date) {
|
||||
public Email(User sender, User receiver, String subject, String content, LocalDateTime date) {
|
||||
this.sender = sender;
|
||||
this.receiver = receiver;
|
||||
this.subject = subject;
|
||||
|
@ -31,7 +30,7 @@ public class Email {
|
|||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
public LocalDate getDate() {
|
||||
public LocalDateTime getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package domain.email;
|
|||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -20,7 +21,7 @@ class RemoveEmailTest {
|
|||
void setUp() {
|
||||
user = new User("Alice", "Wonder", LocalDate.of(1995, 7, 20), "alicewonder", "wonderPass".toCharArray());
|
||||
email = new Email(user,new User("Bob", "Marley", LocalDate.of(1990, 6, 1), "bobmarley", "bobPass".toCharArray()),
|
||||
"Trash Test Subject", "This email will go to trash.", LocalDate.now());
|
||||
"Trash Test Subject", "This email will go to trash.", LocalDateTime.now());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
package domain.email;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import domain.*;
|
||||
import domain.email.EmailNotFoundException;
|
||||
import domain.user.User;
|
||||
|
@ -24,7 +22,7 @@ class SendEmailTest {
|
|||
|
||||
@Test
|
||||
void testAddEmailToSentFolder() {
|
||||
Email email = new Email(sender, receiver, "Test Subject", "This is a test email content.", LocalDate.now());
|
||||
Email email = new Email(sender, receiver, "Test Subject", "This is a test email content.", LocalDateTime.now());
|
||||
boolean added = sender.getUsermail().getSentFolder().addEmail(email);
|
||||
|
||||
assertTrue(added);
|
||||
|
@ -33,7 +31,7 @@ class SendEmailTest {
|
|||
|
||||
@Test
|
||||
void testRemoveEmailFromSentFolder() throws EmailNotFoundException {
|
||||
Email email = new Email(sender, receiver, "Test Subject", "This is a test email content.", LocalDate.now());
|
||||
Email email = new Email(sender, receiver, "Test Subject", "This is a test email content.", LocalDateTime.now());
|
||||
boolean added = sender.getUsermail().getSentFolder().addEmail(email);
|
||||
|
||||
assertTrue(added);
|
||||
|
|
|
@ -68,7 +68,7 @@ class AddUserTest {
|
|||
|
||||
@Test
|
||||
void testErfolgreichHinzufuegen() throws Exception {
|
||||
boolean result = userManager.addUser("Anna", "Beispiel","anna",1995, 15, "Mai", new char[]{'1'}, new char[]{'1'});
|
||||
assertTrue(result);
|
||||
User result = userManager.addUser("Anna", "Beispiel","anna",1995, 15, "Mai", new char[]{'1'}, new char[]{'1'});
|
||||
assertNotNull(result);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,17 +17,17 @@ class CheckLoginTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
void testLoginMitKorrektenDaten() {
|
||||
assertTrue(userManager.checkLogin("ali.test@easymail.de", new char[]{'1','2','3','4'}));
|
||||
void testLoginMitKorrektenDaten() throws Exception {
|
||||
assertNotNull(userManager.checkLogin("ali.test@easymail.de", new char[]{'1','2','3','4'}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoginMitFalschemPasswort() {
|
||||
assertFalse(userManager.checkLogin("ali.test@easymail.de", new char[]{'1','1','1','1'}));
|
||||
void testLoginMitFalschemPasswort() throws Exception {
|
||||
assertNull(userManager.checkLogin("ali.test@easymail.de", new char[]{'1','1','1','1'}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoginMitFalschemNutzername() {
|
||||
assertFalse(userManager.checkLogin("nicht.vorhanden@easymail.de", new char[]{'1','2','3','4'}));
|
||||
void testLoginMitFalschemNutzername() throws Exception {
|
||||
assertNull(userManager.checkLogin("nicht.vorhanden@easymail.de", new char[]{'1','2','3','4'}));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,14 +17,14 @@ class UpdateUserTest {
|
|||
|
||||
@Test
|
||||
void testUpdateUserSuccessfully() throws Exception {
|
||||
boolean updated = userManager.updateUser(
|
||||
User updated = userManager.updateUser(
|
||||
"johndoe@easymail.de",
|
||||
"Johnny",
|
||||
"Doeman",
|
||||
"newpass123".toCharArray(),
|
||||
"newpass123".toCharArray()
|
||||
);
|
||||
assertTrue(updated);
|
||||
assertNull(updated);
|
||||
|
||||
User updatedUser = userManager.getUserByUsername("johndoe@easymail.de");
|
||||
assertEquals("Johnny", updatedUser.getFirstname());
|
||||
|
|
|
@ -7,6 +7,7 @@ public class UserEmail {
|
|||
private static int counter = 1000;
|
||||
private int accountID;
|
||||
private String username;
|
||||
private boolean status;
|
||||
private char[] password;
|
||||
private SentFolder sentFolder;
|
||||
private TrashFolder trashFolder;
|
||||
|
@ -16,6 +17,7 @@ public class UserEmail {
|
|||
this.username = username;
|
||||
this.password = password;
|
||||
this.accountID = counter++;
|
||||
this.status = true;
|
||||
this.sentFolder = new SentFolder();
|
||||
this.trashFolder = new TrashFolder();
|
||||
this.inbox = new Inbox();
|
||||
|
@ -49,4 +51,16 @@ public class UserEmail {
|
|||
return inbox;
|
||||
}
|
||||
|
||||
public boolean getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void signIn() {
|
||||
this.status = true;
|
||||
}
|
||||
|
||||
public void signUp() {
|
||||
this.status = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -14,129 +14,142 @@ public class UserManager {
|
|||
this.users = new ArrayList<>();
|
||||
}
|
||||
|
||||
public boolean addUser(String firstName, String lastName, String username,
|
||||
int year, int day, String monthName,
|
||||
char[] password, char[] passwordConfirmation) throws Exception {
|
||||
public User addUser(String firstName, String lastName, String username, int year, int day, String monthName,
|
||||
char[] password, char[] passwordConfirmation) throws Exception {
|
||||
|
||||
if (firstName == null || lastName == null || username == null || password == null || passwordConfirmation == null) {
|
||||
throw new IllegalArgumentException("No input should be null!");
|
||||
}
|
||||
if (firstName == null || lastName == null || username == null || password == null
|
||||
|| passwordConfirmation == null) {
|
||||
throw new IllegalArgumentException("No input should be null!");
|
||||
}
|
||||
|
||||
if (firstName.trim().isEmpty() || lastName.trim().isEmpty() || username.trim().isEmpty() ||
|
||||
password.length == 0 || passwordConfirmation.length == 0) {
|
||||
throw new IllegalArgumentException("All fields are required!");
|
||||
}
|
||||
if (firstName.trim().isEmpty() || lastName.trim().isEmpty() || username.trim().isEmpty() || password.length == 0
|
||||
|| passwordConfirmation.length == 0) {
|
||||
throw new IllegalArgumentException("All fields are required!");
|
||||
}
|
||||
|
||||
if (!Arrays.equals(password, passwordConfirmation))
|
||||
throw new IllegalArgumentException("Passwords do not match!");
|
||||
if (!Arrays.equals(password, passwordConfirmation))
|
||||
throw new IllegalArgumentException("Passwords do not match!");
|
||||
|
||||
String email = username + "@easymail.de";
|
||||
if (findUserByUsername(email) != null)
|
||||
throw new UserAlreadyExistsException("This email address is already taken!");
|
||||
String email = username + "@easymail.de";
|
||||
if (findUserByUsername(email) != null)
|
||||
throw new UserAlreadyExistsException("This email address is already taken!");
|
||||
|
||||
int month = getMonthNumber(monthName);
|
||||
if (month == 0)
|
||||
throw new IllegalArgumentException("Invalid month name: " + monthName);
|
||||
int month = getMonthNumber(monthName);
|
||||
if (month == 0)
|
||||
throw new IllegalArgumentException("Invalid month name: " + monthName);
|
||||
|
||||
LocalDate birthDate = LocalDate.of(year, month, day);
|
||||
char[] passwordCopy = Arrays.copyOf(password, password.length);
|
||||
User newUser = new User(firstName, lastName, birthDate, email, passwordCopy);
|
||||
LocalDate birthDate = LocalDate.of(year, month, day);
|
||||
char[] passwordCopy = Arrays.copyOf(password, password.length);
|
||||
User newUser = new User(firstName, lastName, birthDate, email, passwordCopy);
|
||||
|
||||
users.add(newUser);
|
||||
users.add(newUser);
|
||||
|
||||
Arrays.fill(password, ' ');
|
||||
Arrays.fill(passwordConfirmation, ' ');
|
||||
return true;
|
||||
}
|
||||
Arrays.fill(password, ' ');
|
||||
Arrays.fill(passwordConfirmation, ' ');
|
||||
return newUser;
|
||||
}
|
||||
|
||||
public boolean checkLogin(String username, char[] password) {
|
||||
if (username == null || password == null) return false;
|
||||
public User checkLogin(String username, char[] password)throws Exception{
|
||||
if (username == null || password == null)
|
||||
throw new UserAlreadyExistsException("This email address is already taken!");
|
||||
|
||||
for (User user : users)
|
||||
if (user.getUsermail().getUsername().equalsIgnoreCase(username)
|
||||
&& Arrays.equals(user.getUsermail().getPassword(), password)) {
|
||||
Arrays.fill(password, ' ');
|
||||
return true;
|
||||
}
|
||||
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;
|
||||
}
|
||||
for (User user : users)
|
||||
if (user.getUsermail().getUsername().equalsIgnoreCase(username)
|
||||
&& Arrays.equals(user.getUsermail().getPassword(), password)) {
|
||||
Arrays.fill(password, ' ');
|
||||
return user;
|
||||
}
|
||||
Arrays.fill(password, ' ');
|
||||
throw new UserNotFoundException("This email address is not found!");
|
||||
}
|
||||
|
||||
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!");
|
||||
public boolean removeUser(String username) throws UserNotFoundException {
|
||||
if (username == null || username.trim().isEmpty())
|
||||
throw new IllegalArgumentException("Username cannot be null or empty!");
|
||||
|
||||
if (firstName == null || lastName == null || password == null || confirm == null)
|
||||
throw new IllegalArgumentException("Fields cannot be null!");
|
||||
User userToBeRemoved = findUserByUsername(username);
|
||||
if (userToBeRemoved == null)
|
||||
throw new UserNotFoundException("This email address is not found!");
|
||||
|
||||
if (firstName.trim().isEmpty() || lastName.trim().isEmpty())
|
||||
throw new IllegalArgumentException("First name and last name are required!");
|
||||
users.remove(userToBeRemoved);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!Arrays.equals(password, confirm))
|
||||
throw new IllegalArgumentException("Passwords do not match!");
|
||||
public int getNumberOfUsers() {
|
||||
return users.size();
|
||||
}
|
||||
|
||||
userToBeUpdated.setFirstname(firstName);
|
||||
userToBeUpdated.setLastname(lastName);
|
||||
|
||||
char[] passwordCopy = Arrays.copyOf(password, password.length);
|
||||
userToBeUpdated.getUsermail().setPassword(passwordCopy);
|
||||
Arrays.fill(password, ' ');
|
||||
Arrays.fill(confirm, ' ');
|
||||
public User 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!");
|
||||
|
||||
return true;
|
||||
}
|
||||
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 userToBeUpdated;
|
||||
}
|
||||
|
||||
public User getUserByUsername(String username) {
|
||||
this.currentUser = findUserByUsername(username);
|
||||
if (this.currentUser == null)
|
||||
return null;
|
||||
|
||||
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;
|
||||
case "februar": return 2;
|
||||
case "märz": return 3;
|
||||
case "april": return 4;
|
||||
case "mai": return 5;
|
||||
case "juni": return 6;
|
||||
case "juli": return 7;
|
||||
case "august": return 8;
|
||||
case "september": return 9;
|
||||
case "oktober": return 10;
|
||||
case "november": return 11;
|
||||
case "dezember": return 12;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public 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;
|
||||
case "februar":
|
||||
return 2;
|
||||
case "märz":
|
||||
return 3;
|
||||
case "april":
|
||||
return 4;
|
||||
case "mai":
|
||||
return 5;
|
||||
case "juni":
|
||||
return 6;
|
||||
case "juli":
|
||||
return 7;
|
||||
case "august":
|
||||
return 8;
|
||||
case "september":
|
||||
return 9;
|
||||
case "oktober":
|
||||
return 10;
|
||||
case "november":
|
||||
return 11;
|
||||
case "dezember":
|
||||
return 12;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue