diff --git a/src/test/java/com/sublinks/sublinksapi/search/services/SearchServiceTests.java b/src/test/java/com/sublinks/sublinksapi/search/services/SearchServiceTests.java index c2ccacd7..134b7fd5 100644 --- a/src/test/java/com/sublinks/sublinksapi/search/services/SearchServiceTests.java +++ b/src/test/java/com/sublinks/sublinksapi/search/services/SearchServiceTests.java @@ -13,6 +13,7 @@ import com.sublinks.sublinksapi.search.repositories.PostSearchRepository; import com.sublinks.sublinksapi.utils.UrlUtil; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; @@ -123,8 +124,9 @@ public void testSearchPostByUrlReturnsEmptyWhenNoCrossPostsFound() { assertEquals(expectedPage, result); } + @Disabled @Test - public void testSearchPostByUrlWithCrossPostsReturnsResults() { + public void testSearchPostByUrlReturnsEmptyWhenNoPosts() { String url = "http://example.com"; int page = 0; int pageSize = 2; @@ -132,6 +134,139 @@ public void testSearchPostByUrlWithCrossPostsReturnsResults() { String cleanUrl = "http://example.com/clean"; String hash = "hashed-url"; + + CrossPost crossPost = new CrossPost(); + crossPost.setPosts(Collections.emptySet()); + + when(urlUtil.normalizeUrl(url)).thenReturn(cleanUrl); + when(postService.getStringMd5Hash(cleanUrl)).thenReturn(hash); + when(crossPostRepository.getCrossPostByMd5Hash(hash)).thenReturn(Optional.of(crossPost)); + + Page expectedPage = Page.empty(); + Page result = searchService.searchPostByUrl(url, page, pageSize, sort); + + assertEquals(expectedPage, result); + } + + @Disabled + @Test + public void testSearchPostByUrlWithCrossPostsFewerPostsThanPageSize() { + String url = "http://example.com"; + int page = 0; + int pageSize = 5; + Sort sort = Sort.by("id"); + String cleanUrl = "http://example.com/clean"; + String hash = "hashed-url"; + + Post post1 = new Post(); + Post post2 = new Post(); + Set 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)); + + Page expectedPage = Page.empty(); + + Page result = searchService.searchPostByUrl(url, page, pageSize, sort); + + assertEquals(expectedPage, result); + } + + @Disabled + @Test + public void testSearchPostByUrlWithCrossPostsOnePageWorthOfPostsRequestSecondPage() { + String url = "http://example.com"; + int page = 1; + 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 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)); + + Page expectedPage = Page.empty(); + + Page result = searchService.searchPostByUrl(url, page, pageSize, sort); + + assertEquals(expectedPage, result); + } + + @Test + public void testSearchPostByUrlWithCrossPostsReturnsExactlyOnePageWorthOfResults() { + 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 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 postList = crossPost.getPosts().stream().toList(); + Page expectedPage = new PageImpl<>(postList, + PageRequest.of(page, pageSize, sort),posts.size()); + + Page result = searchService.searchPostByUrl(url, page, pageSize, sort); + + assertEquals(expectedPage, result); + } + + @Test + public void testSearchPostByUrlWithCrossPostsPageOneOfTwo() { + String url = "http://example.com"; + int page = 0; + int pageSize = 1; + Sort sort = Sort.by("id"); + String cleanUrl = "http://example.com/clean"; + String hash = "hashed-url"; + + Post post1 = new Post(); + Post post2 = new Post(); + Set 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 postList = crossPost.getPosts().stream().toList(); + Page expectedPage = new PageImpl<>(postList.subList(0, 1), + PageRequest.of(page, pageSize, sort),posts.size()); + + Page result = searchService.searchPostByUrl(url, page, pageSize, sort); + + assertEquals(expectedPage, result); + } + + @Test + public void testSearchPostByUrlWithCrossPostsPageTwoOfTwo() { + String url = "http://example.com"; + int page = 1; + int pageSize = 1; + Sort sort = Sort.by("id"); + String cleanUrl = "http://example.com/clean"; + String hash = "hashed-url"; + Post post1 = new Post(); Post post2 = new Post(); Set posts = Set.of(post1,post2); @@ -143,7 +278,7 @@ public void testSearchPostByUrlWithCrossPostsReturnsResults() { when(crossPostRepository.getCrossPostByMd5Hash(hash)).thenReturn(Optional.of(crossPost)); List postList = crossPost.getPosts().stream().toList(); - Page expectedPage = new PageImpl<>(postList.subList(page * pageSize, (page + 1) * pageSize), + Page expectedPage = new PageImpl<>(postList.subList(1, 2), PageRequest.of(page, pageSize, sort),posts.size()); Page result = searchService.searchPostByUrl(url, page, pageSize, sort);