implement easyMail without test
parent
cfd37a9a03
commit
46c2fb84b8
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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'}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,16 +14,16 @@ 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
|
||||||
|
|| passwordConfirmation == null) {
|
||||||
throw new IllegalArgumentException("No input should be 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!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,20 +46,21 @@ public class UserManager {
|
||||||
|
|
||||||
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 {
|
public boolean removeUser(String username) throws UserNotFoundException {
|
||||||
|
@ -77,7 +78,9 @@ public class UserManager {
|
||||||
public int getNumberOfUsers() {
|
public int getNumberOfUsers() {
|
||||||
return users.size();
|
return users.size();
|
||||||
}
|
}
|
||||||
public boolean updateUser(String username, String firstName, String lastName, char[] password, char[] confirm) throws Exception {
|
|
||||||
|
public User updateUser(String username, String firstName, String lastName, char[] password, char[] confirm)
|
||||||
|
throws Exception {
|
||||||
User userToBeUpdated = findUserByUsername(username);
|
User userToBeUpdated = findUserByUsername(username);
|
||||||
if (userToBeUpdated == null)
|
if (userToBeUpdated == null)
|
||||||
throw new UserNotFoundException("This email address is not found!");
|
throw new UserNotFoundException("This email address is not found!");
|
||||||
|
@ -99,7 +102,7 @@ public class UserManager {
|
||||||
Arrays.fill(password, ' ');
|
Arrays.fill(password, ' ');
|
||||||
Arrays.fill(confirm, ' ');
|
Arrays.fill(confirm, ' ');
|
||||||
|
|
||||||
return true;
|
return userToBeUpdated;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getUserByUsername(String username) {
|
public User getUserByUsername(String username) {
|
||||||
|
@ -110,7 +113,7 @@ public class UserManager {
|
||||||
return currentUser;
|
return currentUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
private User findUserByUsername(String username) {
|
public User findUserByUsername(String username) {
|
||||||
for (User tempUser : users)
|
for (User tempUser : users)
|
||||||
if (tempUser.getUsermail().getUsername().equalsIgnoreCase(username))
|
if (tempUser.getUsermail().getUsername().equalsIgnoreCase(username))
|
||||||
return tempUser;
|
return tempUser;
|
||||||
|
@ -118,25 +121,35 @@ public class UserManager {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private int getMonthNumber(String txtMonth) {
|
private int getMonthNumber(String txtMonth) {
|
||||||
switch (txtMonth.toLowerCase()) {
|
switch (txtMonth.toLowerCase()) {
|
||||||
case "januar": return 1;
|
case "januar":
|
||||||
case "februar": return 2;
|
return 1;
|
||||||
case "märz": return 3;
|
case "februar":
|
||||||
case "april": return 4;
|
return 2;
|
||||||
case "mai": return 5;
|
case "märz":
|
||||||
case "juni": return 6;
|
return 3;
|
||||||
case "juli": return 7;
|
case "april":
|
||||||
case "august": return 8;
|
return 4;
|
||||||
case "september": return 9;
|
case "mai":
|
||||||
case "oktober": return 10;
|
return 5;
|
||||||
case "november": return 11;
|
case "juni":
|
||||||
case "dezember": return 12;
|
return 6;
|
||||||
default: return 0;
|
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