Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

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

when(personSearchRepository.searchAllByKeyword(query, PageRequest.of(page, pageSize, sort)))
.thenReturn(expectedPage);

Page<Person> result = searchService.searchPerson(query, page, pageSize, sort);
assertEquals(expectedPage, result);
}

}