|
| 1 | +package messagingservice; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import messagingservice.dto.MessageDto; |
| 5 | +import messagingservice.dto.ContactDto; |
| 6 | +import messagingservice.dto.SendMessageRequest; |
| 7 | +import model.UserDTO; |
| 8 | +import org.junit.jupiter.api.DisplayName; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 11 | +import org.mockito.Mockito; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 14 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 15 | +import org.springframework.data.domain.Page; |
| 16 | +import org.springframework.data.domain.PageImpl; |
| 17 | +import org.springframework.data.domain.PageRequest; |
| 18 | +import org.springframework.http.MediaType; |
| 19 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 20 | +import org.springframework.test.web.servlet.MockMvc; |
| 21 | + |
| 22 | +import java.time.LocalDateTime; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Arrays; |
| 25 | + |
| 26 | +import static org.hamcrest.Matchers.*; |
| 27 | +import static org.mockito.ArgumentMatchers.any; |
| 28 | +import static org.mockito.ArgumentMatchers.eq; |
| 29 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 30 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 31 | + |
| 32 | +/** |
| 33 | + * Integration tests for {@link MessagingController}. |
| 34 | + */ |
| 35 | +@ExtendWith(SpringExtension.class) |
| 36 | +@WebMvcTest(MessagingController.class) |
| 37 | +class MessagingControllerTest { |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private MockMvc mockMvc; |
| 41 | + |
| 42 | + @MockBean |
| 43 | + private MessagingService messagingService; |
| 44 | + |
| 45 | + private final ObjectMapper mapper = new ObjectMapper(); |
| 46 | + |
| 47 | + @Test |
| 48 | + @DisplayName("POST /messaging/send returns sent message") |
| 49 | + void sendMessageEndpointReturnsSentMessage() throws Exception { |
| 50 | + // Given |
| 51 | + SendMessageRequest request = new SendMessageRequest(); |
| 52 | + request.setFromUserId("user1"); |
| 53 | + request.setToUserId("user2"); |
| 54 | + request.setContent("Hello World"); |
| 55 | + |
| 56 | + MessageDto expectedResponse = new MessageDto(1L, "user1", "user2", "Hello World", LocalDateTime.now()); |
| 57 | + |
| 58 | + Mockito.when(messagingService.sendMessage("user1", "user2", "Hello World")) |
| 59 | + .thenReturn(expectedResponse); |
| 60 | + |
| 61 | + // When & Then |
| 62 | + mockMvc.perform(post("/messaging/send") |
| 63 | + .contentType(MediaType.APPLICATION_JSON) |
| 64 | + .content(mapper.writeValueAsString(request))) |
| 65 | + .andExpect(status().isOk()) |
| 66 | + .andExpect(jsonPath("$.fromUserId", is("user1"))) |
| 67 | + .andExpect(jsonPath("$.toUserId", is("user2"))) |
| 68 | + .andExpect(jsonPath("$.content", is("Hello World"))); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + @DisplayName("GET /messaging/conversation returns paginated conversation") |
| 73 | + void getConversationEndpointReturnsPaginatedMessages() throws Exception { |
| 74 | + // Given |
| 75 | + List<MessageDto> messages = Arrays.asList( |
| 76 | + new MessageDto(1L, "user1", "user2", "Hi", LocalDateTime.now()), |
| 77 | + new MessageDto(2L, "user2", "user1", "Hello", LocalDateTime.now()) |
| 78 | + ); |
| 79 | + Page<MessageDto> messagePage = new PageImpl<>(messages, PageRequest.of(0, 10), 2); |
| 80 | + |
| 81 | + Mockito.when(messagingService.getConversation("user1", "user2", 0, 10)) |
| 82 | + .thenReturn(messagePage); |
| 83 | + |
| 84 | + // When & Then |
| 85 | + mockMvc.perform(get("/messaging/conversation") |
| 86 | + .param("userA", "user1") |
| 87 | + .param("userB", "user2") |
| 88 | + .param("page", "0") |
| 89 | + .param("size", "10")) |
| 90 | + .andExpect(status().isOk()) |
| 91 | + .andExpect(jsonPath("$.content", hasSize(2))) |
| 92 | + .andExpect(jsonPath("$.totalElements", is(2))) |
| 93 | + .andExpect(jsonPath("$.content[0].content", is("Hi"))) |
| 94 | + .andExpect(jsonPath("$.content[1].content", is("Hello"))); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + @DisplayName("POST /messaging/contact adds contact and returns contact info") |
| 99 | + void addContactEndpointAddsContactAndReturnsInfo() throws Exception { |
| 100 | + // Given |
| 101 | + ContactDto expectedResponse = new ContactDto("user1", "user2", LocalDateTime.now()); |
| 102 | + |
| 103 | + Mockito.when(messagingService.addContact("user1", "user2")) |
| 104 | + .thenReturn(expectedResponse); |
| 105 | + |
| 106 | + // When & Then |
| 107 | + mockMvc.perform(post("/messaging/contact") |
| 108 | + .param("userId", "user1") |
| 109 | + .param("contactId", "user2")) |
| 110 | + .andExpect(status().isOk()) |
| 111 | + .andExpect(jsonPath("$.userId", is("user1"))) |
| 112 | + .andExpect(jsonPath("$.contactId", is("user2"))); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + @DisplayName("GET /messaging/contacts/{userId} returns user contacts") |
| 117 | + void getContactsEndpointReturnsUserContacts() throws Exception { |
| 118 | + // Given |
| 119 | + List<UserDTO> contacts = Arrays.asList( |
| 120 | + new UserDTO("user2", "User Two", "", "", "", java.util.Map.of(), java.util.List.of()), |
| 121 | + new UserDTO("user3", "User Three", "", "", "", java.util.Map.of(), java.util.List.of()) |
| 122 | + ); |
| 123 | + |
| 124 | + Mockito.when(messagingService.getContacts("user1")).thenReturn(contacts); |
| 125 | + |
| 126 | + // When & Then |
| 127 | + mockMvc.perform(get("/messaging/contacts/user1")) |
| 128 | + .andExpect(status().isOk()) |
| 129 | + .andExpect(jsonPath("$", hasSize(2))) |
| 130 | + .andExpect(jsonPath("$[0].id", is("user2"))) |
| 131 | + .andExpect(jsonPath("$[1].id", is("user3"))); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + @DisplayName("POST /messaging/send with invalid request returns bad request") |
| 136 | + void sendMessageWithInvalidRequestReturnsBadRequest() throws Exception { |
| 137 | + // Given - Invalid request with empty fields |
| 138 | + SendMessageRequest invalidRequest = new SendMessageRequest(); |
| 139 | + invalidRequest.setFromUserId(""); |
| 140 | + invalidRequest.setToUserId(""); |
| 141 | + invalidRequest.setContent(""); |
| 142 | + |
| 143 | + // When & Then |
| 144 | + mockMvc.perform(post("/messaging/send") |
| 145 | + .contentType(MediaType.APPLICATION_JSON) |
| 146 | + .content(mapper.writeValueAsString(invalidRequest))) |
| 147 | + .andExpect(status().isBadRequest()); |
| 148 | + } |
| 149 | +} |
0 commit comments