From 446438ae5d9b82fdaf66f4ee69f4f33936a7892a Mon Sep 17 00:00:00 2001 From: Fredrik Marjoni Date: Tue, 10 Mar 2026 15:05:45 +0100 Subject: [PATCH] Update&Test[Service]: Add get methods and tests to make the frontend access a bit of the repository --- .../app/model/donation/DonationService.java | 20 ++++++++++++++++++- .../group5/app/model/user/UserService.java | 13 ++++++++++-- .../model/donation/DonationServiceTest.java | 10 ++++++++++ .../app/model/user/UserServiceTest.java | 5 +++++ 4 files changed, 45 insertions(+), 3 deletions(-) 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 804d901..690a632 100644 --- a/src/main/java/edu/group5/app/model/donation/DonationService.java +++ b/src/main/java/edu/group5/app/model/donation/DonationService.java @@ -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. @@ -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; } } \ No newline at end of file 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 d430a59..0b754c8 100644 --- a/src/main/java/edu/group5/app/model/user/UserService.java +++ b/src/main/java/edu/group5/app/model/user/UserService.java @@ -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. @@ -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; } @@ -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); } } diff --git a/src/test/java/edu/group5/app/model/donation/DonationServiceTest.java b/src/test/java/edu/group5/app/model/donation/DonationServiceTest.java index bd7f11a..80d37bf 100644 --- a/src/test/java/edu/group5/app/model/donation/DonationServiceTest.java +++ b/src/test/java/edu/group5/app/model/donation/DonationServiceTest.java @@ -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, 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 20397a6..7f45dc7 100644 --- a/src/test/java/edu/group5/app/model/user/UserServiceTest.java +++ b/src/test/java/edu/group5/app/model/user/UserServiceTest.java @@ -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",