Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
435de08537 |
@@ -27,14 +27,14 @@ public class PostController {
|
||||
public ResponseEntity<PostResponse> getPostById(@PathVariable long id) {
|
||||
return ResponseEntity
|
||||
.ok()
|
||||
.body(toResponse(postService.getPostById(id)));
|
||||
.body(toResponse(postService.getById(id)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<PostResponse> updatePost(@PathVariable long id, @RequestBody PostUpdateDto postUpdateDto) {
|
||||
return ResponseEntity
|
||||
.ok()
|
||||
.body(toResponse(postService.updatePost(id, postUpdateDto)));
|
||||
.body(toResponse(postService.update(id, postUpdateDto)));
|
||||
}
|
||||
|
||||
public PostResponse toResponse(PostEntity postEntity) {
|
||||
|
||||
@@ -25,6 +25,6 @@ public class PostCreateController {
|
||||
public ResponseEntity<PostResponse> createPost(@RequestBody PostCreateDto postCreateDto) {
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.CREATED)
|
||||
.body(postController.toResponse(postService.createPost(postCreateDto)));
|
||||
.body(postController.toResponse(postService.create(postCreateDto)));
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ public class UserController {
|
||||
public ResponseEntity<UserResponse> getUserById(@PathVariable long id) {
|
||||
return ResponseEntity
|
||||
.ok()
|
||||
.body(toResponse(userService.getByIdOrElseThrow(id)));
|
||||
.body(toResponse(userService.getById(id)));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/verify")
|
||||
@@ -68,7 +68,7 @@ public class UserController {
|
||||
@RequestBody UserUpdateDto userUpdateDto
|
||||
) {
|
||||
UserEntity userEntity = userService.getByEmail(email);
|
||||
userEntity = userService.updateUser(userEntity.getId(), userUpdateDto);
|
||||
userEntity = userService.update(userEntity.getId(), userUpdateDto);
|
||||
return ResponseEntity
|
||||
.ok()
|
||||
.body(toMyProfileResponse(userEntity));
|
||||
|
||||
@@ -24,7 +24,7 @@ public class UserCreateController {
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<UserResponse> createUser(@RequestBody UserCreateDto userCreateDto) {
|
||||
UserEntity userEntity = userService.createUser(userCreateDto);
|
||||
UserEntity userEntity = userService.create(userCreateDto);
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.CREATED)
|
||||
.body(userController.toResponse(userEntity));
|
||||
|
||||
@@ -17,12 +17,12 @@ public class PostService {
|
||||
private final PostRepository postRepository;
|
||||
private final UserService userService;
|
||||
|
||||
public PostEntity getPostById(long id) {
|
||||
public PostEntity getById(long id) {
|
||||
return postRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Posts", id));
|
||||
}
|
||||
|
||||
public PostEntity createPost(PostCreateDto postCreateDto) {
|
||||
UserEntity userEntity = userService.getByIdOrElseThrow(postCreateDto.getWriterId());
|
||||
public PostEntity create(PostCreateDto postCreateDto) {
|
||||
UserEntity userEntity = userService.getById(postCreateDto.getWriterId());
|
||||
PostEntity postEntity = new PostEntity();
|
||||
postEntity.setWriter(userEntity);
|
||||
postEntity.setContent(postCreateDto.getContent());
|
||||
@@ -30,8 +30,8 @@ public class PostService {
|
||||
return postRepository.save(postEntity);
|
||||
}
|
||||
|
||||
public PostEntity updatePost(long id, PostUpdateDto postUpdateDto) {
|
||||
PostEntity postEntity = getPostById(id);
|
||||
public PostEntity update(long id, PostUpdateDto postUpdateDto) {
|
||||
PostEntity postEntity = getById(id);
|
||||
postEntity.setContent(postUpdateDto.getContent());
|
||||
postEntity.setModifiedAt(Clock.systemUTC().millis());
|
||||
return postRepository.save(postEntity);
|
||||
|
||||
@@ -23,22 +23,18 @@ public class UserService {
|
||||
private final UserRepository userRepository;
|
||||
private final JavaMailSender mailSender;
|
||||
|
||||
public Optional<UserEntity> getById(long id) {
|
||||
return userRepository.findByIdAndStatus(id, UserStatus.ACTIVE);
|
||||
}
|
||||
|
||||
public UserEntity getByEmail(String email) {
|
||||
return userRepository.findByEmailAndStatus(email, UserStatus.ACTIVE)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Users", email));
|
||||
}
|
||||
|
||||
public UserEntity getByIdOrElseThrow(long id) {
|
||||
public UserEntity getById(long id) {
|
||||
return userRepository.findByIdAndStatus(id, UserStatus.ACTIVE)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Users", id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserEntity createUser(UserCreateDto userCreateDto) {
|
||||
public UserEntity create(UserCreateDto userCreateDto) {
|
||||
UserEntity userEntity = new UserEntity();
|
||||
userEntity.setEmail(userCreateDto.getEmail());
|
||||
userEntity.setNickname(userCreateDto.getNickname());
|
||||
@@ -52,8 +48,8 @@ public class UserService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserEntity updateUser(long id, UserUpdateDto userUpdateDto) {
|
||||
UserEntity userEntity = getByIdOrElseThrow(id);
|
||||
public UserEntity update(long id, UserUpdateDto userUpdateDto) {
|
||||
UserEntity userEntity = getById(id);
|
||||
userEntity.setNickname(userUpdateDto.getNickname());
|
||||
userEntity.setAddress(userUpdateDto.getAddress());
|
||||
userEntity = userRepository.save(userEntity);
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@AutoConfigureTestDatabase
|
||||
public class HealthCheckTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
void 헬스_체크_응답이_200으로_내려온다() throws Exception {
|
||||
// given
|
||||
// when
|
||||
// then
|
||||
mockMvc.perform(get("/health_check.html"))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import com.example.demo.model.dto.PostUpdateDto;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
|
||||
import org.springframework.test.context.jdbc.SqlGroup;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@AutoConfigureTestDatabase
|
||||
@SqlGroup({
|
||||
@Sql(value = "/sql/post-controller-test-data.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD),
|
||||
@Sql(value = "/sql/delete-all-data.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
|
||||
})
|
||||
public class PostControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void 사용자는_게시물을_단건_조회_할_수_있다() throws Exception {
|
||||
// given
|
||||
// when
|
||||
// then
|
||||
mockMvc.perform(get("/api/posts/1"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.id").isNumber())
|
||||
.andExpect(jsonPath("$.content").value("helloworld"))
|
||||
.andExpect(jsonPath("$.writer.id").isNumber())
|
||||
.andExpect(jsonPath("$.writer.email").value("kok202@naver.com"))
|
||||
.andExpect(jsonPath("$.writer.nickname").value("kok202"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 사용자가_존재하지_않는_게시물을_조회할_경우_에러가_난다() throws Exception {
|
||||
// given
|
||||
// when
|
||||
// then
|
||||
mockMvc.perform(get("/api/posts/123456789"))
|
||||
.andExpect(status().isNotFound())
|
||||
.andExpect(content().string("Posts에서 ID 123456789를 찾을 수 없습니다."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 사용자는_게시물을_수정할_수_있다() throws Exception {
|
||||
// given
|
||||
PostUpdateDto postUpdateDto = PostUpdateDto.builder()
|
||||
.content("foobar")
|
||||
.build();
|
||||
|
||||
// when
|
||||
// then
|
||||
mockMvc.perform(
|
||||
put("/api/posts/1")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(postUpdateDto)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.id").isNumber())
|
||||
.andExpect(jsonPath("$.content").value("foobar"))
|
||||
.andExpect(jsonPath("$.writer.id").isNumber())
|
||||
.andExpect(jsonPath("$.writer.email").value("kok202@naver.com"))
|
||||
.andExpect(jsonPath("$.writer.nickname").value("kok202"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import com.example.demo.model.dto.PostCreateDto;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
|
||||
import org.springframework.test.context.jdbc.SqlGroup;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@AutoConfigureTestDatabase
|
||||
@SqlGroup({
|
||||
@Sql(value = "/sql/post-create-controller-test-data.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD),
|
||||
@Sql(value = "/sql/delete-all-data.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
|
||||
})
|
||||
public class PostCreateControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void 사용자는_게시물을_작성할_수_있다() throws Exception {
|
||||
// given
|
||||
PostCreateDto postCreateDto = PostCreateDto.builder()
|
||||
.writerId(1)
|
||||
.content("helloworld")
|
||||
.build();
|
||||
|
||||
// when
|
||||
// then
|
||||
mockMvc.perform(
|
||||
post("/api/posts")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(postCreateDto)))
|
||||
.andExpect(status().isCreated())
|
||||
.andExpect(jsonPath("$.id").isNumber())
|
||||
.andExpect(jsonPath("$.content").value("helloworld"))
|
||||
.andExpect(jsonPath("$.writer.id").isNumber())
|
||||
.andExpect(jsonPath("$.writer.email").value("kok202@naver.com"))
|
||||
.andExpect(jsonPath("$.writer.nickname").value("kok202"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import com.example.demo.model.UserStatus;
|
||||
import com.example.demo.model.dto.UserUpdateDto;
|
||||
import com.example.demo.repository.UserEntity;
|
||||
import com.example.demo.repository.UserRepository;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
|
||||
import org.springframework.test.context.jdbc.SqlGroup;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@AutoConfigureTestDatabase
|
||||
@SqlGroup({
|
||||
@Sql(value = "/sql/user-controller-test-data.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD),
|
||||
@Sql(value = "/sql/delete-all-data.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
|
||||
})
|
||||
public class UserControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void 사용자는_특정_유저의_정보를_개인정보는_소거된채_전달_받을_수_있다() throws Exception {
|
||||
// given
|
||||
// when
|
||||
// then
|
||||
mockMvc.perform(get("/api/users/1"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.id").value(1))
|
||||
.andExpect(jsonPath("$.email").value("kok202@naver.com"))
|
||||
.andExpect(jsonPath("$.nickname").value("kok202"))
|
||||
.andExpect(jsonPath("$.address").doesNotExist())
|
||||
.andExpect(jsonPath("$.status").value("ACTIVE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 사용자는_존재하지_않는_유저의_아이디로_api_호출할_경우_404_응답을_받는다() throws Exception {
|
||||
// given
|
||||
// when
|
||||
// then
|
||||
mockMvc.perform(get("/api/users/123456789"))
|
||||
.andExpect(status().isNotFound())
|
||||
.andExpect(content().string("Users에서 ID 123456789를 찾을 수 없습니다."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 사용자는_인증_코드로_계정을_활성화_시킬_수_있다() throws Exception {
|
||||
// given
|
||||
// when
|
||||
// then
|
||||
mockMvc.perform(
|
||||
get("/api/users/2/verify")
|
||||
.queryParam("certificationCode", "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab"))
|
||||
.andExpect(status().isFound());
|
||||
UserEntity userEntity = userRepository.findById(1L).get();
|
||||
assertThat(userEntity.getStatus()).isEqualTo(UserStatus.ACTIVE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 사용자는_인증_코드가_일치하지_않을_경우_권한_없음_에러를_내려준다() throws Exception {
|
||||
// given
|
||||
// when
|
||||
// then
|
||||
mockMvc.perform(
|
||||
get("/api/users/2/verify")
|
||||
.queryParam("certificationCode", "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaac"))
|
||||
.andExpect(status().isForbidden());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 사용자는_내_정보를_불러올_때_개인정보인_주소도_갖고_올_수_있다() throws Exception {
|
||||
// given
|
||||
// when
|
||||
// then
|
||||
mockMvc.perform(
|
||||
get("/api/users/me")
|
||||
.header("EMAIL", "kok202@naver.com"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.id").value(1))
|
||||
.andExpect(jsonPath("$.email").value("kok202@naver.com"))
|
||||
.andExpect(jsonPath("$.nickname").value("kok202"))
|
||||
.andExpect(jsonPath("$.address").value("Seoul"))
|
||||
.andExpect(jsonPath("$.status").value("ACTIVE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 사용자는_내_정보를_수정할_수_있다() throws Exception {
|
||||
// given
|
||||
UserUpdateDto userUpdateDto = UserUpdateDto.builder()
|
||||
.nickname("kok202-n")
|
||||
.address("Pangyo")
|
||||
.build();
|
||||
|
||||
// when
|
||||
// then
|
||||
mockMvc.perform(
|
||||
put("/api/users/me")
|
||||
.header("EMAIL", "kok202@naver.com")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(userUpdateDto)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.id").value(1))
|
||||
.andExpect(jsonPath("$.email").value("kok202@naver.com"))
|
||||
.andExpect(jsonPath("$.nickname").value("kok202-n"))
|
||||
.andExpect(jsonPath("$.address").value("Pangyo"))
|
||||
.andExpect(jsonPath("$.status").value("ACTIVE"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import com.example.demo.model.dto.UserCreateDto;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.BDDMockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
|
||||
import org.springframework.test.context.jdbc.SqlGroup;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@AutoConfigureTestDatabase
|
||||
@SqlGroup({
|
||||
@Sql(value = "/sql/delete-all-data.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
|
||||
})
|
||||
public class UserCreateControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
@MockBean
|
||||
private JavaMailSender mailSender;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void 사용자는_회원_가입을_할_수있고_회원가입된_사용자는_PENDING_상태이다() throws Exception {
|
||||
// given
|
||||
UserCreateDto userCreateDto = UserCreateDto.builder()
|
||||
.email("kok202@kakao.com")
|
||||
.nickname("kok202")
|
||||
.address("Pangyo")
|
||||
.build();
|
||||
BDDMockito.doNothing().when(mailSender).send(any(SimpleMailMessage.class));
|
||||
|
||||
// when
|
||||
// then
|
||||
mockMvc.perform(
|
||||
post("/api/users")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(userCreateDto)))
|
||||
.andExpect(status().isCreated())
|
||||
.andExpect(jsonPath("$.id").isNumber())
|
||||
.andExpect(jsonPath("$.email").value("kok202@kakao.com"))
|
||||
.andExpect(jsonPath("$.nickname").value("kok202"))
|
||||
.andExpect(jsonPath("$.status").value("PENDING"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.example.demo.repository;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
import com.example.demo.model.UserStatus;
|
||||
import java.util.Optional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@DataJpaTest(showSql = true)
|
||||
@TestPropertySource("classpath:test-application.properties")
|
||||
@Sql("/sql/user-repository-test-data.sql")
|
||||
public class UserRepositoryTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Test
|
||||
void findByIdAndStatus_로_유저_데이터를_찾아올_수_있다() {
|
||||
// given
|
||||
// when
|
||||
Optional<UserEntity> result = userRepository.findByIdAndStatus(1, UserStatus.ACTIVE);
|
||||
|
||||
// then
|
||||
assertThat(result.isPresent()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByIdAndStatus_는_데이터가_없으면_Optional_empty_를_내려준다() {
|
||||
// given
|
||||
// when
|
||||
Optional<UserEntity> result = userRepository.findByIdAndStatus(1, UserStatus.PENDING);
|
||||
|
||||
// then
|
||||
assertThat(result.isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByEmailAndStatus_로_유저_데이터를_찾아올_수_있다() {
|
||||
// given
|
||||
// when
|
||||
Optional<UserEntity> result = userRepository.findByEmailAndStatus("kok202@naver.com", UserStatus.ACTIVE);
|
||||
|
||||
// then
|
||||
assertThat(result.isPresent()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByEmailAndStatus_는_데이터가_없으면_Optional_empty_를_내려준다() {
|
||||
// given
|
||||
// when
|
||||
Optional<UserEntity> result = userRepository.findByEmailAndStatus("kok202@naver.com", UserStatus.PENDING);
|
||||
|
||||
// then
|
||||
assertThat(result.isEmpty()).isTrue();
|
||||
}
|
||||
}
|
||||
71
src/test/java/com/example/demo/service/PostServiceTest.java
Normal file
71
src/test/java/com/example/demo/service/PostServiceTest.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package com.example.demo.service;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
import com.example.demo.model.dto.PostCreateDto;
|
||||
import com.example.demo.model.dto.PostUpdateDto;
|
||||
import com.example.demo.repository.PostEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
|
||||
import org.springframework.test.context.jdbc.SqlGroup;
|
||||
|
||||
@SpringBootTest
|
||||
@TestPropertySource("classpath:test-application.properties")
|
||||
@SqlGroup({
|
||||
@Sql(value = "/sql/post-service-test-data.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD),
|
||||
@Sql(value = "/sql/delete-all-data.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
|
||||
})
|
||||
public class PostServiceTest {
|
||||
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
@Test
|
||||
void getById는_존재하는_게시물을_내려준다() {
|
||||
// given
|
||||
// when
|
||||
PostEntity result = postService.getById(1);
|
||||
|
||||
// then
|
||||
assertThat(result.getContent()).isEqualTo("helloworld");
|
||||
assertThat(result.getWriter().getEmail()).isEqualTo("kok202@naver.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
void postCreateDto_를_이용하여_게시물을_생성할_수_있다() {
|
||||
// given
|
||||
PostCreateDto postCreateDto = PostCreateDto.builder()
|
||||
.writerId(1)
|
||||
.content("foobar")
|
||||
.build();
|
||||
|
||||
// when
|
||||
PostEntity result = postService.create(postCreateDto);
|
||||
|
||||
// then
|
||||
assertThat(result.getId()).isNotNull();
|
||||
assertThat(result.getContent()).isEqualTo("foobar");
|
||||
assertThat(result.getCreatedAt()).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void postUpdateDto_를_이용하여_게시물을_수정할_수_있다() {
|
||||
// given
|
||||
PostUpdateDto postUpdateDto = PostUpdateDto.builder()
|
||||
.content("hello world :)")
|
||||
.build();
|
||||
|
||||
// when
|
||||
postService.update(1, postUpdateDto);
|
||||
|
||||
// then
|
||||
PostEntity postEntity= postService.getById(1);
|
||||
assertThat(postEntity.getContent()).isEqualTo("hello world :)");
|
||||
assertThat(postEntity.getModifiedAt()).isGreaterThan(0);
|
||||
}
|
||||
|
||||
}
|
||||
152
src/test/java/com/example/demo/service/UserServiceTest.java
Normal file
152
src/test/java/com/example/demo/service/UserServiceTest.java
Normal file
@@ -0,0 +1,152 @@
|
||||
package com.example.demo.service;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
|
||||
import com.example.demo.exception.CertificationCodeNotMatchedException;
|
||||
import com.example.demo.exception.ResourceNotFoundException;
|
||||
import com.example.demo.model.UserStatus;
|
||||
import com.example.demo.model.dto.UserCreateDto;
|
||||
import com.example.demo.model.dto.UserUpdateDto;
|
||||
import com.example.demo.repository.UserEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.BDDMockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
|
||||
import org.springframework.test.context.jdbc.SqlGroup;
|
||||
|
||||
@SpringBootTest
|
||||
@TestPropertySource("classpath:test-application.properties")
|
||||
@SqlGroup({
|
||||
@Sql(value = "/sql/user-service-test-data.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD),
|
||||
@Sql(value = "/sql/delete-all-data.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
|
||||
})
|
||||
public class UserServiceTest {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@MockBean
|
||||
private JavaMailSender mailSender;
|
||||
|
||||
@Test
|
||||
void getByEmail은_ACTIVE_상태인_유저를_찾아올_수_있다() {
|
||||
// given
|
||||
String email = "kok202@naver.com";
|
||||
|
||||
// when
|
||||
UserEntity result = userService.getByEmail(email);
|
||||
|
||||
// then
|
||||
assertThat(result.getNickname()).isEqualTo("kok202");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getByEmail은_PENDING_상태인_유저는_찾아올_수_없다() {
|
||||
// given
|
||||
String email = "kok303@naver.com";
|
||||
|
||||
// when
|
||||
// then
|
||||
assertThatThrownBy(() -> {
|
||||
UserEntity result = userService.getByEmail(email);
|
||||
}).isInstanceOf(ResourceNotFoundException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getById는_ACTIVE_상태인_유저를_찾아올_수_있다() {
|
||||
// given
|
||||
// when
|
||||
UserEntity result = userService.getById(1);
|
||||
|
||||
// then
|
||||
assertThat(result.getNickname()).isEqualTo("kok202");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getById는_PENDING_상태인_유저는_찾아올_수_없다() {
|
||||
// given
|
||||
// when
|
||||
// then
|
||||
assertThatThrownBy(() -> {
|
||||
UserEntity result = userService.getById(2);
|
||||
}).isInstanceOf(ResourceNotFoundException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void userCreateDto_를_이용하여_유저를_생성할_수_있다() {
|
||||
// given
|
||||
UserCreateDto userCreateDto = UserCreateDto.builder()
|
||||
.email("kok202@kakao.com")
|
||||
.address("Gyeongi")
|
||||
.nickname("kok202-k")
|
||||
.build();
|
||||
BDDMockito.doNothing().when(mailSender).send(any(SimpleMailMessage.class));
|
||||
|
||||
// when
|
||||
UserEntity result = userService.create(userCreateDto);
|
||||
|
||||
// then
|
||||
assertThat(result.getId()).isNotNull();
|
||||
assertThat(result.getStatus()).isEqualTo(UserStatus.PENDING);
|
||||
// assertThat(result.getCertificationCode()).isEqualTo("T.T"); // FIXME
|
||||
}
|
||||
|
||||
@Test
|
||||
void userUpdateDto_를_이용하여_유저를_수정할_수_있다() {
|
||||
// given
|
||||
UserUpdateDto userUpdateDto = UserUpdateDto.builder()
|
||||
.address("Incheon")
|
||||
.nickname("kok202-n")
|
||||
.build();
|
||||
|
||||
// when
|
||||
userService.update(1, userUpdateDto);
|
||||
|
||||
// then
|
||||
UserEntity userEntity = userService.getById(1);
|
||||
assertThat(userEntity.getId()).isNotNull();
|
||||
assertThat(userEntity.getAddress()).isEqualTo("Incheon");
|
||||
assertThat(userEntity.getNickname()).isEqualTo("kok202-n");
|
||||
}
|
||||
|
||||
@Test
|
||||
void user를_로그인_시키면_마지막_로그인_시간이_변경된다() {
|
||||
// given
|
||||
// when
|
||||
userService.login(1);
|
||||
|
||||
// then
|
||||
UserEntity userEntity = userService.getById(1);
|
||||
assertThat(userEntity.getLastLoginAt()).isGreaterThan(0L);
|
||||
// assertThat(result.getLastLoginAt()).isEqualTo("T.T"); // FIXME
|
||||
}
|
||||
|
||||
@Test
|
||||
void PENDING_상태의_사용자는_인증_코드로_ACTIVE_시킬_수_있다() {
|
||||
// given
|
||||
// when
|
||||
userService.verifyEmail(2, "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab");
|
||||
|
||||
// then
|
||||
UserEntity userEntity = userService.getById(2);
|
||||
assertThat(userEntity.getStatus()).isEqualTo(UserStatus.ACTIVE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void PENDING_상태의_사용자는_잘못된_인증_코드를_받으면_에러를_던진다() {
|
||||
// given
|
||||
// when
|
||||
// then
|
||||
assertThatThrownBy(() -> {
|
||||
userService.verifyEmail(2, "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaac");
|
||||
}).isInstanceOf(CertificationCodeNotMatchedException.class);
|
||||
}
|
||||
|
||||
}
|
||||
3
src/test/resources/sql/delete-all-data.sql
Normal file
3
src/test/resources/sql/delete-all-data.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
|
||||
delete from `posts` where 1;
|
||||
delete from `users` where 1;
|
||||
5
src/test/resources/sql/post-controller-test-data.sql
Normal file
5
src/test/resources/sql/post-controller-test-data.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
insert into `users` (`id`, `email`, `nickname`, `address`, `certification_code`, `status`, `last_login_at`)
|
||||
values (1, 'kok202@naver.com', 'kok202', 'Seoul', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'ACTIVE', 0);
|
||||
insert into `posts` (`id`, `content`, `created_at`, `modified_at`, `user_id`)
|
||||
values (1, 'helloworld', 1678530673958, 0, 1);
|
||||
@@ -0,0 +1,3 @@
|
||||
|
||||
insert into `users` (`id`, `email`, `nickname`, `address`, `certification_code`, `status`, `last_login_at`)
|
||||
values (1, 'kok202@naver.com', 'kok202', 'Seoul', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'ACTIVE', 0);
|
||||
7
src/test/resources/sql/post-service-test-data.sql
Normal file
7
src/test/resources/sql/post-service-test-data.sql
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
insert into `users` (`id`, `email`, `nickname`, `address`, `certification_code`, `status`, `last_login_at`)
|
||||
values (1, 'kok202@naver.com', 'kok202', 'Seoul', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'ACTIVE', 0);
|
||||
insert into `users` (`id`, `email`, `nickname`, `address`, `certification_code`, `status`, `last_login_at`)
|
||||
values (2, 'kok303@naver.com', 'kok303', 'Seoul', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab', 'PENDING', 0);
|
||||
insert into `posts` (`id`, `content`, `created_at`, `modified_at`, `user_id`)
|
||||
values (1, 'helloworld', 1678530673958, 0, 1);
|
||||
5
src/test/resources/sql/user-controller-test-data.sql
Normal file
5
src/test/resources/sql/user-controller-test-data.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
insert into `users` (`id`, `email`, `nickname`, `address`, `certification_code`, `status`, `last_login_at`)
|
||||
values (1, 'kok202@naver.com', 'kok202', 'Seoul', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'ACTIVE', 0);
|
||||
insert into `users` (`id`, `email`, `nickname`, `address`, `certification_code`, `status`, `last_login_at`)
|
||||
values (2, 'kok303@naver.com', 'kok303', 'Seoul', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab', 'PENDING', 0);
|
||||
3
src/test/resources/sql/user-repository-test-data.sql
Normal file
3
src/test/resources/sql/user-repository-test-data.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
|
||||
insert into `users` (`id`, `email`, `nickname`, `address`, `certification_code`, `status`, `last_login_at`)
|
||||
values (1, 'kok202@naver.com', 'kok202', 'Seoul', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'ACTIVE', 0);
|
||||
5
src/test/resources/sql/user-service-test-data.sql
Normal file
5
src/test/resources/sql/user-service-test-data.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
insert into `users` (`id`, `email`, `nickname`, `address`, `certification_code`, `status`, `last_login_at`)
|
||||
values (1, 'kok202@naver.com', 'kok202', 'Seoul', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'ACTIVE', 0);
|
||||
insert into `users` (`id`, `email`, `nickname`, `address`, `certification_code`, `status`, `last_login_at`)
|
||||
values (2, 'kok303@naver.com', 'kok303', 'Seoul', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab', 'PENDING', 0);
|
||||
9
src/test/resources/test-application.properties
Normal file
9
src/test/resources/test-application.properties
Normal file
@@ -0,0 +1,9 @@
|
||||
spring.datasource.url=jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_DELAY=-1
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.h2.console.enabled=true
|
||||
|
||||
spring.jpa.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
Reference in New Issue
Block a user