-
-
Notifications
You must be signed in to change notification settings - Fork 17
#246-added unit test to search package #396
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d8c4cd8
#246-added unit test to search package
Ashwinn11 345c7c0
Merge branch 'sublinks:main' into search-unit-tests
Ashwinn11 84cb524
Merge branch 'sublinks:main' into search-unit-tests
Ashwinn11 75719f8
added more coverage for Search package unit tests
Ashwinn11 9f59b8d
added more coverage for Search package unit tests
Ashwinn11 137a986
Merge branch 'main' into search-unit-tests
lazyguru 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
169 changes: 169 additions & 0 deletions
169
src/test/java/com/sublinks/sublinksapi/search/services/SearchServiceTests.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,169 @@ | ||
| package com.sublinks.sublinksapi.search.services; | ||
|
|
||
| import com.sublinks.sublinksapi.comment.entities.Comment; | ||
| import com.sublinks.sublinksapi.community.entities.Community; | ||
| import com.sublinks.sublinksapi.person.entities.Person; | ||
| import com.sublinks.sublinksapi.post.entities.CrossPost; | ||
| import com.sublinks.sublinksapi.post.entities.Post; | ||
| import com.sublinks.sublinksapi.post.repositories.CrossPostRepository; | ||
| import com.sublinks.sublinksapi.post.services.PostService; | ||
| import com.sublinks.sublinksapi.search.repositories.CommentSearchRepository; | ||
| import com.sublinks.sublinksapi.search.repositories.CommunitySearchRepository; | ||
| import com.sublinks.sublinksapi.search.repositories.PersonSearchRepository; | ||
| import com.sublinks.sublinksapi.search.repositories.PostSearchRepository; | ||
| import com.sublinks.sublinksapi.utils.UrlUtil; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockitoAnnotations; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.PageImpl; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Sort; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| public class SearchServiceTests { | ||
| @Mock | ||
| private PostSearchRepository postSearchRepository; | ||
|
|
||
| @Mock | ||
| private CommentSearchRepository commentSearchRepository; | ||
|
|
||
| @Mock | ||
| private CommunitySearchRepository communitySearchRepository; | ||
|
|
||
| @Mock | ||
| private PersonSearchRepository personSearchRepository; | ||
|
|
||
| @Mock | ||
| private PostService postService; | ||
|
|
||
| @Mock | ||
| private CrossPostRepository crossPostRepository; | ||
|
|
||
| @Mock | ||
| private UrlUtil urlUtil; | ||
|
|
||
| @InjectMocks | ||
| private SearchService searchService; | ||
|
|
||
| @BeforeEach | ||
| public void setup(){ | ||
| MockitoAnnotations.openMocks(this); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSearchCommunity(){ | ||
| String query = "test"; | ||
| int page = 0; | ||
| int pageSize = 10; | ||
| Sort sort = Sort.by("id"); | ||
|
|
||
| Page<Community> expectedPage = new PageImpl<>( | ||
| Collections.emptyList(), PageRequest.of(page, pageSize, sort), 0); | ||
| when(communitySearchRepository.searchAllByKeyword(query, PageRequest.of(page, pageSize, sort))) | ||
| .thenReturn(expectedPage); | ||
|
|
||
| Page<Community> result = searchService.searchCommunity(query, page, pageSize, sort); | ||
| assertEquals(expectedPage, result); | ||
| } | ||
| @Test | ||
| public void testSearchPost() { | ||
| String query = "test"; | ||
| int page = 0; | ||
| int pageSize = 10; | ||
| Sort sort = Sort.by("id"); | ||
|
|
||
| Page<Post> expectedPage = new PageImpl<>(Collections.emptyList(), PageRequest.of(page, pageSize, sort), 0); | ||
| when(postSearchRepository.searchAllByKeyword(query, PageRequest.of(page, pageSize, sort))) | ||
| .thenReturn(expectedPage); | ||
|
|
||
| Page<Post> result = searchService.searchPost(query, page, pageSize, sort); | ||
| assertEquals(expectedPage, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSearchComments() { | ||
| String query = "test"; | ||
| int page = 0; | ||
| int pageSize = 10; | ||
| Sort sort = Sort.by("id"); | ||
|
|
||
| Page<Comment> expectedPage = new PageImpl<>(Collections.emptyList(), PageRequest.of(page, pageSize, sort), 0); | ||
| when(commentSearchRepository.searchAllByKeyword(query, PageRequest.of(page, pageSize, sort))) | ||
| .thenReturn(expectedPage); | ||
|
|
||
| Page<Comment> result = searchService.searchComments(query, page, pageSize, sort); | ||
| assertEquals(expectedPage, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSearchPostByUrlReturnsEmptyWhenNoCrossPostsFound() { | ||
| String url = "http://example.com"; | ||
| int page = 0; | ||
| int pageSize = 2; | ||
| Sort sort = Sort.by("id"); | ||
| String cleanUrl = "http://example.com/clean"; | ||
| String hash = "hashed-url"; | ||
|
|
||
| when(urlUtil.normalizeUrl(url)).thenReturn(cleanUrl); | ||
| when(postService.getStringMd5Hash(cleanUrl)).thenReturn(hash); | ||
| when(crossPostRepository.getCrossPostByMd5Hash(hash)).thenReturn(Optional.empty()); | ||
|
|
||
| Page<Post> expectedPage = Page.empty(); | ||
| Page<Post> result = searchService.searchPostByUrl(url, page, pageSize, sort); | ||
|
|
||
| assertEquals(expectedPage, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSearchPostByUrlWithCrossPostsReturnsResults() { | ||
| String url = "http://example.com"; | ||
| int page = 0; | ||
| int pageSize = 2; | ||
| Sort sort = Sort.by("id"); | ||
| String cleanUrl = "http://example.com/clean"; | ||
| String hash = "hashed-url"; | ||
|
|
||
| Post post1 = new Post(); | ||
| Post post2 = new Post(); | ||
| Set<Post> posts = Set.of(post1,post2); | ||
| CrossPost crossPost = new CrossPost(); | ||
| crossPost.setPosts(posts); | ||
|
|
||
| when(urlUtil.normalizeUrl(url)).thenReturn(cleanUrl); | ||
| when(postService.getStringMd5Hash(cleanUrl)).thenReturn(hash); | ||
| when(crossPostRepository.getCrossPostByMd5Hash(hash)).thenReturn(Optional.of(crossPost)); | ||
|
|
||
| List<Post> postList = crossPost.getPosts().stream().toList(); | ||
| Page<Post> expectedPage = new PageImpl<>(postList.subList(page * pageSize, (page + 1) * pageSize), | ||
| PageRequest.of(page, pageSize, sort),posts.size()); | ||
|
|
||
| Page<Post> result = searchService.searchPostByUrl(url, page, pageSize, sort); | ||
|
|
||
| assertEquals(expectedPage, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSearchPerson() { | ||
| String query = "test"; | ||
| int page = 0; | ||
| int pageSize = 10; | ||
| Sort sort = Sort.by("id"); | ||
|
|
||
| Page<Person> expectedPage = new PageImpl<>(Collections.emptyList(), PageRequest.of(page, pageSize, sort), 0); | ||
| when(personSearchRepository.searchAllByKeyword(query, PageRequest.of(page, pageSize, sort))) | ||
| .thenReturn(expectedPage); | ||
|
|
||
| Page<Person> result = searchService.searchPerson(query, page, pageSize, sort); | ||
| assertEquals(expectedPage, result); | ||
| } | ||
|
|
||
| } | ||
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.
Either this test should return results or the test should be renamed to indicate it doesn't return results when no match is found (a valid test case) and then a test should be added that handles results being found. As it is now, this test is a little misleading