|
| 1 | +package com.cf.cfteam.controllers.codeforces; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 4 | +import com.cf.cfteam.BaseIntegrationTest; |
| 5 | +import com.cf.cfteam.models.entities.codeforces.Group; |
| 6 | +import com.cf.cfteam.models.entities.security.Role; |
| 7 | +import com.cf.cfteam.models.entities.security.User; |
| 8 | +import com.cf.cfteam.repositories.jpa.codeforces.GroupRepository; |
| 9 | +import com.cf.cfteam.repositories.jpa.security.UserRepository; |
| 10 | +import com.cf.cfteam.transfer.payloads.codeforces.GroupPayload; |
| 11 | +import lombok.SneakyThrows; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.http.MediaType; |
| 15 | +import org.springframework.test.context.ActiveProfiles; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 21 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 22 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 23 | + |
| 24 | +@ActiveProfiles("test") |
| 25 | +public class GroupControllerIntegrationTest extends BaseIntegrationTest { |
| 26 | + |
| 27 | + private static final String URI = "/api/cf/groups"; |
| 28 | + |
| 29 | + private static final Long BAD_USER_ID = 9999L; |
| 30 | + private static final Long BAD_GROUP_ID = 9999L; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private GroupRepository groupRepository; |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private UserRepository userRepository; |
| 37 | + |
| 38 | + @Test |
| 39 | + @SneakyThrows |
| 40 | + public void getAllGroupsByUser_notEmpty_success() { |
| 41 | + User user = createUser(); |
| 42 | + user = userRepository.save(user); |
| 43 | + |
| 44 | + Group group = createGroup(user); |
| 45 | + group = groupRepository.save(group); |
| 46 | + |
| 47 | + |
| 48 | + var mvcResponse = mockMvc.perform(get(URI + "/user/" + user.getId()) |
| 49 | + .header("Authorization", userBearerToken)) |
| 50 | + .andExpectAll( |
| 51 | + status().isOk(), |
| 52 | + content().contentType(MediaType.APPLICATION_JSON)) |
| 53 | + .andReturn() |
| 54 | + .getResponse(); |
| 55 | + |
| 56 | + var groups = objectMapper.readValue( |
| 57 | + mvcResponse.getContentAsString(), |
| 58 | + new TypeReference<List<Group>>() { |
| 59 | + } |
| 60 | + ); |
| 61 | + |
| 62 | + assertThat(groups).hasSize(1) |
| 63 | + .contains(group); |
| 64 | + |
| 65 | + deleteGroupFromDb(group); |
| 66 | + deleteUserFromDb(user); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + @SneakyThrows |
| 71 | + void getAllGroupsByUser_shouldThrowUserNotFound_whenUserNotFound() { |
| 72 | + mockMvc.perform(get(URI + "/user/" + BAD_USER_ID) |
| 73 | + .header("Authorization", userBearerToken)) |
| 74 | + .andExpectAll( |
| 75 | + status().isNotFound(), |
| 76 | + content().contentType(MediaType.APPLICATION_JSON), |
| 77 | + jsonPath("$.details.id").value(BAD_USER_ID), |
| 78 | + jsonPath("$.message").value("id.not_found"), |
| 79 | + jsonPath("$.error").value("Not Found"), |
| 80 | + jsonPath("$.status").value(404) |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + @SneakyThrows |
| 86 | + public void getGroupById_notEmpty() { |
| 87 | + User user = createUser(); |
| 88 | + user = userRepository.save(user); |
| 89 | + |
| 90 | + Group group = createGroup(user); |
| 91 | + group = groupRepository.save(group); |
| 92 | + |
| 93 | + var mvcResponse = mockMvc.perform(get(URI + "/" + group.getId()) |
| 94 | + .header("Authorization", userBearerToken)) |
| 95 | + .andExpectAll( |
| 96 | + status().isOk(), |
| 97 | + content().contentType(MediaType.APPLICATION_JSON)) |
| 98 | + .andReturn() |
| 99 | + .getResponse(); |
| 100 | + |
| 101 | + var responseGroupe = objectMapper.readValue(mvcResponse.getContentAsString(), Group.class); |
| 102 | + |
| 103 | + assertThat(responseGroupe).isEqualTo(group); |
| 104 | + |
| 105 | + deleteGroupFromDb(group);; |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + @SneakyThrows |
| 110 | + void getGroupById_shouldThrowGroupNotFound_whenGroupNotFound() { |
| 111 | + mockMvc.perform(get(URI + "/" + BAD_USER_ID) |
| 112 | + .header("Authorization", userBearerToken)) |
| 113 | + .andExpectAll( |
| 114 | + status().isNotFound(), |
| 115 | + content().contentType(MediaType.APPLICATION_JSON), |
| 116 | + jsonPath("$.details.id").value(BAD_USER_ID), |
| 117 | + jsonPath("$.message").value("id.not_found"), |
| 118 | + jsonPath("$.error").value("Not Found"), |
| 119 | + jsonPath("$.status").value(404) |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + @SneakyThrows |
| 125 | + public void addGroupToUser_success() { |
| 126 | + User user = createUser(); |
| 127 | + user = userRepository.save(user); |
| 128 | + |
| 129 | + var payload = createGroupPayload(); |
| 130 | + |
| 131 | + var mvcResponse = mockMvc.perform(post(URI + "/user/" + user.getId()) |
| 132 | + .header("Authorization", userBearerToken) |
| 133 | + .contentType(MediaType.APPLICATION_JSON) |
| 134 | + .content(objectMapper.writeValueAsString(payload))) |
| 135 | + .andExpectAll( |
| 136 | + status().isOk(), |
| 137 | + content().contentType(MediaType.APPLICATION_JSON)) |
| 138 | + .andReturn() |
| 139 | + .getResponse(); |
| 140 | + |
| 141 | + var responseGroupe = objectMapper.readValue(mvcResponse.getContentAsString(), Group.class); |
| 142 | + var groupFromDb = groupRepository.findById(responseGroupe.getId()); |
| 143 | + |
| 144 | + assertAll( |
| 145 | + () -> assertThat(groupFromDb).isPresent(), |
| 146 | + () -> assertThat(groupFromDb.get()).isEqualTo(responseGroupe), |
| 147 | + () -> assertThat(responseGroupe.getDescription()).isEqualTo(payload.description()), |
| 148 | + () -> assertThat(responseGroupe.getName()).isEqualTo(payload.name()) |
| 149 | + ); |
| 150 | + |
| 151 | + deleteGroupFromDb(groupFromDb.get()); |
| 152 | + deleteUserFromDb(user); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + @SneakyThrows |
| 157 | + void addGroupToUser_shouldThrowUserNotFound_whenUserNotFound() { |
| 158 | + var payload = createGroupPayload(); |
| 159 | + |
| 160 | + mockMvc.perform(post(URI + "/user/" + BAD_USER_ID) |
| 161 | + .header("Authorization", userBearerToken) |
| 162 | + .contentType(MediaType.APPLICATION_JSON) |
| 163 | + .content(objectMapper.writeValueAsString(payload))) |
| 164 | + .andExpectAll( |
| 165 | + status().isNotFound(), |
| 166 | + content().contentType(MediaType.APPLICATION_JSON), |
| 167 | + jsonPath("$.details.id").value(BAD_USER_ID), |
| 168 | + jsonPath("$.message").value("id.not_found"), |
| 169 | + jsonPath("$.error").value("Not Found"), |
| 170 | + jsonPath("$.status").value(404) |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + @SneakyThrows |
| 176 | + public void updateGroup_success() { |
| 177 | + User user = createUser(); |
| 178 | + user = userRepository.save(user); |
| 179 | + |
| 180 | + var payload = createGroupPayload("new name", "new description"); |
| 181 | + |
| 182 | + Group group = createGroup(user); |
| 183 | + group = groupRepository.save(group); |
| 184 | + |
| 185 | + |
| 186 | + var mvcResponse = mockMvc.perform(put(URI + "/" + group.getId()) |
| 187 | + .header("Authorization", userBearerToken) |
| 188 | + .contentType(MediaType.APPLICATION_JSON) |
| 189 | + .content(objectMapper.writeValueAsString(payload))) |
| 190 | + .andExpectAll( |
| 191 | + status().isOk(), |
| 192 | + content().contentType(MediaType.APPLICATION_JSON)) |
| 193 | + .andReturn() |
| 194 | + .getResponse(); |
| 195 | + |
| 196 | + |
| 197 | + var responseGroupe = objectMapper.readValue(mvcResponse.getContentAsString(), Group.class); |
| 198 | + var groupFromDb = groupRepository.findById(responseGroupe.getId()); |
| 199 | + |
| 200 | + assertAll( |
| 201 | + () -> assertThat(groupFromDb).isPresent(), |
| 202 | + () -> assertThat(groupFromDb.get().getDescription()).isEqualTo(payload.description()), |
| 203 | + () -> assertThat(groupFromDb.get().getName()).isEqualTo(payload.name()) |
| 204 | + ); |
| 205 | + |
| 206 | + deleteGroupFromDb(groupFromDb.get()); |
| 207 | + deleteUserFromDb(user); |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + @SneakyThrows |
| 212 | + void updateGroup_shouldThrowGroupNotFound_whenGroupNotFound() { |
| 213 | + var payload = createGroupPayload(); |
| 214 | + |
| 215 | + mockMvc.perform(put(URI + "/" + BAD_GROUP_ID) |
| 216 | + .header("Authorization", userBearerToken) |
| 217 | + .contentType(MediaType.APPLICATION_JSON) |
| 218 | + .content(objectMapper.writeValueAsString(payload))) |
| 219 | + .andExpectAll( |
| 220 | + status().isNotFound(), |
| 221 | + content().contentType(MediaType.APPLICATION_JSON), |
| 222 | + jsonPath("$.details.id").value(BAD_USER_ID), |
| 223 | + jsonPath("$.message").value("id.not_found"), |
| 224 | + jsonPath("$.error").value("Not Found"), |
| 225 | + jsonPath("$.status").value(404) |
| 226 | + ); |
| 227 | + } |
| 228 | + |
| 229 | + @Test |
| 230 | + @SneakyThrows |
| 231 | + public void deleteGroup_success() { |
| 232 | + User user = createUser(); |
| 233 | + user = userRepository.save(user); |
| 234 | + |
| 235 | + Group group = createGroup(user); |
| 236 | + group = groupRepository.save(group); |
| 237 | + |
| 238 | + mockMvc.perform(delete(URI + "/" + group.getId()) |
| 239 | + .header("Authorization", userBearerToken)) |
| 240 | + .andExpectAll( |
| 241 | + status().isNoContent(), |
| 242 | + content().string("") |
| 243 | + ); |
| 244 | + |
| 245 | + var groupFromDb = groupRepository.findById(group.getId()); |
| 246 | + |
| 247 | + assertThat(groupFromDb).isNotPresent(); |
| 248 | + |
| 249 | + deleteUserFromDb(user); |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + @SneakyThrows |
| 254 | + public void deleteAllGroupsByUser_success() { |
| 255 | + User user = createUser(); |
| 256 | + user = userRepository.save(user); |
| 257 | + |
| 258 | + Group group1 = createGroup(user); |
| 259 | + group1 = groupRepository.save(group1); |
| 260 | + |
| 261 | + Group group2 = createGroup(user); |
| 262 | + group2 = groupRepository.save(group2); |
| 263 | + |
| 264 | + mockMvc.perform(delete(URI + "/user/" + user.getId()) |
| 265 | + .header("Authorization", userBearerToken)) |
| 266 | + .andExpectAll( |
| 267 | + status().isNoContent(), |
| 268 | + content().string("") |
| 269 | + ); |
| 270 | + |
| 271 | + var groupFromDb1 = groupRepository.findById(group1.getId()); |
| 272 | + var groupFromDb2 = groupRepository.findById(group2.getId()); |
| 273 | + |
| 274 | + assertAll( |
| 275 | + () -> assertThat(groupFromDb1).isNotPresent(), |
| 276 | + () -> assertThat(groupFromDb2).isNotPresent() |
| 277 | + ); |
| 278 | + |
| 279 | + var mvcResponse = mockMvc.perform(get(URI + "/user/" + user.getId()) |
| 280 | + .header("Authorization", userBearerToken)) |
| 281 | + .andExpectAll( |
| 282 | + status().isOk(), |
| 283 | + content().contentType(MediaType.APPLICATION_JSON)) |
| 284 | + .andReturn() |
| 285 | + .getResponse(); |
| 286 | + |
| 287 | + var groups = objectMapper.readValue( |
| 288 | + mvcResponse.getContentAsString(), |
| 289 | + new TypeReference<List<Group>>() { |
| 290 | + } |
| 291 | + ); |
| 292 | + |
| 293 | + assertThat(groups).isEmpty(); |
| 294 | + |
| 295 | + deleteUserFromDb(user); |
| 296 | + } |
| 297 | + |
| 298 | + private User createUser() { |
| 299 | + return User.builder() |
| 300 | + .name("User name") |
| 301 | + .login("User login") |
| 302 | + .hashedPassword("Password") |
| 303 | + .role(Role.USER) |
| 304 | + .build(); |
| 305 | + } |
| 306 | + |
| 307 | + private Group createGroup(User user) { |
| 308 | + return Group.builder() |
| 309 | + .name("Test Group") |
| 310 | + .description("Test description") |
| 311 | + .user(user) |
| 312 | + .build(); |
| 313 | + } |
| 314 | + |
| 315 | + private GroupPayload createGroupPayload() { |
| 316 | + return GroupPayload.builder() |
| 317 | + .name("Test group") |
| 318 | + .description("Test description") |
| 319 | + .build(); |
| 320 | + } |
| 321 | + |
| 322 | + private GroupPayload createGroupPayload(String name, String description) { |
| 323 | + return GroupPayload.builder() |
| 324 | + .name(name) |
| 325 | + .description(description) |
| 326 | + .build(); |
| 327 | + } |
| 328 | + |
| 329 | + private void deleteUserFromDb(User user) { |
| 330 | + userRepository.delete(user); |
| 331 | + } |
| 332 | + |
| 333 | + private void deleteGroupFromDb(Group group) { |
| 334 | + groupRepository.delete(group); |
| 335 | + } |
| 336 | +} |
0 commit comments