From 1b8f74e7d81714e05d023753bc89cb15040a21fc Mon Sep 17 00:00:00 2001 From: wlgns12370 Date: Sun, 7 Apr 2024 13:46:07 +0900 Subject: [PATCH 1/3] =?UTF-8?q?GETP-113=20feat:=20=ED=94=BC=ED=94=8C=20?= =?UTF-8?q?=EC=A2=8B=EC=95=84=EC=9A=94=20=EC=B7=A8=EC=86=8C=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PeopleLikeController.java | 18 ++++++++++++++++++ .../people/exception/PeopleLikeErrorCode.java | 3 ++- .../repository/PeopleLikeRepository.java | 3 +++ .../people/service/PeopleLikeService.java | 16 ++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/main/java/es/princip/getp/domain/people/controller/PeopleLikeController.java b/src/main/java/es/princip/getp/domain/people/controller/PeopleLikeController.java index 361e3897..882eae50 100644 --- a/src/main/java/es/princip/getp/domain/people/controller/PeopleLikeController.java +++ b/src/main/java/es/princip/getp/domain/people/controller/PeopleLikeController.java @@ -4,6 +4,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -37,4 +38,21 @@ public ResponseEntity> like( return ResponseEntity.status(HttpStatus.CREATED) .body(ApiResponse.success(HttpStatus.CREATED)); } + + /** + * 피플 좋아요 취소 + * + * @param peopleId 좋아요를 취소할 피플 ID + * @param principalDetails 로그인한 사용자 정보 + */ + @DeleteMapping("/{peopleId}/likes") + @PreAuthorize("hasRole('CLIENT') and isAuthenticated()") + public ResponseEntity> unlike( + @PathVariable Long peopleId, + @AuthenticationPrincipal PrincipalDetails principalDetails + ) { + peopleLikeService.unlike(principalDetails.getMember().getMemberId(), peopleId); + return ResponseEntity.status(HttpStatus.OK) + .body(ApiResponse.success(HttpStatus.OK)); + } } diff --git a/src/main/java/es/princip/getp/domain/people/exception/PeopleLikeErrorCode.java b/src/main/java/es/princip/getp/domain/people/exception/PeopleLikeErrorCode.java index 042e4f13..35415d2d 100644 --- a/src/main/java/es/princip/getp/domain/people/exception/PeopleLikeErrorCode.java +++ b/src/main/java/es/princip/getp/domain/people/exception/PeopleLikeErrorCode.java @@ -6,7 +6,8 @@ import es.princip.getp.global.exception.ErrorDescription; public enum PeopleLikeErrorCode implements ErrorCode { - ALREADY_LIKED(HttpStatus.CONFLICT, "이미 좋아요를 누른 피플입니다."); + ALREADY_LIKED(HttpStatus.CONFLICT, "이미 좋아요를 누른 피플입니다."), + PEOPLE_LIKE_NOT_FOUND(HttpStatus.NOT_FOUND, "좋아요를 누른 피플을 찾을 수 없습니다."); private final HttpStatus status; private final ErrorDescription description; diff --git a/src/main/java/es/princip/getp/domain/people/repository/PeopleLikeRepository.java b/src/main/java/es/princip/getp/domain/people/repository/PeopleLikeRepository.java index 7cce8fe8..81a366fb 100644 --- a/src/main/java/es/princip/getp/domain/people/repository/PeopleLikeRepository.java +++ b/src/main/java/es/princip/getp/domain/people/repository/PeopleLikeRepository.java @@ -1,9 +1,12 @@ package es.princip.getp.domain.people.repository; import es.princip.getp.domain.people.domain.entity.PeopleLike; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; public interface PeopleLikeRepository extends JpaRepository { boolean existsByClient_ClientIdAndPeople_PeopleId(Long clientId, Long peopleId); + + Optional findByPeople_PeopleIdAndClient_ClientId(Long peopleId, Long clientId); } \ No newline at end of file diff --git a/src/main/java/es/princip/getp/domain/people/service/PeopleLikeService.java b/src/main/java/es/princip/getp/domain/people/service/PeopleLikeService.java index 1aebefdf..b9aec4fb 100644 --- a/src/main/java/es/princip/getp/domain/people/service/PeopleLikeService.java +++ b/src/main/java/es/princip/getp/domain/people/service/PeopleLikeService.java @@ -1,5 +1,7 @@ package es.princip.getp.domain.people.service; +import java.util.Optional; + import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -20,6 +22,14 @@ public class PeopleLikeService { private final ClientService clientService; private final PeopleLikeRepository peopleLikeRepository; + private PeopleLike get(Optional peopleLike) { + return peopleLike.orElseThrow(() -> new BusinessLogicException(PeopleLikeErrorCode.PEOPLE_LIKE_NOT_FOUND)); + } + + private PeopleLike get(Long memberId, Long peopleId) { + return get(peopleLikeRepository.findByPeople_PeopleIdAndClient_ClientId(memberId, peopleId)); + } + private void checkPeopleIsAlreadyLiked(Long clientId, Long peopleId) { if (peopleLikeRepository.existsByClient_ClientIdAndPeople_PeopleId(clientId, peopleId)) { throw new BusinessLogicException(PeopleLikeErrorCode.ALREADY_LIKED); @@ -38,4 +48,10 @@ public void like(Long memberId, Long peopleId) { .build() ); } + + @Transactional + public void unlike(Long memberId, Long peopleId) { + PeopleLike peopleLike = get(memberId, peopleId); + peopleLikeRepository.delete(peopleLike); + } } From 0081a057be2ca5eb42978f4c0ed926b719a433c9 Mon Sep 17 00:00:00 2001 From: wlgns12370 Date: Sun, 7 Apr 2024 14:33:29 +0900 Subject: [PATCH 2/3] =?UTF-8?q?GETP-113=20test:=20=ED=94=BC=ED=94=8C=20?= =?UTF-8?q?=EC=A2=8B=EC=95=84=EC=9A=94=20=EC=B7=A8=EC=86=8C=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../people/service/PeopleLikeService.java | 2 +- .../controller/ProjectLikeControllerTest.java | 33 +++++++++++++++++++ .../people/service/PeopleLikeServiceTest.java | 29 ++++++++++++++++ .../getp/fixture/PeopleLikeFixture.java | 14 ++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/test/java/es/princip/getp/fixture/PeopleLikeFixture.java diff --git a/src/main/java/es/princip/getp/domain/people/service/PeopleLikeService.java b/src/main/java/es/princip/getp/domain/people/service/PeopleLikeService.java index b9aec4fb..f489908f 100644 --- a/src/main/java/es/princip/getp/domain/people/service/PeopleLikeService.java +++ b/src/main/java/es/princip/getp/domain/people/service/PeopleLikeService.java @@ -26,7 +26,7 @@ private PeopleLike get(Optional peopleLike) { return peopleLike.orElseThrow(() -> new BusinessLogicException(PeopleLikeErrorCode.PEOPLE_LIKE_NOT_FOUND)); } - private PeopleLike get(Long memberId, Long peopleId) { + public PeopleLike get(Long memberId, Long peopleId) { return get(peopleLikeRepository.findByPeople_PeopleIdAndClient_ClientId(memberId, peopleId)); } diff --git a/src/test/java/es/princip/getp/domain/people/controller/ProjectLikeControllerTest.java b/src/test/java/es/princip/getp/domain/people/controller/ProjectLikeControllerTest.java index 9a36994b..afebd996 100644 --- a/src/test/java/es/princip/getp/domain/people/controller/ProjectLikeControllerTest.java +++ b/src/test/java/es/princip/getp/domain/people/controller/ProjectLikeControllerTest.java @@ -4,6 +4,7 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.willDoNothing; import static org.mockito.BDDMockito.willThrow; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -90,5 +91,37 @@ void like_WhenPeopleIsNotFound_ShouldBeFailed() throws Exception { .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); } + + @DisplayName("의뢰자는 피플에게 좋아요 취소를 할 수 있다.") + @WithCustomMockUser(memberType = MemberType.ROLE_CLIENT) + @Test + void unlike() throws Exception { + willDoNothing().given(peopleLikeService).unlike(any(), eq(peopleId)); + + mockMvc.perform(delete("/people/{peopleId}/likes", peopleId) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } + + @DisplayName("의뢰자는 이미 좋아요를 누른 피플에게 다시 좋아요를 취소할 수 없다.") + @WithCustomMockUser(memberType = MemberType.ROLE_CLIENT) + @Test + void unlike_WhenPeopleIsAlreadyLiked_ShouldBeFailed() throws Exception { + willThrow(new BusinessLogicException(PeopleLikeErrorCode.ALREADY_LIKED)) + .given(peopleLikeService).unlike(any(), eq(peopleId)); + + mockMvc.perform(delete("/people/{peopleId}/likes", peopleId) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isConflict()); + } + + @DisplayName("피플은 피플에게 좋아요를 취소할 수 없다.") + @WithCustomMockUser(memberType = MemberType.ROLE_PEOPLE) + @Test + void unlike_WhenMemberTypeIsPeople_ShouldBeFailed() throws Exception { + mockMvc.perform(delete("/people/{peopleId}/likes", peopleId) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isForbidden()); + } } } \ No newline at end of file diff --git a/src/test/java/es/princip/getp/domain/people/service/PeopleLikeServiceTest.java b/src/test/java/es/princip/getp/domain/people/service/PeopleLikeServiceTest.java index ebd67796..a1db8f33 100644 --- a/src/test/java/es/princip/getp/domain/people/service/PeopleLikeServiceTest.java +++ b/src/test/java/es/princip/getp/domain/people/service/PeopleLikeServiceTest.java @@ -3,16 +3,21 @@ import static es.princip.getp.fixture.MemberFixture.createMember; import static es.princip.getp.fixture.PeopleFixture.createPeople; import static es.princip.getp.fixture.ClientFixture.createClient; +import static es.princip.getp.fixture.PeopleLikeFixture.creatPeopleLike; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.BDDAssertions.catchThrowableOfType; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; + +import java.util.Optional; + import es.princip.getp.domain.client.domain.entity.Client; import es.princip.getp.domain.client.service.ClientService; import es.princip.getp.domain.member.domain.entity.Member; import es.princip.getp.domain.people.domain.entity.People; +import es.princip.getp.domain.people.domain.entity.PeopleLike; import es.princip.getp.domain.people.exception.PeopleErrorCode; import es.princip.getp.domain.people.exception.PeopleLikeErrorCode; import es.princip.getp.domain.people.repository.PeopleLikeRepository; @@ -50,6 +55,7 @@ class Like { private final People people = createPeople(member); private final Client client = createClient(); + private final PeopleLike peopleLike = creatPeopleLike(client, people); @DisplayName("피플에게 좋아요를 누른다.") @Test @@ -93,5 +99,28 @@ void like_WhenPeopleIsNotFound_ShouldThrowException() { BusinessLogicException.class); assertThat(exception.getErrorCode()).isEqualTo(PeopleErrorCode.PEOPLE_NOT_FOUND); } + + @DisplayName("피플에게 좋아요를 취소한다.") + @Test + void unlike() { + given(peopleLikeRepository.findByPeople_PeopleIdAndClient_ClientId(memberId, peopleId)).willReturn(Optional.of(peopleLike)); + + peopleLikeService.unlike(memberId, peopleId); + + verify(peopleLikeRepository, times(1)).delete(peopleLike); + } + + @DisplayName("좋아요를 누르지 않은 피플에게 의뢰자가 좋아요를 취소할 경우 예외를 던진다.") + @Test + void unlike_ThrowExceptionWhenLikerCancelsLikeForPeopleNotLiked() { + given(peopleLikeRepository.findByPeople_PeopleIdAndClient_ClientId(peopleId, memberId)).willThrow( + new BusinessLogicException(PeopleLikeErrorCode.PEOPLE_LIKE_NOT_FOUND) + ); + + BusinessLogicException exception = + catchThrowableOfType(() -> peopleLikeService.unlike(memberId, peopleId), + BusinessLogicException.class); + assertThat(exception.getErrorCode()).isEqualTo(PeopleLikeErrorCode.PEOPLE_LIKE_NOT_FOUND); + } } } \ No newline at end of file diff --git a/src/test/java/es/princip/getp/fixture/PeopleLikeFixture.java b/src/test/java/es/princip/getp/fixture/PeopleLikeFixture.java new file mode 100644 index 00000000..227f45b3 --- /dev/null +++ b/src/test/java/es/princip/getp/fixture/PeopleLikeFixture.java @@ -0,0 +1,14 @@ +package es.princip.getp.fixture; + +import es.princip.getp.domain.client.domain.entity.Client; +import es.princip.getp.domain.people.domain.entity.People; +import es.princip.getp.domain.people.domain.entity.PeopleLike; + +public class PeopleLikeFixture { + public static PeopleLike creatPeopleLike(final Client client, final People people) { + return PeopleLike.builder() + .client(client) + .people(people) + .build(); + } +} From 3caed902a802a5864649a95512934b63a497eef4 Mon Sep 17 00:00:00 2001 From: Changyu Shin Date: Thu, 11 Jul 2024 13:19:07 +0900 Subject: [PATCH 3/3] =?UTF-8?q?GET-P=20113=20feat:=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=A6=AC=EB=B7=B0=20=ED=94=BC=EB=93=9C=EB=B0=B1=20=EB=B0=98?= =?UTF-8?q?=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PeopleLikeController.java | 18 +++---- .../people/exception/PeopleLikeErrorCode.java | 5 +- .../people/service/PeopleLikeService.java | 15 +++--- ...est.java => PeopleLikeControllerTest.java} | 54 +++++++++---------- .../people/service/PeopleLikeServiceTest.java | 32 +++++------ .../getp/fixture/PeopleLikeFixture.java | 2 +- 6 files changed, 57 insertions(+), 69 deletions(-) rename src/test/java/es/princip/getp/domain/people/controller/{ProjectLikeControllerTest.java => PeopleLikeControllerTest.java} (78%) diff --git a/src/main/java/es/princip/getp/domain/people/controller/PeopleLikeController.java b/src/main/java/es/princip/getp/domain/people/controller/PeopleLikeController.java index 882eae50..080f7333 100644 --- a/src/main/java/es/princip/getp/domain/people/controller/PeopleLikeController.java +++ b/src/main/java/es/princip/getp/domain/people/controller/PeopleLikeController.java @@ -1,19 +1,15 @@ package es.princip.getp.domain.people.controller; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.security.core.annotation.AuthenticationPrincipal; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; import es.princip.getp.domain.people.service.PeopleLikeService; import es.princip.getp.global.security.details.PrincipalDetails; import es.princip.getp.global.util.ApiResponse; import es.princip.getp.global.util.ApiResponse.ApiSuccessResult; import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/people") @@ -52,7 +48,7 @@ public ResponseEntity> unlike( @AuthenticationPrincipal PrincipalDetails principalDetails ) { peopleLikeService.unlike(principalDetails.getMember().getMemberId(), peopleId); - return ResponseEntity.status(HttpStatus.OK) - .body(ApiResponse.success(HttpStatus.OK)); + return ResponseEntity.status(HttpStatus.NO_CONTENT) + .body(ApiResponse.success(HttpStatus.NO_CONTENT)); } } diff --git a/src/main/java/es/princip/getp/domain/people/exception/PeopleLikeErrorCode.java b/src/main/java/es/princip/getp/domain/people/exception/PeopleLikeErrorCode.java index 35415d2d..859f13cb 100644 --- a/src/main/java/es/princip/getp/domain/people/exception/PeopleLikeErrorCode.java +++ b/src/main/java/es/princip/getp/domain/people/exception/PeopleLikeErrorCode.java @@ -1,13 +1,12 @@ package es.princip.getp.domain.people.exception; -import org.springframework.http.HttpStatus; - import es.princip.getp.global.exception.ErrorCode; import es.princip.getp.global.exception.ErrorDescription; +import org.springframework.http.HttpStatus; public enum PeopleLikeErrorCode implements ErrorCode { ALREADY_LIKED(HttpStatus.CONFLICT, "이미 좋아요를 누른 피플입니다."), - PEOPLE_LIKE_NOT_FOUND(HttpStatus.NOT_FOUND, "좋아요를 누른 피플을 찾을 수 없습니다."); + NEVER_LIKED(HttpStatus.NOT_FOUND, "해당 피플에게 좋아요를 누른 적이 없습니다."); private final HttpStatus status; private final ErrorDescription description; diff --git a/src/main/java/es/princip/getp/domain/people/service/PeopleLikeService.java b/src/main/java/es/princip/getp/domain/people/service/PeopleLikeService.java index f489908f..15959cf0 100644 --- a/src/main/java/es/princip/getp/domain/people/service/PeopleLikeService.java +++ b/src/main/java/es/princip/getp/domain/people/service/PeopleLikeService.java @@ -1,10 +1,5 @@ package es.princip.getp.domain.people.service; -import java.util.Optional; - -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - import es.princip.getp.domain.client.domain.entity.Client; import es.princip.getp.domain.client.service.ClientService; import es.princip.getp.domain.people.domain.entity.People; @@ -13,6 +8,10 @@ import es.princip.getp.domain.people.repository.PeopleLikeRepository; import es.princip.getp.global.exception.BusinessLogicException; import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Optional; @Service @RequiredArgsConstructor @@ -23,10 +22,10 @@ public class PeopleLikeService { private final PeopleLikeRepository peopleLikeRepository; private PeopleLike get(Optional peopleLike) { - return peopleLike.orElseThrow(() -> new BusinessLogicException(PeopleLikeErrorCode.PEOPLE_LIKE_NOT_FOUND)); + return peopleLike.orElseThrow(() -> new BusinessLogicException(PeopleLikeErrorCode.NEVER_LIKED)); } - public PeopleLike get(Long memberId, Long peopleId) { + public PeopleLike getByMemberIdAndPeopleId(Long memberId, Long peopleId) { return get(peopleLikeRepository.findByPeople_PeopleIdAndClient_ClientId(memberId, peopleId)); } @@ -51,7 +50,7 @@ public void like(Long memberId, Long peopleId) { @Transactional public void unlike(Long memberId, Long peopleId) { - PeopleLike peopleLike = get(memberId, peopleId); + PeopleLike peopleLike = getByMemberIdAndPeopleId(memberId, peopleId); peopleLikeRepository.delete(peopleLike); } } diff --git a/src/test/java/es/princip/getp/domain/people/controller/ProjectLikeControllerTest.java b/src/test/java/es/princip/getp/domain/people/controller/PeopleLikeControllerTest.java similarity index 78% rename from src/test/java/es/princip/getp/domain/people/controller/ProjectLikeControllerTest.java rename to src/test/java/es/princip/getp/domain/people/controller/PeopleLikeControllerTest.java index afebd996..30a968c0 100644 --- a/src/test/java/es/princip/getp/domain/people/controller/ProjectLikeControllerTest.java +++ b/src/test/java/es/princip/getp/domain/people/controller/PeopleLikeControllerTest.java @@ -1,13 +1,5 @@ package es.princip.getp.domain.people.controller; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.BDDMockito.willDoNothing; -import static org.mockito.BDDMockito.willThrow; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - import es.princip.getp.domain.member.domain.enums.MemberType; import es.princip.getp.domain.people.exception.PeopleErrorCode; import es.princip.getp.domain.people.exception.PeopleLikeErrorCode; @@ -30,12 +22,20 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.BDDMockito.willDoNothing; +import static org.mockito.BDDMockito.willThrow; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + @RequiredArgsConstructor @WebMvcTest(PeopleLikeController.class) @Import({SecurityConfig.class, SecurityTestConfig.class}) @ActiveProfiles("test") @ExtendWith(PrincipalDetailsParameterResolver.class) -public class ProjectLikeControllerTest { +public class PeopleLikeControllerTest { @Autowired private MockMvc mockMvc; @@ -55,11 +55,11 @@ void like() throws Exception { willDoNothing().given(peopleLikeService).like(any(), eq(peopleId)); mockMvc.perform(post("/people/{peopleId}/likes", peopleId) - .contentType(MediaType.APPLICATION_JSON)) + .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isCreated()); } - @DisplayName("의뢰자는 이미 좋아요를 누른 피플에게 다시 좋아요를 누를 수 없다.") + @DisplayName("의뢰자는 피플에게 중복으로 좋아요를 누를 수 없다.") @WithCustomMockUser(memberType = MemberType.ROLE_CLIENT) @Test void like_WhenPeopleIsAlreadyLiked_ShouldBeFailed() throws Exception { @@ -67,7 +67,7 @@ void like_WhenPeopleIsAlreadyLiked_ShouldBeFailed() throws Exception { .given(peopleLikeService).like(any(), eq(peopleId)); mockMvc.perform(post("/people/{peopleId}/likes", peopleId) - .contentType(MediaType.APPLICATION_JSON)) + .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isConflict()); } @@ -76,7 +76,7 @@ void like_WhenPeopleIsAlreadyLiked_ShouldBeFailed() throws Exception { @Test void like_WhenMemberTypeIsPeople_ShouldBeFailed() throws Exception { mockMvc.perform(post("/people/{peopleId}/likes", peopleId) - .contentType(MediaType.APPLICATION_JSON)) + .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isForbidden()); } @@ -88,31 +88,25 @@ void like_WhenPeopleIsNotFound_ShouldBeFailed() throws Exception { .given(peopleLikeService).like(any(), eq(peopleId)); mockMvc.perform(post("/people/{peopleId}/likes", peopleId) - .contentType(MediaType.APPLICATION_JSON)) + .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); } + } - @DisplayName("의뢰자는 피플에게 좋아요 취소를 할 수 있다.") - @WithCustomMockUser(memberType = MemberType.ROLE_CLIENT) - @Test - void unlike() throws Exception { - willDoNothing().given(peopleLikeService).unlike(any(), eq(peopleId)); + @Nested + class Unlike { - mockMvc.perform(delete("/people/{peopleId}/likes", peopleId) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); - } + private final Long peopleId = 1L; - @DisplayName("의뢰자는 이미 좋아요를 누른 피플에게 다시 좋아요를 취소할 수 없다.") + @DisplayName("의뢰자는 피플에게 눌렀던 좋아요를 취소를 할 수 있다.") @WithCustomMockUser(memberType = MemberType.ROLE_CLIENT) @Test - void unlike_WhenPeopleIsAlreadyLiked_ShouldBeFailed() throws Exception { - willThrow(new BusinessLogicException(PeopleLikeErrorCode.ALREADY_LIKED)) - .given(peopleLikeService).unlike(any(), eq(peopleId)); + void unlike() throws Exception { + willDoNothing().given(peopleLikeService).unlike(any(), eq(peopleId)); mockMvc.perform(delete("/people/{peopleId}/likes", peopleId) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isConflict()); + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNoContent()); } @DisplayName("피플은 피플에게 좋아요를 취소할 수 없다.") @@ -120,7 +114,7 @@ void unlike_WhenPeopleIsAlreadyLiked_ShouldBeFailed() throws Exception { @Test void unlike_WhenMemberTypeIsPeople_ShouldBeFailed() throws Exception { mockMvc.perform(delete("/people/{peopleId}/likes", peopleId) - .contentType(MediaType.APPLICATION_JSON)) + .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isForbidden()); } } diff --git a/src/test/java/es/princip/getp/domain/people/service/PeopleLikeServiceTest.java b/src/test/java/es/princip/getp/domain/people/service/PeopleLikeServiceTest.java index a1db8f33..cfef40f8 100644 --- a/src/test/java/es/princip/getp/domain/people/service/PeopleLikeServiceTest.java +++ b/src/test/java/es/princip/getp/domain/people/service/PeopleLikeServiceTest.java @@ -1,18 +1,5 @@ package es.princip.getp.domain.people.service; -import static es.princip.getp.fixture.MemberFixture.createMember; -import static es.princip.getp.fixture.PeopleFixture.createPeople; -import static es.princip.getp.fixture.ClientFixture.createClient; -import static es.princip.getp.fixture.PeopleLikeFixture.creatPeopleLike; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.BDDAssertions.catchThrowableOfType; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -import java.util.Optional; - import es.princip.getp.domain.client.domain.entity.Client; import es.princip.getp.domain.client.service.ClientService; import es.princip.getp.domain.member.domain.entity.Member; @@ -30,6 +17,19 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Optional; + +import static es.princip.getp.fixture.ClientFixture.createClient; +import static es.princip.getp.fixture.MemberFixture.createMember; +import static es.princip.getp.fixture.PeopleFixture.createPeople; +import static es.princip.getp.fixture.PeopleLikeFixture.createPeopleLike; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.BDDAssertions.catchThrowableOfType; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + @ExtendWith(MockitoExtension.class) class PeopleLikeServiceTest { @@ -55,7 +55,7 @@ class Like { private final People people = createPeople(member); private final Client client = createClient(); - private final PeopleLike peopleLike = creatPeopleLike(client, people); + private final PeopleLike peopleLike = createPeopleLike(client, people); @DisplayName("피플에게 좋아요를 누른다.") @Test @@ -114,13 +114,13 @@ void unlike() { @Test void unlike_ThrowExceptionWhenLikerCancelsLikeForPeopleNotLiked() { given(peopleLikeRepository.findByPeople_PeopleIdAndClient_ClientId(peopleId, memberId)).willThrow( - new BusinessLogicException(PeopleLikeErrorCode.PEOPLE_LIKE_NOT_FOUND) + new BusinessLogicException(PeopleLikeErrorCode.NEVER_LIKED) ); BusinessLogicException exception = catchThrowableOfType(() -> peopleLikeService.unlike(memberId, peopleId), BusinessLogicException.class); - assertThat(exception.getErrorCode()).isEqualTo(PeopleLikeErrorCode.PEOPLE_LIKE_NOT_FOUND); + assertThat(exception.getErrorCode()).isEqualTo(PeopleLikeErrorCode.NEVER_LIKED); } } } \ No newline at end of file diff --git a/src/test/java/es/princip/getp/fixture/PeopleLikeFixture.java b/src/test/java/es/princip/getp/fixture/PeopleLikeFixture.java index 227f45b3..0705f214 100644 --- a/src/test/java/es/princip/getp/fixture/PeopleLikeFixture.java +++ b/src/test/java/es/princip/getp/fixture/PeopleLikeFixture.java @@ -5,7 +5,7 @@ import es.princip.getp.domain.people.domain.entity.PeopleLike; public class PeopleLikeFixture { - public static PeopleLike creatPeopleLike(final Client client, final People people) { + public static PeopleLike createPeopleLike(final Client client, final People people) { return PeopleLike.builder() .client(client) .people(people)