Spaces:
Sleeping
Sleeping
File size: 5,176 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
package com.krishna.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.krishna.dto.PostRequestDTO;
import com.krishna.dto.PostUpdateDTO;
import com.krishna.model.PostModel;
import com.krishna.model.UserModel;
import com.krishna.repository.LikeRepository;
import com.krishna.repository.PostRepository;
import com.krishna.repository.UserRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(PostController.class)
class PostControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private PostRepository postRepository;
@MockBean
private UserRepository userRepository;
@MockBean
private LikeRepository likeRepository;
@Autowired
private ObjectMapper objectMapper;
private UUID userId;
private UserModel user;
private PostModel post;
@BeforeEach
void setUp() {
userId = UUID.randomUUID();
user = new UserModel();
user.setId(userId);
user.setFullName("Krishna");
user.setUsername("krish");
user.setEmail("krish@example.com");
post = new PostModel();
post.setId(1L);
post.setContent("Hello world");
post.setImageUrl("image.jpg");
post.setUserModel(user);
}
@Test
void createPost_ShouldReturnCreated() throws Exception {
PostRequestDTO request = new PostRequestDTO(userId, "New post", "img.jpg");
when(userRepository.findById(userId)).thenReturn(Optional.of(user));
when(postRepository.save(any(PostModel.class))).thenReturn(post);
mockMvc.perform(post("/posts/add")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isCreated());
}
@Test
void createPost_ShouldReturnNotFound_WhenUserNotFound() throws Exception {
PostRequestDTO request = new PostRequestDTO(userId, "Post", "img.jpg");
when(userRepository.findById(userId)).thenReturn(Optional.empty());
mockMvc.perform(post("/posts/add")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isNotFound());
}
@Test
void getAllPosts_ShouldReturnListOfPosts() throws Exception {
List<PostModel> posts = List.of(post);
when(postRepository.findAll()).thenReturn(posts);
mockMvc.perform(get("/posts"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].content").value("Hello world"));
}
@Test
void updatePost_ShouldReturnUpdatedPost() throws Exception {
PostUpdateDTO updateDTO = new PostUpdateDTO("Updated content", "updated.jpg");
// Simulate that the post has been updated
post.setContent(updateDTO.getContent());
post.setImageUrl(updateDTO.getImageUrl());
when(postRepository.findById(1L)).thenReturn(Optional.of(post));
when(postRepository.save(any(PostModel.class))).thenReturn(post);
mockMvc.perform(put("/posts/1")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(updateDTO)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("Updated content"));
}
@Test
void updatePost_ShouldReturnNotFound() throws Exception {
PostUpdateDTO updateDTO = new PostUpdateDTO("No post", "no.jpg");
when(postRepository.findById(2L)).thenReturn(Optional.empty());
mockMvc.perform(put("/posts/2")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(updateDTO)))
.andExpect(status().isNotFound());
}
@Test
void deletePost_ShouldReturnSuccess() throws Exception {
when(postRepository.findById(1L)).thenReturn(Optional.of(post));
doNothing().when(likeRepository).deleteByPostId(1L);
doNothing().when(postRepository).deleteById(1L);
mockMvc.perform(delete("/posts/1"))
.andExpect(status().isOk())
.andExpect(content().string("Post deleted successfully"));
}
@Test
void deletePost_ShouldReturnNotFound() throws Exception {
when(postRepository.findById(99L)).thenReturn(Optional.empty());
mockMvc.perform(delete("/posts/99"))
.andExpect(status().isNotFound())
.andExpect(content().string("Post not found"));
}
}
|