File size: 3,150 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
82
83
84
85
86
87
88
89
90
91
92
93
package com.krishna.controller;

import com.krishna.service.LikeService;
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.UUID;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest(LikeController.class)
class LikeControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private LikeService likeService;

    @Test
    void toggleLike_ShouldReturnLikedMessage() throws Exception {
        UUID userId = UUID.randomUUID();
        Long postId = 1L;

        when(likeService.toggleLike(userId, postId)).thenReturn(true);

        mockMvc.perform(post("/likes/toggle")
                        .param("userId", userId.toString())
                        .param("postId", postId.toString()))
                .andExpect(status().isOk())
                .andExpect(content().string("Post liked successfully"));
    }

    @Test
    void toggleLike_ShouldReturnUnlikedMessage() throws Exception {
        UUID userId = UUID.randomUUID();
        Long postId = 2L;

        when(likeService.toggleLike(userId, postId)).thenReturn(false);

        mockMvc.perform(post("/likes/toggle")
                        .param("userId", userId.toString())
                        .param("postId", postId.toString()))
                .andExpect(status().isOk())
                .andExpect(content().string("Post unliked"));
    }

    @Test
    void getLikeCount_ShouldReturnCount() throws Exception {
        Long postId = 3L;
        when(likeService.getLikeCount(postId)).thenReturn(42L);

        mockMvc.perform(get("/likes/count/{postId}", postId))
                .andExpect(status().isOk())
                .andExpect(content().string("42"));
    }

    @Test
    void checkIfUserLikedPost_ShouldReturnTrue() throws Exception {
        UUID userId = UUID.randomUUID();
        Long postId = 4L;

        when(likeService.hasUserLikedPost(userId, postId)).thenReturn(true);

        mockMvc.perform(get("/likes/status")
                        .param("userId", userId.toString())
                        .param("postId", postId.toString()))
                .andExpect(status().isOk())
                .andExpect(content().string("true"));
    }

    @Test
    void checkIfUserLikedPost_ShouldReturnFalse() throws Exception {
        UUID userId = UUID.randomUUID();
        Long postId = 5L;

        when(likeService.hasUserLikedPost(userId, postId)).thenReturn(false);

        mockMvc.perform(get("/likes/status")
                        .param("userId", userId.toString())
                        .param("postId", postId.toString()))
                .andExpect(status().isOk())
                .andExpect(content().string("false"));
    }
}