-
Notifications
You must be signed in to change notification settings - Fork 52
Add unit tests #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add unit tests #111
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,8 +16,11 @@ | |
| public class JwtAuthenticationHelper { | ||
|
|
||
| private static final long JWT_TOKEN_VALIDITY = 60 * 60; | ||
| @Value("${jwt.secretKey}") | ||
| private String secret; | ||
| private final String secret; | ||
|
|
||
| public JwtAuthenticationHelper(@Value("${jwt.secretKey}") String secret) { | ||
| this.secret = secret; | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use constructor injection instead of field injection in order to write tests. |
||
|
|
||
| public String getUsernameFromToken(String token) { | ||
| String username = getClaimsFromToken(token).getSubject(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| package com.libraryman_api; | ||
|
|
||
| import com.libraryman_api.book.Book; | ||
| import com.libraryman_api.book.BookDto; | ||
| import com.libraryman_api.borrowing.Borrowings; | ||
| import com.libraryman_api.borrowing.BorrowingsDto; | ||
| import com.libraryman_api.fine.Fine; | ||
| import com.libraryman_api.member.Members; | ||
| import com.libraryman_api.member.Role; | ||
| import com.libraryman_api.member.dto.MembersDto; | ||
| import com.libraryman_api.member.dto.UpdateMembersDto; | ||
| import com.libraryman_api.member.dto.UpdatePasswordDto; | ||
| import com.libraryman_api.newsletter.NewsletterSubscriber; | ||
| import com.libraryman_api.security.model.LoginRequest; | ||
| import org.springframework.data.domain.*; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.Calendar; | ||
| import java.util.Date; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
|
|
||
| public class TestUtil { | ||
| private static final Random randomNumberUtils = new Random(); | ||
|
|
||
| public static int getRandomInt() { | ||
| return randomNumberUtils.nextInt(100, 1000); | ||
| } | ||
|
|
||
| public static Book getBook() { | ||
| Book book = new Book(); | ||
| book.setBookId(Constant.BOOK_ID); | ||
| book.setTitle(Constant.BOOK_TITLE); | ||
| book.setAuthor("Sarah Maas"); | ||
| book.setIsbn("978-0-7475-3269-9"); | ||
| book.setPublisher("Penguin Random House"); | ||
| book.setPublishedYear(2025); | ||
| book.setGenre("Fiction"); | ||
| book.setCopiesAvailable(5); | ||
| return book; | ||
| } | ||
|
|
||
| public static BookDto getBookDto() { | ||
| BookDto inputDto = new BookDto(); | ||
| inputDto.setBookId(Constant.BOOK_ID); | ||
| inputDto.setTitle(Constant.BOOK_TITLE); | ||
| inputDto.setAuthor("Sarah Maas"); | ||
| inputDto.setIsbn("978-0-7475-3269-9"); | ||
| inputDto.setPublisher("Penguin Random House"); | ||
| inputDto.setPublishedYear(2025); | ||
| inputDto.setGenre("Fiction"); | ||
| inputDto.setCopiesAvailable(5); | ||
| return inputDto; | ||
| } | ||
|
|
||
| public static BorrowingsDto getBorrowingsDto() { | ||
| BorrowingsDto borrowingsDto = new BorrowingsDto(); | ||
| borrowingsDto.setBorrowingId(1); | ||
| borrowingsDto.setBook(getBookDto()); | ||
| borrowingsDto.setFine(null); | ||
| borrowingsDto.setMember(getMembersDto()); | ||
| borrowingsDto.setBorrowDate(new Date()); | ||
| borrowingsDto.setReturnDate(adjustDays(new Date(), 7)); | ||
| return borrowingsDto; | ||
| } | ||
|
|
||
| public static Fine getFine() { | ||
| Fine fine = new Fine(); | ||
| fine.setAmount(BigDecimal.valueOf(100.00)); | ||
| fine.setPaid(false); | ||
| return fine; | ||
| } | ||
|
|
||
| public static MembersDto getMembersDto() { | ||
| MembersDto membersDto = new MembersDto(); | ||
| membersDto.setMemberId(Constant.MEMBER_ID); | ||
| membersDto.setName("Jane Doe"); | ||
| membersDto.setUsername("Jane01"); | ||
| membersDto.setEmail("jane.doe@gmail.com"); | ||
| membersDto.setPassword("password"); | ||
| membersDto.setRole(Role.USER); | ||
| membersDto.setMembershipDate(new Date()); | ||
| return membersDto; | ||
| } | ||
|
|
||
| public static UpdateMembersDto getUpdateMembersDto() { | ||
| UpdateMembersDto updateMembersDto = new UpdateMembersDto(); | ||
| updateMembersDto.setName("David Green"); | ||
| updateMembersDto.setUsername("DGreen"); | ||
| updateMembersDto.setEmail("david.green@gmail.com"); | ||
| return updateMembersDto; | ||
| } | ||
|
|
||
| public static Borrowings getBorrowings() { | ||
| Borrowings borrowings = new Borrowings(); | ||
| borrowings.setBook(getBook()); | ||
| borrowings.setMember(getMembers()); | ||
| borrowings.setBorrowDate(new Date()); | ||
| borrowings.setDueDate(adjustDays(new Date(), 14)); | ||
| borrowings.setReturnDate(null); | ||
| return borrowings; | ||
| } | ||
|
|
||
| public static Date adjustDays(Date date, int days) { | ||
| Calendar calendar = Calendar.getInstance(); | ||
| calendar.setTime(date); | ||
| calendar.add(Calendar.DAY_OF_MONTH, days); | ||
| return calendar.getTime(); | ||
| } | ||
|
|
||
| public static Pageable getPageRequest(String sortBy) { | ||
| return PageRequest.of(0, 10, Sort.by(sortBy)); | ||
| } | ||
|
|
||
| public static Page<Book> getBookPage() { | ||
| return new PageImpl<>(List.of(getBook())); | ||
| } | ||
|
|
||
| public static Members getMembers() { | ||
| Members members = new Members(); | ||
| members.setMemberId(Constant.MEMBER_ID); | ||
| members.setUsername("John01"); | ||
| members.setName("John Doe"); | ||
| members.setEmail("john@gmail.com"); | ||
| members.setPassword("password"); | ||
| members.setRole(Role.USER); | ||
| members.setMembershipDate(new Date()); | ||
| return members; | ||
| } | ||
|
|
||
| public static UpdatePasswordDto getUpdatePasswordDto() { | ||
| UpdatePasswordDto updatePasswordDto = new UpdatePasswordDto(); | ||
| updatePasswordDto.setCurrentPassword("password"); | ||
| updatePasswordDto.setNewPassword("newPassword"); | ||
| return updatePasswordDto; | ||
| } | ||
|
|
||
| public static NewsletterSubscriber getNewsletterSubscriber() { | ||
| NewsletterSubscriber newsletterSubscriber = new NewsletterSubscriber(); | ||
| newsletterSubscriber.setEmail("emma@hotmail.com"); | ||
| newsletterSubscriber.setActive(false); | ||
| return newsletterSubscriber; | ||
| } | ||
|
|
||
| public static LoginRequest getLoginRequest() { | ||
| LoginRequest request = new LoginRequest(); | ||
| request.setUsername("username"); | ||
| request.setPassword("password"); | ||
| return request; | ||
| } | ||
|
|
||
| public static class Constant { | ||
| public static final int BOOK_ID = 11; | ||
| public static final int BORROWING_ID = 22; | ||
| public static final int MEMBER_ID = 33; | ||
| public static final String BOOK_TITLE = "Test Book"; | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
src/test/java/com/libraryman_api/analytics/AnalyticsServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package com.libraryman_api.analytics; | ||
|
|
||
| import com.libraryman_api.book.BookRepository; | ||
| import com.libraryman_api.borrowing.BorrowingRepository; | ||
| import com.libraryman_api.member.MemberRepository; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| /** | ||
| * Tests the {@link AnalyticsService} class. | ||
| */ | ||
| @ExtendWith(MockitoExtension.class) | ||
| class AnalyticsServiceTest { | ||
| @Mock | ||
| private BookRepository bookRepository; | ||
| @Mock | ||
| private BorrowingRepository borrowingRepository; | ||
| @Mock | ||
| private MemberRepository memberRepository; | ||
| @InjectMocks | ||
| private AnalyticsService analyticsService; | ||
|
|
||
| @Test | ||
| void getLibraryOverview() { | ||
| when(bookRepository.count()).thenReturn(100L); | ||
| when(borrowingRepository.count()).thenReturn(50L); | ||
| when(memberRepository.count()).thenReturn(25L); | ||
|
|
||
| Map<String, Object> overview = analyticsService.getLibraryOverview(); | ||
|
|
||
| assertEquals(100L, overview.get("totalBooks")); | ||
| assertEquals(25L, overview.get("totalMembers")); | ||
| assertEquals(50L, overview.get("totalBorrowings")); | ||
| } | ||
|
|
||
| @Test | ||
| void getPopularBooks() { | ||
| List<Map<String, Object>> expectedList = List.of(Map.of("title", "Book A", "borrowCount", 10), Map.of("title", "Book B", "borrowCount", 8)); | ||
| when(borrowingRepository.findMostBorrowedBooks(2)).thenReturn(expectedList); | ||
|
|
||
| List<Map<String, Object>> result = analyticsService.getPopularBooks(2); | ||
|
|
||
| assertEquals(expectedList, result); | ||
| } | ||
|
|
||
| @Test | ||
| void getBorrowingTrends() { | ||
| Map<String, Long> trends = Map.of("2025-06-01", 5L, "2025-06-02", 3L); | ||
| when(borrowingRepository.getBorrowingTrendsBetweenDates(any(), any())).thenReturn(trends); | ||
|
|
||
| Map<String, Long> result = analyticsService.getBorrowingTrends(LocalDate.now().minusDays(1), LocalDate.now()); | ||
|
|
||
| assertEquals(trends, result); | ||
| } | ||
|
|
||
| @Test | ||
| void getMemberActivityReport() { | ||
| List<Map<String, Object>> report = List.of(Map.of("memberId", 1, "borrowCount", 10), Map.of("memberId", 2, "borrowCount", 5)); | ||
| when(memberRepository.getMemberActivityReport()).thenReturn(report); | ||
|
|
||
| List<Map<String, Object>> result = analyticsService.getMemberActivityReport(); | ||
|
|
||
| assertEquals(report, result); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the test that requires the Spring application context to start up into a folder for Integration tests. (IT)
More integration tests can be added here.