|
| 1 | +package com.joaov1ct0r.restful_api_users_java.modules.posts.controllers; |
| 2 | + |
| 3 | +import com.github.javafaker.Faker; |
| 4 | +import com.joaov1ct0r.restful_api_users_java.modules.auth.dtos.SignInDTO; |
| 5 | +import com.joaov1ct0r.restful_api_users_java.modules.domain.dtos.ResponseDTO; |
| 6 | +import com.joaov1ct0r.restful_api_users_java.modules.posts.dtos.CreatePostDTO; |
| 7 | +import com.joaov1ct0r.restful_api_users_java.modules.posts.dtos.DeletePostDTO; |
| 8 | +import com.joaov1ct0r.restful_api_users_java.modules.posts.dtos.PostDTO; |
| 9 | +import com.joaov1ct0r.restful_api_users_java.modules.posts.repositories.PostRepository; |
| 10 | +import com.joaov1ct0r.restful_api_users_java.modules.users.dtos.UserDTO; |
| 11 | +import com.joaov1ct0r.restful_api_users_java.modules.users.entities.UserEntity; |
| 12 | +import com.joaov1ct0r.restful_api_users_java.modules.utils.TestUtils; |
| 13 | +import jakarta.servlet.http.Cookie; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.runner.RunWith; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.boot.test.context.SpringBootTest; |
| 19 | +import org.springframework.http.MediaType; |
| 20 | +import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers; |
| 21 | +import org.springframework.test.context.ActiveProfiles; |
| 22 | +import org.springframework.test.context.junit4.SpringRunner; |
| 23 | +import org.springframework.test.web.servlet.MockMvc; |
| 24 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 25 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 26 | +import org.springframework.web.context.WebApplicationContext; |
| 27 | +import java.time.LocalDateTime; |
| 28 | +import java.util.UUID; |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +@RunWith(SpringRunner.class) |
| 32 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 33 | +@ActiveProfiles("test") |
| 34 | +public class DeletePostControllerTest { |
| 35 | + private MockMvc mvc; |
| 36 | + |
| 37 | + private Faker faker; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private WebApplicationContext context; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private PostRepository postRepository; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setup() { |
| 47 | + this.mvc = MockMvcBuilders.webAppContextSetup(this.context) |
| 48 | + .apply(SecurityMockMvcConfigurers.springSecurity()) |
| 49 | + .build(); |
| 50 | + |
| 51 | + this.faker = new Faker(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void shouldBeAbleToDeleteAPost() throws Exception { |
| 56 | + UUID userId = UUID.randomUUID(); |
| 57 | + var createUserDTO = new UserEntity( |
| 58 | + userId, |
| 59 | + faker.name().username(), |
| 60 | + faker.internet().emailAddress(), |
| 61 | + faker.name().firstName(), |
| 62 | + faker.internet().password(), |
| 63 | + "any_photo_url", |
| 64 | + LocalDateTime.now(), |
| 65 | + null |
| 66 | + ); |
| 67 | + var createUserJson = TestUtils.stringToMMF(createUserDTO); |
| 68 | + var createUserResponse = mvc.perform( |
| 69 | + MockMvcRequestBuilders.multipart("/signup/") |
| 70 | + .file(createUserJson) |
| 71 | + .contentType(MediaType.MULTIPART_FORM_DATA) |
| 72 | + .with(request -> { |
| 73 | + request.setMethod("POST"); |
| 74 | + return request; |
| 75 | + }) |
| 76 | + ).andReturn().getResponse().getContentAsString(); |
| 77 | + ResponseDTO response = TestUtils.jsonToObject(createUserResponse, ResponseDTO.class); |
| 78 | + assert response.getResource() != null; |
| 79 | + UserDTO userDTO = TestUtils.jsonToUserDTO(response.getResource().toString(), UserDTO.class); |
| 80 | + |
| 81 | + var signInDTO = new SignInDTO( |
| 82 | + createUserDTO.getUsername(), |
| 83 | + createUserDTO.getPassword() |
| 84 | + ); |
| 85 | + Cookie cookieAuthorization = mvc.perform( |
| 86 | + MockMvcRequestBuilders.post("/signin/") |
| 87 | + .contentType(MediaType.APPLICATION_JSON) |
| 88 | + .content(TestUtils.objectToJson(signInDTO)) |
| 89 | + ).andReturn().getResponse().getCookie("authorization"); |
| 90 | + |
| 91 | + assert cookieAuthorization != null; |
| 92 | + |
| 93 | + CreatePostDTO post = new CreatePostDTO("any_content"); |
| 94 | + |
| 95 | + String createPostResponse = this.mvc.perform(MockMvcRequestBuilders.post("/post/") |
| 96 | + .cookie(cookieAuthorization) |
| 97 | + .content(TestUtils.objectToJson(post)) |
| 98 | + .contentType(MediaType.APPLICATION_JSON) |
| 99 | + ).andReturn().getResponse().getContentAsString(); |
| 100 | + |
| 101 | + ResponseDTO createdPostResponseDTO = TestUtils.jsonToObject(createPostResponse, ResponseDTO.class); |
| 102 | + assert createdPostResponseDTO.getResource() != null; |
| 103 | + |
| 104 | + PostDTO createdPost = TestUtils.jsonToPostDTO(createdPostResponseDTO.getResource().toString(), PostDTO.class); |
| 105 | + |
| 106 | + DeletePostDTO deletePostDTO = new DeletePostDTO(createdPost.getId().toString()); |
| 107 | + |
| 108 | + var deleteUserResponse = mvc.perform( |
| 109 | + MockMvcRequestBuilders.delete("/post/") |
| 110 | + .cookie(cookieAuthorization) |
| 111 | + .contentType(MediaType.APPLICATION_JSON) |
| 112 | + .content(TestUtils.objectToJson(deletePostDTO)) |
| 113 | + ).andReturn().getResponse(); |
| 114 | + |
| 115 | + int deleteUserResponseStatusCode = deleteUserResponse.getStatus(); |
| 116 | + |
| 117 | + assertThat(deleteUserResponseStatusCode).isEqualTo(204); |
| 118 | + } |
| 119 | +} |
0 commit comments