Skip to content

Commit

Permalink
Update&Test[Service]: Add get methods and tests to make the frontend …
Browse files Browse the repository at this point in the history
…access a bit of the repository
  • Loading branch information
Fredrik Marjoni committed Mar 10, 2026
1 parent 6982dd9 commit 446438a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/main/java/edu/group5/app/model/donation/DonationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ public DonationService(DonationRepository donationRepository,
this.organizationRepository = organizationRepository;
}

/**
* Getter for the DonationRepository used by this service.
* This method allows access to the donation repository for managing donation records and retrieving donation information.
* @return the DonationRepository instance used by this service
*/
public DonationRepository getDonationRepository() {
return this.donationRepository;
}

/**
* Getter for the OrganizationRepository used by this service.
* This method allows access to the organization repository for validating organization information when processing donations.
* @return the OrganizationRepository instance used by this service
*/
public OrganizationRepository getOrganizationRepository() {
return this.organizationRepository;
}

/**
* Processes a donation from a customer to a specified organization with a given amount.
* Validates the customer, organization number, and donation amount before creating a donation record.
Expand All @@ -57,7 +75,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.addContent(donation);
this.donationRepository.addContent(donation);
return true;
}
}
13 changes: 11 additions & 2 deletions src/main/java/edu/group5/app/model/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}

/**
* Getter for the UserRepository used by this service.
* This method allows access to the user repository for managing user data and performing operations such as registration and login.
* @return the UserRepository instance used by this service
*/
public UserRepository getUserRepository() {
return this.userRepository;
}

/**
* Registers a new user with the given information. Validates the input data and creates a new User object
* based on the specified role. Currently supports registration for customers only.
Expand Down Expand Up @@ -48,7 +57,7 @@ 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.addContent(user);
this.userRepository.addContent(user);
return true;
}

Expand All @@ -64,7 +73,7 @@ public boolean login(String email, String password) {
if (email == null || email.trim().isEmpty() || password == null || password.trim().isEmpty()) {
return false;
}
User user = userRepository.findUserByEmail(email);
User user = this.userRepository.findUserByEmail(email);
return user != null && user.verifyPassword(password);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ void testConstructorThrowsIfOrganizationRepositoryIsNull() {
assertEquals("OrganizationRepository cannot be null", exception.getMessage());
}

@Test
void testGetDonationRepository() {
assertEquals(donationRepository, donationService.getDonationRepository());
}

@Test
void testGetOrganizationRepository() {
assertEquals(organizationRepository, donationService.getOrganizationRepository());
}

@Test
void donateReturnsFalseIfCustomerNull() {
boolean result = donationService.donate(null,
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/edu/group5/app/model/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ void constructorthrowsIfNull() {
assertEquals("UserRepository cannot be null", ex.getMessage());
}

@Test
void testGetUserRepository() {
assertEquals(repo, service.getUserRepository());
}

@Test
void registerUserValid() {
boolean result = service.registerUser("Customer", "Alice", "Smith",
Expand Down

0 comments on commit 446438a

Please sign in to comment.