implement easyMail without test

feature/easyMail
Obai Albek 2025-05-30 21:23:05 +02:00
parent cfd37a9a03
commit 46c2fb84b8
9 changed files with 240 additions and 127 deletions

View File

@ -1,8 +1,96 @@
package domain; package domain;
import domain.user.UserManager; import java.time.LocalDateTime;
import domain.email.Email;
import domain.user.*;
public class EasyMail { 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);
}
} }

View File

@ -1,6 +1,5 @@
package domain.email; package domain.email;
import java.time.LocalDateTime;
import java.time.LocalDate;
import domain.user.*; import domain.user.*;
@ -9,9 +8,9 @@ public class Email {
private User receiver; private User receiver;
private String subject; private String subject;
private String content; 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.sender = sender;
this.receiver = receiver; this.receiver = receiver;
this.subject = subject; this.subject = subject;
@ -31,7 +30,7 @@ public class Email {
public String getContent() { public String getContent() {
return content; return content;
} }
public LocalDate getDate() { public LocalDateTime getDate() {
return date; return date;
} }

View File

@ -3,6 +3,7 @@ package domain.email;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -20,7 +21,7 @@ class RemoveEmailTest {
void setUp() { void setUp() {
user = new User("Alice", "Wonder", LocalDate.of(1995, 7, 20), "alicewonder", "wonderPass".toCharArray()); 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()), 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 @Test

View File

@ -1,12 +1,10 @@
package domain.email; package domain.email;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import domain.*; import domain.*;
import domain.email.EmailNotFoundException; import domain.email.EmailNotFoundException;
import domain.user.User; import domain.user.User;
@ -24,7 +22,7 @@ class SendEmailTest {
@Test @Test
void testAddEmailToSentFolder() { 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); boolean added = sender.getUsermail().getSentFolder().addEmail(email);
assertTrue(added); assertTrue(added);
@ -33,7 +31,7 @@ class SendEmailTest {
@Test @Test
void testRemoveEmailFromSentFolder() throws EmailNotFoundException { 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); boolean added = sender.getUsermail().getSentFolder().addEmail(email);
assertTrue(added); assertTrue(added);

View File

@ -68,7 +68,7 @@ class AddUserTest {
@Test @Test
void testErfolgreichHinzufuegen() throws Exception { void testErfolgreichHinzufuegen() throws Exception {
boolean result = userManager.addUser("Anna", "Beispiel","anna",1995, 15, "Mai", new char[]{'1'}, new char[]{'1'}); User result = userManager.addUser("Anna", "Beispiel","anna",1995, 15, "Mai", new char[]{'1'}, new char[]{'1'});
assertTrue(result); assertNotNull(result);
} }
} }

View File

@ -17,17 +17,17 @@ class CheckLoginTest {
} }
@Test @Test
void testLoginMitKorrektenDaten() { void testLoginMitKorrektenDaten() throws Exception {
assertTrue(userManager.checkLogin("ali.test@easymail.de", new char[]{'1','2','3','4'})); assertNotNull(userManager.checkLogin("ali.test@easymail.de", new char[]{'1','2','3','4'}));
} }
@Test @Test
void testLoginMitFalschemPasswort() { void testLoginMitFalschemPasswort() throws Exception {
assertFalse(userManager.checkLogin("ali.test@easymail.de", new char[]{'1','1','1','1'})); assertNull(userManager.checkLogin("ali.test@easymail.de", new char[]{'1','1','1','1'}));
} }
@Test @Test
void testLoginMitFalschemNutzername() { void testLoginMitFalschemNutzername() throws Exception {
assertFalse(userManager.checkLogin("nicht.vorhanden@easymail.de", new char[]{'1','2','3','4'})); assertNull(userManager.checkLogin("nicht.vorhanden@easymail.de", new char[]{'1','2','3','4'}));
} }
} }

View File

@ -17,14 +17,14 @@ class UpdateUserTest {
@Test @Test
void testUpdateUserSuccessfully() throws Exception { void testUpdateUserSuccessfully() throws Exception {
boolean updated = userManager.updateUser( User updated = userManager.updateUser(
"johndoe@easymail.de", "johndoe@easymail.de",
"Johnny", "Johnny",
"Doeman", "Doeman",
"newpass123".toCharArray(), "newpass123".toCharArray(),
"newpass123".toCharArray() "newpass123".toCharArray()
); );
assertTrue(updated); assertNull(updated);
User updatedUser = userManager.getUserByUsername("johndoe@easymail.de"); User updatedUser = userManager.getUserByUsername("johndoe@easymail.de");
assertEquals("Johnny", updatedUser.getFirstname()); assertEquals("Johnny", updatedUser.getFirstname());

View File

@ -7,6 +7,7 @@ public class UserEmail {
private static int counter = 1000; private static int counter = 1000;
private int accountID; private int accountID;
private String username; private String username;
private boolean status;
private char[] password; private char[] password;
private SentFolder sentFolder; private SentFolder sentFolder;
private TrashFolder trashFolder; private TrashFolder trashFolder;
@ -16,6 +17,7 @@ public class UserEmail {
this.username = username; this.username = username;
this.password = password; this.password = password;
this.accountID = counter++; this.accountID = counter++;
this.status = true;
this.sentFolder = new SentFolder(); this.sentFolder = new SentFolder();
this.trashFolder = new TrashFolder(); this.trashFolder = new TrashFolder();
this.inbox = new Inbox(); this.inbox = new Inbox();
@ -49,4 +51,16 @@ public class UserEmail {
return inbox; return inbox;
} }
public boolean getStatus() {
return status;
}
public void signIn() {
this.status = true;
}
public void signUp() {
this.status = false;
}
} }

View File

@ -14,129 +14,142 @@ public class UserManager {
this.users = new ArrayList<>(); this.users = new ArrayList<>();
} }
public boolean addUser(String firstName, String lastName, String username, public User addUser(String firstName, String lastName, String username, int year, int day, String monthName,
int year, int day, String monthName, char[] password, char[] passwordConfirmation) throws Exception {
char[] password, char[] passwordConfirmation) throws Exception {
if (firstName == null || lastName == null || username == null || password == null || passwordConfirmation == null) { if (firstName == null || lastName == null || username == null || password == null
throw new IllegalArgumentException("No input should be null!"); || passwordConfirmation == null) {
} throw new IllegalArgumentException("No input should be null!");
}
if (firstName.trim().isEmpty() || lastName.trim().isEmpty() || username.trim().isEmpty() || if (firstName.trim().isEmpty() || lastName.trim().isEmpty() || username.trim().isEmpty() || password.length == 0
password.length == 0 || passwordConfirmation.length == 0) { || passwordConfirmation.length == 0) {
throw new IllegalArgumentException("All fields are required!"); throw new IllegalArgumentException("All fields are required!");
} }
if (!Arrays.equals(password, passwordConfirmation)) if (!Arrays.equals(password, passwordConfirmation))
throw new IllegalArgumentException("Passwords do not match!"); throw new IllegalArgumentException("Passwords do not match!");
String email = username + "@easymail.de"; String email = username + "@easymail.de";
if (findUserByUsername(email) != null) if (findUserByUsername(email) != null)
throw new UserAlreadyExistsException("This email address is already taken!"); throw new UserAlreadyExistsException("This email address is already taken!");
int month = getMonthNumber(monthName); int month = getMonthNumber(monthName);
if (month == 0) if (month == 0)
throw new IllegalArgumentException("Invalid month name: " + monthName); throw new IllegalArgumentException("Invalid month name: " + monthName);
LocalDate birthDate = LocalDate.of(year, month, day); LocalDate birthDate = LocalDate.of(year, month, day);
char[] passwordCopy = Arrays.copyOf(password, password.length); char[] passwordCopy = Arrays.copyOf(password, password.length);
User newUser = new User(firstName, lastName, birthDate, email, passwordCopy); User newUser = new User(firstName, lastName, birthDate, email, passwordCopy);
users.add(newUser); users.add(newUser);
Arrays.fill(password, ' '); Arrays.fill(password, ' ');
Arrays.fill(passwordConfirmation, ' '); Arrays.fill(passwordConfirmation, ' ');
return true; return newUser;
} }
public boolean checkLogin(String username, char[] password) { public User checkLogin(String username, char[] password)throws Exception{
if (username == null || password == null) return false; if (username == null || password == null)
throw new UserAlreadyExistsException("This email address is already taken!");
for (User user : users) for (User user : users)
if (user.getUsermail().getUsername().equalsIgnoreCase(username) if (user.getUsermail().getUsername().equalsIgnoreCase(username)
&& Arrays.equals(user.getUsermail().getPassword(), password)) { && Arrays.equals(user.getUsermail().getPassword(), password)) {
Arrays.fill(password, ' '); Arrays.fill(password, ' ');
return true; return user;
} }
Arrays.fill(password, ' '); Arrays.fill(password, ' ');
return false; 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!");
User userToBeRemoved = findUserByUsername(username);
if (userToBeRemoved == null)
throw new UserNotFoundException("This email address is not found!");
users.remove(userToBeRemoved);
return true;
}
public int getNumberOfUsers() { public boolean removeUser(String username) throws UserNotFoundException {
return users.size(); if (username == null || username.trim().isEmpty())
} throw new IllegalArgumentException("Username cannot be null or empty!");
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) User userToBeRemoved = findUserByUsername(username);
throw new IllegalArgumentException("Fields cannot be null!"); if (userToBeRemoved == null)
throw new UserNotFoundException("This email address is not found!");
if (firstName.trim().isEmpty() || lastName.trim().isEmpty()) users.remove(userToBeRemoved);
throw new IllegalArgumentException("First name and last name are required!"); return true;
}
if (!Arrays.equals(password, confirm)) public int getNumberOfUsers() {
throw new IllegalArgumentException("Passwords do not match!"); return users.size();
}
userToBeUpdated.setFirstname(firstName); public User updateUser(String username, String firstName, String lastName, char[] password, char[] confirm)
userToBeUpdated.setLastname(lastName); throws Exception {
User userToBeUpdated = findUserByUsername(username);
char[] passwordCopy = Arrays.copyOf(password, password.length); if (userToBeUpdated == null)
userToBeUpdated.getUsermail().setPassword(passwordCopy); throw new UserNotFoundException("This email address is not found!");
Arrays.fill(password, ' ');
Arrays.fill(confirm, ' ');
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; 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;
}
}
} }