From 09e4ff247ca237888ff8c3c78664838b7458ae6f Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 16:12:08 +0900 Subject: [PATCH 01/24] =?UTF-8?q?[style]=20ArgumentResolver=20=EA=B0=9C?= =?UTF-8?q?=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 네이버 컨벤션에 부합하게 개행 처리 --- .../web/resolver/session/SessionCustomerArgumentResolver.java | 2 +- .../lab/web/resolver/session/SessionVendorArgumentResolver.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/camp/woowak/lab/web/resolver/session/SessionCustomerArgumentResolver.java b/src/main/java/camp/woowak/lab/web/resolver/session/SessionCustomerArgumentResolver.java index a172b231..8b0a1cfa 100644 --- a/src/main/java/camp/woowak/lab/web/resolver/session/SessionCustomerArgumentResolver.java +++ b/src/main/java/camp/woowak/lab/web/resolver/session/SessionCustomerArgumentResolver.java @@ -21,7 +21,7 @@ public boolean supportsParameter(MethodParameter parameter) { @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, - NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { + NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { AuthenticationPrincipal parameterAnnotation = parameter.getParameterAnnotation(AuthenticationPrincipal.class); if (parameterAnnotation == null) { return null; diff --git a/src/main/java/camp/woowak/lab/web/resolver/session/SessionVendorArgumentResolver.java b/src/main/java/camp/woowak/lab/web/resolver/session/SessionVendorArgumentResolver.java index c6cafbfd..eff4eb02 100644 --- a/src/main/java/camp/woowak/lab/web/resolver/session/SessionVendorArgumentResolver.java +++ b/src/main/java/camp/woowak/lab/web/resolver/session/SessionVendorArgumentResolver.java @@ -21,7 +21,7 @@ public boolean supportsParameter(MethodParameter parameter) { @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, - NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { + NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { AuthenticationPrincipal parameterAnnotation = parameter.getParameterAnnotation(AuthenticationPrincipal.class); if (parameterAnnotation == null) { return null; From ec571d283e07de6ad4c00e5015cb9a724f403a0d Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 16:12:30 +0900 Subject: [PATCH 02/24] =?UTF-8?q?[fix]=20LoginVendor=20=EB=B0=8F=20LoginCu?= =?UTF-8?q?stomer=EC=9D=98=20id=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../camp/woowak/lab/web/authentication/LoginCustomer.java | 8 +++++--- .../camp/woowak/lab/web/authentication/LoginMember.java | 2 +- .../camp/woowak/lab/web/authentication/LoginVendor.java | 8 +++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/camp/woowak/lab/web/authentication/LoginCustomer.java b/src/main/java/camp/woowak/lab/web/authentication/LoginCustomer.java index 0a55c31d..b0d57ee9 100644 --- a/src/main/java/camp/woowak/lab/web/authentication/LoginCustomer.java +++ b/src/main/java/camp/woowak/lab/web/authentication/LoginCustomer.java @@ -1,14 +1,16 @@ package camp.woowak.lab.web.authentication; +import java.util.UUID; + public class LoginCustomer implements LoginMember { - private final Long id; + private final UUID id; - public LoginCustomer(Long id) { + public LoginCustomer(UUID id) { this.id = id; } @Override - public Long getId() { + public UUID getId() { return id; } } diff --git a/src/main/java/camp/woowak/lab/web/authentication/LoginMember.java b/src/main/java/camp/woowak/lab/web/authentication/LoginMember.java index 5079e9b8..27cffece 100644 --- a/src/main/java/camp/woowak/lab/web/authentication/LoginMember.java +++ b/src/main/java/camp/woowak/lab/web/authentication/LoginMember.java @@ -1,5 +1,5 @@ package camp.woowak.lab.web.authentication; public interface LoginMember { - Long getId(); + Object getId(); } diff --git a/src/main/java/camp/woowak/lab/web/authentication/LoginVendor.java b/src/main/java/camp/woowak/lab/web/authentication/LoginVendor.java index 4ac97539..c4ad20a3 100644 --- a/src/main/java/camp/woowak/lab/web/authentication/LoginVendor.java +++ b/src/main/java/camp/woowak/lab/web/authentication/LoginVendor.java @@ -1,14 +1,16 @@ package camp.woowak.lab.web.authentication; +import java.util.UUID; + public class LoginVendor implements LoginMember { - private final Long id; + private final UUID id; - public LoginVendor(Long id) { + public LoginVendor(UUID id) { this.id = id; } @Override - public Long getId() { + public UUID getId() { return id; } } From 36fc073892bcecdcb1a74dad199971336dceb2b9 Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 16:17:42 +0900 Subject: [PATCH 03/24] =?UTF-8?q?[feat]=20POST=20/vendor/login=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EC=95=A4=EB=93=9C=20=ED=8F=AC=EC=9D=B8?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SignINVendorRequest에서 검증 수행 후 Service 에 위임 --- .../lab/web/api/vendor/VendorApiController.java | 7 +++++++ .../dto/request/vendor/SignInVendorRequest.java | 14 ++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 src/main/java/camp/woowak/lab/web/dto/request/vendor/SignInVendorRequest.java diff --git a/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java b/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java index 3d54139a..90809264 100644 --- a/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java +++ b/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java @@ -11,6 +11,7 @@ import camp.woowak.lab.vendor.service.command.SignUpVendorCommand; import camp.woowak.lab.web.api.utils.APIResponse; import camp.woowak.lab.web.api.utils.APIUtils; +import camp.woowak.lab.web.dto.request.vendor.SignInVendorRequest; import camp.woowak.lab.web.dto.request.vendor.SignUpVendorRequest; import camp.woowak.lab.web.dto.response.vendor.SignUpVendorResponse; import jakarta.validation.Valid; @@ -32,4 +33,10 @@ public ResponseEntity> signUpVendor( Long registeredId = signUpVendorService.signUp(command); return APIUtils.of(HttpStatus.CREATED, new SignUpVendorResponse(registeredId)); } + + @PostMapping("/vendor/login") + @ResponseStatus(HttpStatus.NO_CONTENT) + public SignUpVendorResponse login(@Valid @RequestBody SignInVendorRequest request) { + return null; + } } diff --git a/src/main/java/camp/woowak/lab/web/dto/request/vendor/SignInVendorRequest.java b/src/main/java/camp/woowak/lab/web/dto/request/vendor/SignInVendorRequest.java new file mode 100644 index 00000000..fb5e3fa8 --- /dev/null +++ b/src/main/java/camp/woowak/lab/web/dto/request/vendor/SignInVendorRequest.java @@ -0,0 +1,14 @@ +package camp.woowak.lab.web.dto.request.vendor; + +import org.hibernate.validator.constraints.Length; + +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.NotBlank; + +public record SignInVendorRequest( + @NotBlank @Email + String email, + @NotBlank @Length(min = 8, max = 30) + String password +) { +} From ce5579f5c9dda457b1ed55d33f84d68954fb66b2 Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 18:07:04 +0900 Subject: [PATCH 04/24] =?UTF-8?q?[fix]=20Vendor=EC=9D=98=20id=EB=A5=BC=20U?= =?UTF-8?q?UID=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/camp/woowak/lab/vendor/domain/Vendor.java | 8 +++++--- .../woowak/lab/vendor/repository/VendorRepository.java | 4 +++- .../woowak/lab/vendor/service/SignUpVendorService.java | 4 ++-- .../woowak/lab/web/api/vendor/VendorApiController.java | 2 +- .../lab/web/dto/response/vendor/SignUpVendorResponse.java | 2 +- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/java/camp/woowak/lab/vendor/domain/Vendor.java b/src/main/java/camp/woowak/lab/vendor/domain/Vendor.java index d6f35a9b..9ccf184b 100644 --- a/src/main/java/camp/woowak/lab/vendor/domain/Vendor.java +++ b/src/main/java/camp/woowak/lab/vendor/domain/Vendor.java @@ -1,5 +1,7 @@ package camp.woowak.lab.vendor.domain; +import java.util.UUID; + import camp.woowak.lab.payaccount.domain.PayAccount; import camp.woowak.lab.vendor.exception.InvalidVendorCreationException; import camp.woowak.lab.web.authentication.PasswordEncoder; @@ -14,8 +16,8 @@ @Entity public class Vendor { @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @GeneratedValue(strategy = GenerationType.UUID) + private UUID id; @Column(nullable = false, length = 50) private String name; @Column(unique = true, nullable = false, length = 100) @@ -43,7 +45,7 @@ public Vendor(String name, String email, String password, String phone, PayAccou this.payAccount = payAccount; } - public Long getId() { + public UUID getId() { return id; } } diff --git a/src/main/java/camp/woowak/lab/vendor/repository/VendorRepository.java b/src/main/java/camp/woowak/lab/vendor/repository/VendorRepository.java index 8cbf9a34..2d2c1c18 100644 --- a/src/main/java/camp/woowak/lab/vendor/repository/VendorRepository.java +++ b/src/main/java/camp/woowak/lab/vendor/repository/VendorRepository.java @@ -1,8 +1,10 @@ package camp.woowak.lab.vendor.repository; +import java.util.UUID; + import org.springframework.data.jpa.repository.JpaRepository; import camp.woowak.lab.vendor.domain.Vendor; -public interface VendorRepository extends JpaRepository { +public interface VendorRepository extends JpaRepository { } diff --git a/src/main/java/camp/woowak/lab/vendor/service/SignUpVendorService.java b/src/main/java/camp/woowak/lab/vendor/service/SignUpVendorService.java index 13b9e30a..1a260a2e 100644 --- a/src/main/java/camp/woowak/lab/vendor/service/SignUpVendorService.java +++ b/src/main/java/camp/woowak/lab/vendor/service/SignUpVendorService.java @@ -26,7 +26,7 @@ public SignUpVendorService( this.passwordEncoder = passwordEncoder; } - public Long signUp(SignUpVendorCommand cmd) { + public String signUp(SignUpVendorCommand cmd) { PayAccount newPayAccount = new PayAccount(); payAccountRepository.save(newPayAccount); Vendor newVendor = @@ -36,6 +36,6 @@ public Long signUp(SignUpVendorCommand cmd) { } catch (DataIntegrityViolationException e) { throw new DuplicateEmailException(); } - return newVendor.getId(); + return newVendor.getId().toString(); } } diff --git a/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java b/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java index 90809264..c7a7bc7b 100644 --- a/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java +++ b/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java @@ -30,7 +30,7 @@ public ResponseEntity> signUpVendor( @Valid @RequestBody SignUpVendorRequest request) { SignUpVendorCommand command = new SignUpVendorCommand(request.name(), request.email(), request.password(), request.phone()); - Long registeredId = signUpVendorService.signUp(command); + String registeredId = signUpVendorService.signUp(command); return APIUtils.of(HttpStatus.CREATED, new SignUpVendorResponse(registeredId)); } diff --git a/src/main/java/camp/woowak/lab/web/dto/response/vendor/SignUpVendorResponse.java b/src/main/java/camp/woowak/lab/web/dto/response/vendor/SignUpVendorResponse.java index 48216659..74d73329 100644 --- a/src/main/java/camp/woowak/lab/web/dto/response/vendor/SignUpVendorResponse.java +++ b/src/main/java/camp/woowak/lab/web/dto/response/vendor/SignUpVendorResponse.java @@ -1,6 +1,6 @@ package camp.woowak.lab.web.dto.response.vendor; public record SignUpVendorResponse( - Long id + String id ) { } From ead21eef148353276d24c34d85b1ff168f88215f Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 18:44:58 +0900 Subject: [PATCH 05/24] =?UTF-8?q?[fix]=20Vendor=20id=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20VendorRepositoryTest?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../woowak/lab/vendor/repository/VendorRepositoryTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/camp/woowak/lab/vendor/repository/VendorRepositoryTest.java b/src/test/java/camp/woowak/lab/vendor/repository/VendorRepositoryTest.java index 173fb485..b68756f4 100644 --- a/src/test/java/camp/woowak/lab/vendor/repository/VendorRepositoryTest.java +++ b/src/test/java/camp/woowak/lab/vendor/repository/VendorRepositoryTest.java @@ -1,6 +1,7 @@ package camp.woowak.lab.vendor.repository; import java.util.Optional; +import java.util.UUID; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; @@ -54,7 +55,7 @@ void success() { // when Vendor vendor = createVendor(payAccount, passwordEncoder); Vendor savedVendor = vendorRepository.save(vendor); - Long savedVendorId = savedVendor.getId(); + UUID savedVendorId = savedVendor.getId(); vendorRepository.flush(); // then From 40d062291f281f714ece29c6b81df7a92c0f7036 Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 18:50:07 +0900 Subject: [PATCH 06/24] =?UTF-8?q?[fix]=20Vendor=20id=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20SignUPVendorServiceTe?= =?UTF-8?q?st=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vendor/service/SignUpVendorService.java | 8 ++++---- .../woowak/lab/fixture/VendorFixture.java | 9 +++++++++ .../camp/woowak/lab/vendor/TestVendor.java | 19 +++++++++++++++++++ .../service/SignUpVendorServiceTest.java | 7 +++++-- 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 src/test/java/camp/woowak/lab/vendor/TestVendor.java diff --git a/src/main/java/camp/woowak/lab/vendor/service/SignUpVendorService.java b/src/main/java/camp/woowak/lab/vendor/service/SignUpVendorService.java index 1a260a2e..f1da21de 100644 --- a/src/main/java/camp/woowak/lab/vendor/service/SignUpVendorService.java +++ b/src/main/java/camp/woowak/lab/vendor/service/SignUpVendorService.java @@ -29,13 +29,13 @@ public SignUpVendorService( public String signUp(SignUpVendorCommand cmd) { PayAccount newPayAccount = new PayAccount(); payAccountRepository.save(newPayAccount); - Vendor newVendor = - new Vendor(cmd.name(), cmd.email(), cmd.password(), cmd.phone(), newPayAccount, passwordEncoder); + Vendor savedVendor; try { - vendorRepository.save(newVendor); + savedVendor = vendorRepository.save( + new Vendor(cmd.name(), cmd.email(), cmd.password(), cmd.phone(), newPayAccount, passwordEncoder)); } catch (DataIntegrityViolationException e) { throw new DuplicateEmailException(); } - return newVendor.getId().toString(); + return savedVendor.getId().toString(); } } diff --git a/src/test/java/camp/woowak/lab/fixture/VendorFixture.java b/src/test/java/camp/woowak/lab/fixture/VendorFixture.java index ccf7063d..75545777 100644 --- a/src/test/java/camp/woowak/lab/fixture/VendorFixture.java +++ b/src/test/java/camp/woowak/lab/fixture/VendorFixture.java @@ -1,6 +1,9 @@ package camp.woowak.lab.fixture; +import java.util.UUID; + import camp.woowak.lab.payaccount.domain.PayAccount; +import camp.woowak.lab.vendor.TestVendor; import camp.woowak.lab.vendor.domain.Vendor; import camp.woowak.lab.web.authentication.PasswordEncoder; @@ -9,6 +12,12 @@ default PayAccount createPayAccount() { return new PayAccount(); } + default Vendor createSavedVendor(UUID id, PayAccount payAccount, PasswordEncoder passwordEncoder) { + return new TestVendor( + id, new Vendor("vendorName", "vendorEmail@example.com", "vendorPassword", "010-0000-0000", payAccount, + passwordEncoder)); + } + default Vendor createVendor(PayAccount payAccount, PasswordEncoder passwordEncoder) { return new Vendor("vendorName", "vendorEmail@example.com", "vendorPassword", "010-0000-0000", payAccount, passwordEncoder); diff --git a/src/test/java/camp/woowak/lab/vendor/TestVendor.java b/src/test/java/camp/woowak/lab/vendor/TestVendor.java new file mode 100644 index 00000000..6b90b56c --- /dev/null +++ b/src/test/java/camp/woowak/lab/vendor/TestVendor.java @@ -0,0 +1,19 @@ +package camp.woowak.lab.vendor; + +import java.util.UUID; + +import camp.woowak.lab.vendor.domain.Vendor; + +public class TestVendor extends Vendor { + private UUID id; + private Vendor vendor; + + public TestVendor(UUID id, Vendor vendor) { + this.id = id; + this.vendor = vendor; + } + + public UUID getId() { + return id; + } +} diff --git a/src/test/java/camp/woowak/lab/vendor/service/SignUpVendorServiceTest.java b/src/test/java/camp/woowak/lab/vendor/service/SignUpVendorServiceTest.java index daeb2398..1c60af75 100644 --- a/src/test/java/camp/woowak/lab/vendor/service/SignUpVendorServiceTest.java +++ b/src/test/java/camp/woowak/lab/vendor/service/SignUpVendorServiceTest.java @@ -2,6 +2,8 @@ import static org.mockito.BDDMockito.*; +import java.util.UUID; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -37,9 +39,10 @@ class SignUpVendorServiceTest implements VendorFixture { @DisplayName("[성공] Vendor가 저장된다.") void success() throws DuplicateEmailException { // given - given(passwordEncoder.encode(Mockito.anyString())).willReturn("password"); PayAccount payAccount = createPayAccount(); - Vendor vendor = createVendor(payAccount, new NoOpPasswordEncoder()); + UUID fakeVendorId = UUID.randomUUID(); + Vendor vendor = createSavedVendor(fakeVendorId, payAccount, new NoOpPasswordEncoder()); + given(passwordEncoder.encode(Mockito.anyString())).willReturn("password"); given(payAccountRepository.save(Mockito.any(PayAccount.class))).willReturn(payAccount); given(vendorRepository.save(Mockito.any(Vendor.class))).willReturn(vendor); From 086777388be9d11f32f43e4bc2c8ce32aabbaead Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 18:52:05 +0900 Subject: [PATCH 07/24] =?UTF-8?q?[fix]=20Vendor=20id=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20VendorApiControllerTe?= =?UTF-8?q?st=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lab/web/api/VendorApiControllerTest.java | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/test/java/camp/woowak/lab/web/api/VendorApiControllerTest.java b/src/test/java/camp/woowak/lab/web/api/VendorApiControllerTest.java index b6083049..73d7188d 100644 --- a/src/test/java/camp/woowak/lab/web/api/VendorApiControllerTest.java +++ b/src/test/java/camp/woowak/lab/web/api/VendorApiControllerTest.java @@ -4,7 +4,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -import java.util.Random; +import java.util.UUID; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -41,9 +41,9 @@ class SignUpVendor { @Test @DisplayName("[성공] 201") void success() throws Exception { - long fakeVendorId = new Random().nextLong(1000L); + UUID fakeVendorId = UUID.randomUUID(); BDDMockito.given(signUpVendorService.signUp(BDDMockito.any(SignUpVendorCommand.class))) - .willReturn(fakeVendorId); + .willReturn(fakeVendorId.toString()); // when ResultActions actions = mockMvc.perform( @@ -58,7 +58,7 @@ void success() throws Exception { // then actions.andExpect(status().isCreated()) .andExpect(jsonPath("$.status").value(HttpStatus.CREATED.value())) - .andExpect(jsonPath("$.data.id").value(fakeVendorId)) + .andExpect(jsonPath("$.data.id").value(fakeVendorId.toString())) .andDo(print()); } @@ -71,9 +71,9 @@ class NameMust { @Test @DisplayName("비어있는 경우") void failWithEmptyName() throws Exception { - long fakeVendorId = new Random().nextLong(1000L); + UUID fakeVendorId = UUID.randomUUID(); BDDMockito.given(signUpVendorService.signUp(BDDMockito.any(SignUpVendorCommand.class))) - .willReturn(fakeVendorId); + .willReturn(fakeVendorId.toString()); // when ResultActions actions = mockMvc.perform( @@ -97,9 +97,9 @@ void failWithEmptyName() throws Exception { @Test @DisplayName("공란인 경우") void failWithBlankName() throws Exception { - long fakeVendorId = new Random().nextLong(1000L); + UUID fakeVendorId = UUID.randomUUID(); BDDMockito.given(signUpVendorService.signUp(BDDMockito.any(SignUpVendorCommand.class))) - .willReturn(fakeVendorId); + .willReturn(fakeVendorId.toString()); // when ResultActions actions = mockMvc.perform( @@ -127,9 +127,9 @@ class EmailMust { @Test @DisplayName("비어있는 경우") void failWithEmptyEmail() throws Exception { - long fakeVendorId = new Random().nextLong(1000L); + UUID fakeVendorId = UUID.randomUUID(); BDDMockito.given(signUpVendorService.signUp(BDDMockito.any(SignUpVendorCommand.class))) - .willReturn(fakeVendorId); + .willReturn(fakeVendorId.toString()); // when ResultActions actions = mockMvc.perform( @@ -152,9 +152,9 @@ void failWithEmptyEmail() throws Exception { @Test @DisplayName("공란인 경우") void failWithBlankEmail() throws Exception { - long fakeVendorId = new Random().nextLong(1000L); + UUID fakeVendorId = UUID.randomUUID(); BDDMockito.given(signUpVendorService.signUp(BDDMockito.any(SignUpVendorCommand.class))) - .willReturn(fakeVendorId); + .willReturn(fakeVendorId.toString()); // when ResultActions actions = mockMvc.perform( @@ -181,9 +181,9 @@ class PasswordMust { @Test @DisplayName("비어있는 경우") void failWithEmptyPassword() throws Exception { - long fakeVendorId = new Random().nextLong(1000L); + UUID fakeVendorId = UUID.randomUUID(); BDDMockito.given(signUpVendorService.signUp(BDDMockito.any(SignUpVendorCommand.class))) - .willReturn(fakeVendorId); + .willReturn(fakeVendorId.toString()); // when ResultActions actions = mockMvc.perform( @@ -207,9 +207,9 @@ void failWithEmptyPassword() throws Exception { @Test @DisplayName("공란인 경우") void failWithBlankPassword() throws Exception { - long fakeVendorId = new Random().nextLong(1000L); + UUID fakeVendorId = UUID.randomUUID(); BDDMockito.given(signUpVendorService.signUp(BDDMockito.any(SignUpVendorCommand.class))) - .willReturn(fakeVendorId); + .willReturn(fakeVendorId.toString()); // when ResultActions actions = mockMvc.perform( @@ -233,9 +233,9 @@ void failWithBlankPassword() throws Exception { @Test @DisplayName("8자 미만인 경우") void failWith7Password() throws Exception { - long fakeVendorId = new Random().nextLong(1000L); + UUID fakeVendorId = UUID.randomUUID(); BDDMockito.given(signUpVendorService.signUp(BDDMockito.any(SignUpVendorCommand.class))) - .willReturn(fakeVendorId); + .willReturn(fakeVendorId.toString()); // when ResultActions actions = mockMvc.perform( @@ -259,9 +259,9 @@ void failWith7Password() throws Exception { @Test @DisplayName("30자 초과인 경우") void failWith31Password() throws Exception { - long fakeVendorId = new Random().nextLong(1000L); + UUID fakeVendorId = UUID.randomUUID(); BDDMockito.given(signUpVendorService.signUp(BDDMockito.any(SignUpVendorCommand.class))) - .willReturn(fakeVendorId); + .willReturn(fakeVendorId.toString()); // when ResultActions actions = mockMvc.perform( @@ -290,9 +290,9 @@ class PhoneMust { @Test @DisplayName("비어있는 경우") void failWithEmptyPhone() throws Exception { - long fakeVendorId = new Random().nextLong(1000L); + UUID fakeVendorId = UUID.randomUUID(); BDDMockito.given(signUpVendorService.signUp(BDDMockito.any(SignUpVendorCommand.class))) - .willReturn(fakeVendorId); + .willReturn(fakeVendorId.toString()); // when ResultActions actions = mockMvc.perform( @@ -316,9 +316,9 @@ void failWithEmptyPhone() throws Exception { @Test @DisplayName("공란인 경우") void failWithBlankPhone() throws Exception { - long fakeVendorId = new Random().nextLong(1000L); + UUID fakeVendorId = UUID.randomUUID(); BDDMockito.given(signUpVendorService.signUp(BDDMockito.any(SignUpVendorCommand.class))) - .willReturn(fakeVendorId); + .willReturn(fakeVendorId.toString()); // when ResultActions actions = mockMvc.perform( @@ -341,9 +341,9 @@ void failWithBlankPhone() throws Exception { @Test @DisplayName("잘못된 형식인 경우") void failWithInvalidPhone() throws Exception { - long fakeVendorId = new Random().nextLong(1000L); + UUID fakeVendorId = UUID.randomUUID(); BDDMockito.given(signUpVendorService.signUp(BDDMockito.any(SignUpVendorCommand.class))) - .willReturn(fakeVendorId); + .willReturn(fakeVendorId.toString()); // when ResultActions actions = mockMvc.perform( From 12e1c609caea11d0de3c1591671beab7ccef7994 Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 18:52:43 +0900 Subject: [PATCH 08/24] =?UTF-8?q?[feat]=20Vendor=EC=9D=98=20=EB=B9=84?= =?UTF-8?q?=EB=B0=80=EB=B2=88=ED=98=B8=20=EC=9D=BC=EC=B9=98=20=EC=97=AC?= =?UTF-8?q?=EB=B6=80=EB=A5=BC=20=ED=99=95=EC=9D=B8=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=8F=84=EB=A9=94=EC=9D=B8=20=EB=A9=94=EC=84=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `+` matches() --- src/main/java/camp/woowak/lab/vendor/domain/Vendor.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/camp/woowak/lab/vendor/domain/Vendor.java b/src/main/java/camp/woowak/lab/vendor/domain/Vendor.java index 9ccf184b..2a4829ac 100644 --- a/src/main/java/camp/woowak/lab/vendor/domain/Vendor.java +++ b/src/main/java/camp/woowak/lab/vendor/domain/Vendor.java @@ -48,4 +48,8 @@ public Vendor(String name, String email, String password, String phone, PayAccou public UUID getId() { return id; } + + public boolean matches(String password, PasswordEncoder passwordEncoder) { + return passwordEncoder.matches(this.password, password); + } } From afb72d63d971bfa2fb859ad20b0b572eb7bf0712 Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 18:54:09 +0900 Subject: [PATCH 09/24] =?UTF-8?q?[feat]=20VendorRepository=EC=97=90?= =?UTF-8?q?=EC=84=9C=20Email=EB=A1=9C=20=EC=A1=B0=ED=9A=8C=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EB=A9=94=EC=84=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `+` findByEmail() `+` findByEmailOrThrow() --- .../lab/vendor/exception/NotFoundVendorException.java | 9 +++++++++ .../woowak/lab/vendor/exception/VendorErrorCode.java | 3 ++- .../woowak/lab/vendor/repository/VendorRepository.java | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/main/java/camp/woowak/lab/vendor/exception/NotFoundVendorException.java diff --git a/src/main/java/camp/woowak/lab/vendor/exception/NotFoundVendorException.java b/src/main/java/camp/woowak/lab/vendor/exception/NotFoundVendorException.java new file mode 100644 index 00000000..96decdb6 --- /dev/null +++ b/src/main/java/camp/woowak/lab/vendor/exception/NotFoundVendorException.java @@ -0,0 +1,9 @@ +package camp.woowak.lab.vendor.exception; + +import camp.woowak.lab.common.exception.NotFoundException; + +public class NotFoundVendorException extends NotFoundException { + public NotFoundVendorException() { + super(VendorErrorCode.NOT_FOUND_VENDOR); + } +} diff --git a/src/main/java/camp/woowak/lab/vendor/exception/VendorErrorCode.java b/src/main/java/camp/woowak/lab/vendor/exception/VendorErrorCode.java index 8ff33311..e47c55ac 100644 --- a/src/main/java/camp/woowak/lab/vendor/exception/VendorErrorCode.java +++ b/src/main/java/camp/woowak/lab/vendor/exception/VendorErrorCode.java @@ -14,7 +14,8 @@ public enum VendorErrorCode implements ErrorCode { INVALID_NAME_EMPTY(HttpStatus.BAD_REQUEST, "v1_7", "이름이 입력되지 않았습니다."), INVALID_NAME_RANGE(HttpStatus.BAD_REQUEST, "v1_8", "이름은 50자를 넘을 수 없습니다."), INVALID_PAY_ACCOUNT_EMPTY(HttpStatus.BAD_REQUEST, "v_1_9", "포인트 계좌가 입력되지 않았습니다."), - DUPLICATE_EMAIL(HttpStatus.BAD_REQUEST, "v_2", "이미 가입된 이메일입니다."); + DUPLICATE_EMAIL(HttpStatus.BAD_REQUEST, "v_2", "이미 가입된 이메일입니다."), + NOT_FOUND_VENDOR(HttpStatus.BAD_REQUEST, "v3", "존재하지 않는 점주입니다."); private final int status; private final String errorCode; diff --git a/src/main/java/camp/woowak/lab/vendor/repository/VendorRepository.java b/src/main/java/camp/woowak/lab/vendor/repository/VendorRepository.java index 2d2c1c18..79352839 100644 --- a/src/main/java/camp/woowak/lab/vendor/repository/VendorRepository.java +++ b/src/main/java/camp/woowak/lab/vendor/repository/VendorRepository.java @@ -1,10 +1,17 @@ package camp.woowak.lab.vendor.repository; +import java.util.Optional; import java.util.UUID; import org.springframework.data.jpa.repository.JpaRepository; import camp.woowak.lab.vendor.domain.Vendor; +import camp.woowak.lab.vendor.exception.NotFoundVendorException; public interface VendorRepository extends JpaRepository { + Optional findByEmail(String email); + + default Vendor findByEmailOrThrow(String email) { + return findByEmail(email).orElseThrow(NotFoundVendorException::new); + } } From c4af2283f14534b22117f07698b481d2b5f23a5d Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 18:58:23 +0900 Subject: [PATCH 10/24] =?UTF-8?q?[feat]=20Vendor=EA=B0=80=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=ED=95=98=EA=B8=B0=20=EC=9C=84=ED=95=9C=20?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 입력된 비밀번호와 Vendor의 비밀번호의 일치 여부 확인 --- .../exception/PasswordMismatchException.java | 9 ++++++ .../lab/vendor/exception/VendorErrorCode.java | 3 +- .../vendor/service/SignInVendorService.java | 32 +++++++++++++++++++ .../service/command/SignInVendorCommand.java | 7 ++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/main/java/camp/woowak/lab/vendor/exception/PasswordMismatchException.java create mode 100644 src/main/java/camp/woowak/lab/vendor/service/SignInVendorService.java create mode 100644 src/main/java/camp/woowak/lab/vendor/service/command/SignInVendorCommand.java diff --git a/src/main/java/camp/woowak/lab/vendor/exception/PasswordMismatchException.java b/src/main/java/camp/woowak/lab/vendor/exception/PasswordMismatchException.java new file mode 100644 index 00000000..08376197 --- /dev/null +++ b/src/main/java/camp/woowak/lab/vendor/exception/PasswordMismatchException.java @@ -0,0 +1,9 @@ +package camp.woowak.lab.vendor.exception; + +import camp.woowak.lab.common.exception.BadRequestException; + +public class PasswordMismatchException extends BadRequestException { + public PasswordMismatchException() { + super(VendorErrorCode.WRONG_PASSWORD); + } +} diff --git a/src/main/java/camp/woowak/lab/vendor/exception/VendorErrorCode.java b/src/main/java/camp/woowak/lab/vendor/exception/VendorErrorCode.java index e47c55ac..895363d9 100644 --- a/src/main/java/camp/woowak/lab/vendor/exception/VendorErrorCode.java +++ b/src/main/java/camp/woowak/lab/vendor/exception/VendorErrorCode.java @@ -15,7 +15,8 @@ public enum VendorErrorCode implements ErrorCode { INVALID_NAME_RANGE(HttpStatus.BAD_REQUEST, "v1_8", "이름은 50자를 넘을 수 없습니다."), INVALID_PAY_ACCOUNT_EMPTY(HttpStatus.BAD_REQUEST, "v_1_9", "포인트 계좌가 입력되지 않았습니다."), DUPLICATE_EMAIL(HttpStatus.BAD_REQUEST, "v_2", "이미 가입된 이메일입니다."), - NOT_FOUND_VENDOR(HttpStatus.BAD_REQUEST, "v3", "존재하지 않는 점주입니다."); + NOT_FOUND_VENDOR(HttpStatus.BAD_REQUEST, "v3", "존재하지 않는 점주입니다."), + WRONG_PASSWORD(HttpStatus.BAD_REQUEST, "v4_1", "잘못된 비밀번호입니다."); private final int status; private final String errorCode; diff --git a/src/main/java/camp/woowak/lab/vendor/service/SignInVendorService.java b/src/main/java/camp/woowak/lab/vendor/service/SignInVendorService.java new file mode 100644 index 00000000..8369affa --- /dev/null +++ b/src/main/java/camp/woowak/lab/vendor/service/SignInVendorService.java @@ -0,0 +1,32 @@ +package camp.woowak.lab.vendor.service; + +import java.util.UUID; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import camp.woowak.lab.vendor.domain.Vendor; +import camp.woowak.lab.vendor.exception.PasswordMismatchException; +import camp.woowak.lab.vendor.repository.VendorRepository; +import camp.woowak.lab.vendor.service.command.SignInVendorCommand; +import camp.woowak.lab.web.authentication.PasswordEncoder; + +@Service +@Transactional(readOnly = true) +public class SignInVendorService { + private final VendorRepository repository; + private final PasswordEncoder passwordEncoder; + + public SignInVendorService(VendorRepository repository, PasswordEncoder passwordEncoder) { + this.repository = repository; + this.passwordEncoder = passwordEncoder; + } + + public UUID signIn(SignInVendorCommand cmd) { + Vendor findVendor = repository.findByEmailOrThrow(cmd.email()); + if (!findVendor.matches(cmd.password(), passwordEncoder)) { + throw new PasswordMismatchException(); + } + return findVendor.getId(); + } +} diff --git a/src/main/java/camp/woowak/lab/vendor/service/command/SignInVendorCommand.java b/src/main/java/camp/woowak/lab/vendor/service/command/SignInVendorCommand.java new file mode 100644 index 00000000..e63dbed6 --- /dev/null +++ b/src/main/java/camp/woowak/lab/vendor/service/command/SignInVendorCommand.java @@ -0,0 +1,7 @@ +package camp.woowak.lab.vendor.service.command; + +public record SignInVendorCommand( + String email, + String password +) { +} From 5d5a8e43496aaae8b74deda5f2d6866480de3262 Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 19:09:00 +0900 Subject: [PATCH 11/24] =?UTF-8?q?[docs]=20SignInVendorService=EA=B0=80=20?= =?UTF-8?q?=EB=8D=98=EC=A7=80=EB=8A=94=20=EC=98=88=EC=99=B8=20=EB=AA=85?= =?UTF-8?q?=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SignInVendorService는 PasswordMismatchException을 발생시킨다. --- .../camp/woowak/lab/vendor/service/SignInVendorService.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/camp/woowak/lab/vendor/service/SignInVendorService.java b/src/main/java/camp/woowak/lab/vendor/service/SignInVendorService.java index 8369affa..c7b262ae 100644 --- a/src/main/java/camp/woowak/lab/vendor/service/SignInVendorService.java +++ b/src/main/java/camp/woowak/lab/vendor/service/SignInVendorService.java @@ -22,6 +22,9 @@ public SignInVendorService(VendorRepository repository, PasswordEncoder password this.passwordEncoder = passwordEncoder; } + /** + * @throws PasswordMismatchException 비밀번호가 일치하지 않으면 + */ public UUID signIn(SignInVendorCommand cmd) { Vendor findVendor = repository.findByEmailOrThrow(cmd.email()); if (!findVendor.matches(cmd.password(), passwordEncoder)) { From 4fb4598355133fe20bba78cc1bcd3578630c432d Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 19:20:58 +0900 Subject: [PATCH 12/24] =?UTF-8?q?[fix]=20matches=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0=20=EC=88=9C?= =?UTF-8?q?=EC=84=9C=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/camp/woowak/lab/vendor/domain/Vendor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/camp/woowak/lab/vendor/domain/Vendor.java b/src/main/java/camp/woowak/lab/vendor/domain/Vendor.java index 2a4829ac..f6bd0472 100644 --- a/src/main/java/camp/woowak/lab/vendor/domain/Vendor.java +++ b/src/main/java/camp/woowak/lab/vendor/domain/Vendor.java @@ -50,6 +50,6 @@ public UUID getId() { } public boolean matches(String password, PasswordEncoder passwordEncoder) { - return passwordEncoder.matches(this.password, password); + return passwordEncoder.matches(password, this.password); } } From 01fdfd526586a154236dd3e89227a90ec1fb98cf Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 19:21:56 +0900 Subject: [PATCH 13/24] =?UTF-8?q?[fix]=20TestVendor=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=EC=9E=90=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 부모 클래스에 필드값을 넣어주어야 제대로 동작한다. --- src/test/java/camp/woowak/lab/fixture/VendorFixture.java | 4 ++-- src/test/java/camp/woowak/lab/vendor/TestVendor.java | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/test/java/camp/woowak/lab/fixture/VendorFixture.java b/src/test/java/camp/woowak/lab/fixture/VendorFixture.java index 75545777..cb5f1850 100644 --- a/src/test/java/camp/woowak/lab/fixture/VendorFixture.java +++ b/src/test/java/camp/woowak/lab/fixture/VendorFixture.java @@ -14,8 +14,8 @@ default PayAccount createPayAccount() { default Vendor createSavedVendor(UUID id, PayAccount payAccount, PasswordEncoder passwordEncoder) { return new TestVendor( - id, new Vendor("vendorName", "vendorEmail@example.com", "vendorPassword", "010-0000-0000", payAccount, - passwordEncoder)); + id, "vendorName", "vendorEmail@example.com", "vendorPassword", "010-0000-0000", payAccount, + passwordEncoder); } default Vendor createVendor(PayAccount payAccount, PasswordEncoder passwordEncoder) { diff --git a/src/test/java/camp/woowak/lab/vendor/TestVendor.java b/src/test/java/camp/woowak/lab/vendor/TestVendor.java index 6b90b56c..a913ab98 100644 --- a/src/test/java/camp/woowak/lab/vendor/TestVendor.java +++ b/src/test/java/camp/woowak/lab/vendor/TestVendor.java @@ -2,15 +2,17 @@ import java.util.UUID; +import camp.woowak.lab.payaccount.domain.PayAccount; import camp.woowak.lab.vendor.domain.Vendor; +import camp.woowak.lab.web.authentication.PasswordEncoder; public class TestVendor extends Vendor { private UUID id; - private Vendor vendor; - public TestVendor(UUID id, Vendor vendor) { + public TestVendor(UUID id, String name, String email, String password, String phone, PayAccount payAccount, + PasswordEncoder passwordEncoder) { + super(name, email, password, phone, payAccount, passwordEncoder); this.id = id; - this.vendor = vendor; } public UUID getId() { From bd810906874ef055745aeb60efcb7fc8720aa7c5 Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 19:22:21 +0900 Subject: [PATCH 14/24] =?UTF-8?q?[test]=20Vendor.matches()=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 --- .../woowak/lab/vendor/domain/VendorTest.java | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/test/java/camp/woowak/lab/vendor/domain/VendorTest.java b/src/test/java/camp/woowak/lab/vendor/domain/VendorTest.java index ad89b05e..bd48189f 100644 --- a/src/test/java/camp/woowak/lab/vendor/domain/VendorTest.java +++ b/src/test/java/camp/woowak/lab/vendor/domain/VendorTest.java @@ -1,18 +1,21 @@ package camp.woowak.lab.vendor.domain; +import java.util.UUID; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import camp.woowak.lab.fixture.VendorFixture; import camp.woowak.lab.payaccount.domain.PayAccount; import camp.woowak.lab.payaccount.domain.TestPayAccount; import camp.woowak.lab.vendor.exception.InvalidVendorCreationException; import camp.woowak.lab.web.authentication.NoOpPasswordEncoder; import camp.woowak.lab.web.authentication.PasswordEncoder; -class VendorTest { +class VendorTest implements VendorFixture { private PayAccount payAccount; private PasswordEncoder passwordEncoder; @@ -209,4 +212,40 @@ void failWithNull() { } } } + + @Nested + @DisplayName("비밀번화 확인은") + class Matches { + @Test + @DisplayName("[성공] 비밀번호가 일치하면 true를 반환한다.") + void return_true() { + // given + Vendor savedVendor = createSavedVendor(UUID.randomUUID(), createPayAccount(), passwordEncoder); + + // when + boolean matches = savedVendor.matches("vendorPassword", passwordEncoder); + + // then + Assertions.assertTrue(matches); + } + + @Test + @DisplayName("[예외] 비밀번호가 불일치하면 false를 반환한다.") + void return_false() { + // given + Vendor savedVendor = createSavedVendor(UUID.randomUUID(), createPayAccount(), passwordEncoder); + + // when + boolean matches = savedVendor.matches("something_wrong", passwordEncoder); + + // then + Assertions.assertFalse(matches); + } + + @Test + void test() { + boolean matches = passwordEncoder.matches("vendorPassword", "vendorPassword"); + Assertions.assertTrue(matches); + } + } } From 26ac760cdf33de125cf5e1b3e58bf5d5e56180eb Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 19:52:23 +0900 Subject: [PATCH 15/24] [test] SignInVendorServiceTest --- .../service/SignInVendorServiceTest.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/test/java/camp/woowak/lab/vendor/service/SignInVendorServiceTest.java diff --git a/src/test/java/camp/woowak/lab/vendor/service/SignInVendorServiceTest.java b/src/test/java/camp/woowak/lab/vendor/service/SignInVendorServiceTest.java new file mode 100644 index 00000000..489ca33d --- /dev/null +++ b/src/test/java/camp/woowak/lab/vendor/service/SignInVendorServiceTest.java @@ -0,0 +1,82 @@ +package camp.woowak.lab.vendor.service; + +import static org.mockito.BDDMockito.*; + +import java.util.UUID; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import camp.woowak.lab.fixture.VendorFixture; +import camp.woowak.lab.payaccount.domain.PayAccount; +import camp.woowak.lab.vendor.domain.Vendor; +import camp.woowak.lab.vendor.exception.DuplicateEmailException; +import camp.woowak.lab.vendor.exception.NotFoundVendorException; +import camp.woowak.lab.vendor.exception.PasswordMismatchException; +import camp.woowak.lab.vendor.repository.VendorRepository; +import camp.woowak.lab.vendor.service.command.SignInVendorCommand; +import camp.woowak.lab.web.authentication.NoOpPasswordEncoder; +import camp.woowak.lab.web.authentication.PasswordEncoder; + +@ExtendWith(MockitoExtension.class) +class SignInVendorServiceTest implements VendorFixture { + @InjectMocks + private SignInVendorService service; + @Mock + private VendorRepository vendorRepository; + @Mock + private PasswordEncoder passwordEncoder; + + @Test + @DisplayName("[성공] 비밀번호가 일치하면 id를 반환한다.") + void success() throws DuplicateEmailException { + // given + PayAccount payAccount = createPayAccount(); + UUID fakeVendorId = UUID.randomUUID(); + Vendor vendor = createSavedVendor(fakeVendorId, payAccount, new NoOpPasswordEncoder()); + given(vendorRepository.findByEmailOrThrow(anyString())).willReturn(vendor); + given(passwordEncoder.matches(anyString(), anyString())).willReturn(true); + + // when + SignInVendorCommand command = new SignInVendorCommand("vendorEmail@email.com", "validPassword"); + UUID id = service.signIn(command); + + // then + Assertions.assertEquals(vendor.getId(), id); + } + + @Test + @DisplayName("[예외] 존재하지 않는 Vendor면 NotFoundVendorException 발생") + void failWithNotFound() throws DuplicateEmailException { + // given + given(vendorRepository.findByEmailOrThrow(anyString())).willThrow(NotFoundVendorException.class); + + // when + SignInVendorCommand command = new SignInVendorCommand("notExists@email.com", "validPassword"); + + // then + Assertions.assertThrows(NotFoundVendorException.class, () -> service.signIn(command)); + } + + @Test + @DisplayName("[예외] 비밀번호가 불일치하면 PasswordMismatchException 발생") + void failWithPasswordMismatch() throws DuplicateEmailException { + // given + PayAccount payAccount = createPayAccount(); + UUID fakeVendorId = UUID.randomUUID(); + Vendor vendor = createSavedVendor(fakeVendorId, payAccount, new NoOpPasswordEncoder()); + given(vendorRepository.findByEmailOrThrow(anyString())).willReturn(vendor); + given(passwordEncoder.matches(anyString(), anyString())).willReturn(false); + + // when + SignInVendorCommand command = new SignInVendorCommand("vendorEmail@email.com", "validPassword"); + + // then + Assertions.assertThrows(PasswordMismatchException.class, () -> service.signIn(command)); + } +} \ No newline at end of file From ae6fb5a9f12123e8df9884f8bca3b2e09f79b61b Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 20:12:44 +0900 Subject: [PATCH 16/24] =?UTF-8?q?[feat]=20=ED=8C=90=EB=A7=A4=EC=9E=90=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20endpoint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/api/vendor/VendorApiController.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java b/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java index c7a7bc7b..5c1d6f79 100644 --- a/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java +++ b/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java @@ -1,5 +1,7 @@ package camp.woowak.lab.web.api.vendor; +import java.util.UUID; + import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; @@ -7,21 +9,28 @@ import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; +import camp.woowak.lab.vendor.service.SignInVendorService; import camp.woowak.lab.vendor.service.SignUpVendorService; +import camp.woowak.lab.vendor.service.command.SignInVendorCommand; import camp.woowak.lab.vendor.service.command.SignUpVendorCommand; import camp.woowak.lab.web.api.utils.APIResponse; import camp.woowak.lab.web.api.utils.APIUtils; +import camp.woowak.lab.web.authentication.LoginVendor; import camp.woowak.lab.web.dto.request.vendor.SignInVendorRequest; import camp.woowak.lab.web.dto.request.vendor.SignUpVendorRequest; import camp.woowak.lab.web.dto.response.vendor.SignUpVendorResponse; +import camp.woowak.lab.web.resolver.session.SessionConst; +import jakarta.servlet.http.HttpSession; import jakarta.validation.Valid; @RestController public class VendorApiController { private final SignUpVendorService signUpVendorService; + private final SignInVendorService signInVendorService; - public VendorApiController(SignUpVendorService signUpVendorService) { + public VendorApiController(SignUpVendorService signUpVendorService, SignInVendorService signInVendorService) { this.signUpVendorService = signUpVendorService; + this.signInVendorService = signInVendorService; } @PostMapping("/vendors") @@ -34,9 +43,12 @@ public ResponseEntity> signUpVendor( return APIUtils.of(HttpStatus.CREATED, new SignUpVendorResponse(registeredId)); } - @PostMapping("/vendor/login") + @PostMapping("/vendors/login") @ResponseStatus(HttpStatus.NO_CONTENT) - public SignUpVendorResponse login(@Valid @RequestBody SignInVendorRequest request) { + public Void login(@Valid @RequestBody SignInVendorRequest request, HttpSession session) { + SignInVendorCommand command = new SignInVendorCommand(request.email(), request.password()); + UUID vendorId = signInVendorService.signIn(command); + session.setAttribute(SessionConst.SESSION_VENDOR_KEY, new LoginVendor(vendorId)); return null; } } From 76e54d0ef7607ba64c984c1d3c1c4adb56bc86b1 Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 20:16:41 +0900 Subject: [PATCH 17/24] =?UTF-8?q?[test]=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lab/web/api/VendorApiControllerTest.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/src/test/java/camp/woowak/lab/web/api/VendorApiControllerTest.java b/src/test/java/camp/woowak/lab/web/api/VendorApiControllerTest.java index 73d7188d..6bc68bec 100644 --- a/src/test/java/camp/woowak/lab/web/api/VendorApiControllerTest.java +++ b/src/test/java/camp/woowak/lab/web/api/VendorApiControllerTest.java @@ -6,6 +6,7 @@ import java.util.UUID; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -22,10 +23,18 @@ import com.fasterxml.jackson.databind.ObjectMapper; import camp.woowak.lab.vendor.exception.DuplicateEmailException; +import camp.woowak.lab.vendor.exception.NotFoundVendorException; +import camp.woowak.lab.vendor.exception.PasswordMismatchException; +import camp.woowak.lab.vendor.service.SignInVendorService; import camp.woowak.lab.vendor.service.SignUpVendorService; +import camp.woowak.lab.vendor.service.command.SignInVendorCommand; import camp.woowak.lab.vendor.service.command.SignUpVendorCommand; import camp.woowak.lab.web.api.vendor.VendorApiController; +import camp.woowak.lab.web.authentication.LoginVendor; +import camp.woowak.lab.web.dto.request.vendor.SignInVendorRequest; import camp.woowak.lab.web.dto.request.vendor.SignUpVendorRequest; +import camp.woowak.lab.web.resolver.session.SessionConst; +import jakarta.servlet.http.HttpSession; @WebMvcTest(controllers = VendorApiController.class) @MockBean(JpaMetamodelMappingContext.class) @@ -34,6 +43,8 @@ class VendorApiControllerTest { private MockMvc mockMvc; @MockBean private SignUpVendorService signUpVendorService; + @MockBean + private SignInVendorService signInVendorService; @Nested @DisplayName("판매자 회원가입: POST /vendors") @@ -391,4 +402,80 @@ void failWithDuplicateEmail() throws Exception { .andDo(print()); } } + + @Nested + @DisplayName("판매자 로그인: POST /vendors/login") + class SignInVendor { + @Test + @DisplayName("[성공] 204") + void success() throws Exception { + UUID fakeVendorId = UUID.randomUUID(); + BDDMockito.given(signInVendorService.signIn(BDDMockito.any(SignInVendorCommand.class))) + .willReturn(fakeVendorId); + + // when + ResultActions actions = mockMvc.perform( + post("/vendors/login") + .content(new ObjectMapper().writeValueAsString( + new SignInVendorRequest("validEmail@validEmail.com", "validPassword"))) + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + ); + + // then + actions.andExpect(status().isNoContent()) + .andExpect(jsonPath("$.status").value(HttpStatus.NO_CONTENT.value())) + .andExpect(result -> { + HttpSession session = result.getRequest().getSession(); + LoginVendor loginVendor = (LoginVendor)session.getAttribute(SessionConst.SESSION_VENDOR_KEY); + Assertions.assertNotNull(loginVendor); + Assertions.assertEquals(loginVendor.getId(), fakeVendorId); + }) + .andDo(print()); + } + + @Test + @DisplayName("[실패] 400 : 존재하지 않는 판매자 이메일") + void failWithNotFoundVendor() throws Exception { + UUID fakeVendorId = UUID.randomUUID(); + BDDMockito.given(signInVendorService.signIn(BDDMockito.any(SignInVendorCommand.class))) + .willThrow(NotFoundVendorException.class); + + // when + ResultActions actions = mockMvc.perform( + post("/vendors/login") + .content(new ObjectMapper().writeValueAsString( + new SignInVendorRequest("notFound@validEmail.com", "validPassword"))) + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + ); + + // then + actions.andExpect(status().isNoContent()) + .andExpect(jsonPath("$.status").value(HttpStatus.BAD_REQUEST.value())) + .andDo(print()); + } + + @Test + @DisplayName("[실패] 400 : 잘못된 비밀번호") + void failWithPasswordMismatch() throws Exception { + UUID fakeVendorId = UUID.randomUUID(); + BDDMockito.given(signInVendorService.signIn(BDDMockito.any(SignInVendorCommand.class))) + .willThrow(PasswordMismatchException.class); + + // when + ResultActions actions = mockMvc.perform( + post("/vendors/login") + .content(new ObjectMapper().writeValueAsString( + new SignInVendorRequest("validEmail@validEmail.com", "wrongPassword"))) + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + ); + + // then + actions.andExpect(status().isNoContent()) + .andExpect(jsonPath("$.status").value(HttpStatus.BAD_REQUEST.value())) + .andDo(print()); + } + } } From c5c2790f5858d2c017184a617d1716ae4de9255d Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 21:44:21 +0900 Subject: [PATCH 18/24] =?UTF-8?q?[fix]=20LoginCustomer=EB=8A=94=20?= =?UTF-8?q?=EB=8B=A4=EB=A5=B8=20=EC=9D=B4=EC=8A=88=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=EB=90=A0=20=EC=98=88=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../camp/woowak/lab/web/authentication/LoginCustomer.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/camp/woowak/lab/web/authentication/LoginCustomer.java b/src/main/java/camp/woowak/lab/web/authentication/LoginCustomer.java index b0d57ee9..0a55c31d 100644 --- a/src/main/java/camp/woowak/lab/web/authentication/LoginCustomer.java +++ b/src/main/java/camp/woowak/lab/web/authentication/LoginCustomer.java @@ -1,16 +1,14 @@ package camp.woowak.lab.web.authentication; -import java.util.UUID; - public class LoginCustomer implements LoginMember { - private final UUID id; + private final Long id; - public LoginCustomer(UUID id) { + public LoginCustomer(Long id) { this.id = id; } @Override - public UUID getId() { + public Long getId() { return id; } } From ccf540da391c5066ac2ddc44b6d80d69e01a272e Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 21:45:00 +0900 Subject: [PATCH 19/24] =?UTF-8?q?[feat]=20Vendor=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `+` PasswordMismatchException `+` NotFoundVendorException --- .../web/api/vendor/VendorApiControllerAdvice.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiControllerAdvice.java b/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiControllerAdvice.java index 1e328a3f..9f72fe1d 100644 --- a/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiControllerAdvice.java +++ b/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiControllerAdvice.java @@ -8,11 +8,23 @@ import camp.woowak.lab.common.advice.DomainExceptionHandler; import camp.woowak.lab.vendor.exception.DuplicateEmailException; import camp.woowak.lab.vendor.exception.InvalidVendorCreationException; +import camp.woowak.lab.vendor.exception.NotFoundVendorException; +import camp.woowak.lab.vendor.exception.PasswordMismatchException; import lombok.extern.slf4j.Slf4j; @Slf4j @DomainExceptionHandler(basePackageClasses = VendorApiController.class) public class VendorApiControllerAdvice { + @ExceptionHandler(PasswordMismatchException.class) + public ResponseEntity handlePasswordMismatchException(PasswordMismatchException ex) { + return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ex.getMessage())).build(); + } + + @ExceptionHandler(NotFoundVendorException.class) + public ResponseEntity handleNotFoundVendorException(NotFoundVendorException ex) { + return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ex.getMessage())).build(); + } + @ExceptionHandler(InvalidVendorCreationException.class) public ResponseEntity handleInvalidVendorCreationException(InvalidVendorCreationException ex) { return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ex.getMessage())).build(); From 15f5820b64f072483990ff5a075f5c792192c9c1 Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 21:45:43 +0900 Subject: [PATCH 20/24] =?UTF-8?q?[test]=20=EC=A0=90=ED=8F=AC=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Vendor의 id 타입의 변경으로 인한 수정 --- .../woowak/lab/web/api/store/StoreApiControllerTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/camp/woowak/lab/web/api/store/StoreApiControllerTest.java b/src/test/java/camp/woowak/lab/web/api/store/StoreApiControllerTest.java index f4eb004f..473978fd 100644 --- a/src/test/java/camp/woowak/lab/web/api/store/StoreApiControllerTest.java +++ b/src/test/java/camp/woowak/lab/web/api/store/StoreApiControllerTest.java @@ -7,6 +7,7 @@ import java.time.LocalDateTime; import java.util.Optional; +import java.util.UUID; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; @@ -76,7 +77,7 @@ void setUp() { void storeRegistrationSuccess() throws Exception { // given Vendor vendor = createVendor(); - LoginVendor loginVendor = new LoginVendor(5L); + LoginVendor loginVendor = new LoginVendor(UUID.randomUUID()); StoreRegistrationRequest request = new StoreRegistrationRequest( "Store Name", "Store Address", @@ -109,7 +110,7 @@ void storeRegistrationSuccess() throws Exception { void storeRegistrationFailure() throws Exception { // given Vendor vendor = createVendor(); - LoginVendor loginVendor = new LoginVendor(5L); + LoginVendor loginVendor = new LoginVendor(UUID.randomUUID()); StoreRegistrationRequest request = new StoreRegistrationRequest( "Store Name", "Invalid Category", From c1f178d624581ddafcb35a1426b8cfdb38bce525 Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 21:46:35 +0900 Subject: [PATCH 21/24] =?UTF-8?q?[test]=20status=20=EA=B2=80=EC=A6=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 응답 상태 코드가 BadRequest인지 검증 --- .../java/camp/woowak/lab/web/api/VendorApiControllerTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/camp/woowak/lab/web/api/VendorApiControllerTest.java b/src/test/java/camp/woowak/lab/web/api/VendorApiControllerTest.java index 6bc68bec..139a6a0e 100644 --- a/src/test/java/camp/woowak/lab/web/api/VendorApiControllerTest.java +++ b/src/test/java/camp/woowak/lab/web/api/VendorApiControllerTest.java @@ -451,7 +451,7 @@ void failWithNotFoundVendor() throws Exception { ); // then - actions.andExpect(status().isNoContent()) + actions.andExpect(status().isBadRequest()) .andExpect(jsonPath("$.status").value(HttpStatus.BAD_REQUEST.value())) .andDo(print()); } @@ -473,7 +473,7 @@ void failWithPasswordMismatch() throws Exception { ); // then - actions.andExpect(status().isNoContent()) + actions.andExpect(status().isBadRequest()) .andExpect(jsonPath("$.status").value(HttpStatus.BAD_REQUEST.value())) .andDo(print()); } From b03b4a893d8bb6f83b70cfb1bee04615535e962f Mon Sep 17 00:00:00 2001 From: donghar Date: Wed, 14 Aug 2024 21:47:25 +0900 Subject: [PATCH 22/24] =?UTF-8?q?[fix]=20=EB=A1=9C=EA=B7=B8=EC=9D=B8?= =?UTF-8?q?=EC=8B=9C=20=EC=9D=91=EB=8B=B5=20=EB=A9=94=EC=8B=9C=EC=A7=80=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit status 값을 담아주는 APIResponseAdvice 활용 --- .../camp/woowak/lab/web/api/vendor/VendorApiController.java | 5 +++-- .../lab/web/dto/response/vendor/SignInVendorResponse.java | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 src/main/java/camp/woowak/lab/web/dto/response/vendor/SignInVendorResponse.java diff --git a/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java b/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java index e98197e0..5f37f6ae 100644 --- a/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java +++ b/src/main/java/camp/woowak/lab/web/api/vendor/VendorApiController.java @@ -15,6 +15,7 @@ import camp.woowak.lab.web.authentication.LoginVendor; import camp.woowak.lab.web.dto.request.vendor.SignInVendorRequest; import camp.woowak.lab.web.dto.request.vendor.SignUpVendorRequest; +import camp.woowak.lab.web.dto.response.vendor.SignInVendorResponse; import camp.woowak.lab.web.dto.response.vendor.SignUpVendorResponse; import camp.woowak.lab.web.resolver.session.SessionConst; import jakarta.servlet.http.HttpSession; @@ -42,10 +43,10 @@ public SignUpVendorResponse signUpVendor(@Valid @RequestBody SignUpVendorRequest @PostMapping("/vendors/login") @ResponseStatus(HttpStatus.NO_CONTENT) - public Void login(@Valid @RequestBody SignInVendorRequest request, HttpSession session) { + public SignInVendorResponse login(@Valid @RequestBody SignInVendorRequest request, HttpSession session) { SignInVendorCommand command = new SignInVendorCommand(request.email(), request.password()); UUID vendorId = signInVendorService.signIn(command); session.setAttribute(SessionConst.SESSION_VENDOR_KEY, new LoginVendor(vendorId)); - return null; + return new SignInVendorResponse("success"); } } diff --git a/src/main/java/camp/woowak/lab/web/dto/response/vendor/SignInVendorResponse.java b/src/main/java/camp/woowak/lab/web/dto/response/vendor/SignInVendorResponse.java new file mode 100644 index 00000000..36a69963 --- /dev/null +++ b/src/main/java/camp/woowak/lab/web/dto/response/vendor/SignInVendorResponse.java @@ -0,0 +1,6 @@ +package camp.woowak.lab.web.dto.response.vendor; + +public record SignInVendorResponse( + String message +) { +} From a30705c1cac83344e346483cb9833dba8422a0db Mon Sep 17 00:00:00 2001 From: donghar Date: Thu, 15 Aug 2024 11:54:02 +0900 Subject: [PATCH 23/24] =?UTF-8?q?[fix]=20Id=20=EC=A0=84=EB=9E=B5=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20Sig?= =?UTF-8?q?nUpVendorService=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Service 내부에서 DataIntegrityViolationException을 받기 위해서는 flush가 수행되어야 한다. --- .../camp/woowak/lab/vendor/service/SignUpVendorService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/camp/woowak/lab/vendor/service/SignUpVendorService.java b/src/main/java/camp/woowak/lab/vendor/service/SignUpVendorService.java index f1da21de..07e5343c 100644 --- a/src/main/java/camp/woowak/lab/vendor/service/SignUpVendorService.java +++ b/src/main/java/camp/woowak/lab/vendor/service/SignUpVendorService.java @@ -31,7 +31,7 @@ public String signUp(SignUpVendorCommand cmd) { payAccountRepository.save(newPayAccount); Vendor savedVendor; try { - savedVendor = vendorRepository.save( + savedVendor = vendorRepository.saveAndFlush( new Vendor(cmd.name(), cmd.email(), cmd.password(), cmd.phone(), newPayAccount, passwordEncoder)); } catch (DataIntegrityViolationException e) { throw new DuplicateEmailException(); From 595ba222c94883e55d89b7c3c0f5f4ca66902c92 Mon Sep 17 00:00:00 2001 From: donghar Date: Thu, 15 Aug 2024 12:05:04 +0900 Subject: [PATCH 24/24] =?UTF-8?q?[test]=20SignUpVendorServiceTest=EC=97=90?= =?UTF-8?q?=EC=84=9C=20SaveAndFlush=EB=A5=BC=20mocking=ED=95=98=EA=B2=8C?= =?UTF-8?q?=20=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/SignUpVendorServiceTest.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/test/java/camp/woowak/lab/vendor/service/SignUpVendorServiceTest.java b/src/test/java/camp/woowak/lab/vendor/service/SignUpVendorServiceTest.java index 1c60af75..0bd77a5d 100644 --- a/src/test/java/camp/woowak/lab/vendor/service/SignUpVendorServiceTest.java +++ b/src/test/java/camp/woowak/lab/vendor/service/SignUpVendorServiceTest.java @@ -43,17 +43,17 @@ void success() throws DuplicateEmailException { UUID fakeVendorId = UUID.randomUUID(); Vendor vendor = createSavedVendor(fakeVendorId, payAccount, new NoOpPasswordEncoder()); given(passwordEncoder.encode(Mockito.anyString())).willReturn("password"); - given(payAccountRepository.save(Mockito.any(PayAccount.class))).willReturn(payAccount); - given(vendorRepository.save(Mockito.any(Vendor.class))).willReturn(vendor); // when + when(payAccountRepository.save(Mockito.any(PayAccount.class))).thenReturn(payAccount); + when(vendorRepository.saveAndFlush(Mockito.any(Vendor.class))).thenReturn(vendor); + + // then SignUpVendorCommand command = new SignUpVendorCommand("vendorName", "vendorEmail@example.com", "password", "010-0000-0000"); service.signUp(command); - - // then then(payAccountRepository).should().save(Mockito.any(PayAccount.class)); - then(vendorRepository).should().save(Mockito.any(Vendor.class)); + then(vendorRepository).should().saveAndFlush(Mockito.any(Vendor.class)); } @Test @@ -61,16 +61,16 @@ void success() throws DuplicateEmailException { void failWithDuplicateEmail() throws DuplicateEmailException { // given given(passwordEncoder.encode(Mockito.anyString())).willReturn("password"); - given(payAccountRepository.save(Mockito.any(PayAccount.class))).willReturn(createPayAccount()); // when - when(vendorRepository.save(Mockito.any(Vendor.class))).thenThrow(DataIntegrityViolationException.class); - SignUpVendorCommand command = - new SignUpVendorCommand("vendorName", "vendorEmail@example.com", "password", "010-0000-0000"); + when(payAccountRepository.save(Mockito.any(PayAccount.class))).thenReturn(createPayAccount()); + when(vendorRepository.saveAndFlush(Mockito.any(Vendor.class))).thenThrow(DataIntegrityViolationException.class); // then + SignUpVendorCommand command = + new SignUpVendorCommand("vendorName", "vendorEmail@example.com", "password", "010-0000-0000"); Assertions.assertThrows(DuplicateEmailException.class, () -> service.signUp(command)); then(payAccountRepository).should().save(Mockito.any(PayAccount.class)); - then(vendorRepository).should().save(Mockito.any(Vendor.class)); + then(vendorRepository).should().saveAndFlush(Mockito.any(Vendor.class)); } }