diff --git a/src/main/java/edu/group5/app/model/DBRepository.java b/src/main/java/edu/group5/app/model/DBRepository.java index 799a977..f3363b7 100644 --- a/src/main/java/edu/group5/app/model/DBRepository.java +++ b/src/main/java/edu/group5/app/model/DBRepository.java @@ -30,11 +30,7 @@ protected void updateContentLock() { } } - public void addContent(K key, V value) { - synchronized (contentLock) { - content.put(key, value); - } - } + public abstract boolean addContent(V value); /** * Exports the repository content as a list of Object arrays, where each array represents a row of data. diff --git a/src/main/java/edu/group5/app/model/donation/DonationRepository.java b/src/main/java/edu/group5/app/model/donation/DonationRepository.java index 8f9b4ec..6181460 100644 --- a/src/main/java/edu/group5/app/model/donation/DonationRepository.java +++ b/src/main/java/edu/group5/app/model/donation/DonationRepository.java @@ -100,14 +100,15 @@ public Map getAllDonations() { * @return {@code true} if the donation was successfully added, and * {@code false} if a donation with the same ID already exists */ - public boolean addDonation(Donation donation) { + @Override + public boolean addContent(Donation donation) { if (donation == null) { throw new IllegalArgumentException("Donation cannot be null"); } if (content.containsKey(donation.donationId())){ - return false; + return false; } - content.put(donation.donationId(), donation); + this.content.put(donation.donationId(), donation); return true; } diff --git a/src/main/java/edu/group5/app/model/donation/DonationService.java b/src/main/java/edu/group5/app/model/donation/DonationService.java index d8fef60..804d901 100644 --- a/src/main/java/edu/group5/app/model/donation/DonationService.java +++ b/src/main/java/edu/group5/app/model/donation/DonationService.java @@ -57,7 +57,7 @@ public boolean donate(Customer customer, int orgNumber, BigDecimal amount, Strin Donation donation = new Donation(donationRepository.getNextDonationId(), customer.getUserId(), org.orgNumber(), amount, Timestamp.from(Instant.now()), paymentMethod); - donationRepository.addDonation(donation); + donationRepository.addContent(donation); return true; } } \ No newline at end of file diff --git a/src/main/java/edu/group5/app/model/user/UserRepository.java b/src/main/java/edu/group5/app/model/user/UserRepository.java index 8ccb790..692126f 100644 --- a/src/main/java/edu/group5/app/model/user/UserRepository.java +++ b/src/main/java/edu/group5/app/model/user/UserRepository.java @@ -76,13 +76,12 @@ public User getUserById(int userId) { * @throws IllegalStateException if no available user ID can be found */ public int getNextUserId() { - int nextId = content.size() + 1; - while (content.containsKey(nextId)) { - nextId++; - if (nextId <= 0) { - throw new IllegalStateException("No available user ID found"); - } + if (content.isEmpty()) { + return 1; } + int maxKey = content.keySet().stream().max(Integer::compareTo).orElseThrow( + () -> new IllegalStateException("No keys found")); + int nextId = maxKey + 1; return nextId; } /* TODO change this when data database is introduced */ @@ -98,14 +97,15 @@ public int getNextUserId() { * @return {@code true} if the user was successfully added, and * {@code false} if a user with the same ID already exists */ - public boolean addUser(User user) { + @Override + public boolean addContent(User user) { if (user == null) { throw new IllegalArgumentException("User cannot be null"); } if (content.containsKey(user.getUserId())){ return false; } - content.put(user.getUserId(), user); + this.content.put(user.getUserId(), user); return true; } diff --git a/src/main/java/edu/group5/app/model/user/UserService.java b/src/main/java/edu/group5/app/model/user/UserService.java index dfe66a1..d430a59 100644 --- a/src/main/java/edu/group5/app/model/user/UserService.java +++ b/src/main/java/edu/group5/app/model/user/UserService.java @@ -48,20 +48,20 @@ public boolean registerUser(String role, String firstName, String lastName, } else { /* TODO when you switch to a real DB, replace getNextUserId with DB auto-increment/identity and ignore manual ID generation in service*/ return false; } - userRepository.addUser(user); + userRepository.addContent(user); return true; } /** * Authenticates a user based on the provided email and password. * @param email the email address of the user attempting to log in; must not be null or empty - * @param password the plaintext password of the user attempting to log in; must not be + * @param password the plaintext password of the user attempting to log in; must not be null or empty * @return true if the login is successful * (i.e., the user exists and the password is correct), false otherwise - * @throws IllegalArgumentException if email is null or empty, or if password is null + * @throws IllegalArgumentException if email is null or empty, or if password is null or empty */ public boolean login(String email, String password) { - if (email == null || email.trim().isEmpty() || password == null) { + if (email == null || email.trim().isEmpty() || password == null || password.trim().isEmpty()) { return false; } User user = userRepository.findUserByEmail(email); diff --git a/src/test/java/edu/group5/app/model/donation/DonationRepositoryTest.java b/src/test/java/edu/group5/app/model/donation/DonationRepositoryTest.java index fd7ec1e..bdf0110 100644 --- a/src/test/java/edu/group5/app/model/donation/DonationRepositoryTest.java +++ b/src/test/java/edu/group5/app/model/donation/DonationRepositoryTest.java @@ -71,29 +71,29 @@ void constructorThrowsIfRowIsNull() { } @Test - void addDonationSuccessfully() { - assertTrue(repo.addDonation(donation1)); + void addContentSuccessfully() { + assertTrue(repo.addContent(donation1)); assertEquals(1, repo.getAllDonations().size()); assertTrue(repo.getAllDonations().containsValue(donation1)); } @Test - void addDonationDuplicateIdFails() { - repo.addDonation(donation1); - assertFalse(repo.addDonation(donation4)); + void addContentDuplicateIdFails() { + repo.addContent(donation1); + assertFalse(repo.addContent(donation4)); assertEquals(1, repo.getAllDonations().size()); } @Test - void addDonationNullThrows() { + void addContentNullThrows() { IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, - () -> repo.addDonation(null)); + () -> repo.addContent(null)); assertEquals("Donation cannot be null", ex.getMessage()); } @Test void getDonationByIdSuccessfully() { - repo.addDonation(donation1); + repo.addContent(donation1); Donation retrieved = repo.getDonationById(1); assertEquals(donation1, retrieved); } @@ -112,7 +112,7 @@ void getDonationByIdReturnsNullIfNotFound() { @Test void getAllDonationsReturnsCopy() { - repo.addDonation(donation1); + repo.addContent(donation1); Map all = repo.getAllDonations(); assertEquals(1, all.size()); all.clear(); @@ -121,9 +121,9 @@ void getAllDonationsReturnsCopy() { @Test void sortByDateAscending() { - repo.addDonation(donation3); - repo.addDonation(donation1); - repo.addDonation(donation2); + repo.addContent(donation3); + repo.addContent(donation1); + repo.addContent(donation2); Map sorted = repo.sortByDate(); Integer[] keys = sorted.keySet().toArray(new Integer[0]); @@ -132,8 +132,8 @@ void sortByDateAscending() { @Test void sortByDateWithSameDateKeepsAll() { - repo.addDonation(donation1); - repo.addDonation(donation4); + repo.addContent(donation1); + repo.addContent(donation4); Map sorted = repo.sortByDate(); assertEquals(1, sorted.size()); assertTrue(sorted.containsKey(1)); @@ -141,8 +141,8 @@ void sortByDateWithSameDateKeepsAll() { @Test void sortByAmountAscending() { - repo.addDonation(donation3); - repo.addDonation(donation1); + repo.addContent(donation3); + repo.addContent(donation1); Map sorted = repo.sortByAmount(); Integer[] keys = sorted.keySet().toArray(new Integer[0]); @@ -151,8 +151,8 @@ void sortByAmountAscending() { @Test void sortByAmountWithSameAmount() { - repo.addDonation(donation1); - repo.addDonation(donation2); + repo.addContent(donation1); + repo.addContent(donation2); Map sorted = repo.sortByAmount(); assertEquals(2, sorted.size()); assertTrue(sorted.containsKey(1)); @@ -161,9 +161,9 @@ void sortByAmountWithSameAmount() { @Test void filterByOrganizationMatches() { - repo.addDonation(donation1); - repo.addDonation(donation2); - repo.addDonation(donation3); + repo.addContent(donation1); + repo.addContent(donation2); + repo.addContent(donation3); Map filtered = repo.filterByOrganization(202); assertEquals(2, filtered.size()); @@ -173,7 +173,7 @@ void filterByOrganizationMatches() { @Test void filterByOrganizationNoMatch() { - repo.addDonation(donation1); + repo.addContent(donation1); Map filtered = repo.filterByOrganization(999); assertTrue(filtered.isEmpty()); } @@ -187,8 +187,8 @@ void filterByOrganizationThrowsIfNegative() { @Test void exportReturnsAllRowsSortedById() { - repo.addDonation(donation2); - repo.addDonation(donation1); + repo.addContent(donation2); + repo.addContent(donation1); List exported = repo.export(); assertEquals(2, exported.size()); @@ -199,8 +199,8 @@ void exportReturnsAllRowsSortedById() { @Test void getNextDonationIdReturnsCorrectNextId() { assertEquals(1, repo.getNextDonationId()); - repo.addDonation(donation1); - repo.addDonation(donation4); + repo.addContent(donation1); + repo.addContent(donation4); assertEquals(2, repo.getNextDonationId()); } @@ -214,7 +214,7 @@ void exportEmptyRepositoryReturnsEmptyList() { void sortByDateHandlesDuplicateKeysWithLambda() { Donation d1 = new Donation(1, 101, 201, new BigDecimal("100"), ts1, "Card"); Donation d2 = new Donation(1, 102, 202, new BigDecimal("200"), ts2, "PayPal"); - repo.addDonation(d1); + repo.addContent(d1); repo.getAllDonations().put(d2.donationId(), d2); Map sorted = repo.sortByDate(); assertEquals(d1, sorted.get(1)); @@ -224,7 +224,7 @@ void sortByDateHandlesDuplicateKeysWithLambda() { void sortByAmountHandlesDuplicateKeysWithLambda() { Donation d1 = new Donation(1, 101, 201, new BigDecimal("100"), ts1, "Card"); Donation d2 = new Donation(1, 102, 202, new BigDecimal("100"), ts2, "PayPal"); - repo.addDonation(d1); + repo.addContent(d1); repo.getAllDonations().put(d2.donationId(), d2); Map sorted = repo.sortByAmount(); assertEquals(d1, sorted.get(1)); @@ -234,7 +234,7 @@ void sortByAmountHandlesDuplicateKeysWithLambda() { void filterByOrganizationHandlesDuplicateKeysWithLambda() { Donation d1 = new Donation(1, 101, 201, new BigDecimal("100"), ts1, "Card"); Donation d2 = new Donation(1, 102, 201, new BigDecimal("200"), ts2, "PayPal"); - repo.addDonation(d1); + repo.addContent(d1); repo.getAllDonations().put(d2.donationId(), d2); Map filtered = repo.filterByOrganization(201); assertEquals(d1, filtered.get(1)); @@ -242,8 +242,8 @@ void filterByOrganizationHandlesDuplicateKeysWithLambda() { @Test void filterByUserIdMatches() { - repo.addDonation(donation1); - repo.addDonation(donation3); + repo.addContent(donation1); + repo.addContent(donation3); Map filtered = repo.filterByUser(102); assertEquals(1, filtered.size()); @@ -252,7 +252,7 @@ void filterByUserIdMatches() { @Test void filterByUserIdNoMatch() { - repo.addDonation(donation1); + repo.addContent(donation1); Map filtered = repo.filterByUser(999); assertTrue(filtered.isEmpty()); } diff --git a/src/test/java/edu/group5/app/model/user/UserRepositoryTest.java b/src/test/java/edu/group5/app/model/user/UserRepositoryTest.java index 1b19c88..0700828 100644 --- a/src/test/java/edu/group5/app/model/user/UserRepositoryTest.java +++ b/src/test/java/edu/group5/app/model/user/UserRepositoryTest.java @@ -47,23 +47,41 @@ void constructorThrowsIfUnknownRole() { } @Test - void addUserSuccessfully() { + void constructorThrowsIfRowNull() { + List invalid = new ArrayList<>(); + invalid.add(null); + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> new UserRepository(invalid)); + assertEquals("Each row must contain exactly 6 elements", ex.getMessage()); + } + + @Test + void constructorThrowsIfRowHasIncorrectLength() { + List invalid = new ArrayList<>(); + invalid.add(new Object[]{1, "Customer", "John"}); + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> new UserRepository(invalid)); + assertEquals("Each row must contain exactly 6 elements", ex.getMessage()); + } + + @Test + void addContentSuccessfully() { Customer user = new Customer(3, "Bob", "Smith", "bob@example.com", "pass"); - assertTrue(repo.addUser(user)); + assertTrue(repo.addContent(user)); assertEquals(3, repo.getUsers().size()); } @Test - void addUserNullThrows() { + void addContentNullThrows() { IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, - () -> repo.addUser(null)); + () -> repo.addContent(null)); assertEquals("User cannot be null", ex.getMessage()); } @Test - void addUserDuplicateReturnsFalse() { + void addContentDuplicateReturnsFalse() { Customer user = new Customer(1, "John", "Cena", "john@example.com", "pass"); - assertFalse(repo.addUser(user)); + assertFalse(repo.addContent(user)); } @Test @@ -113,6 +131,12 @@ void getNextUserIdReturnsNextAvailable() { assertEquals(3, nextId); } + @Test + void getNextUserIdReturns1IfEmpty() { + UserRepository emptyRepo = new UserRepository(new ArrayList<>()); + assertEquals(1, emptyRepo.getNextUserId()); + } + @Test void exportContainsAllUsers() { List exported = repo.export(); diff --git a/src/test/java/edu/group5/app/model/user/UserServiceTest.java b/src/test/java/edu/group5/app/model/user/UserServiceTest.java index 57d4bf2..20397a6 100644 --- a/src/test/java/edu/group5/app/model/user/UserServiceTest.java +++ b/src/test/java/edu/group5/app/model/user/UserServiceTest.java @@ -54,8 +54,18 @@ void registerUserInvalidInputsReturnFalse() { assertFalse(service.registerUser("Customer", "A", null, "a@b.com", "pass")); assertFalse(service.registerUser("Customer", "A", "B", null, "pass")); assertFalse(service.registerUser("Customer", "A", "B", "a@b.com", null)); + assertFalse(service.registerUser("", "A", "B", "a@b.com", "pass")); + assertFalse(service.registerUser("Customer", "", "B", "a@b.com", "pass")); + assertFalse(service.registerUser("Customer", "A", "", "a@b.com", "pass")); + assertFalse(service.registerUser("Customer", "A", "B", "", "pass")); + assertFalse(service.registerUser("Customer", "A", "B", "a@b.com", "")); + + assertFalse(service.registerUser(" ", "A", "B", "a@b.com", "pass")); assertFalse(service.registerUser("Customer", " ", "B", "a@b.com", "pass")); + assertFalse(service.registerUser("Customer", "A", " ", "a@b.com", "pass")); + assertFalse(service.registerUser("Customer", "A", "B", " ", "pass")); + assertFalse(service.registerUser("Customer", "A", "B", "a@b.com", " ")); } @Test @@ -78,7 +88,7 @@ void loginValidPassword() { BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); String hashedPassword = encoder.encode(plainPassword); User testUser = new Customer(10, "Test", "User", "test@example.com", hashedPassword); - repo.addUser(testUser); + repo.addContent(testUser); boolean result = service.login("test@example.com", plainPassword); assertTrue(result); @@ -90,10 +100,14 @@ void loginInvalidPassword() { BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); String hashedPassword = encoder.encode(plainPassword); User testUser = new Customer(10, "Test", "User", "test@example.com", hashedPassword); - repo.addUser(testUser); + repo.addContent(testUser); boolean result = service.login("test@example.com", "wrongpass"); + boolean result2 = service.login("test@example.com", null); + boolean result3 = service.login("test@example.com", " "); assertFalse(result); + assertFalse(result2); + assertFalse(result3); } @Test