File size: 2,626 Bytes
bb5169d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.krishna.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.krishna.dto.CommentDTO;
import com.krishna.model.CommentModel;
import com.krishna.service.CommentService;
import org.junit.jupiter.api.BeforeEach;
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.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 java.util.UUID;

import static org.mockito.ArgumentMatchers.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest(CommentController.class)
public class CommentControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private CommentService commentService;

    @Autowired
    private ObjectMapper objectMapper;

    private CommentModel mockComment;

    @BeforeEach
    void setup() {
        mockComment = new CommentModel();
        mockComment.setId(1L);
        mockComment.setContent("Test comment");
        // Optional: You can mock user/post if needed in your model
    }

    @Test
    void testAddComment() throws Exception {
        CommentDTO dto = new CommentDTO();
        dto.setContent("Test comment");
        dto.setPostId(100L);
        dto.setUserId(UUID.randomUUID());

        Mockito.when(commentService.addComment(any(CommentDTO.class))).thenReturn(mockComment);

        mockMvc.perform(post("/comments/add")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(dto)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.content").value("Test comment"));
    }

    @Test
    void testGetCommentsByPost() throws Exception {
        Mockito.when(commentService.getCommentsByPost(100L)).thenReturn(List.of(mockComment));

        mockMvc.perform(get("/comments/post/100"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$[0].content").value("Test comment"));
    }

    @Test
    void testDeletePost() throws Exception {
        Mockito.doNothing().when(commentService).deleteComment(100L);

        mockMvc.perform(delete("/comments/100"))
                .andExpect(status().isOk())
                .andExpect(content().string("Comment deleted successfully."));
    }
}