implements and test email-manager
parent
5dd01944e6
commit
cfd37a9a03
|
|
@ -0,0 +1,8 @@
|
||||||
|
package domain;
|
||||||
|
|
||||||
|
import domain.user.UserManager;
|
||||||
|
|
||||||
|
public class EasyMail {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
package domain.email;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import domain.user.*;
|
||||||
|
|
||||||
|
public class Email {
|
||||||
|
private User sender;
|
||||||
|
private User receiver;
|
||||||
|
private String subject;
|
||||||
|
private String content;
|
||||||
|
private LocalDate date;
|
||||||
|
|
||||||
|
public Email(User sender, User receiver, String subject, String content, LocalDate date) {
|
||||||
|
this.sender = sender;
|
||||||
|
this.receiver = receiver;
|
||||||
|
this.subject = subject;
|
||||||
|
this.content = content;
|
||||||
|
this.date = date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User getSender() {
|
||||||
|
return sender;
|
||||||
|
}
|
||||||
|
public User getReceiver() {
|
||||||
|
return receiver;
|
||||||
|
}
|
||||||
|
public String getSubject() {
|
||||||
|
return subject;
|
||||||
|
}
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
public LocalDate getDate() {
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Email [sender=" + sender + ", receiver=" + receiver + ", subject=" + subject + ", content=" + content
|
||||||
|
+ ", date=" + date + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
package domain.email;
|
||||||
|
|
||||||
|
public interface EmailFolder {
|
||||||
|
|
||||||
|
boolean addEmail(Email email);
|
||||||
|
Email removeEmail(String subject) throws EmailNotFoundException;
|
||||||
|
Email getEmailBySubject(String subject) throws EmailNotFoundException ;
|
||||||
|
int getNumberOfEmails();
|
||||||
|
boolean clearAllEmails();
|
||||||
|
String[] listAllEmails();
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package domain.email;
|
||||||
|
|
||||||
|
public class EmailNotFoundException extends Exception {
|
||||||
|
public EmailNotFoundException(String error) {
|
||||||
|
super(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,68 @@
|
||||||
|
package domain.email;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class Inbox implements EmailFolder {
|
||||||
|
|
||||||
|
private ArrayList<Email> receivedEmails;
|
||||||
|
|
||||||
|
public Inbox() {
|
||||||
|
this.receivedEmails = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addEmail(Email email) {
|
||||||
|
if (email == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
receivedEmails.add(email);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Email removeEmail(String subject) throws EmailNotFoundException {
|
||||||
|
Email emailToBeRemoved = getEmailBySubject(subject);
|
||||||
|
if (emailToBeRemoved == null) {
|
||||||
|
throw new EmailNotFoundException("Email not found in inbox!");
|
||||||
|
}
|
||||||
|
receivedEmails.remove(emailToBeRemoved);
|
||||||
|
return emailToBeRemoved;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Email getEmailBySubject(String subject) throws EmailNotFoundException {
|
||||||
|
if (subject == null || subject.trim().isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Subject cannot be null or empty!");
|
||||||
|
}
|
||||||
|
for (Email email : receivedEmails) {
|
||||||
|
if (email.getSubject().equalsIgnoreCase(subject)) {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new EmailNotFoundException("Email with subject '" + subject + "' not found!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getNumberOfEmails() {
|
||||||
|
return receivedEmails.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean clearAllEmails() {
|
||||||
|
if (receivedEmails.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
receivedEmails.clear();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] listAllEmails() {
|
||||||
|
String[] subjects = new String[receivedEmails.size()];
|
||||||
|
for (int i = 0; i < receivedEmails.size(); i++) {
|
||||||
|
subjects[i] = receivedEmails.get(i).getSubject();
|
||||||
|
}
|
||||||
|
return subjects;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
package domain.email;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import domain.*;
|
||||||
|
import domain.email.EmailNotFoundException;
|
||||||
|
import domain.user.*;
|
||||||
|
|
||||||
|
class RemoveEmailTest {
|
||||||
|
|
||||||
|
private User user;
|
||||||
|
private Email email;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAddEmailToTrash() {
|
||||||
|
boolean added = user.getUsermail().getTrashFolder().addEmail(email);
|
||||||
|
|
||||||
|
assertTrue(added); // erfolgreich hinzugefügt
|
||||||
|
assertEquals(1, user.getUsermail().getTrashFolder().getNumberOfEmails()); // Anzahl prüfen
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRemoveEmailNotFoundInTrash() {
|
||||||
|
assertThrows(EmailNotFoundException.class, () -> {
|
||||||
|
user.getUsermail().getTrashFolder().removeEmail("Nonexistent Subject");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
package domain.email;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import domain.*;
|
||||||
|
import domain.email.EmailNotFoundException;
|
||||||
|
import domain.user.User;
|
||||||
|
|
||||||
|
class SendEmailTest {
|
||||||
|
|
||||||
|
private User sender;
|
||||||
|
private User receiver;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
sender = new User("John", "Doe", LocalDate.of(1990, 1, 1), "johndoe", "password123".toCharArray());
|
||||||
|
receiver = new User("Jane", "Smith", LocalDate.of(1992, 5, 10), "janesmith", "securePass".toCharArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAddEmailToSentFolder() {
|
||||||
|
Email email = new Email(sender, receiver, "Test Subject", "This is a test email content.", LocalDate.now());
|
||||||
|
boolean added = sender.getUsermail().getSentFolder().addEmail(email);
|
||||||
|
|
||||||
|
assertTrue(added);
|
||||||
|
assertEquals(1, sender.getUsermail().getSentFolder().getNumberOfEmails()); // 1 Email gespeichert
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRemoveEmailFromSentFolder() throws EmailNotFoundException {
|
||||||
|
Email email = new Email(sender, receiver, "Test Subject", "This is a test email content.", LocalDate.now());
|
||||||
|
boolean added = sender.getUsermail().getSentFolder().addEmail(email);
|
||||||
|
|
||||||
|
assertTrue(added);
|
||||||
|
assertEquals(1, sender.getUsermail().getSentFolder().getNumberOfEmails());
|
||||||
|
|
||||||
|
Email removed = sender.getUsermail().getSentFolder().removeEmail("Test Subject");
|
||||||
|
assertNotNull(removed);
|
||||||
|
assertEquals(0, sender.getUsermail().getSentFolder().getNumberOfEmails());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
package domain.email;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class SentFolder implements EmailFolder {
|
||||||
|
|
||||||
|
private ArrayList<Email> emails;
|
||||||
|
|
||||||
|
public SentFolder() {
|
||||||
|
this.emails = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addEmail(Email email) {
|
||||||
|
if (email == null) return false;
|
||||||
|
return emails.add(email);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Email removeEmail(String subject) throws EmailNotFoundException {
|
||||||
|
Email emailToBeRemoved = getEmailBySubject(subject);
|
||||||
|
if (emailToBeRemoved == null)
|
||||||
|
throw new EmailNotFoundException("Email not found!");
|
||||||
|
|
||||||
|
emails.remove(emailToBeRemoved);
|
||||||
|
return emailToBeRemoved;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Email getEmailBySubject(String subject) throws EmailNotFoundException {
|
||||||
|
if (subject == null)
|
||||||
|
throw new EmailNotFoundException("Email not found!");
|
||||||
|
|
||||||
|
for (Email email : emails)
|
||||||
|
if (email.getSubject().equalsIgnoreCase(subject))
|
||||||
|
return email;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getNumberOfEmails() {
|
||||||
|
return emails.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean clearAllEmails() {
|
||||||
|
if (emails.isEmpty())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
emails.clear();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] listAllEmails() {
|
||||||
|
String[] subjects = new String[emails.size()];
|
||||||
|
for (int i = 0; i < emails.size(); i++)
|
||||||
|
subjects[i] = emails.get(i).toString();
|
||||||
|
|
||||||
|
return subjects;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,67 @@
|
||||||
|
package domain.email;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class TrashFolder implements EmailFolder {
|
||||||
|
|
||||||
|
private ArrayList<Email> removedEmails;
|
||||||
|
|
||||||
|
public TrashFolder() {
|
||||||
|
this.removedEmails = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addEmail(Email email) {
|
||||||
|
if (email == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
removedEmails.add(email);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Email removeEmail(String subject) throws EmailNotFoundException {
|
||||||
|
Email emailToBeRemoved = getEmailBySubject(subject);
|
||||||
|
if (emailToBeRemoved == null)
|
||||||
|
throw new EmailNotFoundException("Email not found in trash!");
|
||||||
|
|
||||||
|
removedEmails.remove(emailToBeRemoved);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Email getEmailBySubject(String subject) {
|
||||||
|
if (subject == null)
|
||||||
|
throw new IllegalArgumentException("No input should be null!");
|
||||||
|
|
||||||
|
for (Email email : removedEmails)
|
||||||
|
if (email.getSubject().equalsIgnoreCase(subject))
|
||||||
|
return email;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getNumberOfEmails() {
|
||||||
|
return removedEmails.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean clearAllEmails() {
|
||||||
|
if (removedEmails.isEmpty()) return false;
|
||||||
|
|
||||||
|
removedEmails.clear();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] listAllEmails() {
|
||||||
|
String[] subjects = new String[removedEmails.size()];
|
||||||
|
for (int i = 0; i < removedEmails.size(); i++)
|
||||||
|
subjects[i] = removedEmails.get(i).toString();
|
||||||
|
|
||||||
|
return subjects;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,48 +0,0 @@
|
||||||
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(" ");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
package domain.unitTest;
|
package domain.user;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import static org.junit.Assert.assertThrows;
|
import static org.junit.Assert.assertThrows;
|
||||||
|
|
||||||
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.UserManager;
|
|
||||||
|
|
||||||
class AddUserTest {
|
class AddUserTest {
|
||||||
private UserManager userManager;
|
private UserManager userManager;
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
package domain.unitTest;
|
package domain.user;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
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.UserManager;
|
|
||||||
|
|
||||||
class CheckLoginTest {
|
class CheckLoginTest {
|
||||||
private UserManager userManager;
|
private UserManager userManager;
|
||||||
|
|
@ -1,14 +1,9 @@
|
||||||
package domain.unitTest;
|
package domain.user;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
|
||||||
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.User;
|
|
||||||
import domain.UserManager;
|
|
||||||
import domain.exception.UserNotFoundException;
|
|
||||||
|
|
||||||
class UpdateUserTest {
|
class UpdateUserTest {
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package domain;
|
package domain.user;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package domain.exception;
|
package domain.user;
|
||||||
|
|
||||||
public class UserAlreadyExistsException extends Exception {
|
public class UserAlreadyExistsException extends Exception {
|
||||||
public UserAlreadyExistsException(String msg) {
|
public UserAlreadyExistsException(String msg) {
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
package domain;
|
package domain.user;
|
||||||
|
|
||||||
|
import domain.email.*;
|
||||||
|
|
||||||
public class UserEmail {
|
public class UserEmail {
|
||||||
|
|
||||||
|
|
@ -6,11 +8,17 @@ public class UserEmail {
|
||||||
private int accountID;
|
private int accountID;
|
||||||
private String username;
|
private String username;
|
||||||
private char[] password;
|
private char[] password;
|
||||||
|
private SentFolder sentFolder;
|
||||||
|
private TrashFolder trashFolder;
|
||||||
|
private Inbox inbox;
|
||||||
|
|
||||||
public UserEmail(String username, char[] password) {
|
public UserEmail(String username, char[] password) {
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.accountID = counter++;
|
this.accountID = counter++;
|
||||||
|
this.sentFolder = new SentFolder();
|
||||||
|
this.trashFolder = new TrashFolder();
|
||||||
|
this.inbox = new Inbox();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
|
|
@ -29,4 +37,16 @@ public class UserEmail {
|
||||||
return accountID;
|
return accountID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SentFolder getSentFolder() {
|
||||||
|
return sentFolder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TrashFolder getTrashFolder() {
|
||||||
|
return trashFolder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Inbox getInbox() {
|
||||||
|
return inbox;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
package domain;
|
package domain.user;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import domain.exception.UserAlreadyExistsException;
|
|
||||||
import domain.exception.UserNotFoundException;
|
|
||||||
|
|
||||||
public class UserManager {
|
public class UserManager {
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package domain.exception;
|
package domain.user;
|
||||||
|
|
||||||
public class UserNotFoundException extends Exception {
|
public class UserNotFoundException extends Exception {
|
||||||
public UserNotFoundException(String error) {
|
public UserNotFoundException(String error) {
|
||||||
Loading…
Reference in New Issue