Skip to content

Commit

Permalink
Update&Test[Repository]: Update Repository and JUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrik Marjoni committed Mar 10, 2026
1 parent a875a55 commit 6982dd9
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 61 deletions.
6 changes: 1 addition & 5 deletions src/main/java/edu/group5/app/model/DBRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ public Map<Integer, Donation> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
16 changes: 8 additions & 8 deletions src/main/java/edu/group5/app/model/user/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -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;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/edu/group5/app/model/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -112,7 +112,7 @@ void getDonationByIdReturnsNullIfNotFound() {

@Test
void getAllDonationsReturnsCopy() {
repo.addDonation(donation1);
repo.addContent(donation1);
Map<Integer, Donation> all = repo.getAllDonations();
assertEquals(1, all.size());
all.clear();
Expand All @@ -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<Integer, Donation> sorted = repo.sortByDate();
Integer[] keys = sorted.keySet().toArray(new Integer[0]);
Expand All @@ -132,17 +132,17 @@ void sortByDateAscending() {

@Test
void sortByDateWithSameDateKeepsAll() {
repo.addDonation(donation1);
repo.addDonation(donation4);
repo.addContent(donation1);
repo.addContent(donation4);
Map<Integer, Donation> sorted = repo.sortByDate();
assertEquals(1, sorted.size());
assertTrue(sorted.containsKey(1));
}

@Test
void sortByAmountAscending() {
repo.addDonation(donation3);
repo.addDonation(donation1);
repo.addContent(donation3);
repo.addContent(donation1);

Map<Integer, Donation> sorted = repo.sortByAmount();
Integer[] keys = sorted.keySet().toArray(new Integer[0]);
Expand All @@ -151,8 +151,8 @@ void sortByAmountAscending() {

@Test
void sortByAmountWithSameAmount() {
repo.addDonation(donation1);
repo.addDonation(donation2);
repo.addContent(donation1);
repo.addContent(donation2);
Map<Integer, Donation> sorted = repo.sortByAmount();
assertEquals(2, sorted.size());
assertTrue(sorted.containsKey(1));
Expand All @@ -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<Integer, Donation> filtered = repo.filterByOrganization(202);
assertEquals(2, filtered.size());
Expand All @@ -173,7 +173,7 @@ void filterByOrganizationMatches() {

@Test
void filterByOrganizationNoMatch() {
repo.addDonation(donation1);
repo.addContent(donation1);
Map<Integer, Donation> filtered = repo.filterByOrganization(999);
assertTrue(filtered.isEmpty());
}
Expand All @@ -187,8 +187,8 @@ void filterByOrganizationThrowsIfNegative() {

@Test
void exportReturnsAllRowsSortedById() {
repo.addDonation(donation2);
repo.addDonation(donation1);
repo.addContent(donation2);
repo.addContent(donation1);

List<Object[]> exported = repo.export();
assertEquals(2, exported.size());
Expand All @@ -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());
}

Expand All @@ -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<Integer, Donation> sorted = repo.sortByDate();
assertEquals(d1, sorted.get(1));
Expand All @@ -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<Integer, Donation> sorted = repo.sortByAmount();
assertEquals(d1, sorted.get(1));
Expand All @@ -234,16 +234,16 @@ 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<Integer, Donation> filtered = repo.filterByOrganization(201);
assertEquals(d1, filtered.get(1));
}

@Test
void filterByUserIdMatches() {
repo.addDonation(donation1);
repo.addDonation(donation3);
repo.addContent(donation1);
repo.addContent(donation3);

Map<Integer, Donation> filtered = repo.filterByUser(102);
assertEquals(1, filtered.size());
Expand All @@ -252,7 +252,7 @@ void filterByUserIdMatches() {

@Test
void filterByUserIdNoMatch() {
repo.addDonation(donation1);
repo.addContent(donation1);
Map<Integer, Donation> filtered = repo.filterByUser(999);
assertTrue(filtered.isEmpty());
}
Expand Down
36 changes: 30 additions & 6 deletions src/test/java/edu/group5/app/model/user/UserRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,41 @@ void constructorThrowsIfUnknownRole() {
}

@Test
void addUserSuccessfully() {
void constructorThrowsIfRowNull() {
List<Object[]> 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<Object[]> 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
Expand Down Expand Up @@ -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<Object[]> exported = repo.export();
Expand Down
18 changes: 16 additions & 2 deletions src/test/java/edu/group5/app/model/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit 6982dd9

Please sign in to comment.