#24 simple sns: 포스트 수정 테스트 코드
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.example.sns.controller.request;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class PostModifyRequest {
|
||||
|
||||
private String title;
|
||||
private String body;
|
||||
}
|
||||
@@ -12,6 +12,8 @@ public enum ErrorCode {
|
||||
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "User not founded."),
|
||||
INVALID_PASSWORD(HttpStatus.UNAUTHORIZED, "Password is invalid."),
|
||||
INVALID_TOKEN(HttpStatus.UNAUTHORIZED, "Token is invalid."),
|
||||
POST_NOT_FOUND(HttpStatus.UNAUTHORIZED, "Post not founded."),
|
||||
INVALID_PERMISSION(HttpStatus.UNAUTHORIZED, "Permission is invalid."),
|
||||
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Internal server error.")
|
||||
|
||||
;
|
||||
|
||||
@@ -30,4 +30,19 @@ public class PostService {
|
||||
postEntityRepository.save(PostEntity.of(title, body, userEntity));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void modify(String title, String body, String username, Integer postId) {
|
||||
UserEntity userEntity = userEntityRepository.findByUsername(username)
|
||||
.orElseThrow(
|
||||
() -> new SnsApplicationException(
|
||||
ErrorCode.USER_NOT_FOUND,
|
||||
String.format("%s not founded", username)
|
||||
)
|
||||
);
|
||||
|
||||
// TODO: post exist
|
||||
|
||||
// TODO: post permission
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.example.sns.controller;
|
||||
|
||||
import com.example.sns.controller.request.PostCreateRequest;
|
||||
import com.example.sns.controller.request.PostModifyRequest;
|
||||
import com.example.sns.exception.ErrorCode;
|
||||
import com.example.sns.exception.SnsApplicationException;
|
||||
import com.example.sns.service.PostService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -13,7 +16,11 @@ import org.springframework.security.test.context.support.WithAnonymousUser;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@@ -57,4 +64,66 @@ public class PostControllerTest {
|
||||
).andDo(print())
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
void 포스트수정() throws Exception {
|
||||
|
||||
String title = "title";
|
||||
String body = "body";
|
||||
|
||||
mockMvc.perform(put("/api/v1/posts/1")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsBytes(new PostModifyRequest(title, body)))
|
||||
).andDo(print())
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithAnonymousUser
|
||||
void 포스트수정시_로그인하지않은경우() throws Exception {
|
||||
|
||||
String title = "title";
|
||||
String body = "body";
|
||||
|
||||
mockMvc.perform(put("/api/v1/posts/1")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsBytes(new PostModifyRequest(title, body)))
|
||||
).andDo(print())
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
void 포스트수정시_본인이_작성한_글이_아닐경우_에러발생() throws Exception {
|
||||
|
||||
String title = "title";
|
||||
String body = "body";
|
||||
|
||||
doThrow(new SnsApplicationException(ErrorCode.INVALID_PERMISSION))
|
||||
.when(postService).modify(eq(title), eq(body), any(), eq(1));
|
||||
|
||||
mockMvc.perform(put("/api/v1/posts/1")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsBytes(new PostModifyRequest(title, body)))
|
||||
).andDo(print())
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
void 포스트수정시_수정하려는_글이_없는경우_에러발생() throws Exception {
|
||||
|
||||
String title = "title";
|
||||
String body = "body";
|
||||
|
||||
doThrow(new SnsApplicationException(ErrorCode.POST_NOT_FOUND))
|
||||
.when(postService).modify(eq(title), eq(body), any(), eq(1));
|
||||
|
||||
mockMvc.perform(put("/api/v1/posts/1")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsBytes(new PostModifyRequest(title, body)))
|
||||
).andDo(print())
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.example.sns.fixture;
|
||||
|
||||
import com.example.sns.model.entity.PostEntity;
|
||||
import com.example.sns.model.entity.UserEntity;
|
||||
|
||||
public class PostEntityFixture {
|
||||
|
||||
public static PostEntity get(String username, Integer postId) {
|
||||
UserEntity user = new UserEntity();
|
||||
user.setId(1);
|
||||
user.setUsername(username);
|
||||
|
||||
PostEntity result = new PostEntity();
|
||||
result.setUser(user);
|
||||
result.setId(postId);
|
||||
return result;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package com.example.sns.service;
|
||||
|
||||
import com.example.sns.exception.ErrorCode;
|
||||
import com.example.sns.exception.SnsApplicationException;
|
||||
import com.example.sns.fixture.PostEntityFixture;
|
||||
import com.example.sns.fixture.UserEntityFixture;
|
||||
import com.example.sns.model.entity.PostEntity;
|
||||
import com.example.sns.model.entity.UserEntity;
|
||||
import com.example.sns.repository.PostEntityRepository;
|
||||
@@ -58,4 +60,59 @@ public class PostServiceTest {
|
||||
|
||||
assertEquals(ErrorCode.USER_NOT_FOUND, e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 포스트수정이_성공한경우() {
|
||||
String title = "title";
|
||||
String body = "body";
|
||||
String username = "username";
|
||||
Integer postId = 1;
|
||||
|
||||
PostEntity postEntity = PostEntityFixture.get(username, postId);
|
||||
UserEntity userEntity = postEntity.getUser();
|
||||
|
||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(userEntity));
|
||||
when(postEntityRepository.findById(postId)).thenReturn(Optional.of(postEntity));
|
||||
|
||||
assertDoesNotThrow(() -> postService.modify(title, body, username, postId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 포스트수정시_포스트가_존재하지않는_경우() {
|
||||
String title = "title";
|
||||
String body = "body";
|
||||
String username = "username";
|
||||
Integer postId = 1;
|
||||
|
||||
PostEntity postEntity = PostEntityFixture.get(username, postId);
|
||||
UserEntity userEntity = postEntity.getUser();
|
||||
|
||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(userEntity));
|
||||
when(postEntityRepository.findById(postId)).thenReturn(Optional.empty());
|
||||
|
||||
SnsApplicationException e =
|
||||
assertThrows(SnsApplicationException.class, () -> postService.modify(title, body, username, postId));
|
||||
|
||||
assertEquals(ErrorCode.POST_NOT_FOUND, e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 포스트수정시_권한이_없는_경우() {
|
||||
String title = "title";
|
||||
String body = "body";
|
||||
String username = "username";
|
||||
Integer postId = 1;
|
||||
|
||||
PostEntity postEntity = PostEntityFixture.get(username, postId);
|
||||
UserEntity writer = UserEntityFixture.get("username1", "password");
|
||||
|
||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(writer));
|
||||
when(postEntityRepository.findById(postId)).thenReturn(Optional.of(postEntity));
|
||||
|
||||
SnsApplicationException e =
|
||||
assertThrows(SnsApplicationException.class, () -> postService.modify(title, body, username, postId));
|
||||
|
||||
assertEquals(ErrorCode.INVALID_PERMISSION, e.getErrorCode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user