From 26ff537d24eb2b7ad735824f04bc11a2182ec9f0 Mon Sep 17 00:00:00 2001 From: shoeone96 Date: Mon, 8 Jan 2024 22:54:43 +0900 Subject: [PATCH 01/14] =?UTF-8?q?feat:=20like=20schema=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mysql/schema.sql | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mysql/schema.sql b/mysql/schema.sql index 642b48a9..489da2aa 100644 --- a/mysql/schema.sql +++ b/mysql/schema.sql @@ -46,3 +46,19 @@ CREATE TABLE posts FOREIGN KEY (user_id) REFERENCES users (id) ); + +CREATE TABLE likes +( + id BIGINT AUTO_INCREMENT, + user_id BIGINT NOT NULL, + post_id BIGINT NOT NULL, + status TINYINT NOT NULL, + created_at DATETIME(6) NULL, + updated_at DATETIME(6) NULL, + CONSTRAINT posts_pk + PRIMARY KEY (id), + FOREIGN KEY (user_id) + REFERENCES users (id), + FOREIGN KEY (post_id) + REFERENCES posts (id), +); From 21cf1fd0bc898d488a502c2c51fd6cd4f95b2d05 Mon Sep 17 00:00:00 2001 From: shoeone96 Date: Mon, 8 Jan 2024 22:55:29 +0900 Subject: [PATCH 02/14] =?UTF-8?q?feat:=20Like=20entity=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backendoori/ootw/like/domain/Like.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/main/java/com/backendoori/ootw/like/domain/Like.java diff --git a/src/main/java/com/backendoori/ootw/like/domain/Like.java b/src/main/java/com/backendoori/ootw/like/domain/Like.java new file mode 100644 index 00000000..98f41a99 --- /dev/null +++ b/src/main/java/com/backendoori/ootw/like/domain/Like.java @@ -0,0 +1,58 @@ +package com.backendoori.ootw.like.domain; + +import com.backendoori.ootw.common.BaseEntity; +import com.backendoori.ootw.post.domain.Post; +import com.backendoori.ootw.user.domain.User; +import io.jsonwebtoken.lang.Assert; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Table(name = "likes") +@Entity +@Getter +@Builder +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Like extends BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id", nullable = false) + private User user; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "post_id", nullable = false) + private Post post; + + @Column(name = "status", columnDefinition = "tinyint") + private Boolean status; + + private Like(Long id, User user, Post post, Boolean status) { + Assert.notNull(user); + Assert.notNull(post); + Assert.notNull(status); + this.id = id; + this.user = user; + this.post = post; + this.status = status; + } + + public void updateStatus() { + this.status = !status; + } + +} From 2adc90189338108d0d3e294ce75734cb3c0e28b1 Mon Sep 17 00:00:00 2001 From: shoeone96 Date: Mon, 8 Jan 2024 22:56:32 +0900 Subject: [PATCH 03/14] =?UTF-8?q?feat:=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EC=9A=94=EC=B2=AD=EA=B3=BC=20=EC=B7=A8=EC=86=8C=EB=A5=BC=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20api=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ootw/like/controller/LikeController.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/main/java/com/backendoori/ootw/like/controller/LikeController.java diff --git a/src/main/java/com/backendoori/ootw/like/controller/LikeController.java b/src/main/java/com/backendoori/ootw/like/controller/LikeController.java new file mode 100644 index 00000000..08c762f9 --- /dev/null +++ b/src/main/java/com/backendoori/ootw/like/controller/LikeController.java @@ -0,0 +1,29 @@ +package com.backendoori.ootw.like.controller; + +import com.backendoori.ootw.like.dto.controller.LikeRequest; +import com.backendoori.ootw.like.dto.controller.LikeResponse; +import com.backendoori.ootw.like.service.LikeService; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.Authentication; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v1/likes") +public class LikeController { + + private final LikeService likeService; + + @PostMapping("") + public ResponseEntity pushLike(Authentication authentication, + @Valid @RequestBody LikeRequest requestDto){ + Long userId = (Long) authentication.getPrincipal(); + return ResponseEntity.ok(likeService.requestLike(userId, requestDto.postId())); + } + +} From be4aca6b9f849508d7698a2e82a9a24365dff034 Mon Sep 17 00:00:00 2001 From: shoeone96 Date: Mon, 8 Jan 2024 22:56:52 +0900 Subject: [PATCH 04/14] =?UTF-8?q?feat:=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EC=9A=94=EC=B2=AD=EA=B3=BC=20=EC=82=AD=EC=A0=9C=20=EC=8B=9C=20?= =?UTF-8?q?=EC=A0=84=EB=8B=AC=EB=90=98=EB=8A=94=20=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=84=B0=20=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ootw/like/dto/controller/LikeRequest.java | 14 +++++++++++++ .../like/dto/controller/LikeResponse.java | 20 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/main/java/com/backendoori/ootw/like/dto/controller/LikeRequest.java create mode 100644 src/main/java/com/backendoori/ootw/like/dto/controller/LikeResponse.java diff --git a/src/main/java/com/backendoori/ootw/like/dto/controller/LikeRequest.java b/src/main/java/com/backendoori/ootw/like/dto/controller/LikeRequest.java new file mode 100644 index 00000000..f63bc7dd --- /dev/null +++ b/src/main/java/com/backendoori/ootw/like/dto/controller/LikeRequest.java @@ -0,0 +1,14 @@ +package com.backendoori.ootw.like.dto.controller; + +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; + +public record LikeRequest( + @Min(value = 1) + @NotNull(message = NULL_MESSAGE) + Long postId +){ + + private static final String NULL_MESSAGE = "반드시 postId 값이 null 입니다."; + +} diff --git a/src/main/java/com/backendoori/ootw/like/dto/controller/LikeResponse.java b/src/main/java/com/backendoori/ootw/like/dto/controller/LikeResponse.java new file mode 100644 index 00000000..e5c9bc9f --- /dev/null +++ b/src/main/java/com/backendoori/ootw/like/dto/controller/LikeResponse.java @@ -0,0 +1,20 @@ +package com.backendoori.ootw.like.dto.controller; + +import com.backendoori.ootw.like.domain.Like; + +public record LikeResponse( + Long likeId, + Long userId, + Long postId, + boolean status + ) { + + public static LikeResponse from(Like like) { + return new LikeResponse( + like.getId(), + like.getUser().getId(), + like.getPost().getId(), + like.getStatus()); + } + +} From 3efe23f793404d34998663edc89a9bca4503d44e Mon Sep 17 00:00:00 2001 From: shoeone96 Date: Mon, 8 Jan 2024 22:57:13 +0900 Subject: [PATCH 05/14] =?UTF-8?q?feat:=20=EC=A2=8B=EC=95=84=EC=9A=94=20db?= =?UTF-8?q?=20=EC=97=B0=EA=B2=B0=EC=9D=84=20=EC=9C=84=ED=95=9C=20repositor?= =?UTF-8?q?y=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ootw/like/repository/LikeRepository.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/main/java/com/backendoori/ootw/like/repository/LikeRepository.java diff --git a/src/main/java/com/backendoori/ootw/like/repository/LikeRepository.java b/src/main/java/com/backendoori/ootw/like/repository/LikeRepository.java new file mode 100644 index 00000000..a7151797 --- /dev/null +++ b/src/main/java/com/backendoori/ootw/like/repository/LikeRepository.java @@ -0,0 +1,13 @@ +package com.backendoori.ootw.like.repository; + +import java.util.Optional; +import com.backendoori.ootw.like.domain.Like; +import com.backendoori.ootw.post.domain.Post; +import com.backendoori.ootw.user.domain.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface LikeRepository extends JpaRepository { + + Optional findByUserAndPost(User user, Post post); + +} From 73e4061fb85ed2884f260c23263e26801c3855c8 Mon Sep 17 00:00:00 2001 From: shoeone96 Date: Mon, 8 Jan 2024 22:57:31 +0900 Subject: [PATCH 06/14] =?UTF-8?q?feat:=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=20=EB=A1=9C=EC=A7=81=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ootw/like/service/LikeService.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/main/java/com/backendoori/ootw/like/service/LikeService.java diff --git a/src/main/java/com/backendoori/ootw/like/service/LikeService.java b/src/main/java/com/backendoori/ootw/like/service/LikeService.java new file mode 100644 index 00000000..28330472 --- /dev/null +++ b/src/main/java/com/backendoori/ootw/like/service/LikeService.java @@ -0,0 +1,60 @@ +package com.backendoori.ootw.like.service; + +import java.util.NoSuchElementException; +import com.backendoori.ootw.exception.UserNotFoundException; +import com.backendoori.ootw.like.domain.Like; +import com.backendoori.ootw.like.dto.controller.LikeResponse; +import com.backendoori.ootw.like.repository.LikeRepository; +import com.backendoori.ootw.post.domain.Post; +import com.backendoori.ootw.post.repository.PostRepository; +import com.backendoori.ootw.user.domain.User; +import com.backendoori.ootw.user.repository.UserRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.jetbrains.annotations.NotNull; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class LikeService { + + private static final String POST_NOT_FOUND_MESSAGE = "해당 게시글이 존재하지 않습니다."; + private static final String LIKE_NOT_FOUND_MESSAGE = "찾으시는 좋아요 정보가 존재하지 않습니다."; + + private final UserRepository userRepository; + private final PostRepository postRepository; + private final LikeRepository likeRepository; + + @Transactional + public LikeResponse requestLike(Long userId, Long postId) { + + User user = userRepository.findById(userId) + .orElseThrow(UserNotFoundException::new); + + Post post = postRepository.findById(postId) + .orElseThrow(() -> new NoSuchElementException(POST_NOT_FOUND_MESSAGE)); + + likeRepository.findByUserAndPost(user, post).ifPresentOrElse( + Like::updateStatus, + likeNotExist(user, post) + ); + + Like like = likeRepository.findByUserAndPost(user, post) + .orElseThrow(() -> new NoSuchElementException(LIKE_NOT_FOUND_MESSAGE)); + + return LikeResponse.from(like); + } + + @NotNull + private Runnable likeNotExist(User user, Post post) { + return () -> { + likeRepository.save( + Like.builder() + .user(user) + .post(post) + .status(true) + .build()); + }; + } + +} From 48706da8c2fd8a62cd225c55ed0f84c1294ab507 Mon Sep 17 00:00:00 2001 From: shoeone96 Date: Mon, 8 Jan 2024 23:08:12 +0900 Subject: [PATCH 07/14] =?UTF-8?q?test:=20like=20=EC=9A=94=EC=B2=AD=20api?= =?UTF-8?q?=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../like/controller/LikeControllerTest.java | 194 ++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 src/test/java/com/backendoori/ootw/like/controller/LikeControllerTest.java diff --git a/src/test/java/com/backendoori/ootw/like/controller/LikeControllerTest.java b/src/test/java/com/backendoori/ootw/like/controller/LikeControllerTest.java new file mode 100644 index 00000000..9c9e7c7d --- /dev/null +++ b/src/test/java/com/backendoori/ootw/like/controller/LikeControllerTest.java @@ -0,0 +1,194 @@ +package com.backendoori.ootw.like.controller; + +import static com.backendoori.ootw.security.jwt.JwtAuthenticationFilter.TOKEN_HEADER; +import static com.backendoori.ootw.security.jwt.JwtAuthenticationFilter.TOKEN_PREFIX; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.backendoori.ootw.like.domain.Like; +import com.backendoori.ootw.like.dto.controller.LikeRequest; +import com.backendoori.ootw.like.repository.LikeRepository; +import com.backendoori.ootw.post.controller.PostController; +import com.backendoori.ootw.post.domain.Post; +import com.backendoori.ootw.post.dto.PostSaveRequest; +import com.backendoori.ootw.post.repository.PostRepository; +import com.backendoori.ootw.post.service.PostService; +import com.backendoori.ootw.security.TokenMockMvcTest; +import com.backendoori.ootw.user.domain.User; +import com.backendoori.ootw.user.repository.UserRepository; +import com.backendoori.ootw.weather.domain.Weather; +import com.backendoori.ootw.weather.dto.WeatherDto; +import jakarta.transaction.Transactional; +import net.datafaker.Faker; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.NullSource; +import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; + +@SpringBootTest +@ExtendWith(MockitoExtension.class) +class LikeControllerTest extends TokenMockMvcTest { + + static Faker faker = new Faker(); + + User user; + User writer; + + Post post; + + @Autowired + PostController postController; + + @Autowired + PostService postService; + + @Autowired + PostRepository postRepository; + + @Autowired + UserRepository userRepository; + + @Autowired + LikeRepository likeRepository; + + + @BeforeEach + void setup() { + likeRepository.deleteAll(); + postRepository.deleteAll(); + userRepository.deleteAll(); + user = userRepository.save(generateUser()); + writer = userRepository.save(generateUser()); + post = postRepository.save(generatePost(writer)); + setToken(user.getId()); + } + + @AfterEach + void cleanup() { + likeRepository.deleteAll(); + postRepository.deleteAll(); + userRepository.deleteAll(); + } + + private User generateUser() { + return User.builder() + .email(faker.internet().emailAddress()) + .password(faker.internet().password()) + .nickname(faker.internet().username()) + .image(faker.internet().url()) + .build(); + } + + private Post generatePost(User user) { + PostSaveRequest postSaveRequest = + new PostSaveRequest("title", faker.gameOfThrones().quote(), weatherDtoGenerator()); + return Post.from(user, postSaveRequest, faker.internet().url()); + } + + private WeatherDto weatherDtoGenerator() { + return new WeatherDto(0.0, -10.0, 10.0, 1, 1); + } + + @Test + @Transactional + @DisplayName("정상적으로 좋아요를 누르면 저장에 성공한다.") + public void likeSuccess() throws Exception { + //given + LikeRequest request = new LikeRequest(post.getId()); + + //when + mockMvc.perform(post("http://localhost:8080/api/v1/likes") + .content(objectMapper.writeValueAsBytes(request)) + .header(TOKEN_HEADER, TOKEN_PREFIX + token) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + ).andExpect(status().isOk()) + .andDo(print()); + + //then + Like byUserAndPost = likeRepository.findByUserAndPost(user, post).get(); + + Assertions.assertThat(byUserAndPost.getPost()).isEqualTo(post); + Assertions.assertThat(byUserAndPost.getUser()).isEqualTo(user); + Assertions.assertThat(byUserAndPost.getStatus()).isEqualTo(true); + + } + + @Test + @Transactional + @DisplayName("이미 좋아요를 누른 경우 좋아요가 취소된다.") + public void likeCancel() throws Exception { + //given + Like like = Like.builder().user(user).post(post).status(true).build(); + likeRepository.save(like); + + LikeRequest request = new LikeRequest(post.getId()); + + //when + mockMvc.perform(post("http://localhost:8080/api/v1/likes") + .content(objectMapper.writeValueAsBytes(request)) + .header(TOKEN_HEADER, TOKEN_PREFIX + token) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + ).andExpect(status().isOk()) + .andDo(print()); + + //then + Like byUserAndPost = likeRepository.findByUserAndPost(user, post).get(); + + Assertions.assertThat(byUserAndPost.getPost()).isEqualTo(post); + Assertions.assertThat(byUserAndPost.getUser()).isEqualTo(user); + Assertions.assertThat(byUserAndPost.getStatus()).isEqualTo(false); + + } + + @ParameterizedTest + @NullSource + @ValueSource(longs = {-1L, 0}) + @DisplayName("유효하지 않은 postId 로 좋아요를 요청하면 실패한다.") + public void likeFailPostNotFound(Long postId) throws Exception { + //given + LikeRequest request = new LikeRequest(postId); + + //when //then + mockMvc.perform(post("http://localhost:8080/api/v1/likes") + .content(objectMapper.writeValueAsBytes(request)) + .header(TOKEN_HEADER, TOKEN_PREFIX + token) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + ).andExpect(status().isBadRequest()) + .andDo(print()); + + } + + @ParameterizedTest + @ValueSource(longs = Long.MAX_VALUE) + @DisplayName("존재하지 않는 post 에 좋아요를 요청하면 실패한다.") + public void likeFailNullPostId(Long postId) throws Exception { + //given + LikeRequest request = new LikeRequest(postId); + + //when //then + mockMvc.perform(post("http://localhost:8080/api/v1/likes") + .content(objectMapper.writeValueAsBytes(request)) + .header(TOKEN_HEADER, TOKEN_PREFIX + token) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + ).andExpect(status().isNotFound()) + .andDo(print()); + + } + +} From 76b2cf64ece2f75a3f9a8137057e4c622577061d Mon Sep 17 00:00:00 2001 From: shoeone96 Date: Mon, 8 Jan 2024 23:08:26 +0900 Subject: [PATCH 08/14] =?UTF-8?q?feat:=20like=20=EC=9A=94=EC=B2=AD=20?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=20=EB=A1=9C=EC=A7=81=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ootw/like/service/LikeServiceTest.java | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java diff --git a/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java b/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java new file mode 100644 index 00000000..416d5868 --- /dev/null +++ b/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java @@ -0,0 +1,167 @@ +package com.backendoori.ootw.like.service; + +import static org.assertj.core.api.Assertions.*; + +import java.util.NoSuchElementException; +import com.backendoori.ootw.exception.UserNotFoundException; +import com.backendoori.ootw.like.domain.Like; +import com.backendoori.ootw.like.dto.controller.LikeRequest; +import com.backendoori.ootw.like.dto.controller.LikeResponse; +import com.backendoori.ootw.like.repository.LikeRepository; +import com.backendoori.ootw.post.controller.PostController; +import com.backendoori.ootw.post.domain.Post; +import com.backendoori.ootw.post.dto.PostSaveRequest; +import com.backendoori.ootw.post.repository.PostRepository; +import com.backendoori.ootw.post.service.PostService; +import com.backendoori.ootw.security.TokenMockMvcTest; +import com.backendoori.ootw.user.domain.User; +import com.backendoori.ootw.user.repository.UserRepository; +import com.backendoori.ootw.weather.domain.Weather; +import com.backendoori.ootw.weather.dto.WeatherDto; +import net.datafaker.Faker; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class LikeServiceTest extends TokenMockMvcTest { + + private static final String POST_NOT_FOUND_MESSAGE = "해당 게시글이 존재하지 않습니다."; + static Faker faker = new Faker(); + + User user; + + User writer; + + Post post; + + @Autowired + PostController postController; + + @Autowired + PostService postService; + + @Autowired + PostRepository postRepository; + + @Autowired + UserRepository userRepository; + + @Autowired + LikeRepository likeRepository; + + @Autowired + LikeService likeService; + + + @BeforeEach + void setup() { + likeRepository.deleteAll(); + postRepository.deleteAll(); + userRepository.deleteAll(); + user = userRepository.save(generateUser()); + writer = userRepository.save(generateUser()); + post = postRepository.save(generatePost(writer)); + setToken(user.getId()); + } + + @AfterEach + void cleanup() { + likeRepository.deleteAll(); + postRepository.deleteAll(); + userRepository.deleteAll(); + } + + private User generateUser() { + return User.builder() + .email(faker.internet().emailAddress()) + .password(faker.internet().password()) + .nickname(faker.internet().username()) + .image(faker.internet().url()) + .build(); + } + + private Post generatePost(User user) { + PostSaveRequest postSaveRequest = + new PostSaveRequest("title", faker.gameOfThrones().quote(), weatherDtoGenerator()); + return Post.from(user, postSaveRequest, faker.internet().url()); + } + + private WeatherDto weatherDtoGenerator() { + return new WeatherDto(0.0, -10.0, 10.0, 1, 1); + } + + @Test + @DisplayName("정상적으로 likeDto를 받아 좋아요를 누르면 성공한다.") + public void likePostSuccess() throws Exception { + //given + Long postId = post.getId(); + Long userId = user.getId(); + + //when + LikeResponse response = likeService.requestLike(userId, postId); + + //then + assertThat(response.postId()).isEqualTo(postId); + assertThat(response.userId()).isEqualTo(userId); + assertThat(response.status()).isEqualTo(true); + + } + + @Test + @DisplayName("이미 좋아요를 누른 게시물의 경우 좋아요가 취소된다.") + public void likePostCancelSuccess() throws Exception { + //given + Like like = Like.builder().user(user).post(post).status(true).build(); + likeRepository.save(like); + Long postId = post.getId(); + Long userId = user.getId(); + + //when + LikeResponse response = likeService.requestLike(userId, postId); + + //then + assertThat(response.postId()).isEqualTo(postId); + assertThat(response.userId()).isEqualTo(userId); + assertThat(response.status()).isEqualTo(false); + + } + + @ParameterizedTest + @ValueSource(longs = {-1, 0, Long.MAX_VALUE}) + @DisplayName("존재하지 않는 게시물에 좋아요를 누르는 경우 요청에 실패한다.") + public void likePostFailWithWrongPostId(Long wrongPostId) { + //given + Long postId = wrongPostId; + Long userId = user.getId(); + + //when, then + assertThatThrownBy(() ->likeService.requestLike(userId, postId)) + .isInstanceOf(NoSuchElementException.class) + .hasMessage(POST_NOT_FOUND_MESSAGE); + + } + + @ParameterizedTest + @ValueSource(longs = {-1, 0, Long.MAX_VALUE}) + @DisplayName("존재하지 않는 유저에 좋아요를 요청한 경우 요청에 실패한다.") + public void likePostFailWithWrongUserId(Long wrongUserId) { + //given + Long postId = post.getId(); + Long userId = wrongUserId; + + //when, then + assertThatThrownBy(() ->likeService.requestLike(userId, postId)) + .isInstanceOf(UserNotFoundException.class) + .hasMessage(UserNotFoundException.DEFAULT_MESSAGE); + + } + + +} From d0b8b0a73ecd8e4d90518fa10df557b2b719266c Mon Sep 17 00:00:00 2001 From: shoeone96 Date: Mon, 8 Jan 2024 23:09:02 +0900 Subject: [PATCH 09/14] =?UTF-8?q?test:=20like=20entity=20test=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ootw/like/domain/LikeTest.java | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 src/test/java/com/backendoori/ootw/like/domain/LikeTest.java diff --git a/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java b/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java new file mode 100644 index 00000000..51dc05b9 --- /dev/null +++ b/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java @@ -0,0 +1,152 @@ +package com.backendoori.ootw.like.domain; + +import static org.assertj.core.api.Assertions.*; + +import com.backendoori.ootw.post.domain.Post; +import com.backendoori.ootw.post.dto.PostSaveRequest; +import com.backendoori.ootw.user.domain.User; +import com.backendoori.ootw.weather.dto.WeatherDto; +import net.datafaker.Faker; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullSource; + +class LikeTest { + + static Faker faker = new Faker(); + + private User generateUser() { + return User.builder() + .id((long) faker.number().positive()) + .email(faker.internet().emailAddress()) + .password(faker.internet().password()) + .nickname(faker.internet().username()) + .image(faker.internet().url()) + .build(); + } + + private Post generatePost(User user) { + PostSaveRequest postSaveRequest = + new PostSaveRequest("title", faker.gameOfThrones().quote(), weatherDtoGenerator()); + return Post.from(user, postSaveRequest, faker.internet().url()); + } + + private WeatherDto weatherDtoGenerator() { + return new WeatherDto(0.0, -10.0, 10.0, 1, 1); + } + + @Test + @DisplayName("정상적으로 Like 객체가 만들어진다.") + public void makeLikeSuccess() { + //given + User user = generateUser(); + User writer = generateUser(); + Post post = generatePost(writer); + + //when, then + assertThatCode(() -> Like.builder() + .user(user) + .post(post) + .status(true) + .build()).doesNotThrowAnyException(); + + assertThatCode(() -> Like.builder() + .user(user) + .post(post) + .status(false) + .build()).doesNotThrowAnyException(); + + } + + @ParameterizedTest + @NullSource + @DisplayName("post가 null 인 경우 좋아요 객체를 생성 시 예외가 발생한다.") + public void makeLikeFailByNullPost(Post post) { + //given + User user = generateUser(); + + //when, then + assertThatThrownBy(() -> Like.builder() + .user(user) + .post(post) + .status(true) + .build()) + .isInstanceOf(IllegalArgumentException.class); + + assertThatThrownBy(() -> Like.builder() + .user(user) + .post(post) + .status(false) + .build()) + .isInstanceOf(IllegalArgumentException.class); + + } + + @ParameterizedTest + @NullSource + @DisplayName("post가 null 인 경우 좋아요 객체를 생성 시 예외가 발생한다.") + public void makeLikeFailByNullUser(User user) { + //given + User writer = generateUser(); + Post post = generatePost(writer); + + //when, then + assertThatThrownBy(() -> Like.builder() + .user(user) + .post(post) + .status(true) + .build()) + .isInstanceOf(IllegalArgumentException.class); + + assertThatThrownBy(() -> Like.builder() + .user(user) + .post(post) + .status(false) + .build()) + .isInstanceOf(IllegalArgumentException.class); + + } + + @Test + @DisplayName("status 값을 주지 않는 경우 예외가 발생한다.") + public void makeLikeFailByNullStatus() { + //given + User user = generateUser(); + User writer = generateUser(); + Post post = generatePost(writer); + + //when, then + assertThatThrownBy(() -> Like.builder() + .user(user) + .post(post) + .build()) + .isInstanceOf(IllegalArgumentException.class); + + } + + @Test + @DisplayName("정상적으로 Like status가 업데이트 된다.") + public void statusUpdateSuccess() { + //given + User user = generateUser(); + User writer = generateUser(); + Post post = generatePost(writer); + + //when, then + Like like = Like.builder() + .user(user) + .post(post) + .status(true) + .build(); + + like.updateStatus(); + assertThat(like.getStatus()).isEqualTo(false); + + like.updateStatus(); + assertThat(like.getStatus()).isEqualTo(true); + + } + + +} From 4cf1359630d78ed404c7c472ad9164255953e897 Mon Sep 17 00:00:00 2001 From: shoeone96 Date: Mon, 8 Jan 2024 23:29:45 +0900 Subject: [PATCH 10/14] =?UTF-8?q?fix:=20sql=20error=20=EA=B5=AC=EB=AC=B8?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mysql/schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mysql/schema.sql b/mysql/schema.sql index 489da2aa..fec82711 100644 --- a/mysql/schema.sql +++ b/mysql/schema.sql @@ -60,5 +60,5 @@ CREATE TABLE likes FOREIGN KEY (user_id) REFERENCES users (id), FOREIGN KEY (post_id) - REFERENCES posts (id), + REFERENCES posts (id) ); From 8352b248e68c222301564a7627315066294eb611 Mon Sep 17 00:00:00 2001 From: LEE JUNGWON <85065626+shoeone96@users.noreply.github.com> Date: Tue, 9 Jan 2024 13:45:30 +0900 Subject: [PATCH 11/14] =?UTF-8?q?style:=20test=20DisplayName=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/com/backendoori/ootw/like/domain/LikeTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java b/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java index 51dc05b9..f5913714 100644 --- a/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java +++ b/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java @@ -85,7 +85,7 @@ public void makeLikeFailByNullPost(Post post) { @ParameterizedTest @NullSource - @DisplayName("post가 null 인 경우 좋아요 객체를 생성 시 예외가 발생한다.") + @DisplayName("user가 null 인 경우 좋아요 객체를 생성 시 예외가 발생한다.") public void makeLikeFailByNullUser(User user) { //given User writer = generateUser(); From d9dcc5f8c7a7447dd97c2451c4b6550b4765abca Mon Sep 17 00:00:00 2001 From: LEE JUNGWON <85065626+shoeone96@users.noreply.github.com> Date: Tue, 9 Jan 2024 17:37:53 +0900 Subject: [PATCH 12/14] =?UTF-8?q?refactor:=20WeatherDto=20=EC=83=81?= =?UTF-8?q?=EC=88=98=EB=A1=9C=20=EC=84=A0=EC=96=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ootw/like/controller/LikeControllerTest.java | 8 ++------ .../java/com/backendoori/ootw/like/domain/LikeTest.java | 8 +++----- .../backendoori/ootw/like/service/LikeServiceTest.java | 8 +++----- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/backendoori/ootw/like/controller/LikeControllerTest.java b/src/test/java/com/backendoori/ootw/like/controller/LikeControllerTest.java index 9c9e7c7d..56e6457b 100644 --- a/src/test/java/com/backendoori/ootw/like/controller/LikeControllerTest.java +++ b/src/test/java/com/backendoori/ootw/like/controller/LikeControllerTest.java @@ -18,7 +18,6 @@ import com.backendoori.ootw.security.TokenMockMvcTest; import com.backendoori.ootw.user.domain.User; import com.backendoori.ootw.user.repository.UserRepository; -import com.backendoori.ootw.weather.domain.Weather; import com.backendoori.ootw.weather.dto.WeatherDto; import jakarta.transaction.Transactional; import net.datafaker.Faker; @@ -29,7 +28,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.NullAndEmptySource; import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; import org.mockito.junit.jupiter.MockitoExtension; @@ -42,6 +40,7 @@ class LikeControllerTest extends TokenMockMvcTest { static Faker faker = new Faker(); + static final WeatherDto weatherDto = new WeatherDto(0.0, -10.0, 10.0, 1, 1); User user; User writer; @@ -93,13 +92,10 @@ private User generateUser() { private Post generatePost(User user) { PostSaveRequest postSaveRequest = - new PostSaveRequest("title", faker.gameOfThrones().quote(), weatherDtoGenerator()); + new PostSaveRequest("title", faker.gameOfThrones().quote(), weatherDto); return Post.from(user, postSaveRequest, faker.internet().url()); } - private WeatherDto weatherDtoGenerator() { - return new WeatherDto(0.0, -10.0, 10.0, 1, 1); - } @Test @Transactional diff --git a/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java b/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java index f5913714..9d352988 100644 --- a/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java +++ b/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java @@ -14,6 +14,8 @@ class LikeTest { + static final WeatherDto weatherDto = new WeatherDto(0.0, -10.0, 10.0, 1, 1); + static Faker faker = new Faker(); private User generateUser() { @@ -28,14 +30,10 @@ private User generateUser() { private Post generatePost(User user) { PostSaveRequest postSaveRequest = - new PostSaveRequest("title", faker.gameOfThrones().quote(), weatherDtoGenerator()); + new PostSaveRequest("title", faker.gameOfThrones().quote(), weatherDto); return Post.from(user, postSaveRequest, faker.internet().url()); } - private WeatherDto weatherDtoGenerator() { - return new WeatherDto(0.0, -10.0, 10.0, 1, 1); - } - @Test @DisplayName("정상적으로 Like 객체가 만들어진다.") public void makeLikeSuccess() { diff --git a/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java b/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java index 416d5868..bd082602 100644 --- a/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java +++ b/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java @@ -33,6 +33,8 @@ public class LikeServiceTest extends TokenMockMvcTest { private static final String POST_NOT_FOUND_MESSAGE = "해당 게시글이 존재하지 않습니다."; + static final WeatherDto weatherDto = new WeatherDto(0.0, -10.0, 10.0, 1, 1); + static Faker faker = new Faker(); User user; @@ -89,14 +91,10 @@ private User generateUser() { private Post generatePost(User user) { PostSaveRequest postSaveRequest = - new PostSaveRequest("title", faker.gameOfThrones().quote(), weatherDtoGenerator()); + new PostSaveRequest("title", faker.gameOfThrones().quote(), weatherDto); return Post.from(user, postSaveRequest, faker.internet().url()); } - private WeatherDto weatherDtoGenerator() { - return new WeatherDto(0.0, -10.0, 10.0, 1, 1); - } - @Test @DisplayName("정상적으로 likeDto를 받아 좋아요를 누르면 성공한다.") public void likePostSuccess() throws Exception { From 73c71935a214c6997d31b416a3a10eeb627ed55f Mon Sep 17 00:00:00 2001 From: LEE JUNGWON <85065626+shoeone96@users.noreply.github.com> Date: Tue, 9 Jan 2024 17:45:38 +0900 Subject: [PATCH 13/14] style: style check --- .../ootw/exception/ImageUploadException.java | 2 +- .../ootw/like/controller/LikeController.java | 2 +- .../ootw/like/dto/controller/LikeRequest.java | 2 +- .../ootw/like/dto/controller/LikeResponse.java | 2 +- .../com/backendoori/ootw/like/domain/LikeTest.java | 4 +++- .../backendoori/ootw/like/service/LikeServiceTest.java | 10 ++++------ 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/backendoori/ootw/exception/ImageUploadException.java b/src/main/java/com/backendoori/ootw/exception/ImageUploadException.java index b9646bc0..b682b9bb 100644 --- a/src/main/java/com/backendoori/ootw/exception/ImageUploadException.java +++ b/src/main/java/com/backendoori/ootw/exception/ImageUploadException.java @@ -5,7 +5,7 @@ @Getter @RequiredArgsConstructor -public class ImageUploadException extends RuntimeException{ +public class ImageUploadException extends RuntimeException { private final String message = "이미지 업로드 중 예외가 발생했습니다."; diff --git a/src/main/java/com/backendoori/ootw/like/controller/LikeController.java b/src/main/java/com/backendoori/ootw/like/controller/LikeController.java index 08c762f9..fa4bb998 100644 --- a/src/main/java/com/backendoori/ootw/like/controller/LikeController.java +++ b/src/main/java/com/backendoori/ootw/like/controller/LikeController.java @@ -21,7 +21,7 @@ public class LikeController { @PostMapping("") public ResponseEntity pushLike(Authentication authentication, - @Valid @RequestBody LikeRequest requestDto){ + @Valid @RequestBody LikeRequest requestDto) { Long userId = (Long) authentication.getPrincipal(); return ResponseEntity.ok(likeService.requestLike(userId, requestDto.postId())); } diff --git a/src/main/java/com/backendoori/ootw/like/dto/controller/LikeRequest.java b/src/main/java/com/backendoori/ootw/like/dto/controller/LikeRequest.java index f63bc7dd..198aa0db 100644 --- a/src/main/java/com/backendoori/ootw/like/dto/controller/LikeRequest.java +++ b/src/main/java/com/backendoori/ootw/like/dto/controller/LikeRequest.java @@ -7,7 +7,7 @@ public record LikeRequest( @Min(value = 1) @NotNull(message = NULL_MESSAGE) Long postId -){ +) { private static final String NULL_MESSAGE = "반드시 postId 값이 null 입니다."; diff --git a/src/main/java/com/backendoori/ootw/like/dto/controller/LikeResponse.java b/src/main/java/com/backendoori/ootw/like/dto/controller/LikeResponse.java index e5c9bc9f..168130f3 100644 --- a/src/main/java/com/backendoori/ootw/like/dto/controller/LikeResponse.java +++ b/src/main/java/com/backendoori/ootw/like/dto/controller/LikeResponse.java @@ -7,7 +7,7 @@ public record LikeResponse( Long userId, Long postId, boolean status - ) { +) { public static LikeResponse from(Like like) { return new LikeResponse( diff --git a/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java b/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java index 9d352988..d4d3abc2 100644 --- a/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java +++ b/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java @@ -1,6 +1,8 @@ package com.backendoori.ootw.like.domain; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import com.backendoori.ootw.post.domain.Post; import com.backendoori.ootw.post.dto.PostSaveRequest; diff --git a/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java b/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java index bd082602..e18b4279 100644 --- a/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java +++ b/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java @@ -1,11 +1,11 @@ package com.backendoori.ootw.like.service; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.util.NoSuchElementException; import com.backendoori.ootw.exception.UserNotFoundException; import com.backendoori.ootw.like.domain.Like; -import com.backendoori.ootw.like.dto.controller.LikeRequest; import com.backendoori.ootw.like.dto.controller.LikeResponse; import com.backendoori.ootw.like.repository.LikeRepository; import com.backendoori.ootw.post.controller.PostController; @@ -16,10 +16,8 @@ import com.backendoori.ootw.security.TokenMockMvcTest; import com.backendoori.ootw.user.domain.User; import com.backendoori.ootw.user.repository.UserRepository; -import com.backendoori.ootw.weather.domain.Weather; import com.backendoori.ootw.weather.dto.WeatherDto; import net.datafaker.Faker; -import org.assertj.core.api.Assertions; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -140,7 +138,7 @@ public void likePostFailWithWrongPostId(Long wrongPostId) { Long userId = user.getId(); //when, then - assertThatThrownBy(() ->likeService.requestLike(userId, postId)) + assertThatThrownBy(() -> likeService.requestLike(userId, postId)) .isInstanceOf(NoSuchElementException.class) .hasMessage(POST_NOT_FOUND_MESSAGE); @@ -155,7 +153,7 @@ public void likePostFailWithWrongUserId(Long wrongUserId) { Long userId = wrongUserId; //when, then - assertThatThrownBy(() ->likeService.requestLike(userId, postId)) + assertThatThrownBy(() -> likeService.requestLike(userId, postId)) .isInstanceOf(UserNotFoundException.class) .hasMessage(UserNotFoundException.DEFAULT_MESSAGE); From 153bcfcfbca154706b1408df7686f6de309b316e Mon Sep 17 00:00:00 2001 From: LEE JUNGWON <85065626+shoeone96@users.noreply.github.com> Date: Tue, 9 Jan 2024 20:23:49 +0900 Subject: [PATCH 14/14] =?UTF-8?q?style:=20=EA=B3=B5=ED=86=B5=20=EC=8A=A4?= =?UTF-8?q?=ED=83=80=EC=9D=BC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ootw/like/controller/LikeController.java | 2 +- .../ootw/like/dto/controller/LikeRequest.java | 3 ++- .../ootw/like/service/LikeService.java | 1 - .../ootw/like/controller/LikeControllerTest.java | 14 +++++++------- .../backendoori/ootw/like/domain/LikeTest.java | 16 ++++++++-------- .../ootw/like/service/LikeServiceTest.java | 14 +++++++------- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/backendoori/ootw/like/controller/LikeController.java b/src/main/java/com/backendoori/ootw/like/controller/LikeController.java index fa4bb998..f4245d52 100644 --- a/src/main/java/com/backendoori/ootw/like/controller/LikeController.java +++ b/src/main/java/com/backendoori/ootw/like/controller/LikeController.java @@ -19,7 +19,7 @@ public class LikeController { private final LikeService likeService; - @PostMapping("") + @PostMapping public ResponseEntity pushLike(Authentication authentication, @Valid @RequestBody LikeRequest requestDto) { Long userId = (Long) authentication.getPrincipal(); diff --git a/src/main/java/com/backendoori/ootw/like/dto/controller/LikeRequest.java b/src/main/java/com/backendoori/ootw/like/dto/controller/LikeRequest.java index 198aa0db..5fd8a3fb 100644 --- a/src/main/java/com/backendoori/ootw/like/dto/controller/LikeRequest.java +++ b/src/main/java/com/backendoori/ootw/like/dto/controller/LikeRequest.java @@ -2,9 +2,10 @@ import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Positive; public record LikeRequest( - @Min(value = 1) + @Positive @NotNull(message = NULL_MESSAGE) Long postId ) { diff --git a/src/main/java/com/backendoori/ootw/like/service/LikeService.java b/src/main/java/com/backendoori/ootw/like/service/LikeService.java index 28330472..6011de0c 100644 --- a/src/main/java/com/backendoori/ootw/like/service/LikeService.java +++ b/src/main/java/com/backendoori/ootw/like/service/LikeService.java @@ -27,7 +27,6 @@ public class LikeService { @Transactional public LikeResponse requestLike(Long userId, Long postId) { - User user = userRepository.findById(userId) .orElseThrow(UserNotFoundException::new); diff --git a/src/test/java/com/backendoori/ootw/like/controller/LikeControllerTest.java b/src/test/java/com/backendoori/ootw/like/controller/LikeControllerTest.java index 56e6457b..f254765c 100644 --- a/src/test/java/com/backendoori/ootw/like/controller/LikeControllerTest.java +++ b/src/test/java/com/backendoori/ootw/like/controller/LikeControllerTest.java @@ -39,7 +39,7 @@ @ExtendWith(MockitoExtension.class) class LikeControllerTest extends TokenMockMvcTest { - static Faker faker = new Faker(); + static final Faker FAKER = new Faker(); static final WeatherDto weatherDto = new WeatherDto(0.0, -10.0, 10.0, 1, 1); User user; @@ -83,17 +83,17 @@ void cleanup() { private User generateUser() { return User.builder() - .email(faker.internet().emailAddress()) - .password(faker.internet().password()) - .nickname(faker.internet().username()) - .image(faker.internet().url()) + .email(FAKER.internet().emailAddress()) + .password(FAKER.internet().password()) + .nickname(FAKER.internet().username()) + .image(FAKER.internet().url()) .build(); } private Post generatePost(User user) { PostSaveRequest postSaveRequest = - new PostSaveRequest("title", faker.gameOfThrones().quote(), weatherDto); - return Post.from(user, postSaveRequest, faker.internet().url()); + new PostSaveRequest("title", FAKER.gameOfThrones().quote(), weatherDto); + return Post.from(user, postSaveRequest, FAKER.internet().url()); } diff --git a/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java b/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java index d4d3abc2..3787f0ee 100644 --- a/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java +++ b/src/test/java/com/backendoori/ootw/like/domain/LikeTest.java @@ -18,22 +18,22 @@ class LikeTest { static final WeatherDto weatherDto = new WeatherDto(0.0, -10.0, 10.0, 1, 1); - static Faker faker = new Faker(); + static final Faker FAKER = new Faker(); private User generateUser() { return User.builder() - .id((long) faker.number().positive()) - .email(faker.internet().emailAddress()) - .password(faker.internet().password()) - .nickname(faker.internet().username()) - .image(faker.internet().url()) + .id((long) FAKER.number().positive()) + .email(FAKER.internet().emailAddress()) + .password(FAKER.internet().password()) + .nickname(FAKER.internet().username()) + .image(FAKER.internet().url()) .build(); } private Post generatePost(User user) { PostSaveRequest postSaveRequest = - new PostSaveRequest("title", faker.gameOfThrones().quote(), weatherDto); - return Post.from(user, postSaveRequest, faker.internet().url()); + new PostSaveRequest("title", FAKER.gameOfThrones().quote(), weatherDto); + return Post.from(user, postSaveRequest, FAKER.internet().url()); } @Test diff --git a/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java b/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java index e18b4279..1f519aa4 100644 --- a/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java +++ b/src/test/java/com/backendoori/ootw/like/service/LikeServiceTest.java @@ -33,7 +33,7 @@ public class LikeServiceTest extends TokenMockMvcTest { private static final String POST_NOT_FOUND_MESSAGE = "해당 게시글이 존재하지 않습니다."; static final WeatherDto weatherDto = new WeatherDto(0.0, -10.0, 10.0, 1, 1); - static Faker faker = new Faker(); + static final Faker FAKER = new Faker(); User user; @@ -80,17 +80,17 @@ void cleanup() { private User generateUser() { return User.builder() - .email(faker.internet().emailAddress()) - .password(faker.internet().password()) - .nickname(faker.internet().username()) - .image(faker.internet().url()) + .email(FAKER.internet().emailAddress()) + .password(FAKER.internet().password()) + .nickname(FAKER.internet().username()) + .image(FAKER.internet().url()) .build(); } private Post generatePost(User user) { PostSaveRequest postSaveRequest = - new PostSaveRequest("title", faker.gameOfThrones().quote(), weatherDto); - return Post.from(user, postSaveRequest, faker.internet().url()); + new PostSaveRequest("title", FAKER.gameOfThrones().quote(), weatherDto); + return Post.from(user, postSaveRequest, FAKER.internet().url()); } @Test