Skip to content

Commit

Permalink
Refactoring & test example
Browse files Browse the repository at this point in the history
  • Loading branch information
tape4 committed Jul 29, 2024
1 parent 2f596d4 commit cba510f
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import umc.kkijuk.server.common.domian.exception.RecruitOwnerMismatchException;
import umc.kkijuk.server.common.domian.exception.InvalidTagNameException;
import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException;
import umc.kkijuk.server.common.domian.exception.ReviewRecruitMismatchException;
import umc.kkijuk.server.common.domian.exception.*;
import umc.kkijuk.server.common.domian.response.ErrorResponse;

@RestControllerAdvice
Expand Down Expand Up @@ -38,4 +35,10 @@ public ErrorResponse RecruitOwnerMismatchException(RecruitOwnerMismatchException
public ErrorResponse InvalidTagNameException(InvalidTagNameException exception) {
return new ErrorResponse(exception.getMessage());
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConfirmPasswordMismatchException.class)
public ErrorResponse ConfirmPasswordMismatchException(ConfirmPasswordMismatchException e) {
return new ErrorResponse(e.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package umc.kkijuk.server.common.domian.exception;

public class ConfirmPasswordMismatchException extends RuntimeException {
public ConfirmPasswordMismatchException() {
super("Passwords do not match");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,14 @@ public class MemberController {
summary = "회원가입 요청",
description = "회원가입 요청을 받아 성공/실패 여부를 반환합니다.")
@PostMapping
public ResponseEntity<CreateMemberResponse> saveMember(@RequestBody @Valid MemberJoinDto memberJoinDto) {
String passwordConfirm = memberJoinDto.getPasswordConfirm();
if (!passwordConfirm.equals(memberJoinDto.getPassword())) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(new CreateMemberResponse("Passwords do not match"));
}

try {
Long loginUser = LoginUser.get().getId();
memberService.join(memberJoinDto.toEntity());
return ResponseEntity
.status(HttpStatus.CREATED)
.body(new CreateMemberResponse(loginUser , "Member created successfully"));
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new CreateMemberResponse("Member creation failed"));
}
public ResponseEntity<CreateMemberResponse> saveMember(
@RequestBody @Valid MemberJoinDto memberJoinDto
) {
Member joinMember = memberService.join(memberJoinDto);

return ResponseEntity
.status(HttpStatus.CREATED)
.body(new CreateMemberResponse(joinMember.getId(), "Member created successfully"));
}

@Operation(
Expand Down Expand Up @@ -86,6 +74,7 @@ public ResponseEntity<ResultResponse> changeMemberInfo(@RequestBody MemberInfoCh
return ResponseEntity.ok()
.body(new ResultResponse("information update success"));
}catch (Exception e){
System.out.println("e.getMessage() = " + e.getMessage());
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ResultResponse("information update failed"));
Expand Down
1 change: 1 addition & 0 deletions src/main/java/umc/kkijuk/server/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.time.LocalDate;
import java.util.List;

import umc.kkijuk.server.career.domain.base.BaseEntity;
import umc.kkijuk.server.common.converter.StringListToStringConverter;

@Entity
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/umc/kkijuk/server/member/service/MemberService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package umc.kkijuk.server.member.service;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import umc.kkijuk.server.common.domian.exception.ConfirmPasswordMismatchException;
import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException;
import umc.kkijuk.server.member.controller.response.CreateMemberResponse;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.member.dto.MemberFieldDto;
import umc.kkijuk.server.member.dto.MemberInfoChangeDto;
import umc.kkijuk.server.member.dto.MemberJoinDto;
import umc.kkijuk.server.member.repository.MemberJpaRepository;

import java.util.List;
Expand All @@ -25,9 +30,16 @@ public Member findOne(Long memberId) {

//아직 인터페이스 구현 x, 추후에 구현 후 MemberService -> MemberServiceImpl로 수정 예정.
@Transactional
public Long join(Member member) {
memberJpaRepository.save(member);
return member.getId();
public Member join(MemberJoinDto memberJoinDto) {

String passwordConfirm = memberJoinDto.getPasswordConfirm();
if (!passwordConfirm.equals(memberJoinDto.getPassword())) {
throw new ConfirmPasswordMismatchException();
}

Member joinMember = memberJoinDto.toEntity();
memberJpaRepository.save(joinMember);
return joinMember;
}

/* security 의존성 추가후 변경할 join 함수 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import umc.kkijuk.server.member.domain.MarketingAgree;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.member.domain.State;
import umc.kkijuk.server.member.dto.MemberJoinDto;
import umc.kkijuk.server.member.service.MemberService;
import umc.kkijuk.server.recruit.domain.Recruit;
import umc.kkijuk.server.recruit.domain.RecruitStatus;
Expand Down Expand Up @@ -76,9 +77,8 @@ class IntroduceControllerTest {

@BeforeEach
public void Init() {
Member member = new Member("[email protected]", "홍길동", "010-7444-1768", LocalDate.parse("1999-03-31"), "passwordTest", MarketingAgree.BOTH, State.ACTIVATE);
Long savedId = memberService.join(member);
requestMember = memberService.findOne(savedId);
MemberJoinDto memberJoinDto = new MemberJoinDto("[email protected]", "홍길동", "010-7444-1768", LocalDate.parse("1999-03-31"), "passwordTest", "passwordTest", MarketingAgree.BOTH, State.ACTIVATE);
requestMember = memberService.join(memberJoinDto);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Transactional;
import umc.kkijuk.server.member.controller.response.MemberFieldResponse;
import umc.kkijuk.server.member.controller.response.MemberInfoResponse;
import umc.kkijuk.server.member.controller.response.ResultResponse;
Expand Down Expand Up @@ -48,7 +49,8 @@ public void setup() {
// Given
Member member1 = new Member("[email protected]", "홍길동", "010-1234-5678",
LocalDate.parse("1999-03-31"), "passwordTest", MarketingAgree.BOTH, State.ACTIVATE);
memberJpaRepository.save(member1);
Member savedMember = memberJpaRepository.save(member1);

// When
ResponseEntity<MemberInfoResponse> response = restTemplate.getForEntity("/member/myPage/info", MemberInfoResponse.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package umc.kkijuk.server.member.service;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -9,6 +11,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Transactional;
import umc.kkijuk.server.common.domian.exception.ConfirmPasswordMismatchException;
import umc.kkijuk.server.member.domain.MarketingAgree;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.member.domain.State;
Expand All @@ -19,6 +22,8 @@
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Expand All @@ -34,20 +39,36 @@ public class MemberServiceTest {
@Autowired
private TestRestTemplate restTemplate;

private String testMemberEmail = "[email protected]";
@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
public void 유저정보db저장및조회() throws Exception {
// given
Member member1 = new Member("[email protected]", "홍길동", "010-7444-1768", LocalDate.parse("1999-03-31"), "passwordTest", MarketingAgree.BOTH, State.ACTIVATE);
MemberJoinDto memberJoinDto = new MemberJoinDto(testMemberEmail, "홍길동", "010-7444-1768", LocalDate.parse("1999-03-31"), "passwordTest", "passwordTest", MarketingAgree.BOTH, State.ACTIVATE);

// when
Long savedId = memberService.join(member1);
Optional<Member> member2 = memberJpaRepository.findById(savedId);
Member joinMember = memberService.join(memberJoinDto);

// then
assertEquals(member1, member2.get());
assertAll(
() -> assertEquals(joinMember.getEmail(), testMemberEmail),
() -> assertEquals(joinMember.getName(), "홍길동")
);
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
public void joinExceptionWIthPasswordIncorrect() {
// given
MemberJoinDto memberJoinDto = new MemberJoinDto(testMemberEmail, "홍길동", "010-7444-1768", LocalDate.parse("1999-03-31"), "passwordTest", "incorrectPassword", MarketingAgree.BOTH, State.ACTIVATE);

// when
// then
assertThatThrownBy(() -> memberService.join(memberJoinDto))
.isInstanceOf(ConfirmPasswordMismatchException.class);
}


@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
public void 회원가입성공() {
Expand Down Expand Up @@ -97,6 +118,4 @@ public class MemberServiceTest {
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(response.getBody()).contains("Passwords do not match");
}


}

0 comments on commit cba510f

Please sign in to comment.