Skip to content

Commit

Permalink
feat: add unit tests to SearchServiceTests
Browse files Browse the repository at this point in the history
add testSearchPostByUrlReturnsEmptyWhenNoPosts - fails, disabled
add testSearchPostByUrlWithCrossPostsFewerPostsThanPageSize - fails, disabled
add testSearchPostByUrlWithCrossPostsOnePageWorthOfPostsRequestSecondPage - fails, disabled
rename testSearchPostByUrlWithCrossPostsReturnsExactlyOnePageWorthOfResults - passes
add testSearchPostByUrlWithCrossPostsPageOneOfTwo - passes
add testSearchPostByUrlWithCrossPostsPageTwoOfTwo - passes
  • Loading branch information
Yeshaya Lazarevich committed Jun 20, 2024
1 parent 9670fd4 commit f9607eb
Showing 1 changed file with 137 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -123,15 +124,149 @@ 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;
Sort sort = Sort.by("id");
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<Post> expectedPage = Page.empty();
Page<Post> 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<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));

Page<Post> expectedPage = Page.empty();

Page<Post> 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<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));

Page<Post> expectedPage = Page.empty();

Page<Post> 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<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,
PageRequest.of(page, pageSize, sort),posts.size());

Page<Post> 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<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(0, 1),
PageRequest.of(page, pageSize, sort),posts.size());

Page<Post> 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<Post> posts = Set.of(post1,post2);
Expand All @@ -143,7 +278,7 @@ public void testSearchPostByUrlWithCrossPostsReturnsResults() {
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),
Page<Post> expectedPage = new PageImpl<>(postList.subList(1, 2),
PageRequest.of(page, pageSize, sort),posts.size());

Page<Post> result = searchService.searchPostByUrl(url, page, pageSize, sort);
Expand Down

0 comments on commit f9607eb

Please sign in to comment.