Add tag module with post associations

This commit is contained in:
Tim
2025-07-02 13:19:55 +08:00
parent 3449310a19
commit f50201aef6
13 changed files with 249 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ package com.openisle.controller;
import com.openisle.model.Post;
import com.openisle.model.User;
import com.openisle.model.Category;
import com.openisle.model.Tag;
import com.openisle.service.PostService;
import com.openisle.service.CommentService;
import com.openisle.service.ReactionService;
@@ -48,6 +49,9 @@ class PostControllerTest {
Category cat = new Category();
cat.setId(1L);
cat.setName("tech");
Tag tag = new Tag();
tag.setId(1L);
tag.setName("java");
Post post = new Post();
post.setId(1L);
post.setTitle("t");
@@ -55,12 +59,13 @@ class PostControllerTest {
post.setCreatedAt(LocalDateTime.now());
post.setAuthor(user);
post.setCategory(cat);
Mockito.when(postService.createPost(eq("alice"), eq(1L), eq("t"), eq("c"))).thenReturn(post);
post.setTags(java.util.Set.of(tag));
Mockito.when(postService.createPost(eq("alice"), eq(1L), eq("t"), eq("c"), eq(java.util.List.of(1L)))).thenReturn(post);
Mockito.when(postService.getPost(1L)).thenReturn(post);
mockMvc.perform(post("/api/posts")
.contentType("application/json")
.content("{\"title\":\"t\",\"content\":\"c\",\"categoryId\":1}")
.content("{\"title\":\"t\",\"content\":\"c\",\"categoryId\":1,\"tagIds\":[1]}")
.principal(new UsernamePasswordAuthenticationToken("alice", "p")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.title").value("t"));
@@ -77,6 +82,9 @@ class PostControllerTest {
Category cat = new Category();
cat.setId(1L);
cat.setName("tech");
Tag tag = new Tag();
tag.setId(1L);
tag.setName("java");
Post post = new Post();
post.setId(2L);
post.setTitle("hello");
@@ -84,6 +92,7 @@ class PostControllerTest {
post.setCreatedAt(LocalDateTime.now());
post.setAuthor(user);
post.setCategory(cat);
post.setTags(java.util.Set.of(tag));
Mockito.when(postService.listPostsByCategories(Mockito.isNull(), Mockito.isNull(), Mockito.isNull()))
.thenReturn(List.of(post));

View File

@@ -0,0 +1,59 @@
package com.openisle.controller;
import com.openisle.model.Tag;
import com.openisle.service.TagService;
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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import static org.mockito.ArgumentMatchers.eq;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(TagController.class)
@AutoConfigureMockMvc(addFilters = false)
class TagControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private TagService tagService;
@Test
void createAndGetTag() throws Exception {
Tag t = new Tag();
t.setId(1L);
t.setName("java");
Mockito.when(tagService.createTag(eq("java"))).thenReturn(t);
Mockito.when(tagService.getTag(1L)).thenReturn(t);
mockMvc.perform(post("/api/tags")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"java\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("java"));
mockMvc.perform(get("/api/tags/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1));
}
@Test
void listTags() throws Exception {
Tag t = new Tag();
t.setId(2L);
t.setName("spring");
Mockito.when(tagService.listTags()).thenReturn(List.of(t));
mockMvc.perform(get("/api/tags"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].name").value("spring"));
}
}

View File

@@ -69,8 +69,13 @@ class ComplexFlowIntegrationTest {
Map.of("name", "general"), adminToken);
Long catId = ((Number)catResp.getBody().get("id")).longValue();
ResponseEntity<Map> tagResp = postJson("/api/tags",
Map.of("name", "java"), adminToken);
Long tagId = ((Number)tagResp.getBody().get("id")).longValue();
ResponseEntity<Map> postResp = postJson("/api/posts",
Map.of("title", "Hello", "content", "World", "categoryId", catId), t1);
Map.of("title", "Hello", "content", "World", "categoryId", catId,
"tagIds", List.of(tagId)), t1);
Long postId = ((Number)postResp.getBody().get("id")).longValue();
ResponseEntity<Map> c1Resp = postJson("/api/posts/" + postId + "/comments",
@@ -122,8 +127,13 @@ class ComplexFlowIntegrationTest {
catId = ((Number)catResp.getBody().get("id")).longValue();
}
ResponseEntity<Map> tagResp = postJson("/api/tags",
Map.of("name", "spring"), adminToken);
Long tagId = ((Number)tagResp.getBody().get("id")).longValue();
ResponseEntity<Map> postResp = postJson("/api/posts",
Map.of("title", "React", "content", "Test", "categoryId", catId), t1);
Map.of("title", "React", "content", "Test", "categoryId", catId,
"tagIds", List.of(tagId)), t1);
Long postId = ((Number)postResp.getBody().get("id")).longValue();
postJson("/api/posts/" + postId + "/reactions",

View File

@@ -73,8 +73,13 @@ class PublishModeIntegrationTest {
Map.of("name", "review"), adminToken);
Long catId = ((Number)catResp.getBody().get("id")).longValue();
ResponseEntity<Map> tagResp = postJson("/api/tags",
Map.of("name", "t1"), adminToken);
Long tagId = ((Number)tagResp.getBody().get("id")).longValue();
ResponseEntity<Map> postResp = postJson("/api/posts",
Map.of("title", "Need", "content", "Review", "categoryId", catId), userToken);
Map.of("title", "Need", "content", "Review", "categoryId", catId,
"tagIds", List.of(tagId)), userToken);
Long postId = ((Number)postResp.getBody().get("id")).longValue();
List<?> list = rest.getForObject("/api/posts", List.class);

View File

@@ -63,8 +63,12 @@ class SearchIntegrationTest {
ResponseEntity<Map> catResp = postJson("/api/categories", Map.of("name", "misc"), admin);
Long catId = ((Number)catResp.getBody().get("id")).longValue();
ResponseEntity<Map> tagResp = postJson("/api/tags", Map.of("name", "misc"), admin);
Long tagId = ((Number)tagResp.getBody().get("id")).longValue();
ResponseEntity<Map> postResp = postJson("/api/posts",
Map.of("title", "Hello World Nice", "content", "Some content", "categoryId", catId), user);
Map.of("title", "Hello World Nice", "content", "Some content", "categoryId", catId,
"tagIds", List.of(tagId)), user);
Long postId = ((Number)postResp.getBody().get("id")).longValue();
postJson("/api/posts/" + postId + "/comments",