mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-22 22:21:09 +08:00
feat: add search module
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package com.openisle.controller;
|
||||
|
||||
import com.openisle.model.Comment;
|
||||
import com.openisle.model.Post;
|
||||
import com.openisle.model.User;
|
||||
import com.openisle.model.PostStatus;
|
||||
import com.openisle.service.SearchService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
@WebMvcTest(SearchController.class)
|
||||
@AutoConfigureMockMvc(addFilters = false)
|
||||
class SearchControllerTest {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private SearchService searchService;
|
||||
|
||||
@Test
|
||||
void userSearchEndpoint() throws Exception {
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setUsername("alice");
|
||||
Mockito.when(searchService.searchUsers("ali")).thenReturn(List.of(user));
|
||||
|
||||
mockMvc.perform(get("/api/search/users").param("keyword", "ali"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$[0].username").value("alice"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void globalSearchAggregatesTypes() throws Exception {
|
||||
User u = new User();
|
||||
u.setId(1L);
|
||||
u.setUsername("bob");
|
||||
Post p = new Post();
|
||||
p.setId(2L);
|
||||
p.setTitle("hello");
|
||||
p.setStatus(PostStatus.PUBLISHED);
|
||||
Comment c = new Comment();
|
||||
c.setId(3L);
|
||||
c.setContent("nice");
|
||||
Mockito.when(searchService.globalSearch("n")).thenReturn(List.of(
|
||||
new SearchService.SearchResult("user", 1L, "bob"),
|
||||
new SearchService.SearchResult("post", 2L, "hello"),
|
||||
new SearchService.SearchResult("comment", 3L, "nice")
|
||||
));
|
||||
|
||||
mockMvc.perform(get("/api/search/global").param("keyword", "n"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$[0].type").value("user"))
|
||||
.andExpect(jsonPath("$[1].type").value("post"))
|
||||
.andExpect(jsonPath("$[2].type").value("comment"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.openisle.integration;
|
||||
|
||||
import com.openisle.model.Role;
|
||||
import com.openisle.model.User;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import com.openisle.service.EmailSender;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class SearchIntegrationTest {
|
||||
@Autowired
|
||||
private TestRestTemplate rest;
|
||||
@Autowired
|
||||
private UserRepository users;
|
||||
@MockBean
|
||||
private EmailSender emailService;
|
||||
|
||||
private String registerAndLogin(String username, String email) {
|
||||
HttpHeaders h = new HttpHeaders();
|
||||
h.setContentType(MediaType.APPLICATION_JSON);
|
||||
rest.postForEntity("/api/auth/register", new HttpEntity<>(
|
||||
Map.of("username", username, "email", email, "password", "pass123"), h), Map.class);
|
||||
User u = users.findByUsername(username).orElseThrow();
|
||||
rest.postForEntity("/api/auth/verify", new HttpEntity<>(
|
||||
Map.of("username", username, "code", u.getVerificationCode()), h), Map.class);
|
||||
ResponseEntity<Map> resp = rest.postForEntity("/api/auth/login", new HttpEntity<>(
|
||||
Map.of("username", username, "password", "pass123"), h), Map.class);
|
||||
return (String) resp.getBody().get("token");
|
||||
}
|
||||
|
||||
private String registerAndLoginAsAdmin(String username, String email) {
|
||||
String token = registerAndLogin(username, email);
|
||||
User u = users.findByUsername(username).orElseThrow();
|
||||
u.setRole(Role.ADMIN);
|
||||
users.save(u);
|
||||
return token;
|
||||
}
|
||||
|
||||
private ResponseEntity<Map> postJson(String url, Map<?,?> body, String token) {
|
||||
HttpHeaders h = new HttpHeaders();
|
||||
h.setContentType(MediaType.APPLICATION_JSON);
|
||||
if (token != null) h.setBearerAuth(token);
|
||||
return rest.exchange(url, HttpMethod.POST, new HttpEntity<>(body, h), Map.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void globalSearchReturnsMixedResults() {
|
||||
String admin = registerAndLoginAsAdmin("admin", "a@a.com");
|
||||
String user = registerAndLogin("bob", "b@b.com");
|
||||
|
||||
ResponseEntity<Map> catResp = postJson("/api/categories", Map.of("name", "misc"), admin);
|
||||
Long catId = ((Number)catResp.getBody().get("id")).longValue();
|
||||
|
||||
ResponseEntity<Map> postResp = postJson("/api/posts",
|
||||
Map.of("title", "Hello World", "content", "Some content", "categoryId", catId), user);
|
||||
Long postId = ((Number)postResp.getBody().get("id")).longValue();
|
||||
|
||||
postJson("/api/posts/" + postId + "/comments",
|
||||
Map.of("content", "Nice article"), admin);
|
||||
|
||||
List<Map<String, Object>> results = rest.getForObject("/api/search/global?keyword=nic", List.class);
|
||||
assertEquals(3, results.size());
|
||||
assertTrue(results.stream().anyMatch(m -> "user".equals(m.get("type"))));
|
||||
assertTrue(results.stream().anyMatch(m -> "post".equals(m.get("type"))));
|
||||
assertTrue(results.stream().anyMatch(m -> "comment".equals(m.get("type"))));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user