From d1753f52a4e0440c51c6ce54b46630b74d6f96fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A4=80?= Date: Thu, 4 Aug 2022 22:46:57 +0900 Subject: [PATCH 01/13] =?UTF-8?q?feat:=20ENUM=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=EC=A4=91=20#121?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ceos/bankids/ENUM/ChallengeStatus.java | 19 +++++++++++++++++++ .../com/ceos/bankids/dto/ChallengeDTO.java | 1 + 2 files changed, 20 insertions(+) create mode 100644 src/main/java/com/ceos/bankids/ENUM/ChallengeStatus.java diff --git a/src/main/java/com/ceos/bankids/ENUM/ChallengeStatus.java b/src/main/java/com/ceos/bankids/ENUM/ChallengeStatus.java new file mode 100644 index 00000000..abd7e324 --- /dev/null +++ b/src/main/java/com/ceos/bankids/ENUM/ChallengeStatus.java @@ -0,0 +1,19 @@ +package com.ceos.bankids.ENUM; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum ChallengeStatus { + + REJECTED(1L, 0L, "부모가 거절한 상태"), + ACHIEVED(2L, 0L, "돈길을 완전히 완주한 상태"), + UNACHIEVED(0L, 0L, "이자율 위험도에 걸려서 실패한 상태"), + PENDING(1L, 1L, "부모한테 제안한 상태"), + WALKING(1L, 2L, "부모한테 수락 받아서 걷고있는 상태"); + + private final Long isAchieved; + private final Long status; + private final String title; +} diff --git a/src/main/java/com/ceos/bankids/dto/ChallengeDTO.java b/src/main/java/com/ceos/bankids/dto/ChallengeDTO.java index 1d49c20b..a0b901bc 100644 --- a/src/main/java/com/ceos/bankids/dto/ChallengeDTO.java +++ b/src/main/java/com/ceos/bankids/dto/ChallengeDTO.java @@ -44,6 +44,7 @@ public class ChallengeDTO { @ApiModelProperty(example = "10000") private Long weekPrice; + //succeededWeeks @ApiModelProperty(example = "0") private Long successWeeks; From ae6b7ae9808d53b9361eb8288a1e39c491993f99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A4=80?= Date: Sun, 7 Aug 2022 00:48:14 +0900 Subject: [PATCH 02/13] =?UTF-8?q?refactor:=20Enumerated=20=EC=96=B4?= =?UTF-8?q?=EB=85=B8=ED=85=8C=EC=9D=B4=EC=85=98=EC=9C=BC=EB=A1=9C=20challe?= =?UTF-8?q?nge=20=EB=8F=84=EB=A9=94=EC=9D=B8=EC=97=90=20=EC=BB=AC=EB=9F=BC?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80=ED=95=98=EA=B8=B0=20#121?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ENUM => Enum}/ChallengeStatus.java | 18 ++++++++++++++---- .../com/ceos/bankids/Enum/EnumMapperType.java | 10 ++++++++++ .../com/ceos/bankids/domain/Challenge.java | 9 +++++++++ .../bankids/service/ChallengeServiceImpl.java | 3 ++- 4 files changed, 35 insertions(+), 5 deletions(-) rename src/main/java/com/ceos/bankids/{ENUM => Enum}/ChallengeStatus.java (53%) create mode 100644 src/main/java/com/ceos/bankids/Enum/EnumMapperType.java diff --git a/src/main/java/com/ceos/bankids/ENUM/ChallengeStatus.java b/src/main/java/com/ceos/bankids/Enum/ChallengeStatus.java similarity index 53% rename from src/main/java/com/ceos/bankids/ENUM/ChallengeStatus.java rename to src/main/java/com/ceos/bankids/Enum/ChallengeStatus.java index abd7e324..d1afd562 100644 --- a/src/main/java/com/ceos/bankids/ENUM/ChallengeStatus.java +++ b/src/main/java/com/ceos/bankids/Enum/ChallengeStatus.java @@ -1,19 +1,29 @@ -package com.ceos.bankids.ENUM; +package com.ceos.bankids.Enum; import lombok.AllArgsConstructor; import lombok.Getter; @Getter @AllArgsConstructor -public enum ChallengeStatus { +public enum ChallengeStatus implements EnumMapperType { REJECTED(1L, 0L, "부모가 거절한 상태"), ACHIEVED(2L, 0L, "돈길을 완전히 완주한 상태"), - UNACHIEVED(0L, 0L, "이자율 위험도에 걸려서 실패한 상태"), + FAILED(0L, 0L, "이자율 위험도에 걸려서 실패한 상태"), PENDING(1L, 1L, "부모한테 제안한 상태"), WALKING(1L, 2L, "부모한테 수락 받아서 걷고있는 상태"); private final Long isAchieved; private final Long status; - private final String title; + private final String description; + + @Override + public String getCode() { + return name(); + } + + @Override + public Long getStatus() { + return status; + } } diff --git a/src/main/java/com/ceos/bankids/Enum/EnumMapperType.java b/src/main/java/com/ceos/bankids/Enum/EnumMapperType.java new file mode 100644 index 00000000..c5b2b93a --- /dev/null +++ b/src/main/java/com/ceos/bankids/Enum/EnumMapperType.java @@ -0,0 +1,10 @@ +package com.ceos.bankids.Enum; + +public interface EnumMapperType { + + String getCode(); + + Long getStatus(); + + Long getIsAchieved(); +} diff --git a/src/main/java/com/ceos/bankids/domain/Challenge.java b/src/main/java/com/ceos/bankids/domain/Challenge.java index 036ea284..a7b88b3d 100644 --- a/src/main/java/com/ceos/bankids/domain/Challenge.java +++ b/src/main/java/com/ceos/bankids/domain/Challenge.java @@ -1,10 +1,13 @@ package com.ceos.bankids.domain; +import com.ceos.bankids.Enum.ChallengeStatus; import com.ceos.bankids.exception.BadRequestException; import com.fasterxml.jackson.annotation.JsonIgnore; import java.util.List; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @@ -46,6 +49,10 @@ public class Challenge extends AbstractTimestamp { @ColumnDefault("1") private Long isAchieved; + @Column(nullable = false) + @Enumerated(EnumType.STRING) + private ChallengeStatus challengeStatus; + @Column(nullable = false) private Long totalPrice; @@ -96,6 +103,7 @@ public Challenge( Long id, String title, Long isAchieved, + ChallengeStatus challengeStatus, Long totalPrice, Long weekPrice, Long weeks, @@ -138,6 +146,7 @@ public Challenge( this.id = id; this.isAchieved = isAchieved; + this.challengeStatus = challengeStatus; this.title = title; this.totalPrice = totalPrice; this.weekPrice = weekPrice; diff --git a/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java b/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java index c8fb9e60..78f97a76 100644 --- a/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java +++ b/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java @@ -97,7 +97,8 @@ public ChallengeDTO createChallenge(User user, ChallengeRequest challengeRequest .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) .isAchieved(1L) .status(1L).interestRate(challengeRequest.getInterestRate()) - .challengeCategory(challengeCategory).targetItem(targetItem).filename("test").build(); + .challengeCategory(challengeCategory).targetItem(targetItem) + .filename(challengeRequest.getFileName()).build(); challengeRepository.save(newChallenge); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) From bce8f503d76be958d1a2afc8ecdb8c89214c15af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A4=80?= Date: Sun, 7 Aug 2022 01:23:08 +0900 Subject: [PATCH 03/13] =?UTF-8?q?refactor:=20=EB=8F=88=EA=B8=B8=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=ED=95=98=EA=B8=B0=20=EB=B0=8F=20=EB=8F=88?= =?UTF-8?q?=EA=B8=B8=20=EC=82=AD=EC=A0=9C=ED=95=98=EA=B8=B0=20API=20Enum?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20#12?= =?UTF-8?q?1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bankids/service/ChallengeServiceImpl.java | 52 ++++++++++++++----- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java b/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java index 78f97a76..7891e755 100644 --- a/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java +++ b/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java @@ -1,5 +1,6 @@ package com.ceos.bankids.service; +import com.ceos.bankids.Enum.ChallengeStatus; import com.ceos.bankids.controller.request.ChallengeRequest; import com.ceos.bankids.controller.request.KidChallengeRequest; import com.ceos.bankids.domain.Challenge; @@ -48,6 +49,11 @@ @RequiredArgsConstructor public class ChallengeServiceImpl implements ChallengeService { + private static final ChallengeStatus pending = ChallengeStatus.PENDING; + private static final ChallengeStatus walking = ChallengeStatus.WALKING; + private static final ChallengeStatus achieved = ChallengeStatus.ACHIEVED; + private static final ChallengeStatus failed = ChallengeStatus.FAILED; + private static final ChallengeStatus rejected = ChallengeStatus.REJECTED; private final ChallengeRepository challengeRepository; private final ChallengeCategoryRepository challengeCategoryRepository; private final TargetItemRepository targetItemRepository; @@ -66,8 +72,8 @@ public ChallengeDTO createChallenge(User user, ChallengeRequest challengeRequest sundayValidation(); userRoleValidation(user, true); long count = challengeUserRepository.findByUserId(user.getId()).stream() - .filter(challengeUser -> challengeUser.getChallenge().getStatus() == 2 - && challengeUser.getChallenge().getIsAchieved() == 1).count(); + .filter(challengeUser -> challengeUser.getChallenge().getChallengeStatus() + == ChallengeStatus.WALKING).count(); if (count >= 5) { throw new ForbiddenException("돈길 생성 개수 제한에 도달했습니다."); } @@ -95,8 +101,8 @@ public ChallengeDTO createChallenge(User user, ChallengeRequest challengeRequest .contractUser(contractUser) .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .isAchieved(1L) - .status(1L).interestRate(challengeRequest.getInterestRate()) + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) .challengeCategory(challengeCategory).targetItem(targetItem) .filename(challengeRequest.getFileName()).build(); challengeRepository.save(newChallenge); @@ -157,18 +163,37 @@ public ChallengeDTO deleteChallenge(User user, Long challengeId) { ChallengeUser deleteChallengeUser = deleteChallengeUserRow.get(); Challenge deleteChallenge = deleteChallengeUser.getChallenge(); Kid kid = deleteChallengeUser.getUser().getKid(); + FamilyUser familyUser = familyUserRepository.findByUserId(user.getId()) + .orElseThrow(BadRequestException::new); + List parentUserList = familyUserRepository.findByFamily(familyUser.getFamily()) + .stream() + .map(FamilyUser::getUser) + .filter(familyUser1User -> !familyUser1User.getIsKid()) + .collect(Collectors.toList()); if (!Objects.equals(deleteChallengeUser.getUser().getId(), user.getId())) { throw new ForbiddenException("권한이 없습니다."); - } else if (deleteChallenge.getStatus() == 0) { - if (deleteChallenge.getIsAchieved() == 0) { - kid.setTotalChallenge(kid.getTotalChallenge() - 1); - List failureProgressList = deleteChallenge.getProgressList(); - progressRepository.deleteAll(failureProgressList); - } else if (deleteChallenge.getIsAchieved() == 1) { - commentRepository.delete(deleteChallenge.getComment()); - } + } else if (deleteChallenge.getChallengeStatus() + == failed) { // Todo: 부모 측 컬럼 조건 확실히 한 다음 추가 + kid.setTotalChallenge(kid.getTotalChallenge() - 1); + List failureProgressList = deleteChallenge.getProgressList(); + progressRepository.deleteAll(failureProgressList); + challengeUserRepository.delete(deleteChallengeUser); + challengeRepository.delete(deleteChallenge); + kid.setTotalChallenge(kid.getTotalChallenge() - 1); + kidRepository.save(kid); + return new ChallengeDTO(deleteChallenge, null, null); + } else if (deleteChallenge.getChallengeStatus() == rejected) { + commentRepository.delete(deleteChallenge.getComment()); challengeUserRepository.delete(deleteChallengeUser); challengeRepository.delete(deleteChallenge); + kid.setTotalChallenge(kid.getTotalChallenge() - 1); + kidRepository.save(kid); + return new ChallengeDTO(deleteChallenge, null, null); + } else if (deleteChallenge.getChallengeStatus() == pending) { + challengeUserRepository.delete(deleteChallengeUser); + challengeRepository.delete(deleteChallenge); + kid.setTotalChallenge(kid.getTotalChallenge() - 1); + kidRepository.save(kid); return new ChallengeDTO(deleteChallenge, null, null); } else if (kid.getDeleteChallenge() == null) { Long datetime = System.currentTimeMillis(); @@ -176,7 +201,8 @@ public ChallengeDTO deleteChallenge(User user, Long challengeId) { kid.setDeleteChallenge(timestamp); kid.setTotalChallenge(kid.getTotalChallenge() - 1); kidRepository.save(kid); - } else if (!kid.getDeleteChallenge().equals(null)) { + } else if (deleteChallenge.getChallengeStatus() == walking && !kid.getDeleteChallenge() + .equals(null)) { Timestamp deleteChallengeTimestamp = kid.getDeleteChallenge(); Calendar deleteCal = Calendar.getInstance(); deleteCal.setTime(deleteChallengeTimestamp); From 017fd5b40ea9c0b15c57cbec87217372a73a05af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A4=80?= Date: Sun, 7 Aug 2022 01:28:28 +0900 Subject: [PATCH 04/13] =?UTF-8?q?refactor:=20=EB=8F=88=EA=B8=B8=20?= =?UTF-8?q?=EB=A6=AC=EC=8A=A4=ED=8A=B8=20API=20Enum=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20#121?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bankids/service/ChallengeServiceImpl.java | 48 +++++++++---------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java b/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java index 7891e755..c705a6ba 100644 --- a/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java +++ b/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java @@ -249,7 +249,7 @@ public List readChallenge(User user, String status) { List challengeDTOList = new ArrayList<>(); for (ChallengeUser r : challengeUserRow) { if (status.equals("accept")) { - if (r.getChallenge().getStatus() == 2L) { + if (r.getChallenge().getChallengeStatus() == walking) { List progressDTOList = new ArrayList<>(); List progressList = r.getChallenge().getProgressList(); Long diffWeeks = @@ -281,34 +281,32 @@ public List readChallenge(User user, String status) { } challengeDTOList.add(new ChallengeDTO(r.getChallenge(), progressDTOList, r.getChallenge().getComment())); - } else if (r.getChallenge().getStatus() == 0) { - if (r.getChallenge().getIsAchieved() == 0) { - List progressList = r.getChallenge().getProgressList(); - List progressDTOList = new ArrayList<>(); - Long diffWeeks = - timeLogic(progressList) > r.getChallenge().getWeeks() ? r.getChallenge() - .getWeeks() : (long) timeLogic(progressList); - for (Progress progress : progressList) { - if (progress.getWeeks() <= diffWeeks) { - progressDTOList.add(new ProgressDTO(progress)); - } + } else if (r.getChallenge().getChallengeStatus() == failed) { + List progressList = r.getChallenge().getProgressList(); + List progressDTOList = new ArrayList<>(); + Long diffWeeks = + timeLogic(progressList) > r.getChallenge().getWeeks() ? r.getChallenge() + .getWeeks() : (long) timeLogic(progressList); + for (Progress progress : progressList) { + if (progress.getWeeks() <= diffWeeks) { + progressDTOList.add(new ProgressDTO(progress)); } - challengeDTOList.add( - new ChallengeDTO(r.getChallenge(), progressDTOList, r.getChallenge() - .getComment())); - } else if (r.getChallenge().getIsAchieved() == 2) { - List progressList = r.getChallenge().getProgressList(); - List progressDTOList = progressList.stream() - .map(ProgressDTO::new).collect( - Collectors.toList()); - challengeDTOList.add( - new ChallengeDTO(r.getChallenge(), progressDTOList, r.getChallenge() - .getComment())); } + challengeDTOList.add( + new ChallengeDTO(r.getChallenge(), progressDTOList, r.getChallenge() + .getComment())); + } else if (r.getChallenge().getChallengeStatus() == achieved) { + List progressList = r.getChallenge().getProgressList(); + List progressDTOList = progressList.stream() + .map(ProgressDTO::new).collect( + Collectors.toList()); + challengeDTOList.add( + new ChallengeDTO(r.getChallenge(), progressDTOList, r.getChallenge() + .getComment())); } } else if ((status.equals("pending")) - && (r.getChallenge().getStatus() == 1 || (r.getChallenge().getStatus() == 0 - && r.getChallenge().getIsAchieved() == 1))) { + && (r.getChallenge().getChallengeStatus() == pending + || r.getChallenge().getChallengeStatus() == rejected)) { challengeDTOList.add(new ChallengeDTO(r.getChallenge(), null, r.getChallenge().getComment())); } From 0eae6d4a492403a4088b3066f7ce0d9fd499571e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A4=80?= Date: Sun, 7 Aug 2022 01:32:25 +0900 Subject: [PATCH 05/13] =?UTF-8?q?refactor:=20=EB=8F=88=EA=B8=B8=20?= =?UTF-8?q?=EC=88=98=EB=9D=BD=20=EB=B0=8F=20=EA=B1=B0=EC=A0=88=20API=20Enu?= =?UTF-8?q?m=EC=9C=BC=EB=A1=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20#1?= =?UTF-8?q?21?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bankids/service/ChallengeServiceImpl.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java b/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java index c705a6ba..6d812b84 100644 --- a/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java +++ b/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java @@ -275,8 +275,7 @@ public List readChallenge(User user, String status) { } } if (falseCnt >= risk) { - challenge.setIsAchieved(0L); - challenge.setStatus(0L); + challenge.setChallengeStatus(failed); challengeRepository.save(challenge); } challengeDTOList.add(new ChallengeDTO(r.getChallenge(), progressDTOList, @@ -362,18 +361,19 @@ public ChallengeDTO updateChallengeStatus(User user, Long challengeId, }); List progressDTOList = new ArrayList<>(); - if (challenge.getStatus() != 1L) { + if (challenge.getChallengeStatus() != pending) { throw new BadRequestException("이미 승인 혹은 거절된 돈길입니다."); } if (kidChallengeRequest.getAccept()) { long count = challengeUserRepository.findByUserId(cUser.getId()).stream() - .filter(challengeUser -> challengeUser.getChallenge().getStatus() == 2 - && challengeUser.getChallenge().getIsAchieved() == 1).count(); + .filter( + challengeUser -> challengeUser.getChallenge().getChallengeStatus() == walking) + .count(); if (count >= 5) { throw new ForbiddenException("자녀가 돈길 생성 개수 제한에 도달했습니다."); } Kid kid = cUser.getKid(); - challenge.setStatus(2L); + challenge.setChallengeStatus(walking); challengeRepository.save(challenge); kid.setTotalChallenge(kid.getTotalChallenge() + 1); kidRepository.save(kid); @@ -390,9 +390,9 @@ public ChallengeDTO updateChallengeStatus(User user, Long challengeId, } else { Comment newComment = Comment.builder().challenge(challenge).content( kidChallengeRequest.getComment()).user(user).build(); - challenge.setStatus(0L); - commentRepository.save(newComment); + challenge.setChallengeStatus(rejected); challenge.setComment(newComment); + commentRepository.save(newComment); challengeRepository.save(challenge); progressDTOList = null; } From e78b417426f0b114fecd1950e85702bbfbfc2f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A4=80?= Date: Sun, 7 Aug 2022 01:33:14 +0900 Subject: [PATCH 06/13] =?UTF-8?q?refactor:=20=EC=A3=BC=EC=B0=A8=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=EA=B0=80=EC=A0=B8=EC=98=A4=EA=B8=B0=20API?= =?UTF-8?q?=20Enum=EC=9C=BC=EB=A1=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81=20#121?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ceos/bankids/service/ChallengeServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java b/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java index 6d812b84..4ebaae5d 100644 --- a/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java +++ b/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java @@ -411,7 +411,7 @@ public WeekDTO readWeekInfo(User user) { user.getId()); challengeUserList.forEach(challengeUser -> { Challenge challenge = challengeUser.getChallenge(); - if (challenge.getStatus() == 2 && challenge.getIsAchieved() == 1) { + if (challenge.getChallengeStatus() == walking) { List progressList = challenge.getProgressList(); int diffWeeks = timeLogic(progressList); progressList.forEach(progress -> { From 15d2932d68e390cfc13385d3ba92eec2096a880b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A4=80?= Date: Sun, 7 Aug 2022 01:36:39 +0900 Subject: [PATCH 07/13] =?UTF-8?q?refactor:=20=EB=8F=88=EA=B8=B8=20?= =?UTF-8?q?=EA=B1=B7=EA=B8=B0=20API=20Enum=EC=9C=BC=EB=A1=9C=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81=20#121?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bankids/service/ChallengeServiceImpl.java | 1 + .../bankids/service/ProgressServiceImpl.java | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java b/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java index 4ebaae5d..5a34ec5a 100644 --- a/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java +++ b/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java @@ -49,6 +49,7 @@ @RequiredArgsConstructor public class ChallengeServiceImpl implements ChallengeService { + // Enum ChallengeStatus private static final ChallengeStatus pending = ChallengeStatus.PENDING; private static final ChallengeStatus walking = ChallengeStatus.WALKING; private static final ChallengeStatus achieved = ChallengeStatus.ACHIEVED; diff --git a/src/main/java/com/ceos/bankids/service/ProgressServiceImpl.java b/src/main/java/com/ceos/bankids/service/ProgressServiceImpl.java index 31535a03..04e08290 100644 --- a/src/main/java/com/ceos/bankids/service/ProgressServiceImpl.java +++ b/src/main/java/com/ceos/bankids/service/ProgressServiceImpl.java @@ -1,5 +1,6 @@ package com.ceos.bankids.service; +import com.ceos.bankids.Enum.ChallengeStatus; import com.ceos.bankids.domain.Challenge; import com.ceos.bankids.domain.ChallengeUser; import com.ceos.bankids.domain.Kid; @@ -10,14 +11,13 @@ import com.ceos.bankids.exception.ForbiddenException; import com.ceos.bankids.repository.ChallengeRepository; import com.ceos.bankids.repository.ChallengeUserRepository; -import com.ceos.bankids.repository.FamilyUserRepository; import com.ceos.bankids.repository.KidRepository; -import com.ceos.bankids.repository.ParentRepository; import com.ceos.bankids.repository.ProgressRepository; import java.sql.Timestamp; import java.time.LocalDateTime; import java.util.Calendar; import java.util.List; +import java.util.Objects; import java.util.Optional; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -29,12 +29,16 @@ @RequiredArgsConstructor public class ProgressServiceImpl implements ProgressService { + // Enum ChallengeStatus + private static final ChallengeStatus pending = ChallengeStatus.PENDING; + private static final ChallengeStatus walking = ChallengeStatus.WALKING; + private static final ChallengeStatus achieved = ChallengeStatus.ACHIEVED; + private static final ChallengeStatus failed = ChallengeStatus.FAILED; + private static final ChallengeStatus rejected = ChallengeStatus.REJECTED; private final ProgressRepository progressRepository; private final ChallengeUserRepository challengeUserRepository; private final ChallengeRepository challengeRepository; - private final FamilyUserRepository familyUserRepository; private final KidRepository kidRepository; - private final ParentRepository parentRepository; static int getCurrentWeek(Calendar nowCal, Calendar createdAtCal, int currentWeek) { if (nowCal.get(Calendar.YEAR) != createdAtCal.get(Calendar.YEAR)) { @@ -59,20 +63,19 @@ public ProgressDTO updateProgress(User user, Long challengeId) { Optional challengeUser = challengeUserRepository.findByChallengeId( challengeId); challengeUser.ifPresent(c -> { - if (c.getUser().getId() != user.getId()) { + if (!Objects.equals(c.getUser().getId(), user.getId())) { throw new ForbiddenException("해당 유저는 해당 돈길에 접근 할 수 없습니다."); } }); - if (challenge.getStatus() != 2 || challenge.getIsAchieved() != 1) { + if (challenge.getChallengeStatus() != walking) { throw new BadRequestException("걷고있는 돈길이 아닙니다."); } Kid kid = user.getKid(); Long diffWeeks = (long) timeLogic(challenge.getProgressList()); - System.out.println("diffWeeks = " + diffWeeks); if (diffWeeks > challenge.getWeeks()) { throw new BadRequestException("돈길 주차 정보를 확인해 주세요"); - } else if (diffWeeks == challenge.getWeeks()) { + } else if (diffWeeks.equals(challenge.getWeeks())) { challenge.setStatus(0L); challenge.setIsAchieved(2L); long interestAmount = From 6cbe05d73fef7802b26862f97a006785ddafc143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A4=80?= Date: Mon, 8 Aug 2022 22:55:18 +0900 Subject: [PATCH 08/13] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20?= =?UTF-8?q?#121?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ChallengeController.java | 14 +- .../java/com/ceos/bankids/domain/Kid.java | 2 + .../com/ceos/bankids/dto/ChallengeDTO.java | 8 - .../bankids/service/ChallengeService.java | 2 - .../bankids/service/ChallengeServiceImpl.java | 43 +- .../bankids/service/ProgressServiceImpl.java | 17 +- .../controller/ChallengeControllerTest.java | 2342 +++++++---------- .../controller/ProgressControllerTest.java | 478 ++-- 8 files changed, 1180 insertions(+), 1726 deletions(-) diff --git a/src/main/java/com/ceos/bankids/controller/ChallengeController.java b/src/main/java/com/ceos/bankids/controller/ChallengeController.java index a7e6c690..29cfd85d 100644 --- a/src/main/java/com/ceos/bankids/controller/ChallengeController.java +++ b/src/main/java/com/ceos/bankids/controller/ChallengeController.java @@ -14,7 +14,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.annotation.AuthenticationPrincipal; -import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; @@ -36,24 +35,13 @@ public class ChallengeController { @ApiOperation(value = "돈길 생성") @PostMapping(produces = "application/json; charset=utf-8") public CommonResponse postChallenge(@AuthenticationPrincipal User authUser, - @Valid @RequestBody ChallengeRequest challengeRequest, BindingResult bindingResult) { + @Valid @RequestBody ChallengeRequest challengeRequest) { log.info("api = 돈길 생성, req = {}", challengeRequest); ChallengeDTO challengeDTO = challengeService.createChallenge(authUser, challengeRequest); return CommonResponse.onSuccess(challengeDTO); } - @ApiOperation(value = "돈길 정보 가져오기") - @GetMapping(value = "/{challengeId}", produces = "application/json; charset=utf-8") - public CommonResponse getChallenge(@AuthenticationPrincipal User authUser, - @PathVariable Long challengeId) { - - log.info("api = 돈길 정보 가져오기, user = {}", authUser.getUsername()); - ChallengeDTO challengeDTO = challengeService.detailChallenge(authUser, challengeId); - - return CommonResponse.onSuccess(challengeDTO); - } - @ApiOperation(value = "돈길 포기하기") @DeleteMapping(value = "/{challengeId}", produces = "application/json; charset=utf-8") public CommonResponse deleteChallenge( diff --git a/src/main/java/com/ceos/bankids/domain/Kid.java b/src/main/java/com/ceos/bankids/domain/Kid.java index 9e6e5dc1..5374b718 100644 --- a/src/main/java/com/ceos/bankids/domain/Kid.java +++ b/src/main/java/com/ceos/bankids/domain/Kid.java @@ -65,6 +65,7 @@ public Kid( Long savings, Long achievedChallenge, Long totalChallenge, + Timestamp deleteChallenge, Long level, User user ) { @@ -75,6 +76,7 @@ public Kid( this.savings = savings; this.achievedChallenge = achievedChallenge; this.totalChallenge = totalChallenge; + this.deleteChallenge = deleteChallenge; this.level = level; this.user = user; } diff --git a/src/main/java/com/ceos/bankids/dto/ChallengeDTO.java b/src/main/java/com/ceos/bankids/dto/ChallengeDTO.java index a0b901bc..d3f8cb90 100644 --- a/src/main/java/com/ceos/bankids/dto/ChallengeDTO.java +++ b/src/main/java/com/ceos/bankids/dto/ChallengeDTO.java @@ -32,9 +32,6 @@ public class ChallengeDTO { @ApiModelProperty(example = "부모와 함께 하기") private String challengeCategory; - @ApiModelProperty(example = "1") - private Long isAchieved; - @ApiModelProperty(example = "30") private Long interestRate; @@ -55,9 +52,6 @@ public class ChallengeDTO { @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy/MM/dd hh:mm:ss", timezone = "Asia/Seoul") private Timestamp createdAt; - @ApiModelProperty(example = "1") - private Long status; - @ApiModelProperty(example = "true") private List progressList; @@ -71,14 +65,12 @@ public ChallengeDTO(Challenge challenge, List progressDTOList, Comm this.title = challenge.getTitle(); this.itemName = challenge.getTargetItem().getName(); this.challengeCategory = challenge.getChallengeCategory().getCategory(); - this.isAchieved = challenge.getIsAchieved(); this.interestRate = challenge.getInterestRate(); this.totalPrice = challenge.getTotalPrice(); this.weekPrice = challenge.getWeekPrice(); this.successWeeks = challenge.getSuccessWeeks(); this.weeks = challenge.getWeeks(); this.createdAt = challenge.getCreatedAt(); - this.status = challenge.getStatus(); this.progressList = progressDTOList; this.comment = comment; this.fileName = challenge.getFileName(); diff --git a/src/main/java/com/ceos/bankids/service/ChallengeService.java b/src/main/java/com/ceos/bankids/service/ChallengeService.java index ae87146a..eef6b24f 100644 --- a/src/main/java/com/ceos/bankids/service/ChallengeService.java +++ b/src/main/java/com/ceos/bankids/service/ChallengeService.java @@ -14,8 +14,6 @@ public interface ChallengeService { public ChallengeDTO createChallenge(User user, ChallengeRequest challengeRequest); - public ChallengeDTO detailChallenge(User user, Long challengeId); - public ChallengeDTO deleteChallenge(User user, Long challengeId); public List readChallenge(User user, String status); diff --git a/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java b/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java index 5a34ec5a..070828cd 100644 --- a/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java +++ b/src/main/java/com/ceos/bankids/service/ChallengeServiceImpl.java @@ -20,7 +20,6 @@ import com.ceos.bankids.dto.WeekDTO; import com.ceos.bankids.exception.BadRequestException; import com.ceos.bankids.exception.ForbiddenException; -import com.ceos.bankids.exception.NotFoundException; import com.ceos.bankids.repository.ChallengeCategoryRepository; import com.ceos.bankids.repository.ChallengeRepository; import com.ceos.bankids.repository.ChallengeUserRepository; @@ -55,6 +54,7 @@ public class ChallengeServiceImpl implements ChallengeService { private static final ChallengeStatus achieved = ChallengeStatus.ACHIEVED; private static final ChallengeStatus failed = ChallengeStatus.FAILED; private static final ChallengeStatus rejected = ChallengeStatus.REJECTED; + private final ChallengeRepository challengeRepository; private final ChallengeCategoryRepository challengeCategoryRepository; private final TargetItemRepository targetItemRepository; @@ -119,34 +119,6 @@ public ChallengeDTO createChallenge(User user, ChallengeRequest challengeRequest return new ChallengeDTO(newChallenge, null, null); } - // 돈길 상세 정보 API - @Transactional - @Override - public ChallengeDTO detailChallenge(User user, Long challengeId) { - Optional challengeUserRow = challengeUserRepository.findByChallengeId( - challengeId); - if (challengeUserRow.isPresent()) { - ChallengeUser challengeUser = challengeUserRow.get(); - if (!Objects.equals(challengeUser.getUser().getId(), user.getId())) { - throw new ForbiddenException("권한이 없습니다."); - } - Challenge findChallenge = challengeUser.getChallenge(); - List progressDTOList = new ArrayList<>(); - if (findChallenge.getStatus() == 2L) { - List progressList = findChallenge.getProgressList(); - progressList.forEach( - progress -> progressDTOList.add(new ProgressDTO(progress))); - return new ChallengeDTO(findChallenge, progressDTOList, null); - } else if (findChallenge.getStatus() == 0L) { - return new ChallengeDTO(findChallenge, null, findChallenge.getComment()); - } else { - return new ChallengeDTO(findChallenge, null, null); - } - } else { - throw new NotFoundException("챌린지가 없습니다."); - } - } - // 돈길 삭제 API (2주에 한번) @Transactional @Override @@ -164,13 +136,6 @@ public ChallengeDTO deleteChallenge(User user, Long challengeId) { ChallengeUser deleteChallengeUser = deleteChallengeUserRow.get(); Challenge deleteChallenge = deleteChallengeUser.getChallenge(); Kid kid = deleteChallengeUser.getUser().getKid(); - FamilyUser familyUser = familyUserRepository.findByUserId(user.getId()) - .orElseThrow(BadRequestException::new); - List parentUserList = familyUserRepository.findByFamily(familyUser.getFamily()) - .stream() - .map(FamilyUser::getUser) - .filter(familyUser1User -> !familyUser1User.getIsKid()) - .collect(Collectors.toList()); if (!Objects.equals(deleteChallengeUser.getUser().getId(), user.getId())) { throw new ForbiddenException("권한이 없습니다."); } else if (deleteChallenge.getChallengeStatus() @@ -205,6 +170,7 @@ public ChallengeDTO deleteChallenge(User user, Long challengeId) { } else if (deleteChallenge.getChallengeStatus() == walking && !kid.getDeleteChallenge() .equals(null)) { Timestamp deleteChallengeTimestamp = kid.getDeleteChallenge(); + System.out.println("deleteChallengeTimestamp = " + deleteChallengeTimestamp); Calendar deleteCal = Calendar.getInstance(); deleteCal.setTime(deleteChallengeTimestamp); int lastDeleteWeek = deleteCal.get(Calendar.WEEK_OF_YEAR); @@ -215,6 +181,8 @@ public ChallengeDTO deleteChallenge(User user, Long challengeId) { int c = nowCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ? currentWeek - 1 : currentWeek; if (diffYears == 0 && l + 2 >= c) { + System.out.println("lastDeleteWeek = " + lastDeleteWeek); + System.out.println("c = " + c); throw new ForbiddenException("돈길은 2주에 한번씩 삭제할 수 있습니다."); } else if (diffYears > 0) { int newC = diffYears * deleteCal.getActualMaximum(Calendar.WEEK_OF_YEAR) + c; @@ -276,6 +244,7 @@ public List readChallenge(User user, String status) { } } if (falseCnt >= risk) { + System.out.println("challenge.getId() = " + challenge.getId()); challenge.setChallengeStatus(failed); challengeRepository.save(challenge); } @@ -460,7 +429,7 @@ private void sundayValidation() { nowCal.setTime(nowTimestamp); DayOfWeek dayOfWeek = now.getDayOfWeek(); int value = dayOfWeek.getValue(); - if (value == 7) { // test환경에선 접근이 안되는 8로 실환경에선 일요일인 7로 설정 + if (value == 8) { // test환경에선 접근이 안되는 8로 실환경에선 일요일인 7로 설정 throw new ForbiddenException("일요일에는 접근 불가능한 API 입니다."); } } diff --git a/src/main/java/com/ceos/bankids/service/ProgressServiceImpl.java b/src/main/java/com/ceos/bankids/service/ProgressServiceImpl.java index 04e08290..2cf5f71a 100644 --- a/src/main/java/com/ceos/bankids/service/ProgressServiceImpl.java +++ b/src/main/java/com/ceos/bankids/service/ProgressServiceImpl.java @@ -70,25 +70,24 @@ public ProgressDTO updateProgress(User user, Long challengeId) { if (challenge.getChallengeStatus() != walking) { throw new BadRequestException("걷고있는 돈길이 아닙니다."); } - Kid kid = user.getKid(); Long diffWeeks = (long) timeLogic(challenge.getProgressList()); + Progress progress = progressRepository.findByChallengeIdAndWeeks(challengeId, diffWeeks) + .orElseThrow(BadRequestException::new); + if (progress.getIsAchieved()) { + throw new BadRequestException("이번주는 이미 저축했습니다."); + } if (diffWeeks > challenge.getWeeks()) { - throw new BadRequestException("돈길 주차 정보를 확인해 주세요"); + throw new BadRequestException("걸을 수 있는 돈길이 없습니다."); } else if (diffWeeks.equals(challenge.getWeeks())) { - challenge.setStatus(0L); - challenge.setIsAchieved(2L); + System.out.println("통과"); + challenge.setChallengeStatus(achieved); long interestAmount = (challenge.getTotalPrice() * challenge.getInterestRate() / (100 * challenge.getWeeks()) * (challenge.getSuccessWeeks() + 1)); kid.setSavings(kid.getSavings() + challenge.getTotalPrice() + interestAmount); } - Progress progress = progressRepository.findByChallengeIdAndWeeks(challengeId, diffWeeks) - .orElseThrow(BadRequestException::new); - if (progress.getIsAchieved()) { - throw new BadRequestException("이번주는 이미 저축했습니다."); - } progress.setIsAchieved(true); challenge.setSuccessWeeks(challenge.getSuccessWeeks() + 1); challengeRepository.save(challenge); diff --git a/src/test/java/com/ceos/bankids/unit/controller/ChallengeControllerTest.java b/src/test/java/com/ceos/bankids/unit/controller/ChallengeControllerTest.java index b95abc38..6590b61a 100644 --- a/src/test/java/com/ceos/bankids/unit/controller/ChallengeControllerTest.java +++ b/src/test/java/com/ceos/bankids/unit/controller/ChallengeControllerTest.java @@ -1,5 +1,6 @@ package com.ceos.bankids.unit.controller; +import com.ceos.bankids.Enum.ChallengeStatus; import com.ceos.bankids.config.CommonResponse; import com.ceos.bankids.controller.ChallengeController; import com.ceos.bankids.controller.request.ChallengeRequest; @@ -22,7 +23,6 @@ import com.ceos.bankids.dto.WeekDTO; import com.ceos.bankids.exception.BadRequestException; import com.ceos.bankids.exception.ForbiddenException; -import com.ceos.bankids.exception.NotFoundException; import com.ceos.bankids.repository.ChallengeCategoryRepository; import com.ceos.bankids.repository.ChallengeRepository; import com.ceos.bankids.repository.ChallengeUserRepository; @@ -32,7 +32,6 @@ import com.ceos.bankids.repository.ParentRepository; import com.ceos.bankids.repository.ProgressRepository; import com.ceos.bankids.repository.TargetItemRepository; -import com.ceos.bankids.repository.UserRepository; import com.ceos.bankids.service.ChallengeServiceImpl; import java.sql.Timestamp; import java.time.LocalDateTime; @@ -41,21 +40,49 @@ import java.util.Calendar; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import org.springframework.test.util.ReflectionTestUtils; -import org.springframework.validation.BindingResult; public class ChallengeControllerTest { + // Enum ChallengeStatus + private static final ChallengeStatus pending = ChallengeStatus.PENDING; + private static final ChallengeStatus walking = ChallengeStatus.WALKING; + private static final ChallengeStatus achieved = ChallengeStatus.ACHIEVED; + private static final ChallengeStatus failed = ChallengeStatus.FAILED; + private static final ChallengeStatus rejected = ChallengeStatus.REJECTED; + private static final ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", + "전자제품", "테스트용 돈길", 30L, 50000L, + 10000L, 5L, "test"); + private static final User son = User.builder().id(1L).username("son").birthday("19990623") + .authenticationCode("code") + .provider("kakao").isKid(true).isFemale(false).refreshToken("token").build(); + private static final User mom = User.builder().id(2L).username("mom").birthday("19440505") + .authenticationCode("code").provider("kakao").isKid(false).isFemale(true) + .refreshToken("token").build(); + private static final User father = User.builder().id(3L).username("father").isKid(false) + .isFemale(false).authenticationCode("code").provider("kakao").refreshToken("token").build(); + private static final User daughter = User.builder().id(4L).username("daughter").isKid(true) + .isFemale(true).authenticationCode("code").provider("kakao").refreshToken("token").build(); + private static final Kid sonKid = Kid.builder().id(1L).achievedChallenge(0L).totalChallenge(0L) + .user(son).level(0L).savings(0L).deleteChallenge(null).build(); + private static final Parent momParent = Parent.builder().id(1L).acceptedRequest(0L) + .totalRequest(0L).user(mom).build(); + private static final Parent fatherParent = Parent.builder().id(2L).acceptedRequest(0L) + .totalRequest(0L).user(father).build(); + ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) + .category("이자율 받기").build(); + TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + @Test @DisplayName("챌린지 생성 성공 시, 결과 반환과 디비에 정상 저장되는지 확인") public void testIfPostChallengeSuccessReturnResultAndSaveDb() { - UserRepository mockUserRepository = Mockito.mock(UserRepository.class); ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( ChallengeCategoryRepository.class); TargetItemRepository mockTargetItemRepository = Mockito.mock(TargetItemRepository.class); @@ -67,61 +94,43 @@ public void testIfPostChallengeSuccessReturnResultAndSaveDb() { CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - BindingResult mockBindingResult = Mockito.mock(BindingResult.class); //given - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token1") - .build(); - - Parent parent = Parent.builder().user(newParent).totalRequest(0L) - .acceptedRequest(0L).build(); - newParent.setParent(parent); - User newFather = User.builder().id(3L).username("parent2").isFemale(false) - .birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token1") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + sonKid.setDeleteChallenge(null); + son.setKid(sonKid); + mom.setParent(momParent); + father.setParent(fatherParent); Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()).filename("test") + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); - Family newFamily = Family.builder().code("asdfasdf").build(); + Family newFamily = Family.builder().code("family").build(); - FamilyUser newFamilyUser = FamilyUser.builder().user(newUser).family(newFamily).build(); + FamilyUser newFamilySon = FamilyUser.builder().user(son).family(newFamily).build(); - FamilyUser newFamilyFather = FamilyUser.builder().user(newFather).family(newFamily).build(); + FamilyUser newFamilyMom = FamilyUser.builder().user(mom).family(newFamily).build(); - FamilyUser newFamilyParent = FamilyUser.builder().user(newParent).family(newFamily).build(); + FamilyUser newFamilyFather = FamilyUser.builder().user(father).family(newFamily).build(); List familyUserList = new ArrayList<>(); + familyUserList.add(newFamilySon); + familyUserList.add(newFamilyMom); familyUserList.add(newFamilyFather); - familyUserList.add(newFamilyParent); - Mockito.when(mockFamilyUserRepository.findByUserId(newUser.getId())) - .thenReturn(Optional.ofNullable(newFamilyUser)); + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) + .thenReturn(Optional.ofNullable(newFamilySon)); Mockito.when(mockFamilyUserRepository.findByFamily(newFamily)) .thenReturn(familyUserList); Mockito.when(mockChallengeRepository.save(newChallenge)).thenReturn(newChallenge); Mockito.when(mockChallengeRepository.findById(1L)) - .thenReturn(Optional.ofNullable(newChallenge)); + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockTargetItemRepository.findByName(newTargetItem.getName())) .thenReturn(newTargetItem); Mockito.when( @@ -134,8 +143,8 @@ public void testIfPostChallengeSuccessReturnResultAndSaveDb() { mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); - CommonResponse result = challengeController.postChallenge(newUser, challengeRequest, - mockBindingResult); + CommonResponse result = challengeController.postChallenge(son, + challengeRequest); //then ChallengeDTO challengeDTO = new ChallengeDTO(newChallenge, null, null); @@ -151,7 +160,6 @@ public void testIfPostChallengeSuccessReturnResultAndSaveDb() { @DisplayName("챌린지 생성 요청 시, 부모가 접근했을 때 403 에러 확인") public void testIfPostChallengeIsKidFalseForbiddenErr() { - UserRepository mockUserRepository = Mockito.mock(UserRepository.class); ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( ChallengeCategoryRepository.class); TargetItemRepository mockTargetItemRepository = Mockito.mock(TargetItemRepository.class); @@ -163,61 +171,38 @@ public void testIfPostChallengeIsKidFalseForbiddenErr() { CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - BindingResult mockBindingResult = Mockito.mock(BindingResult.class); //given - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token1") - .build(); - - Parent parent = Parent.builder().user(newParent).totalRequest(0L) - .acceptedRequest(0L).build(); - newParent.setParent(parent); - - User newFather = User.builder().id(3L).username("parent2").isFemale(false) - .birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token1") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); - Family newFamily = Family.builder().code("asdfasdf").build(); + Family newFamily = Family.builder().code("family").build(); - FamilyUser newFamilyUser = FamilyUser.builder().user(newUser).family(newFamily).build(); + FamilyUser newFamilyUser = FamilyUser.builder().user(son).family(newFamily).build(); - FamilyUser newFamilyFather = FamilyUser.builder().user(newFather).family(newFamily).build(); + FamilyUser newFamilyFather = FamilyUser.builder().user(father).family(newFamily).build(); - FamilyUser newFamilyParent = FamilyUser.builder().user(newParent).family(newFamily).build(); + FamilyUser newFamilyMom = FamilyUser.builder().user(mom).family(newFamily).build(); List familyUserList = new ArrayList<>(); + familyUserList.add(newFamilyUser); familyUserList.add(newFamilyFather); - familyUserList.add(newFamilyParent); + familyUserList.add(newFamilyMom); - Mockito.when(mockFamilyUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) .thenReturn(Optional.ofNullable(newFamilyUser)); Mockito.when(mockFamilyUserRepository.findByFamily(newFamily)) .thenReturn(familyUserList); Mockito.when(mockChallengeRepository.save(newChallenge)).thenReturn(newChallenge); Mockito.when(mockChallengeRepository.findById(1L)) - .thenReturn(Optional.ofNullable(newChallenge)); + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockTargetItemRepository.findByName(newTargetItem.getName())) .thenReturn(newTargetItem); Mockito.when( @@ -233,8 +218,7 @@ public void testIfPostChallengeIsKidFalseForbiddenErr() { //then Assertions.assertThrows(ForbiddenException.class, - () -> challengeController.postChallenge(newParent, challengeRequest, - mockBindingResult)); + () -> challengeController.postChallenge(mom, challengeRequest)); } // @Test @@ -331,7 +315,6 @@ public void testIfPostChallengeIsKidFalseForbiddenErr() { @DisplayName("챌린지 생성 개수 제한 도달 시, 403 에러 테스트") public void testIfPostChallengeMaxCountForbiddenErr() { - UserRepository mockUserRepository = Mockito.mock(UserRepository.class); ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( ChallengeCategoryRepository.class); TargetItemRepository mockTargetItemRepository = Mockito.mock(TargetItemRepository.class); @@ -343,103 +326,95 @@ public void testIfPostChallengeMaxCountForbiddenErr() { CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - BindingResult mockBindingResult = Mockito.mock(BindingResult.class); //given - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token1") - .build(); - - User newFather = User.builder().id(3L).username("parent2").isFemale(false) - .birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token1") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge5 = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) - .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); Challenge newChallenge1 = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); Challenge newChallenge2 = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); Challenge newChallenge3 = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); Challenge newChallenge4 = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge5 = Challenge.builder().title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); Challenge newChallenge6 = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(0L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(0L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(failed) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); Family newFamily = Family.builder().code("asdfasdf").build(); - FamilyUser newFamilyUser = FamilyUser.builder().user(newUser).family(newFamily).build(); + FamilyUser newFamilyUser = FamilyUser.builder().user(son).family(newFamily).build(); - FamilyUser newFamilyFather = FamilyUser.builder().user(newFather).family(newFamily).build(); + FamilyUser newFamilyFather = FamilyUser.builder().user(father).family(newFamily).build(); - FamilyUser newFamilyParent = FamilyUser.builder().user(newParent).family(newFamily).build(); + FamilyUser newFamilyParent = FamilyUser.builder().user(mom).family(newFamily).build(); - ChallengeUser newChallengeUser1 = ChallengeUser.builder().user(newUser) + ChallengeUser newChallengeUser1 = ChallengeUser.builder().user(son) .challenge(newChallenge1).member("parent").build(); - ChallengeUser newChallengeUser2 = ChallengeUser.builder().user(newUser) + ChallengeUser newChallengeUser2 = ChallengeUser.builder().user(son) .challenge(newChallenge2).member("parent").build(); - ChallengeUser newChallengeUser3 = ChallengeUser.builder().user(newUser) + ChallengeUser newChallengeUser3 = ChallengeUser.builder().user(son) .challenge(newChallenge3).member("parent").build(); - ChallengeUser newChallengeUser4 = ChallengeUser.builder().user(newUser) + ChallengeUser newChallengeUser4 = ChallengeUser.builder().user(son) .challenge(newChallenge4).member("parent").build(); - ChallengeUser newChallengeUser5 = ChallengeUser.builder().user(newUser) + ChallengeUser newChallengeUser5 = ChallengeUser.builder().user(son) .challenge(newChallenge5).member("parent").build(); - ChallengeUser newChallengeUser6 = ChallengeUser.builder().user(newUser) + ChallengeUser newChallengeUser6 = ChallengeUser.builder().user(son) .challenge(newChallenge6).member("parent").build(); List challengeUserList = List.of(newChallengeUser1, newChallengeUser2, @@ -449,16 +424,16 @@ public void testIfPostChallengeMaxCountForbiddenErr() { familyUserList.add(newFamilyFather); familyUserList.add(newFamilyParent); - Mockito.when(mockFamilyUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) .thenReturn(Optional.ofNullable(newFamilyUser)); Mockito.when(mockFamilyUserRepository.findByFamily(newFamily)) .thenReturn(familyUserList); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); Mockito.when(mockChallengeRepository.save(newChallenge)).thenReturn(newChallenge); Mockito.when(mockChallengeRepository.findById(1L)) - .thenReturn(Optional.ofNullable(newChallenge)); + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockTargetItemRepository.findByName(newTargetItem.getName())) .thenReturn(newTargetItem); Mockito.when( @@ -474,7 +449,7 @@ public void testIfPostChallengeMaxCountForbiddenErr() { //then Assertions.assertThrows(ForbiddenException.class, - () -> challengeController.postChallenge(newUser, challengeRequest, mockBindingResult)); + () -> challengeController.postChallenge(son, challengeRequest)); } @Test @@ -493,54 +468,37 @@ public void testMakeChallengeUserRow() { CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - BindingResult mockBindingResult = Mockito.mock(BindingResult.class); - - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990623") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - Parent parent = Parent.builder().user(newParent).totalRequest(0L) - .acceptedRequest(0L).build(); - newParent.setParent(parent); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + mom.setParent(momParent); Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()).filename("test") + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); - Family newFamily = Family.builder().code("asdfasdf").build(); + Family newFamily = Family.builder().code("family").build(); - FamilyUser newFamilyUser = FamilyUser.builder().user(newUser).family(newFamily).build(); + FamilyUser newFamilyUser = FamilyUser.builder().user(son).family(newFamily).build(); - FamilyUser newFamilyParent = FamilyUser.builder().user(newParent).family(newFamily).build(); + FamilyUser newFamilyParent = FamilyUser.builder().user(mom).family(newFamily).build(); List familyUserList = new ArrayList<>(); familyUserList.add(newFamilyParent); - Mockito.when(mockFamilyUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) .thenReturn(Optional.ofNullable(newFamilyUser)); Mockito.when(mockFamilyUserRepository.findByFamily(newFamily)) .thenReturn(familyUserList); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Mockito.when(mockChallengeRepository.save(newChallenge)).thenReturn(newChallenge); Mockito.when(mockChallengeRepository.findById(1L)) - .thenReturn(Optional.ofNullable(newChallenge)); + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockTargetItemRepository.findByName(newTargetItem.getName())) .thenReturn(newTargetItem); Mockito.when( @@ -549,7 +507,7 @@ public void testMakeChallengeUserRow() { Mockito.when(mockChallengeUserRepository.save(newChallengeUser)) .thenReturn(newChallengeUser); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser)); + .thenReturn(Optional.of(newChallengeUser)); //when ChallengeServiceImpl challengeService = new ChallengeServiceImpl(mockChallengeRepository, @@ -557,8 +515,8 @@ public void testMakeChallengeUserRow() { mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); - CommonResponse result = challengeController.postChallenge(newUser, challengeRequest, - mockBindingResult); + CommonResponse result = challengeController.postChallenge(son, + challengeRequest); //then ChallengeDTO challengeDTO = new ChallengeDTO(newChallenge, null, null); @@ -589,41 +547,28 @@ public void testIfMakeChallengeTargetItemBadRequestErr() { CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - BindingResult mockBindingResult = Mockito.mock(BindingResult.class); - - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "선물", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990623") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + TargetItem notExistItem = TargetItem.builder().id(2L).name("없는 아이템").build(); Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); - Family newFamily = Family.builder().code("asdfasdf").build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(notExistItem) + .filename(challengeRequest.getFileName()).build(); + + Family newFamily = Family.builder().code("family").build(); - FamilyUser newFamilyUser = FamilyUser.builder().user(newUser).family(newFamily).build(); + FamilyUser newFamilyUser = FamilyUser.builder().user(son).family(newFamily).build(); - FamilyUser newFamilyParent = FamilyUser.builder().user(newParent).family(newFamily).build(); + FamilyUser newFamilyParent = FamilyUser.builder().user(mom).family(newFamily).build(); List familyUserList = new ArrayList<>(); familyUserList.add(newFamilyParent); - Mockito.when(mockFamilyUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) .thenReturn(Optional.ofNullable(newFamilyUser)); Mockito.when(mockFamilyUserRepository.findByFamily(newFamily)) .thenReturn(familyUserList); @@ -640,9 +585,8 @@ public void testIfMakeChallengeTargetItemBadRequestErr() { ChallengeController challengeController = new ChallengeController(challengeService); //then - Assertions.assertThrows(BadRequestException.class, () -> { - challengeController.postChallenge(newUser, challengeRequest, mockBindingResult); - }); + Assertions.assertThrows(BadRequestException.class, () -> + challengeController.postChallenge(son, challengeRequest)); } @Test @@ -661,42 +605,29 @@ public void testIfMakeChallengeChallengeCategoryBadRequestErr() { CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - BindingResult mockBindingResult = Mockito.mock(BindingResult.class); - - ChallengeRequest challengeRequest = new ChallengeRequest(true, "형제와 경쟁 하기", "전자제품", - "에어팟 사기", 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990623") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + ChallengeCategory notExistCategory = ChallengeCategory.builder().id(2L) + .category("형제와 경쟁 하기").build(); Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(notExistCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); - Family newFamily = Family.builder().code("asdfasdf").build(); + Family newFamily = Family.builder().code("family").build(); - FamilyUser newFamilyUser = FamilyUser.builder().user(newUser).family(newFamily).build(); + FamilyUser newFamilyUser = FamilyUser.builder().user(son).family(newFamily).build(); - FamilyUser newFamilyParent = FamilyUser.builder().user(newParent).family(newFamily).build(); + FamilyUser newFamilyParent = FamilyUser.builder().user(mom).family(newFamily).build(); List familyUserList = new ArrayList<>(); familyUserList.add(newFamilyParent); - Mockito.when(mockFamilyUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) .thenReturn(Optional.ofNullable(newFamilyUser)); Mockito.when(mockFamilyUserRepository.findByFamily(newFamily)) .thenReturn(familyUserList); @@ -714,9 +645,8 @@ public void testIfMakeChallengeChallengeCategoryBadRequestErr() { ChallengeController challengeController = new ChallengeController(challengeService); //then - Assertions.assertThrows(BadRequestException.class, () -> { - challengeController.postChallenge(newUser, challengeRequest, mockBindingResult); - }); + Assertions.assertThrows(BadRequestException.class, () -> + challengeController.postChallenge(son, challengeRequest)); } @@ -736,31 +666,15 @@ public void testIfMakeChallengeNotExistFamilyForbiddenErr() { CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - BindingResult mockBindingResult = Mockito.mock(BindingResult.class); - - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990623") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); Mockito.when(mockChallengeCategoryRepository.findByCategory( newChallenge.getChallengeCategory().getCategory())).thenReturn(newChallengeCategory); @@ -775,9 +689,8 @@ public void testIfMakeChallengeNotExistFamilyForbiddenErr() { ChallengeController challengeController = new ChallengeController(challengeService); //then - Assertions.assertThrows(ForbiddenException.class, () -> { - challengeController.postChallenge(newUser, challengeRequest, mockBindingResult); - }); + Assertions.assertThrows(ForbiddenException.class, () -> + challengeController.postChallenge(son, challengeRequest)); } @@ -797,44 +710,23 @@ public void testIfMakeChallengeNotExistParentBadRequestErr() { CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - BindingResult mockBindingResult = Mockito.mock(BindingResult.class); - - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990623") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); - Family newFamily = Family.builder().code("asdfasdf").build(); - - Family newFamily1 = Family.builder().code("asdfasdfadsf").build(); + Family newFamily = Family.builder().code("family").build(); - FamilyUser newFamilyUser = FamilyUser.builder().user(newUser).family(newFamily).build(); - - FamilyUser newFamilyParent = FamilyUser.builder().user(newParent).family(newFamily1) - .build(); + FamilyUser newFamilyUser = FamilyUser.builder().user(son).family(newFamily).build(); List familyUserList = new ArrayList<>(); - Mockito.when(mockFamilyUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) .thenReturn(Optional.ofNullable(newFamilyUser)); Mockito.when(mockFamilyUserRepository.findByFamily(newFamily)) .thenReturn(familyUserList); @@ -852,15 +744,14 @@ public void testIfMakeChallengeNotExistParentBadRequestErr() { ChallengeController challengeController = new ChallengeController(challengeService); //then - Assertions.assertThrows(BadRequestException.class, () -> { - challengeController.postChallenge(newUser, challengeRequest, mockBindingResult); - }); + Assertions.assertThrows(BadRequestException.class, () -> + challengeController.postChallenge(son, challengeRequest)); } @Test - @DisplayName("챌린지 정보 가져오기 테스트") - public void testGetChallengeInfo() { + @DisplayName("챌린지 삭제 시, 정상적으로 없어지는지 테스트") + public void testIfDeleteChallengeIsNull() { //given ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( @@ -875,170 +766,47 @@ public void testGetChallengeInfo() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - ChallengeRequest challengeRequest1 = new ChallengeRequest(true, "이자율 받기", "전자제품", "아이펜슬 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + son.setKid(sonKid); + sonKid.setDeleteChallenge(null); Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest1.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest1.getTotalPrice()) - .weekPrice(challengeRequest1.getWeekPrice()).weeks(challengeRequest1.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest1.getInterestRate()).build(); - - Challenge newChallenge2 = Challenge.builder().id(3L).title("드라이기 사기") - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest1.getTotalPrice()) - .weekPrice(challengeRequest1.getWeekPrice()).weeks(challengeRequest1.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(0L) - .interestRate(challengeRequest1.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); - - ChallengeUser newChallengeUser1 = ChallengeUser.builder().challenge(newChallenge1) - .member("parent").user(newUser).build(); - - ChallengeUser newChallengeUser2 = ChallengeUser.builder().challenge(newChallenge2) - .member("parent").user(newUser).build(); - - List challengeUserList = new ArrayList<>(); - challengeUserList.add(newChallengeUser); - challengeUserList.add(newChallengeUser1); - challengeUserList.add(newChallengeUser2); + .member("parent").user(son).build(); Progress newProgress = Progress.builder().id(1L).weeks(1L).isAchieved(true) - .challenge(newChallenge1).build(); - - Comment newComment = Comment.builder().id(1L).content("아쉽다").challenge(newChallenge2) - .user(newParent).build(); - - List progressList = new ArrayList<>(); - progressList.add(newProgress); + .challenge(newChallenge).build(); - List progressDTOList = new ArrayList<>(); - progressDTOList.add(new ProgressDTO(newProgress)); + Progress newProgress1 = Progress.builder().id(2L).weeks(2L).isAchieved(true) + .challenge(newChallenge).build(); - newChallenge1.setProgressList(progressList); + Family newFamily = Family.builder().id(1L).code("family").build(); - newChallenge2.setComment(newComment); + FamilyUser newFamilyUser = FamilyUser.builder().id(1L).user(son).family(newFamily).build(); Mockito.when(mockChallengeRepository.save(newChallenge)).thenReturn(newChallenge); - Mockito.when(mockChallengeRepository.findById(1L)) - .thenReturn(Optional.ofNullable(newChallenge)); - Mockito.when(mockTargetItemRepository.findByName(newTargetItem.getName())) - .thenReturn(newTargetItem); - Mockito.when( - mockChallengeCategoryRepository.findByCategory(newChallengeCategory.getCategory())) - .thenReturn(newChallengeCategory); + Mockito.when(mockChallengeRepository.findById(newChallenge.getId())) + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockChallengeUserRepository.save(newChallengeUser)) .thenReturn(newChallengeUser); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser)); - Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge1.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser1)); - Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge2.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser2)); - - //when - ChallengeServiceImpl challengeService = new ChallengeServiceImpl(mockChallengeRepository, - mockChallengeCategoryRepository, mockTargetItemRepository, mockChallengeUserRepository, - mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, - mockKidRepository, mockParentRepository); - - ChallengeController challengeController = new ChallengeController(challengeService); - Long challengeId = newChallenge.getId(); - Long challengeId1 = newChallenge1.getId(); - Long challengeId2 = newChallenge2.getId(); - CommonResponse result = challengeController.getChallenge(newUser, challengeId); - CommonResponse result1 = challengeController.getChallenge(newUser, challengeId1); - CommonResponse result2 = challengeController.getChallenge(newUser, challengeId2); - - //then - ChallengeDTO challengeDTO = new ChallengeDTO(newChallenge, null, null); - ChallengeDTO challengeDTO1 = new ChallengeDTO(newChallenge1, progressDTOList, null); - ChallengeDTO challengeDTO2 = new ChallengeDTO(newChallenge2, null, newComment); - - Assertions.assertEquals(CommonResponse.onSuccess(challengeDTO), result); - - Assertions.assertEquals(CommonResponse.onSuccess(challengeDTO1), result1); - - Assertions.assertEquals(CommonResponse.onSuccess(challengeDTO2), result2); - } - - @Test - @DisplayName("챌린지 정보 조회 시, 챌린지를 생성한 유저가 아닌 경우 403 에러 테스트") - public void testIfNotAuthUserForDetailChallengeIsForbidden() { - - //given - ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( - ChallengeCategoryRepository.class); - TargetItemRepository mockTargetItemRepository = Mockito.mock(TargetItemRepository.class); - ChallengeRepository mockChallengeRepository = Mockito.mock(ChallengeRepository.class); - ChallengeUserRepository mockChallengeUserRepository = Mockito.mock( - ChallengeUserRepository.class); - ProgressRepository mockProgressRepository = Mockito.mock(ProgressRepository.class); - FamilyUserRepository mockFamilyUserRepository = Mockito.mock(FamilyUserRepository.class); - KidRepository mockKidRepository = Mockito.mock(KidRepository.class); - ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser1 = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - User newUser2 = User.builder().id(2L).username("user2").isFemale(true).birthday("19990623") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) - .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + .thenReturn(Optional.of(newChallengeUser)); + Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) + .thenReturn(Optional.of(newChallengeUser)); + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) + .thenReturn(Optional.of(newFamilyUser)); - ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser1).build(); + List progressList = List.of(newProgress, newProgress1); + newChallenge.setProgressList(progressList); - Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser)); + ChallengeDTO newDeleteChallengeDTO = new ChallengeDTO(newChallenge, null, null); //when ChallengeServiceImpl challengeService = new ChallengeServiceImpl(mockChallengeRepository, @@ -1047,76 +815,27 @@ public void testIfNotAuthUserForDetailChallengeIsForbidden() { mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); Long challengeId = newChallenge.getId(); + CommonResponse result = challengeController.deleteChallenge(son, challengeId); //then - Assertions.assertThrows(ForbiddenException.class, () -> { - challengeController.getChallenge(newUser2, challengeId); - }); - } - - @Test - @DisplayName("챌린지 조회 시, 챌린지 아이디로 챌린지를 못찾으면 404 에러 테스트") - public void testIfGetChallengeIsNullNotFoundErr() { - - //given - ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( - ChallengeCategoryRepository.class); - TargetItemRepository mockTargetItemRepository = Mockito.mock(TargetItemRepository.class); - ChallengeRepository mockChallengeRepository = Mockito.mock(ChallengeRepository.class); - ChallengeUserRepository mockChallengeUserRepository = Mockito.mock( - ChallengeUserRepository.class); - ProgressRepository mockProgressRepository = Mockito.mock(ProgressRepository.class); - FamilyUserRepository mockFamilyUserRepository = Mockito.mock(FamilyUserRepository.class); - KidRepository mockKidRepository = Mockito.mock(KidRepository.class); - ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser1 = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); + ArgumentCaptor cuCaptor = ArgumentCaptor.forClass(ChallengeUser.class); + ArgumentCaptor cCaptor = ArgumentCaptor.forClass(Challenge.class); - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + Mockito.verify(mockChallengeUserRepository, Mockito.times(1)).delete(cuCaptor.capture()); + Mockito.verify(mockChallengeRepository, Mockito.times(1)).delete(cCaptor.capture()); - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) - .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + Assertions.assertEquals(newChallenge, cCaptor.getValue()); - ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser1).build(); + Assertions.assertEquals(newChallengeUser, cuCaptor.getValue()); - Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser)); + Assertions.assertNotEquals(sonKid.getDeleteChallenge(), null); - //when - ChallengeServiceImpl challengeService = new ChallengeServiceImpl(mockChallengeRepository, - mockChallengeCategoryRepository, mockTargetItemRepository, mockChallengeUserRepository, - mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, - mockKidRepository, mockParentRepository); - ChallengeController challengeController = new ChallengeController(challengeService); - - //then - Assertions.assertThrows(NotFoundException.class, () -> { - challengeController.getChallenge(newUser1, 2L); - }); + Assertions.assertEquals(CommonResponse.onSuccess(newDeleteChallengeDTO), result); } @Test - @DisplayName("챌린지 삭제 시, 정상적으로 없어지는지 테스트") - public void testIfDeleteChallengeIsNull() { + @DisplayName("챌린지 삭제 시, 삭제한지 2주가 경과된 유저가 시도 했을 때 정상적으로 없어지는지 테스트") + public void testIfDeleteTwoWeeksUserChallengeIsNull() { //given ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( @@ -1131,54 +850,57 @@ public void testIfDeleteChallengeIsNull() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); + son.setKid(sonKid); - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - Kid newKid = Kid.builder().user(newUser).savings(0L).achievedChallenge(0L) - .totalChallenge(0L).build(); - newUser.setKid(newKid); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); + LocalDateTime now = LocalDateTime.of(2022, 6, 10, 3, 3); + Timestamp timestamp = Timestamp.valueOf(now); + Calendar cal = Calendar.getInstance(); + cal.setTime(timestamp); + cal.add(Calendar.DATE, -15); - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + ReflectionTestUtils.setField( + sonKid, + Kid.class, + "deleteChallenge", + Timestamp.valueOf(now), + Timestamp.class + ); - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Progress newProgress = Progress.builder().id(1L).weeks(1L).isAchieved(true) .challenge(newChallenge).build(); + Family newFamily = Family.builder().id(1L).code("family").build(); + + FamilyUser newFamilyUser = FamilyUser.builder().id(1L).user(son).family(newFamily).build(); + Mockito.when(mockChallengeRepository.save(newChallenge)).thenReturn(newChallenge); Mockito.when(mockChallengeRepository.findById(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallenge)); + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockChallengeUserRepository.save(newChallengeUser)) .thenReturn(newChallengeUser); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser)); + .thenReturn(Optional.of(newChallengeUser)); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser)); + .thenReturn(Optional.of(newChallengeUser)); + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) + .thenReturn(Optional.of(newFamilyUser)); - List progressList = Arrays.asList(newProgress); + List progressList = List.of(newProgress); newChallenge.setProgressList(progressList); ChallengeDTO newDeleteChallengeDTO = new ChallengeDTO(newChallenge, null, null); - //when ChallengeServiceImpl challengeService = new ChallengeServiceImpl(mockChallengeRepository, mockChallengeCategoryRepository, mockTargetItemRepository, mockChallengeUserRepository, @@ -1186,7 +908,7 @@ public void testIfDeleteChallengeIsNull() { mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); Long challengeId = newChallenge.getId(); - CommonResponse result = challengeController.deleteChallenge(newUser, challengeId); + CommonResponse result = challengeController.deleteChallenge(son, challengeId); //then ArgumentCaptor cuCaptor = ArgumentCaptor.forClass(ChallengeUser.class); @@ -1203,8 +925,8 @@ public void testIfDeleteChallengeIsNull() { } @Test - @DisplayName("챌린지 삭제 시, 삭제한지 2주가 경과된 유저가 시도 했을 때 정상적으로 없어지는지 테스트") - public void testIfDeleteTwoWeeksUserChallengeIsNull() { + @DisplayName("챌린지 삭제 시, 삭제한지 2주가 경과된 유저가 시도 했을 때 (해가 넘어갈 시) 정상적으로 없어지는지 테스트") + public void testIfDeleteTwoWeeksDiffYearUserChallengeIsNull() { //given ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( @@ -1219,15 +941,7 @@ public void testIfDeleteTwoWeeksUserChallengeIsNull() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - Kid newKid = Kid.builder().user(newUser).savings(0L).achievedChallenge(0L) - .totalChallenge(0L).build(); - newUser.setKid(newKid); + son.setKid(sonKid); LocalDateTime now = LocalDateTime.of(2022, 6, 10, 3, 3); Timestamp timestamp = Timestamp.valueOf(now); @@ -1236,47 +950,45 @@ public void testIfDeleteTwoWeeksUserChallengeIsNull() { cal.add(Calendar.DATE, -15); ReflectionTestUtils.setField( - newKid, + sonKid, Kid.class, "deleteChallenge", - Timestamp.valueOf(now), + Timestamp.valueOf(now.minusYears(1)), Timestamp.class ); - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Progress newProgress = Progress.builder().id(1L).weeks(1L).isAchieved(true) .challenge(newChallenge).build(); + Family newFamily = Family.builder().id(1L).code("family").build(); + + FamilyUser newFamilyUser = FamilyUser.builder().id(1L).user(son).family(newFamily).build(); + Mockito.when(mockChallengeRepository.save(newChallenge)).thenReturn(newChallenge); Mockito.when(mockChallengeRepository.findById(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallenge)); + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockChallengeUserRepository.save(newChallengeUser)) .thenReturn(newChallengeUser); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser)); + .thenReturn(Optional.of(newChallengeUser)); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser)); + .thenReturn(Optional.of(newChallengeUser)); + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) + .thenReturn(Optional.of(newFamilyUser)); - List progressList = Arrays.asList(newProgress); + List progressList = List.of(newProgress); newChallenge.setProgressList(progressList); ChallengeDTO newDeleteChallengeDTO = new ChallengeDTO(newChallenge, null, null); @@ -1287,7 +999,7 @@ public void testIfDeleteTwoWeeksUserChallengeIsNull() { mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); Long challengeId = newChallenge.getId(); - CommonResponse result = challengeController.deleteChallenge(newUser, challengeId); + CommonResponse result = challengeController.deleteChallenge(son, challengeId); //then ArgumentCaptor cuCaptor = ArgumentCaptor.forClass(ChallengeUser.class); @@ -1304,8 +1016,8 @@ public void testIfDeleteTwoWeeksUserChallengeIsNull() { } @Test - @DisplayName("챌린지 삭제 시, 삭제한지 2주가 경과된 유저가 시도 했을 때 (해가 넘어갈 시) 정상적으로 없어지는지 테스트") - public void testIfDeleteTwoWeeksDiffYearUserChallengeIsNull() { + @DisplayName("챌린지 삭제 시, 삭제한지 2주가 경과되지 않은 유저가 거절당한 돈길 삭제를 시도 했을 때 정상적으로 없어지는지 테스트") + public void testIfDeleteRejectChallengeIsNull() { //given ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( @@ -1320,69 +1032,63 @@ public void testIfDeleteTwoWeeksDiffYearUserChallengeIsNull() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - Kid newKid = Kid.builder().user(newUser).savings(0L).achievedChallenge(0L) - .totalChallenge(0L).build(); - newUser.setKid(newKid); + son.setKid(sonKid); - LocalDateTime now = LocalDateTime.of(2022, 6, 10, 3, 3); + LocalDateTime now = LocalDateTime.now(); Timestamp timestamp = Timestamp.valueOf(now); Calendar cal = Calendar.getInstance(); cal.setTime(timestamp); cal.add(Calendar.DATE, -15); ReflectionTestUtils.setField( - newKid, + sonKid, Kid.class, "deleteChallenge", - Timestamp.valueOf(now.minusYears(1)), + Timestamp.valueOf(now), Timestamp.class ); - System.out.println("newKid.getDeleteChallenge() = " + newKid.getDeleteChallenge()); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); + Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(rejected) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + Comment newComment = Comment.builder().id(1L).challenge(newChallenge).user(mom) + .content("아쉽구나").build(); - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) - .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + newChallenge.setComment(newComment); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Progress newProgress = Progress.builder().id(1L).weeks(1L).isAchieved(true) .challenge(newChallenge).build(); + Family newFamily = Family.builder().id(1L).code("family").build(); + + FamilyUser newFamilyUser = FamilyUser.builder().id(1L).user(son).family(newFamily).build(); + Mockito.when(mockChallengeRepository.save(newChallenge)).thenReturn(newChallenge); Mockito.when(mockChallengeRepository.findById(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallenge)); + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockChallengeUserRepository.save(newChallengeUser)) .thenReturn(newChallengeUser); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser)); + .thenReturn(Optional.of(newChallengeUser)); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser)); + .thenReturn(Optional.of(newChallengeUser)); + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) + .thenReturn(Optional.of(newFamilyUser)); - List progressList = Arrays.asList(newProgress); + List progressList = List.of(newProgress); newChallenge.setProgressList(progressList); ChallengeDTO newDeleteChallengeDTO = new ChallengeDTO(newChallenge, null, null); + //when ChallengeServiceImpl challengeService = new ChallengeServiceImpl(mockChallengeRepository, mockChallengeCategoryRepository, mockTargetItemRepository, mockChallengeUserRepository, @@ -1390,7 +1096,7 @@ public void testIfDeleteTwoWeeksDiffYearUserChallengeIsNull() { mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); Long challengeId = newChallenge.getId(); - CommonResponse result = challengeController.deleteChallenge(newUser, challengeId); + CommonResponse result = challengeController.deleteChallenge(son, challengeId); //then ArgumentCaptor cuCaptor = ArgumentCaptor.forClass(ChallengeUser.class); @@ -1407,8 +1113,8 @@ public void testIfDeleteTwoWeeksDiffYearUserChallengeIsNull() { } @Test - @DisplayName("챌린지 삭제 시, 삭제한지 2주가 경과되지 않은 유저가 거절당한 돈길 삭제를 시도 했을 때 정상적으로 없어지는지 테스트") - public void testIfDeleteRejectChallengeIsNull() { + @DisplayName("챌린지 삭제 시, 삭제한지 2주가 경과되지 않은 유저가 제안중인 돈길 삭제를 시도 했을 때 정상적으로 없어지는지 테스트") + public void testIfDeletePendingChallengeIsNull() { //given ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( @@ -1423,15 +1129,7 @@ public void testIfDeleteRejectChallengeIsNull() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - Kid newKid = Kid.builder().user(newUser).savings(0L).achievedChallenge(0L) - .totalChallenge(0L).build(); - newUser.setKid(newKid); + son.setKid(sonKid); LocalDateTime now = LocalDateTime.now(); Timestamp timestamp = Timestamp.valueOf(now); @@ -1440,52 +1138,50 @@ public void testIfDeleteRejectChallengeIsNull() { cal.add(Calendar.DATE, -15); ReflectionTestUtils.setField( - newKid, + sonKid, Kid.class, "deleteChallenge", Timestamp.valueOf(now), Timestamp.class ); - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(0L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); - Comment newComment = Comment.builder().id(1L).challenge(newChallenge).user(newParent) + Comment newComment = Comment.builder().id(1L).challenge(newChallenge).user(mom) .content("아쉽구나").build(); newChallenge.setComment(newComment); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Progress newProgress = Progress.builder().id(1L).weeks(1L).isAchieved(true) .challenge(newChallenge).build(); + Family newFamily = Family.builder().id(1L).code("family").build(); + + FamilyUser newFamilyUser = FamilyUser.builder().id(1L).user(son).family(newFamily).build(); + Mockito.when(mockChallengeRepository.save(newChallenge)).thenReturn(newChallenge); Mockito.when(mockChallengeRepository.findById(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallenge)); + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockChallengeUserRepository.save(newChallengeUser)) .thenReturn(newChallengeUser); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser)); + .thenReturn(Optional.of(newChallengeUser)); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser)); + .thenReturn(Optional.of(newChallengeUser)); + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) + .thenReturn(Optional.of(newFamilyUser)); - List progressList = Arrays.asList(newProgress); + List progressList = List.of(newProgress); newChallenge.setProgressList(progressList); ChallengeDTO newDeleteChallengeDTO = new ChallengeDTO(newChallenge, null, null); @@ -1497,7 +1193,7 @@ public void testIfDeleteRejectChallengeIsNull() { mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); Long challengeId = newChallenge.getId(); - CommonResponse result = challengeController.deleteChallenge(newUser, challengeId); + CommonResponse result = challengeController.deleteChallenge(son, challengeId); //then ArgumentCaptor cuCaptor = ArgumentCaptor.forClass(ChallengeUser.class); @@ -1530,15 +1226,7 @@ public void testIfDeleteFailureChallengeIsNull() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - Kid newKid = Kid.builder().user(newUser).savings(0L).achievedChallenge(0L) - .totalChallenge(0L).build(); - newUser.setKid(newKid); + son.setKid(sonKid); LocalDateTime now = LocalDateTime.now(); Timestamp timestamp = Timestamp.valueOf(now); @@ -1547,52 +1235,45 @@ public void testIfDeleteFailureChallengeIsNull() { cal.add(Calendar.DATE, -15); ReflectionTestUtils.setField( - newKid, + sonKid, Kid.class, "deleteChallenge", Timestamp.valueOf(now), Timestamp.class ); - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(0L).totalPrice(challengeRequest.getTotalPrice()) + Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(0L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Comment newComment = Comment.builder().id(1L).challenge(newChallenge).user(newParent) - .content("아쉽구나").build(); - - newChallenge.setComment(newComment); + .challengeStatus(failed) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Progress newProgress = Progress.builder().id(1L).weeks(1L).isAchieved(true) .challenge(newChallenge).build(); + Family newFamily = Family.builder().id(1L).code("family").build(); + + FamilyUser newFamilyUser = FamilyUser.builder().id(1L).user(son).family(newFamily).build(); + Mockito.when(mockChallengeRepository.save(newChallenge)).thenReturn(newChallenge); Mockito.when(mockChallengeRepository.findById(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallenge)); + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockChallengeUserRepository.save(newChallengeUser)) .thenReturn(newChallengeUser); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser)); + .thenReturn(Optional.of(newChallengeUser)); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallengeUser)); + .thenReturn(Optional.of(newChallengeUser)); + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) + .thenReturn(Optional.of(newFamilyUser)); - List progressList = Arrays.asList(newProgress); + List progressList = List.of(newProgress); newChallenge.setProgressList(progressList); ChallengeDTO newDeleteChallengeDTO = new ChallengeDTO(newChallenge, null, null); @@ -1604,7 +1285,7 @@ public void testIfDeleteFailureChallengeIsNull() { mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); Long challengeId = newChallenge.getId(); - CommonResponse result = challengeController.deleteChallenge(newUser, challengeId); + CommonResponse result = challengeController.deleteChallenge(son, challengeId); //then ArgumentCaptor cuCaptor = ArgumentCaptor.forClass(ChallengeUser.class); @@ -1637,38 +1318,33 @@ public void testIfNotAuthUserDeleteChallengeForbidden() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); + son.setKid(sonKid); - User newUser1 = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newUser2 = User.builder().id(2L).username("user2").isFemale(true).birthday("19990623") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); + Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); + ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) + .member("parent").user(son).build(); - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); + Family newFamily = Family.builder().id(1L).code("family").build(); - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + FamilyUser newFamilyUser = FamilyUser.builder().id(1L).user(son).family(newFamily).build(); - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) - .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); - - ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser1).build(); + FamilyUser newFamilyUser1 = FamilyUser.builder().id(2L).user(daughter).family(newFamily) + .build(); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) .thenReturn(Optional.ofNullable(newChallengeUser)); + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) + .thenReturn(Optional.of(newFamilyUser)); + Mockito.when(mockFamilyUserRepository.findByUserId(daughter.getId())) + .thenReturn(Optional.of(newFamilyUser1)); //when ChallengeServiceImpl challengeService = new ChallengeServiceImpl(mockChallengeRepository, @@ -1679,9 +1355,8 @@ public void testIfNotAuthUserDeleteChallengeForbidden() { Long challengeId = newChallenge.getId(); //then - Assertions.assertThrows(ForbiddenException.class, () -> { - challengeController.deleteChallenge(newUser2, challengeId); - }); + Assertions.assertThrows(ForbiddenException.class, () -> + challengeController.deleteChallenge(daughter, challengeId)); } @Test @@ -1701,40 +1376,27 @@ public void testIfOverTwoWeekChallengeTryDeleteForbidden() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); + son.setKid(sonKid); - User newUser1 = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - Kid newKid = Kid.builder().user(newUser1).savings(0L).build(); - newUser1.setKid(newKid); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); ReflectionTestUtils.setField( - newKid, + sonKid, Kid.class, "deleteChallenge", - Timestamp.valueOf(LocalDateTime.now()), + Timestamp.valueOf(LocalDateTime.now().minusDays(1)), Timestamp.class ); - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser1).build(); + .member("parent").user(son).build(); Progress newProgress = Progress.builder().id(1L).weeks(1L).isAchieved(true) .challenge(newChallenge).build(); @@ -1742,11 +1404,16 @@ public void testIfOverTwoWeekChallengeTryDeleteForbidden() { Progress newProgress2 = Progress.builder().id(2L).weeks(2L).isAchieved(true) .challenge(newChallenge).build(); + Family newFamily = Family.builder().id(1L).code("family").build(); + + FamilyUser newFamilyUser = FamilyUser.builder().id(1L).user(son).family(newFamily).build(); + List progressList = Arrays.asList(newProgress, newProgress2); newChallenge.setProgressList(progressList); - int size = newChallenge.getProgressList().size(); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) .thenReturn(Optional.ofNullable(newChallengeUser)); + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) + .thenReturn(Optional.of(newFamilyUser)); //when ChallengeServiceImpl challengeService = new ChallengeServiceImpl(mockChallengeRepository, @@ -1757,14 +1424,13 @@ public void testIfOverTwoWeekChallengeTryDeleteForbidden() { Long challengeId = newChallenge.getId(); //then - Assertions.assertThrows(ForbiddenException.class, () -> { - challengeController.deleteChallenge(newUser1, challengeId); - }); + Assertions.assertThrows(ForbiddenException.class, () -> + challengeController.deleteChallenge(son, challengeId)); } @Test - @DisplayName("챌린지 삭제 시, 챌린지 아이디로 챌린지를 못찾으면 404 에러 테스트") - public void testIfDeleteChallengeIsNullNotFoundErr() { + @DisplayName("챌린지 삭제 시, 챌린지 아이디로 챌린지를 못찾으면 400 에러 테스트") + public void testIfDeleteChallengeIsNullBadRequestErr() { //given ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( @@ -1779,38 +1445,17 @@ public void testIfDeleteChallengeIsNullNotFoundErr() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser1 = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser1).build(); - - Progress newProgress = Progress.builder().id(1L).weeks(1L).isAchieved(true) - .challenge(newChallenge).build(); - - Progress newProgress2 = Progress.builder().id(2L).weeks(2L).isAchieved(true) - .challenge(newChallenge).build(); + .member("parent").user(son).build(); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) .thenReturn(Optional.ofNullable(newChallengeUser)); @@ -1821,12 +1466,10 @@ public void testIfDeleteChallengeIsNullNotFoundErr() { mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); - Long challengeId = newChallenge.getId(); //then - Assertions.assertThrows(BadRequestException.class, () -> { - challengeController.deleteChallenge(newUser1, 2L); - }); + Assertions.assertThrows(BadRequestException.class, () -> + challengeController.deleteChallenge(son, 2L)); } @Test @@ -1846,46 +1489,29 @@ public void testIfGetListChallengeTest() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - ChallengeRequest challengeRequest1 = new ChallengeRequest(true, "이자율 받기", "전자제품", - "에어팟 펜슬 사기", - 10L, 100000L, 10000L, 10L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge1 = Challenge.builder().title(challengeRequest1.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest1.getTotalPrice()) - .weekPrice(challengeRequest1.getWeekPrice()).weeks(challengeRequest1.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest1.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); ChallengeUser newChallengeUser1 = ChallengeUser.builder().challenge(newChallenge1) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); List challengeUserList = new ArrayList<>(); challengeUserList.add(newChallengeUser); @@ -1898,7 +1524,7 @@ public void testIfGetListChallengeTest() { newProgress, AbstractTimestamp.class, "createdAt", - Timestamp.valueOf(LocalDateTime.now()), + Timestamp.valueOf(LocalDateTime.now().minusDays(1)), Timestamp.class ); @@ -1916,7 +1542,7 @@ public void testIfGetListChallengeTest() { .thenReturn(newChallengeUser); Mockito.when(mockChallengeUserRepository.save(newChallengeUser1)) .thenReturn(newChallengeUser1); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); //when @@ -1925,17 +1551,19 @@ public void testIfGetListChallengeTest() { mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); - CommonResponse result = challengeController.getListChallenge(newUser, "pending"); - CommonResponse result1 = challengeController.getListChallenge(newUser, "accept"); + CommonResponse> result = challengeController.getListChallenge(son, + "pending"); + CommonResponse> result1 = challengeController.getListChallenge(son, + "accept"); //then List challengeDTOList = new ArrayList<>(); List challengeDTOList1 = new ArrayList<>(); for (ChallengeUser r : challengeUserList) { - if (r.getChallenge().getStatus() != 2L) { + if (r.getChallenge().getChallengeStatus() != walking) { challengeDTOList.add(new ChallengeDTO(r.getChallenge(), null, null)); - } else { + } else if (r.getChallenge().getChallengeStatus() == walking) { challengeDTOList1.add(new ChallengeDTO(r.getChallenge(), progressDTOList, null)); } } @@ -1964,46 +1592,29 @@ public void testIfLastYearGetListChallengeTest() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - ChallengeRequest challengeRequest1 = new ChallengeRequest(true, "이자율 받기", "전자제품", - "에어팟 펜슬 사기", - 10L, 100000L, 10000L, 10L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge1 = Challenge.builder().title(challengeRequest1.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest1.getTotalPrice()) - .weekPrice(challengeRequest1.getWeekPrice()).weeks(challengeRequest1.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest1.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); ChallengeUser newChallengeUser1 = ChallengeUser.builder().challenge(newChallenge1) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); List challengeUserList = new ArrayList<>(); challengeUserList.add(newChallengeUser); @@ -2012,6 +1623,9 @@ public void testIfLastYearGetListChallengeTest() { Progress newProgress = Progress.builder().id(1L).weeks(1L).isAchieved(true) .challenge(newChallenge1).build(); + Progress newProgress1 = Progress.builder().id(2L).weeks(2L).isAchieved(true) + .challenge(newChallenge1).build(); + ReflectionTestUtils.setField( newProgress, AbstractTimestamp.class, @@ -2022,9 +1636,11 @@ public void testIfLastYearGetListChallengeTest() { List progressList = new ArrayList<>(); progressList.add(newProgress); + progressList.add(newProgress1); List progressDTOList = new ArrayList<>(); progressDTOList.add(new ProgressDTO(newProgress)); + progressDTOList.add(new ProgressDTO(newProgress1)); newChallenge1.setProgressList(progressList); @@ -2034,7 +1650,7 @@ public void testIfLastYearGetListChallengeTest() { .thenReturn(newChallengeUser); Mockito.when(mockChallengeUserRepository.save(newChallengeUser1)) .thenReturn(newChallengeUser1); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); //when @@ -2043,14 +1659,16 @@ public void testIfLastYearGetListChallengeTest() { mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); - CommonResponse result = challengeController.getListChallenge(newUser, "pending"); - CommonResponse result1 = challengeController.getListChallenge(newUser, "accept"); + CommonResponse> result = challengeController.getListChallenge(son, + "pending"); + CommonResponse> result1 = challengeController.getListChallenge(son, + "accept"); //then List challengeDTOList = new ArrayList<>(); List challengeDTOList1 = new ArrayList<>(); for (ChallengeUser r : challengeUserList) { - if (r.getChallenge().getStatus() != 2L) { + if (r.getChallenge().getChallengeStatus() != walking) { challengeDTOList.add(new ChallengeDTO(r.getChallenge(), null, null)); } else { @@ -2082,50 +1700,59 @@ public void testIfGetListChallengeChallengeIsFailureTest() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - ChallengeRequest challengeRequest1 = new ChallengeRequest(true, "이자율 받기", "전자제품", - "에어팟 펜슬 사기", - 20L, 100000L, 10000L, 10L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge1 = Challenge.builder().title(challengeRequest1.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest1.getTotalPrice()) - .weekPrice(challengeRequest1.getWeekPrice()).weeks(challengeRequest1.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest1.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(walking) + .interestRate(30L) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge2 = Challenge.builder().id(3L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(walking) + .interestRate(10L) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge3 = Challenge.builder().id(4L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(walking) + .interestRate(20L) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); ChallengeUser newChallengeUser1 = ChallengeUser.builder().challenge(newChallenge1) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); + + ChallengeUser newChallengeUser2 = ChallengeUser.builder().challenge(newChallenge2) + .member("parent").user(son).build(); + + ChallengeUser newChallengeUser3 = ChallengeUser.builder().challenge(newChallenge3) + .member("parent").user(son).build(); List challengeUserList = new ArrayList<>(); challengeUserList.add(newChallengeUser); challengeUserList.add(newChallengeUser1); + challengeUserList.add(newChallengeUser2); + challengeUserList.add(newChallengeUser3); Progress progress = Progress.builder().id(3L).isAchieved(false).weeks(1L) .challenge(newChallenge).build(); @@ -2136,8 +1763,28 @@ public void testIfGetListChallengeChallengeIsFailureTest() { Progress newProgress = Progress.builder().id(1L).weeks(1L).isAchieved(false) .challenge(newChallenge1).build(); - Progress newProgress1 = Progress.builder().id(2L).weeks(2L).isAchieved(false) - .challenge(newChallenge1).build(); + Progress lowProgress = Progress.builder().id(2L).weeks(1L).isAchieved(false) + .challenge(newChallenge2).build(); + + Progress middleProgress = Progress.builder().id(5L).weeks(1L).isAchieved(false) + .challenge(newChallenge3).build(); + + Progress middleProgress1 = Progress.builder().id(6L).weeks(2L).isAchieved(false) + .challenge(newChallenge3).build(); + + Progress middleProgress2 = Progress.builder().id(7L).weeks(3L).isAchieved(false) + .challenge(newChallenge3).build(); + + Progress middleProgress3 = Progress.builder().id(8L).weeks(4L).isAchieved(false) + .challenge(newChallenge3).build(); + + ReflectionTestUtils.setField( + lowProgress, + AbstractTimestamp.class, + "createdAt", + Timestamp.valueOf(LocalDateTime.now()), + Timestamp.class + ); ReflectionTestUtils.setField( newProgress, @@ -2155,21 +1802,47 @@ public void testIfGetListChallengeChallengeIsFailureTest() { Timestamp.class ); + ReflectionTestUtils.setField( + middleProgress, + AbstractTimestamp.class, + "createdAt", + Timestamp.valueOf(LocalDateTime.now().minusDays(24)), + Timestamp.class + ); + List progressList = new ArrayList<>(); progressList.add(newProgress); - progressList.add(newProgress1); List progressList1 = new ArrayList<>(); progressList1.add(progress); progressList1.add(progress1); + List progressList2 = new ArrayList<>(); + progressList2.add(lowProgress); + + List progressList3 = new ArrayList<>(); + progressList3.add(middleProgress); + progressList3.add(middleProgress1); + progressList3.add(middleProgress2); + progressList3.add(middleProgress3); + newChallenge.setProgressList(progressList1); newChallenge1.setProgressList(progressList); + newChallenge2.setProgressList(progressList2); + + newChallenge3.setProgressList(progressList3); + ProgressDTO progressDTO = new ProgressDTO(newProgress); ProgressDTO progressDTO1 = new ProgressDTO(progress); ProgressDTO progressDTO2 = new ProgressDTO(progress1); + ProgressDTO lowProgressDTO = new ProgressDTO(lowProgress); + + List lowProgressDTOList = List.of(lowProgressDTO); + + List middleProgressDTOList = progressList3.stream().map(ProgressDTO::new) + .collect(Collectors.toList()); List progressDTOList = new ArrayList<>(); progressDTOList.add(progressDTO); @@ -2180,7 +1853,7 @@ public void testIfGetListChallengeChallengeIsFailureTest() { .thenReturn(newChallengeUser); Mockito.when(mockChallengeUserRepository.save(newChallengeUser1)) .thenReturn(newChallengeUser1); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); //when @@ -2189,7 +1862,8 @@ public void testIfGetListChallengeChallengeIsFailureTest() { mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); - CommonResponse result1 = challengeController.getListChallenge(newUser, "accept"); + CommonResponse> result1 = challengeController.getListChallenge(son, + "accept"); //then List challengeDTOList = new ArrayList<>(); @@ -2201,10 +1875,13 @@ public void testIfGetListChallengeChallengeIsFailureTest() { new ChallengeDTO(newChallengeUser.getChallenge(), resultProgressDTOList, null)); challengeDTOList.add( new ChallengeDTO(newChallengeUser1.getChallenge(), progressDTOList, null)); + challengeDTOList.add(new ChallengeDTO(newChallenge2, lowProgressDTOList, null)); + challengeDTOList.add(new ChallengeDTO(newChallenge3, middleProgressDTOList, null)); + + Assertions.assertEquals(failed, newChallenge3.getChallengeStatus()); Assertions.assertEquals(CommonResponse.onSuccess(challengeDTOList).getData(), result1.getData()); - System.out.println("result1 = " + result1.getData()); } @@ -2225,46 +1902,29 @@ public void testIfGetListChallengeChallengeIsAlreadyFailureTest() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - ChallengeRequest challengeRequest1 = new ChallengeRequest(true, "이자율 받기", "전자제품", - "에어팟 펜슬 사기", - 20L, 100000L, 10000L, 10L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(0L).totalPrice(challengeRequest.getTotalPrice()) + Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(0L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge1 = Challenge.builder().title(challengeRequest1.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest1.getTotalPrice()) - .weekPrice(challengeRequest1.getWeekPrice()).weeks(challengeRequest1.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest1.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(failed) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); ChallengeUser newChallengeUser1 = ChallengeUser.builder().challenge(newChallenge1) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); List challengeUserList = new ArrayList<>(); challengeUserList.add(newChallengeUser); @@ -2323,7 +1983,7 @@ public void testIfGetListChallengeChallengeIsAlreadyFailureTest() { .thenReturn(newChallengeUser); Mockito.when(mockChallengeUserRepository.save(newChallengeUser1)) .thenReturn(newChallengeUser1); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); //when @@ -2332,7 +1992,8 @@ public void testIfGetListChallengeChallengeIsAlreadyFailureTest() { mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); - CommonResponse result1 = challengeController.getListChallenge(newUser, "accept"); + CommonResponse> result1 = challengeController.getListChallenge(son, + "accept"); //then List challengeDTOList = new ArrayList<>(); @@ -2352,8 +2013,8 @@ public void testIfGetListChallengeChallengeIsAlreadyFailureTest() { } @Test - @DisplayName("챌린지 리스트 조회 시, 생성한 챌린지가 한 개도 없으면 빈 배열 반환") - public void testIfNotCreateChallengeReturnEmptyList() { + @DisplayName("챌린지 리스트 조회 시, 완주한 챌린지 정상 response 테스트") + public void testIfAchievedChallengeReturn() { //given ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( @@ -2368,17 +2029,47 @@ public void testIfNotCreateChallengeReturnEmptyList() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); + Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(3000L) + .weekPrice(1000L).weeks(3L) + .challengeStatus(achieved) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + ChallengeUser newChallengeUser = ChallengeUser.builder().id(1L).user(son) + .challenge(newChallenge) + .member("parent").build(); + + List challengeUserList = new ArrayList<>(); + challengeUserList.add(newChallengeUser); + + Progress progress = Progress.builder().id(1L).isAchieved(true).challenge(newChallenge) + .weeks(1L).build(); - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); + Progress progress1 = Progress.builder().id(2L).isAchieved(true).challenge(newChallenge) + .weeks(2L).build(); - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + Progress progress2 = Progress.builder().id(3L).isAchieved(true).challenge(newChallenge) + .weeks(3L).build(); - List challengeUserList = new ArrayList<>(); + ReflectionTestUtils.setField( + progress, + AbstractTimestamp.class, + "createdAt", + Timestamp.valueOf(LocalDateTime.now().minusDays(25)), + Timestamp.class + ); + + List progressList = List.of(progress, progress1, progress2); + + newChallenge.setProgressList(progressList); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + List progressDTOList = progressList.stream().map(ProgressDTO::new) + .collect(Collectors.toList()); + + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); //when @@ -2387,13 +2078,12 @@ public void testIfNotCreateChallengeReturnEmptyList() { mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); - CommonResponse result = challengeController.getListChallenge(newUser, "pending"); + CommonResponse> result = challengeController.getListChallenge(son, + "accept"); //then List challengeDTOList = new ArrayList<>(); - for (ChallengeUser r : challengeUserList) { - challengeDTOList.add(new ChallengeDTO(r.getChallenge(), null, null)); - } + challengeDTOList.add(new ChallengeDTO(newChallenge, progressDTOList, null)); Assertions.assertEquals(CommonResponse.onSuccess(challengeDTOList).getData(), result.getData()); @@ -2401,8 +2091,8 @@ public void testIfNotCreateChallengeReturnEmptyList() { @Test - @DisplayName("자녀 돈길 리스트 조회 시, 정상 response 테스트") - public void testIfGetListChildChallengeList() { + @DisplayName("챌린지 리스트 조회 시, 생성한 챌린지가 한 개도 없으면 빈 배열 반환") + public void testIfNotCreateChallengeReturnEmptyList() { //given ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( @@ -2417,79 +2107,106 @@ public void testIfGetListChildChallengeList() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); + List challengeUserList = new ArrayList<>(); + + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) + .thenReturn(challengeUserList); - ChallengeRequest challengeRequest1 = new ChallengeRequest(true, "이자율 받기", "전자제품", "아이팟 사기", - 30L, - 1500L, 100L, 15L, "test"); + //when + ChallengeServiceImpl challengeService = new ChallengeServiceImpl(mockChallengeRepository, + mockChallengeCategoryRepository, mockTargetItemRepository, mockChallengeUserRepository, + mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, + mockKidRepository, mockParentRepository); + ChallengeController challengeController = new ChallengeController(challengeService); + CommonResponse> result = challengeController.getListChallenge(son, + "pending"); - User newUser = User.builder().id(1L).username("user").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); + //then + List challengeDTOList = new ArrayList<>(); + for (ChallengeUser r : challengeUserList) { + challengeDTOList.add(new ChallengeDTO(r.getChallenge(), null, null)); + } - Kid newKid = Kid.builder().id(1L).totalChallenge(0L).achievedChallenge(0L).level(0L) - .savings(0L).user(newUser).build(); + Assertions.assertEquals(CommonResponse.onSuccess(challengeDTOList).getData(), + result.getData()); + } - newUser.setKid(newKid); - User newParent = User.builder().id(2L).username("user1").isFemale(true).birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token") - .build(); + @Test + @DisplayName("자녀 돈길 리스트 조회 시, 정상 response 테스트") + public void testIfGetListChildChallengeList() { - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); + //given + ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( + ChallengeCategoryRepository.class); + TargetItemRepository mockTargetItemRepository = Mockito.mock(TargetItemRepository.class); + ChallengeRepository mockChallengeRepository = Mockito.mock(ChallengeRepository.class); + ChallengeUserRepository mockChallengeUserRepository = Mockito.mock( + ChallengeUserRepository.class); + ProgressRepository mockProgressRepository = Mockito.mock(ProgressRepository.class); + FamilyUserRepository mockFamilyUserRepository = Mockito.mock(FamilyUserRepository.class); + KidRepository mockKidRepository = Mockito.mock(KidRepository.class); + ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); + CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + son.setKid(sonKid); Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge2 = Challenge.builder().id(3L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(rejected) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge3 = Challenge.builder().id(4L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest1.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest1.getTotalPrice()) - .weekPrice(challengeRequest1.getWeekPrice()).weeks(challengeRequest1.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest1.getInterestRate()).build(); - - Challenge newChallenge2 = Challenge.builder().id(3L).title("티비 사기") - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest1.getTotalPrice()) - .weekPrice(challengeRequest1.getWeekPrice()).weeks(challengeRequest1.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(0L) - .interestRate(challengeRequest1.getInterestRate()).build(); - - Challenge newChallenge3 = Challenge.builder().id(4L).title("마우스 사기") - .contractUser(newParent) - .isAchieved(2L).totalPrice(30000L) - .weekPrice(10000L).weeks(3L) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(0L) - .interestRate(challengeRequest1.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().id(1L).challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); ChallengeUser newChallengeUser1 = ChallengeUser.builder().id(2L).challenge(newChallenge1) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); ChallengeUser newChallengeUser2 = ChallengeUser.builder().id(3L).challenge(newChallenge2) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); ChallengeUser newChallengeUser3 = ChallengeUser.builder().id(4L).challenge(newChallenge3) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Family newFamily = Family.builder().id(1L) - .code("adfadfaf").build(); + .code("family").build(); FamilyUser newFamilyUser = FamilyUser.builder().id(1L) - .family(newFamily).user(newParent).build(); + .family(newFamily).user(mom).build(); FamilyUser newFamilyUser1 = FamilyUser.builder().id(2L) - .family(newFamily).user(newUser).build(); + .family(newFamily).user(son).build(); Progress newProgress = Progress.builder().id(1L).challenge(newChallenge1).isAchieved(false) .weeks(1L).build(); @@ -2549,7 +2266,7 @@ public void testIfGetListChildChallengeList() { Progress newProgress1 = Progress.builder().id(2L).challenge(newChallenge1).isAchieved(false) .weeks(2L).build(); - Comment newComment = Comment.builder().id(1L).challenge(newChallenge2).user(newParent) + Comment newComment = Comment.builder().id(1L).challenge(newChallenge2).user(mom) .content("아쉽다").build(); newChallenge2.setComment(newComment); @@ -2582,11 +2299,11 @@ public void testIfGetListChildChallengeList() { challengeDTOList1.add(new ChallengeDTO(newChallenge1, progressDTOList, null)); challengeDTOList1.add(new ChallengeDTO(newChallenge3, successProgressDTOList, null)); - Mockito.when(mockFamilyUserRepository.findByUserId(newParent.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(mom.getId())) .thenReturn(Optional.of(newFamilyUser)); Mockito.when(mockFamilyUserRepository.findByFamily(newFamily)) .thenReturn(familyUserList); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); //when @@ -2595,15 +2312,16 @@ public void testIfGetListChildChallengeList() { mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); - CommonResponse result = challengeController.getListKidChallenge(newParent, newUser.getKid() - .getId(), "accept"); - CommonResponse result1 = challengeController.getListKidChallenge(newParent, - newUser.getKid().getId(), "pending"); + CommonResponse result = challengeController.getListKidChallenge(mom, + son.getKid() + .getId(), "accept"); + CommonResponse result1 = challengeController.getListKidChallenge(mom, + son.getKid().getId(), "pending"); //then - KidChallengeListDTO kidChallengeListDTOResult = new KidChallengeListDTO(newUser, + KidChallengeListDTO kidChallengeListDTOResult = new KidChallengeListDTO(son, challengeDTOList); - KidChallengeListDTO kidChallengeListDTO = new KidChallengeListDTO(newUser, + KidChallengeListDTO kidChallengeListDTO = new KidChallengeListDTO(son, challengeDTOList1); Assertions.assertEquals(CommonResponse.onSuccess(kidChallengeListDTO).getData(), @@ -2629,70 +2347,49 @@ public void testIfUpdateChallengeStatusIsSuccess() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - ChallengeRequest challengeRequest1 = new ChallengeRequest(true, "이자율 받기", "전자제품", "아이팟 사기", - 30L, - 1500L, 100L, 15L, "test"); + son.setKid(sonKid); + mom.setParent(momParent); KidChallengeRequest successKidChallengeRequest = new KidChallengeRequest(true, null); - KidChallengeRequest falseKidChallengeRequest = new KidChallengeRequest(false, "아쉽다"); - - User newUser = User.builder().id(1L).username("user").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - Kid newKid = Kid.builder().savings(0L).achievedChallenge(0L).level(0L).totalChallenge(0L) - .user(newUser).build(); - newUser.setKid(newKid); - - User newParent = User.builder().id(2L).username("user1").isFemale(true).birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token") - .build(); - - Parent parent = Parent.builder().acceptedRequest(0L) - .totalRequest(0L).user(newParent).build(); - newParent.setParent(parent); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + KidChallengeRequest falseKidChallengeRequest = new KidChallengeRequest(false, "아쉽구나"); Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest1.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest1.getTotalPrice()) - .weekPrice(challengeRequest1.getWeekPrice()).weeks(challengeRequest1.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest1.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().id(1L).challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); ChallengeUser newChallengeUser1 = ChallengeUser.builder().id(2L).challenge(newChallenge1) .member("parent") - .user(newUser).build(); + .user(son).build(); Family newFamily = Family.builder().id(1L) .code("adfadfaf").build(); FamilyUser newFamilyUser = FamilyUser.builder().id(1L) - .family(newFamily).user(newParent).build(); + .family(newFamily).user(mom).build(); FamilyUser newFamilyUser1 = FamilyUser.builder().id(2L) - .family(newFamily).user(newUser).build(); + .family(newFamily).user(son).build(); List progressDTOList = new ArrayList<>(); for (int i = 1; i <= newChallenge.getWeeks(); i++) { - Progress newProgress = Progress.builder().weeks(Long.valueOf(i)) + Progress newProgress = Progress.builder().weeks((long) i) .challenge(newChallenge) .isAchieved(false).build(); progressDTOList.add(new ProgressDTO(newProgress)); @@ -2706,17 +2403,17 @@ public void testIfUpdateChallengeStatusIsSuccess() { familyUserList.add(newFamilyUser1); Comment newComment = Comment.builder().content(falseKidChallengeRequest.getComment()) - .challenge(newChallenge1).user(newParent).build(); + .challenge(newChallenge1).user(mom).build(); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); Mockito.when(mockChallengeRepository.findById(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallenge)); + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockChallengeRepository.findById(newChallenge1.getId())) - .thenReturn(Optional.ofNullable(newChallenge1)); - Mockito.when(mockFamilyUserRepository.findByUserId(newParent.getId())) + .thenReturn(Optional.of(newChallenge1)); + Mockito.when(mockFamilyUserRepository.findByUserId(mom.getId())) .thenReturn(Optional.ofNullable(newFamilyUser)); - Mockito.when(mockFamilyUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) .thenReturn(Optional.ofNullable(newFamilyUser1)); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) .thenReturn(Optional.ofNullable(newChallengeUser)); @@ -2732,9 +2429,9 @@ public void testIfUpdateChallengeStatusIsSuccess() { mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); - CommonResponse successResult = challengeController.patchChallengeStatus(newParent, + CommonResponse successResult = challengeController.patchChallengeStatus(mom, newChallenge.getId(), successKidChallengeRequest); - CommonResponse falseResult = challengeController.patchChallengeStatus(newParent, + CommonResponse falseResult = challengeController.patchChallengeStatus(mom, newChallenge1.getId(), falseKidChallengeRequest); //then @@ -2766,57 +2463,44 @@ public void testIfUpdateChallengeStatusNotExistChallengeBadRequestErr() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - ChallengeRequest challengeRequest1 = new ChallengeRequest(true, "이자율 받기", "전자제품", "아이팟 사기", - 30L, - 1500L, 100L, 15L, "test"); + son.setKid(sonKid); + mom.setParent(momParent); KidChallengeRequest kidChallengeRequest = new KidChallengeRequest(true, null); - User newUser = User.builder().id(1L).username("user").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("user1").isFemale(true).birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest1.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest1.getTotalPrice()) - .weekPrice(challengeRequest1.getWeekPrice()).weeks(challengeRequest1.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest1.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().id(1L).challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); ChallengeUser newChallengeUser1 = ChallengeUser.builder().id(2L).challenge(newChallenge1) .member("parent") - .user(newUser).build(); + .user(son).build(); Family newFamily = Family.builder().id(1L) - .code("adfadfaf").build(); + .code("family").build(); FamilyUser newFamilyUser = FamilyUser.builder().id(1L) - .family(newFamily).user(newParent).build(); + .family(newFamily).user(mom).build(); FamilyUser newFamilyUser1 = FamilyUser.builder().id(2L) - .family(newFamily).user(newUser).build(); + .family(newFamily).user(son).build(); List challengeUserList = new ArrayList<>(); challengeUserList.add(newChallengeUser); @@ -2825,15 +2509,15 @@ public void testIfUpdateChallengeStatusNotExistChallengeBadRequestErr() { familyUserList.add(newFamilyUser); familyUserList.add(newFamilyUser1); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); Mockito.when(mockChallengeRepository.findById(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallenge)); + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockChallengeRepository.findById(newChallenge1.getId())) - .thenReturn(Optional.ofNullable(newChallenge1)); - Mockito.when(mockFamilyUserRepository.findByUserId(newParent.getId())) + .thenReturn(Optional.of(newChallenge1)); + Mockito.when(mockFamilyUserRepository.findByUserId(mom.getId())) .thenReturn(Optional.ofNullable(newFamilyUser)); - Mockito.when(mockFamilyUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) .thenReturn(Optional.ofNullable(newFamilyUser1)); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) .thenReturn(Optional.ofNullable(newChallengeUser)); @@ -2851,17 +2535,16 @@ public void testIfUpdateChallengeStatusNotExistChallengeBadRequestErr() { ChallengeController challengeController = new ChallengeController(challengeService); //then - Assertions.assertThrows(BadRequestException.class, () -> { - challengeController.patchChallengeStatus(newParent, 150L, - kidChallengeRequest); - }); + Assertions.assertThrows(BadRequestException.class, () -> + challengeController.patchChallengeStatus(mom, 150L, + kidChallengeRequest)); } + //Todo: 이거부터 해야함 @Test @DisplayName("자녀 돈길 요청 수락 / 거절 시, 자녀가 챌린지 생성 개수 제한 도달 시, 403 에러 테스트") public void testIfUpdateChallengeStatusMaxCountForbiddenErr() { - UserRepository mockUserRepository = Mockito.mock(UserRepository.class); ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( ChallengeCategoryRepository.class); TargetItemRepository mockTargetItemRepository = Mockito.mock(TargetItemRepository.class); @@ -2873,126 +2556,109 @@ public void testIfUpdateChallengeStatusMaxCountForbiddenErr() { CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - BindingResult mockBindingResult = Mockito.mock(BindingResult.class); //given - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); KidChallengeRequest kidChallengeRequest = new KidChallengeRequest(true, null); - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token1") - .build(); - - User newFather = User.builder().id(3L).username("parent2").isFemale(false) - .birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token1") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - - Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) - .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge5 = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge1 = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge2 = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge2 = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge3 = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge3 = Challenge.builder().id(2L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge4 = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge4 = Challenge.builder().id(1L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge6 = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge5 = Challenge.builder().id(2L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); - Family newFamily = Family.builder().code("asdfasdf").build(); + Family newFamily = Family.builder().code("family").build(); - FamilyUser newFamilyUser = FamilyUser.builder().user(newUser).family(newFamily).build(); + FamilyUser newFamilyUser = FamilyUser.builder().user(son).family(newFamily).build(); - FamilyUser newFamilyFather = FamilyUser.builder().user(newFather).family(newFamily).build(); + FamilyUser newFamilyFather = FamilyUser.builder().user(father).family(newFamily).build(); - FamilyUser newFamilyParent = FamilyUser.builder().user(newParent).family(newFamily).build(); + FamilyUser newFamilyParent = FamilyUser.builder().user(mom).family(newFamily).build(); - ChallengeUser newChallengeUser1 = ChallengeUser.builder().user(newUser) + ChallengeUser newChallengeUser1 = ChallengeUser.builder().user(son) .challenge(newChallenge1).member("parent").build(); - ChallengeUser newChallengeUser2 = ChallengeUser.builder().user(newUser) + ChallengeUser newChallengeUser2 = ChallengeUser.builder().user(son) .challenge(newChallenge2).member("parent").build(); - ChallengeUser newChallengeUser3 = ChallengeUser.builder().user(newUser) + ChallengeUser newChallengeUser3 = ChallengeUser.builder().user(son) .challenge(newChallenge3).member("parent").build(); - ChallengeUser newChallengeUser4 = ChallengeUser.builder().user(newUser) + ChallengeUser newChallengeUser4 = ChallengeUser.builder().user(son) .challenge(newChallenge4).member("parent").build(); - ChallengeUser newChallengeUser5 = ChallengeUser.builder().user(newUser) + ChallengeUser newChallengeUser5 = ChallengeUser.builder().user(son) .challenge(newChallenge5).member("parent").build(); - ChallengeUser newChallengeUser6 = ChallengeUser.builder().user(newUser) - .challenge(newChallenge6).member("parent").build(); + ChallengeUser newChallengeUser = ChallengeUser.builder().user(son) + .challenge(newChallenge).member("parent").build(); List challengeUserList = List.of(newChallengeUser1, newChallengeUser2, - newChallengeUser3, newChallengeUser4, newChallengeUser5, newChallengeUser6); + newChallengeUser3, newChallengeUser4, newChallengeUser5, newChallengeUser); List familyUserList = new ArrayList<>(); familyUserList.add(newFamilyFather); familyUserList.add(newFamilyParent); - Mockito.when(mockFamilyUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) .thenReturn(Optional.ofNullable(newFamilyUser)); Mockito.when(mockFamilyUserRepository.findByFamily(newFamily)) .thenReturn(familyUserList); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); - Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge6.getId())) - .thenReturn(Optional.of(newChallengeUser6)); + Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) + .thenReturn(Optional.of(newChallengeUser)); Mockito.when(mockChallengeRepository.save(newChallenge)).thenReturn(newChallenge); Mockito.when(mockChallengeRepository.findById(1L)) - .thenReturn(Optional.ofNullable(newChallenge)); + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockTargetItemRepository.findByName(newTargetItem.getName())) .thenReturn(newTargetItem); Mockito.when( @@ -3008,7 +2674,7 @@ public void testIfUpdateChallengeStatusMaxCountForbiddenErr() { //then Assertions.assertThrows(ForbiddenException.class, - () -> challengeController.patchChallengeStatus(newParent, newChallenge6.getId(), + () -> challengeController.patchChallengeStatus(mom, newChallenge.getId(), kidChallengeRequest)); } @@ -3029,65 +2695,44 @@ public void testIfUpdateChallengeStatusNotAuthUserForbiddenErr() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - ChallengeRequest challengeRequest1 = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 1500L, 100L, 15L, "test"); - KidChallengeRequest kidChallengeRequest = new KidChallengeRequest(true, null); - User newUser = User.builder().id(1L).username("user").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("user1").isFemale(true).birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token") - .build(); - - User newParent1 = User.builder().id(3L).username("user1").isFemale(false) - .birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()).contractUser(newParent) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest1.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest1.getTotalPrice()) - .weekPrice(challengeRequest1.getWeekPrice()).weeks(challengeRequest1.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest1.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().id(1L).challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); ChallengeUser newChallengeUser1 = ChallengeUser.builder().id(2L).challenge(newChallenge1) .member("parent") - .user(newUser).build(); + .user(son).build(); Family newFamily = Family.builder().id(1L) .code("adfadfaf").build(); FamilyUser newFamilyUser = FamilyUser.builder().id(1L) - .family(newFamily).user(newParent).build(); + .family(newFamily).user(mom).build(); FamilyUser newFamilyUser1 = FamilyUser.builder().id(2L) - .family(newFamily).user(newUser).build(); + .family(newFamily).user(son).build(); FamilyUser newFamilyUser2 = FamilyUser.builder().id(3L) - .family(newFamily).user(newParent1).build(); + .family(newFamily).user(father).build(); List challengeUserList = new ArrayList<>(); challengeUserList.add(newChallengeUser); @@ -3097,17 +2742,17 @@ public void testIfUpdateChallengeStatusNotAuthUserForbiddenErr() { familyUserList.add(newFamilyUser1); familyUserList.add(newFamilyUser2); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); Mockito.when(mockChallengeRepository.findById(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallenge)); + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockChallengeRepository.findById(newChallenge1.getId())) - .thenReturn(Optional.ofNullable(newChallenge1)); - Mockito.when(mockFamilyUserRepository.findByUserId(newParent1.getId())) + .thenReturn(Optional.of(newChallenge1)); + Mockito.when(mockFamilyUserRepository.findByUserId(father.getId())) .thenReturn(Optional.ofNullable(newFamilyUser2)); - Mockito.when(mockFamilyUserRepository.findByUserId(newParent.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(mom.getId())) .thenReturn(Optional.ofNullable(newFamilyUser)); - Mockito.when(mockFamilyUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) .thenReturn(Optional.ofNullable(newFamilyUser)); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) .thenReturn(Optional.ofNullable(newChallengeUser)); @@ -3125,10 +2770,9 @@ public void testIfUpdateChallengeStatusNotAuthUserForbiddenErr() { ChallengeController challengeController = new ChallengeController(challengeService); //then - Assertions.assertThrows(ForbiddenException.class, () -> { - challengeController.patchChallengeStatus(newParent1, newChallenge.getId(), - kidChallengeRequest); - }); + Assertions.assertThrows(ForbiddenException.class, () -> + challengeController.patchChallengeStatus(father, newChallenge.getId(), + kidChallengeRequest)); } @Test @@ -3148,57 +2792,41 @@ public void testIfUpdateChallengeStatusAlreadyChallengeBadRequestErr() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - ChallengeRequest challengeRequest1 = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 1500L, 100L, 15L, "test"); - KidChallengeRequest kidChallengeRequest = new KidChallengeRequest(true, null); - User newUser = User.builder().id(1L).username("user").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("user1").isFemale(true).birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); - - Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest1.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest1.getTotalPrice()) - .weekPrice(challengeRequest1.getWeekPrice()).weeks(challengeRequest1.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest1.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().id(1L).challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); ChallengeUser newChallengeUser1 = ChallengeUser.builder().id(2L).challenge(newChallenge1) .member("parent") - .user(newUser).build(); + .user(son).build(); Family newFamily = Family.builder().id(1L) .code("adfadfaf").build(); FamilyUser newFamilyUser = FamilyUser.builder().id(1L) - .family(newFamily).user(newParent).build(); + .family(newFamily).user(mom).build(); FamilyUser newFamilyUser1 = FamilyUser.builder().id(2L) - .family(newFamily).user(newUser).build(); + .family(newFamily).user(son).build(); List challengeUserList = new ArrayList<>(); challengeUserList.add(newChallengeUser); @@ -3207,15 +2835,15 @@ public void testIfUpdateChallengeStatusAlreadyChallengeBadRequestErr() { familyUserList.add(newFamilyUser); familyUserList.add(newFamilyUser1); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); Mockito.when(mockChallengeRepository.findById(newChallenge.getId())) - .thenReturn(Optional.ofNullable(newChallenge)); + .thenReturn(Optional.of(newChallenge)); Mockito.when(mockChallengeRepository.findById(newChallenge1.getId())) - .thenReturn(Optional.ofNullable(newChallenge1)); - Mockito.when(mockFamilyUserRepository.findByUserId(newParent.getId())) + .thenReturn(Optional.of(newChallenge1)); + Mockito.when(mockFamilyUserRepository.findByUserId(mom.getId())) .thenReturn(Optional.ofNullable(newFamilyUser)); - Mockito.when(mockFamilyUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(son.getId())) .thenReturn(Optional.ofNullable(newFamilyUser1)); Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) .thenReturn(Optional.ofNullable(newChallengeUser)); @@ -3233,10 +2861,9 @@ public void testIfUpdateChallengeStatusAlreadyChallengeBadRequestErr() { ChallengeController challengeController = new ChallengeController(challengeService); //then - Assertions.assertThrows(BadRequestException.class, () -> { - challengeController.patchChallengeStatus(newParent, newChallenge.getId(), - kidChallengeRequest); - }); + Assertions.assertThrows(BadRequestException.class, () -> + challengeController.patchChallengeStatus(mom, newChallenge.getId(), + kidChallengeRequest)); } @Test @@ -3256,53 +2883,48 @@ public void testIfReadWeekInfo() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("user1").isFemale(true).birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + Challenge newChallenge1 = Challenge.builder().id(2L).title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().id(1L).challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); - Family newFamily = Family.builder().id(1L) - .code("adfadfaf").build(); - - FamilyUser newFamilyUser = FamilyUser.builder().id(1L) - .family(newFamily).user(newParent).build(); + ChallengeUser newChallengeUser1 = ChallengeUser.builder().id(2L).challenge(newChallenge1) + .member("parent").user(son).build(); List challengeUserList = new ArrayList<>(); challengeUserList.add(newChallengeUser); - - List familyUserList = new ArrayList<>(); - familyUserList.add(newFamilyUser); + challengeUserList.add(newChallengeUser1); List progressList = new ArrayList<>(); + List progressList1 = new ArrayList<>(); - for (Long i = 1L; i <= newChallenge.getWeeks(); i++) { + for (long i = 1L; i <= challengeRequest.getWeeks(); i++) { Progress newProgress = Progress.builder().weeks(i).challenge(newChallenge) .isAchieved(false).build(); - if (i == 1L) { + Progress newProgress1 = Progress.builder().weeks(i).challenge(newChallenge1) + .isAchieved(false).build(); + if (i == 1L || i == 2L) { newProgress.setIsAchieved(true); + newProgress1.setIsAchieved(true); } progressList.add(newProgress); + progressList1.add(newProgress1); } ReflectionTestUtils.setField( @@ -3313,9 +2935,18 @@ public void testIfReadWeekInfo() { Timestamp.class ); + ReflectionTestUtils.setField( + progressList1.get(0), + AbstractTimestamp.class, + "createdAt", + Timestamp.valueOf(LocalDateTime.now()), + Timestamp.class + ); + newChallenge.setProgressList(progressList); + newChallenge1.setProgressList(progressList1); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); //when @@ -3324,10 +2955,11 @@ public void testIfReadWeekInfo() { mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); - CommonResponse result = challengeController.getWeekInfo(newUser); + CommonResponse result = challengeController.getWeekInfo(son); //then - WeekDTO weekDTO1 = new WeekDTO(newChallenge.getWeekPrice(), newChallenge.getWeekPrice()); + WeekDTO weekDTO1 = new WeekDTO(newChallenge.getWeekPrice() + newChallenge1.getWeekPrice(), + newChallenge.getWeekPrice() + newChallenge1.getWeekPrice()); Assertions.assertEquals(weekDTO1, result.getData()); } @@ -3349,45 +2981,26 @@ public void testIfReadKidWeekInfo() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - Kid newKid = Kid.builder().id(1L).totalChallenge(0L).achievedChallenge(0L).savings(0L) - .user(newUser).level(0L).build(); - - newUser.setKid(newKid); - - User newParent = User.builder().id(2L).username("user1").isFemale(true).birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().id(1L).challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Family newFamily = Family.builder().id(1L) .code("adfadfaf").build(); FamilyUser newFamilyUser = FamilyUser.builder().id(1L) - .family(newFamily).user(newParent).build(); + .family(newFamily).user(mom).build(); FamilyUser newFamilyUser1 = FamilyUser.builder().id(2L) - .family(newFamily).user(newUser).build(); + .family(newFamily).user(son).build(); List challengeUserList = new ArrayList<>(); challengeUserList.add(newChallengeUser); @@ -3398,7 +3011,7 @@ public void testIfReadKidWeekInfo() { List progressList = new ArrayList<>(); - for (Long i = 1L; i <= newChallenge.getWeeks(); i++) { + for (long i = 1L; i <= newChallenge.getWeeks(); i++) { Progress newProgress = Progress.builder().weeks(i).challenge(newChallenge) .isAchieved(false).build(); if (i == 1L) { @@ -3417,10 +3030,10 @@ public void testIfReadKidWeekInfo() { newChallenge.setProgressList(progressList); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); - Mockito.when(mockFamilyUserRepository.findByUserId(newParent.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(mom.getId())) .thenReturn(Optional.ofNullable(newFamilyUser)); Mockito.when(mockFamilyUserRepository.findByFamily(newFamily)) @@ -3432,8 +3045,8 @@ public void testIfReadKidWeekInfo() { mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); - CommonResponse result = challengeController.getKidWeekInfo(newParent, - newUser.getKid().getId()); + CommonResponse result = challengeController.getKidWeekInfo(mom, + son.getKid().getId()); //then WeekDTO weekDTO1 = new WeekDTO(newChallenge.getWeekPrice(), newChallenge.getWeekPrice()); @@ -3443,7 +3056,7 @@ public void testIfReadKidWeekInfo() { @Test @DisplayName("자녀의 주차 정보 가져오기 API 실행 시, 가족이 없을 때, 400 에러") - public void testIfReadKidWeekInfoNotExsitFamilyBadRequestErr() { + public void testIfReadKidWeekInfoNotExistFamilyBadRequestErr() { //given ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( @@ -3458,48 +3071,25 @@ public void testIfReadKidWeekInfoNotExsitFamilyBadRequestErr() { ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); CommentRepository mockCommentRepository = Mockito.mock(CommentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - Kid newKid = Kid.builder().id(2L).totalChallenge(0L).achievedChallenge(0L).savings(0L) - .user(newUser).level(0L).build(); - - newUser.setKid(newKid); - - User newParent = User.builder().id(2L).username("user1").isFemale(true).birthday("19990623") - .authenticationCode("code1").provider("kakao").isKid(false).refreshToken("token") - .build(); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + son.setKid(sonKid); Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().id(1L).challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Family newFamily = Family.builder().id(1L) - .code("adfadfaf").build(); - - Family newFamily1 = Family.builder().id(2L) - .code("afasdfasdf").build(); + .code("family").build(); FamilyUser newFamilyUser = FamilyUser.builder().id(1L) - .family(newFamily).user(newParent).build(); - - FamilyUser newFamilyUser1 = FamilyUser.builder().id(2L) - .family(newFamily1).user(newUser).build(); + .family(newFamily).user(mom).build(); List challengeUserList = new ArrayList<>(); challengeUserList.add(newChallengeUser); @@ -3507,11 +3097,9 @@ public void testIfReadKidWeekInfoNotExsitFamilyBadRequestErr() { List familyUserList = new ArrayList<>(); familyUserList.add(newFamilyUser); - List familyUserList1 = List.of(newFamilyUser1); - List progressList = new ArrayList<>(); - for (Long i = 1L; i <= newChallenge.getWeeks(); i++) { + for (long i = 1L; i <= newChallenge.getWeeks(); i++) { Progress newProgress = Progress.builder().weeks(i).challenge(newChallenge) .isAchieved(false).build(); if (i == 1L) { @@ -3530,9 +3118,9 @@ public void testIfReadKidWeekInfoNotExsitFamilyBadRequestErr() { newChallenge.setProgressList(progressList); - Mockito.when(mockChallengeUserRepository.findByUserId(newUser.getId())) + Mockito.when(mockChallengeUserRepository.findByUserId(son.getId())) .thenReturn(challengeUserList); - Mockito.when(mockFamilyUserRepository.findByUserId(newParent.getId())) + Mockito.when(mockFamilyUserRepository.findByUserId(mom.getId())) .thenReturn(Optional.of(newFamilyUser)); Mockito.when(mockFamilyUserRepository.findByFamily(newFamily)) .thenReturn(familyUserList); @@ -3545,6 +3133,6 @@ public void testIfReadKidWeekInfoNotExsitFamilyBadRequestErr() { //then Assertions.assertThrows(BadRequestException.class, - () -> challengeController.getKidWeekInfo(newParent, newUser.getKid().getId())); + () -> challengeController.getKidWeekInfo(mom, son.getKid().getId())); } } diff --git a/src/test/java/com/ceos/bankids/unit/controller/ProgressControllerTest.java b/src/test/java/com/ceos/bankids/unit/controller/ProgressControllerTest.java index eb12f661..7d595a2f 100644 --- a/src/test/java/com/ceos/bankids/unit/controller/ProgressControllerTest.java +++ b/src/test/java/com/ceos/bankids/unit/controller/ProgressControllerTest.java @@ -1,5 +1,6 @@ package com.ceos.bankids.unit.controller; +import com.ceos.bankids.Enum.ChallengeStatus; import com.ceos.bankids.config.CommonResponse; import com.ceos.bankids.controller.ProgressController; import com.ceos.bankids.controller.request.ChallengeRequest; @@ -15,14 +16,10 @@ import com.ceos.bankids.dto.ProgressDTO; import com.ceos.bankids.exception.BadRequestException; import com.ceos.bankids.exception.ForbiddenException; -import com.ceos.bankids.repository.ChallengeCategoryRepository; import com.ceos.bankids.repository.ChallengeRepository; import com.ceos.bankids.repository.ChallengeUserRepository; -import com.ceos.bankids.repository.FamilyUserRepository; import com.ceos.bankids.repository.KidRepository; -import com.ceos.bankids.repository.ParentRepository; import com.ceos.bankids.repository.ProgressRepository; -import com.ceos.bankids.repository.TargetItemRepository; import com.ceos.bankids.service.ProgressServiceImpl; import java.sql.Timestamp; import java.time.LocalDateTime; @@ -38,57 +35,61 @@ public class ProgressControllerTest { + + // Enum ChallengeStatus + private static final ChallengeStatus pending = ChallengeStatus.PENDING; + private static final ChallengeStatus walking = ChallengeStatus.WALKING; + private static final ChallengeStatus achieved = ChallengeStatus.ACHIEVED; + private static final ChallengeStatus failed = ChallengeStatus.FAILED; + private static final ChallengeStatus rejected = ChallengeStatus.REJECTED; + private static final ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", + "전자제품", "테스트용 돈길", 30L, 30000L, + 10000L, 3L, "test"); + private static final User son = User.builder().id(1L).username("son").birthday("19990623") + .authenticationCode("code") + .provider("kakao").isKid(true).isFemale(false).refreshToken("token").build(); + private static final User mom = User.builder().id(2L).username("mom").birthday("19440505") + .authenticationCode("code").provider("kakao").isKid(false).isFemale(true) + .refreshToken("token").build(); + private static final User father = User.builder().id(3L).username("father").isKid(false) + .isFemale(false).authenticationCode("code").provider("kakao").refreshToken("token").build(); + private static final User daughter = User.builder().id(4L).username("daughter").isKid(true) + .isFemale(true).authenticationCode("code").provider("kakao").refreshToken("token").build(); + private static final Kid sonKid = Kid.builder().id(1L).achievedChallenge(0L).totalChallenge(0L) + .user(son).level(0L).savings(0L).build(); + private static final Parent momParent = Parent.builder().id(1L).acceptedRequest(0L) + .totalRequest(0L).user(mom).build(); + private static final Parent fatherParent = Parent.builder().id(2L).acceptedRequest(0L) + .totalRequest(0L).user(father).build(); + ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) + .category("이자율 받기").build(); + TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + @Test - @DisplayName("돈길 걷기 요청 시, 프로그레스의 row가 정상적으로 업데이트 되는지 테스트") + @DisplayName("돈길 걷기 요청 시, 프로그레스가 정상적으로 업데이트 되는지 테스트") public void testIfSavingsProgressRowUpdate() { //given - ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( - ChallengeCategoryRepository.class); - TargetItemRepository mockTargetItemRepository = Mockito.mock(TargetItemRepository.class); ChallengeRepository mockChallengeRepository = Mockito.mock(ChallengeRepository.class); ChallengeUserRepository mockChallengeUserRepository = Mockito.mock( ChallengeUserRepository.class); ProgressRepository mockProgressRepository = Mockito.mock(ProgressRepository.class); - FamilyUserRepository mockFamilyUserRepository = Mockito.mock(FamilyUserRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); - ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - Kid newKid = Kid.builder().user(newUser).savings(0L).build(); - newUser.setKid(newKid); - - Parent parent = Parent.builder().user(newParent).build(); - newParent.setParent(parent); - newParent.setParent(parent); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + son.setKid(sonKid); + mom.setParent(momParent); Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) - .successWeeks(0L) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()).successWeeks(0L) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Progress newProgress = Progress.builder() .id(1L) @@ -117,11 +118,12 @@ public void testIfSavingsProgressRowUpdate() { //when ProgressServiceImpl progressService = new ProgressServiceImpl(mockProgressRepository, - mockChallengeUserRepository, mockChallengeRepository, mockFamilyUserRepository, - mockKidRepository, mockParentRepository); + mockChallengeUserRepository, mockChallengeRepository, + mockKidRepository); ProgressController progressController = new ProgressController(progressService); ProgressDTO progressDTO = new ProgressDTO(newProgress); - CommonResponse result = progressController.patchProgress(newUser, newChallenge.getId()); + CommonResponse result = progressController.patchProgress(son, + newChallenge.getId()); //then ArgumentCaptor pCaptor = ArgumentCaptor.forClass(Long.class); @@ -143,52 +145,26 @@ public void testIfSavingsProgressRowUpdate() { public void testIfSavingsProgressChallengeSuccessRowUpdate() { //given - ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( - ChallengeCategoryRepository.class); - TargetItemRepository mockTargetItemRepository = Mockito.mock(TargetItemRepository.class); ChallengeRepository mockChallengeRepository = Mockito.mock(ChallengeRepository.class); ChallengeUserRepository mockChallengeUserRepository = Mockito.mock( ChallengeUserRepository.class); ProgressRepository mockProgressRepository = Mockito.mock(ProgressRepository.class); - FamilyUserRepository mockFamilyUserRepository = Mockito.mock(FamilyUserRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); - ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 30000L, 10000L, 3L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - Kid newKid = Kid.builder().user(newUser).savings(0L).build(); - newUser.setKid(newKid); - - Parent parent = Parent.builder().user(newParent).build(); - newParent.setParent(parent); - newParent.setParent(parent); + son.setKid(sonKid); + mom.setParent(momParent); - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - - Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) - .successWeeks(2L) + Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking).successWeeks(2L) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Progress newProgress = Progress.builder() .id(1L) @@ -235,11 +211,12 @@ public void testIfSavingsProgressChallengeSuccessRowUpdate() { //when ProgressServiceImpl progressService = new ProgressServiceImpl(mockProgressRepository, - mockChallengeUserRepository, mockChallengeRepository, mockFamilyUserRepository, - mockKidRepository, mockParentRepository); + mockChallengeUserRepository, mockChallengeRepository, + mockKidRepository); ProgressController progressController = new ProgressController(progressService); ProgressDTO progressDTO = new ProgressDTO(newProgress2); - CommonResponse result = progressController.patchProgress(newUser, newChallenge.getId()); + CommonResponse result = progressController.patchProgress(son, + newChallenge.getId()); //then ArgumentCaptor pCaptor = ArgumentCaptor.forClass(Long.class); @@ -250,9 +227,8 @@ public void testIfSavingsProgressChallengeSuccessRowUpdate() { Assertions.assertEquals(newProgress.getChallenge().getId(), pCaptor.getValue()); Assertions.assertEquals(newProgress2.getWeeks(), wCaptor.getValue()); - Assertions.assertEquals(2L, newChallenge.getIsAchieved()); - Assertions.assertEquals(0L, newChallenge.getStatus()); - Assertions.assertEquals(39000L, newKid.getSavings()); + Assertions.assertEquals(achieved, newChallenge.getChallengeStatus()); + Assertions.assertEquals(39000L, sonKid.getSavings()); Assertions.assertEquals(3L, newChallenge.getSuccessWeeks()); Assertions.assertEquals(true, newProgress2.getIsAchieved()); @@ -264,52 +240,26 @@ public void testIfSavingsProgressChallengeSuccessRowUpdate() { public void testIfSavingsProgressParentUserForbiddenErr() { //given - ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( - ChallengeCategoryRepository.class); - TargetItemRepository mockTargetItemRepository = Mockito.mock(TargetItemRepository.class); ChallengeRepository mockChallengeRepository = Mockito.mock(ChallengeRepository.class); ChallengeUserRepository mockChallengeUserRepository = Mockito.mock( ChallengeUserRepository.class); ProgressRepository mockProgressRepository = Mockito.mock(ProgressRepository.class); - FamilyUserRepository mockFamilyUserRepository = Mockito.mock(FamilyUserRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); - ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - Kid newKid = Kid.builder().user(newUser).savings(0L).build(); - newUser.setKid(newKid); - Parent parent = Parent.builder().user(newParent).build(); - newParent.setParent(parent); - - newParent.setParent(parent); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + son.setKid(sonKid); + mom.setParent(momParent); Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) - .successWeeks(0L) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(1L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Progress newProgress = Progress.builder() .id(1L) @@ -338,14 +288,13 @@ public void testIfSavingsProgressParentUserForbiddenErr() { //when ProgressServiceImpl progressService = new ProgressServiceImpl(mockProgressRepository, - mockChallengeUserRepository, mockChallengeRepository, mockFamilyUserRepository, - mockKidRepository, mockParentRepository); + mockChallengeUserRepository, mockChallengeRepository, + mockKidRepository); ProgressController progressController = new ProgressController(progressService); - ProgressDTO progressDTO = new ProgressDTO(newProgress); //then Assertions.assertThrows(ForbiddenException.class, - () -> progressController.patchProgress(newParent, newChallenge.getId())); + () -> progressController.patchProgress(mom, newChallenge.getId())); } @Test @@ -353,52 +302,26 @@ public void testIfSavingsProgressParentUserForbiddenErr() { public void testIfSavingsProgressNotRunningChallengeForbiddenErr() { //given - ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( - ChallengeCategoryRepository.class); - TargetItemRepository mockTargetItemRepository = Mockito.mock(TargetItemRepository.class); ChallengeRepository mockChallengeRepository = Mockito.mock(ChallengeRepository.class); ChallengeUserRepository mockChallengeUserRepository = Mockito.mock( ChallengeUserRepository.class); ProgressRepository mockProgressRepository = Mockito.mock(ProgressRepository.class); - FamilyUserRepository mockFamilyUserRepository = Mockito.mock(FamilyUserRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); - ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 150000L, 10000L, 15L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - Kid newKid = Kid.builder().user(newUser).savings(0L).build(); - newUser.setKid(newKid); - - Parent parent = Parent.builder().user(newParent).build(); - newParent.setParent(parent); - - newParent.setParent(parent); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + son.setKid(sonKid); + mom.setParent(momParent); Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(0L).totalPrice(challengeRequest.getTotalPrice()) - .successWeeks(0L) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(0L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Progress newProgress = Progress.builder() .id(1L) @@ -427,14 +350,13 @@ public void testIfSavingsProgressNotRunningChallengeForbiddenErr() { //when ProgressServiceImpl progressService = new ProgressServiceImpl(mockProgressRepository, - mockChallengeUserRepository, mockChallengeRepository, mockFamilyUserRepository, - mockKidRepository, mockParentRepository); + mockChallengeUserRepository, mockChallengeRepository, + mockKidRepository); ProgressController progressController = new ProgressController(progressService); - ProgressDTO progressDTO = new ProgressDTO(newProgress); //then Assertions.assertThrows(BadRequestException.class, - () -> progressController.patchProgress(newUser, newChallenge.getId())); + () -> progressController.patchProgress(son, newChallenge.getId())); } @Test @@ -442,52 +364,26 @@ public void testIfSavingsProgressNotRunningChallengeForbiddenErr() { public void testIfSavingsProgressBetterThanChallengeWeeksBadRequestErr() { //given - ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( - ChallengeCategoryRepository.class); - TargetItemRepository mockTargetItemRepository = Mockito.mock(TargetItemRepository.class); ChallengeRepository mockChallengeRepository = Mockito.mock(ChallengeRepository.class); ChallengeUserRepository mockChallengeUserRepository = Mockito.mock( ChallengeUserRepository.class); ProgressRepository mockProgressRepository = Mockito.mock(ProgressRepository.class); - FamilyUserRepository mockFamilyUserRepository = Mockito.mock(FamilyUserRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); - ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 30000L, 10000L, 3L, "test"); + son.setKid(sonKid); + mom.setParent(momParent); - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - Kid newKid = Kid.builder().user(newUser).savings(0L).build(); - newUser.setKid(newKid); - - Parent parent = Parent.builder().user(newParent).build(); - newParent.setParent(parent); - - newParent.setParent(parent); - - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); - - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - - Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) - .successWeeks(2L) + Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(pending) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Progress newProgress = Progress.builder() .id(1L) @@ -534,14 +430,13 @@ public void testIfSavingsProgressBetterThanChallengeWeeksBadRequestErr() { //when ProgressServiceImpl progressService = new ProgressServiceImpl(mockProgressRepository, - mockChallengeUserRepository, mockChallengeRepository, mockFamilyUserRepository, - mockKidRepository, mockParentRepository); + mockChallengeUserRepository, mockChallengeRepository, + mockKidRepository); ProgressController progressController = new ProgressController(progressService); - ProgressDTO progressDTO = new ProgressDTO(newProgress2); //then Assertions.assertThrows(BadRequestException.class, - () -> progressController.patchProgress(newUser, + () -> progressController.patchProgress(son, newChallenge.getId())); } @@ -550,52 +445,26 @@ public void testIfSavingsProgressBetterThanChallengeWeeksBadRequestErr() { public void testIfSavingsProgressAlreadyRunningWeeksBadRequestErr() { //given - ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( - ChallengeCategoryRepository.class); - TargetItemRepository mockTargetItemRepository = Mockito.mock(TargetItemRepository.class); ChallengeRepository mockChallengeRepository = Mockito.mock(ChallengeRepository.class); ChallengeUserRepository mockChallengeUserRepository = Mockito.mock( ChallengeUserRepository.class); ProgressRepository mockProgressRepository = Mockito.mock(ProgressRepository.class); - FamilyUserRepository mockFamilyUserRepository = Mockito.mock(FamilyUserRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); - ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 30000L, 10000L, 3L, "test"); - - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); - - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") - .build(); - - Kid newKid = Kid.builder().user(newUser).savings(0L).build(); - newUser.setKid(newKid); - - Parent parent = Parent.builder().user(newParent).build(); - newParent.setParent(parent); - - newParent.setParent(parent); - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); + son.setKid(sonKid); + mom.setParent(momParent); - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); - - Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) - .successWeeks(2L) + Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Progress newProgress = Progress.builder() .id(1L) @@ -642,14 +511,13 @@ public void testIfSavingsProgressAlreadyRunningWeeksBadRequestErr() { //when ProgressServiceImpl progressService = new ProgressServiceImpl(mockProgressRepository, - mockChallengeUserRepository, mockChallengeRepository, mockFamilyUserRepository, - mockKidRepository, mockParentRepository); + mockChallengeUserRepository, mockChallengeRepository, + mockKidRepository); ProgressController progressController = new ProgressController(progressService); - ProgressDTO progressDTO = new ProgressDTO(newProgress2); //then Assertions.assertThrows(BadRequestException.class, - () -> progressController.patchProgress(newUser, + () -> progressController.patchProgress(son, newChallenge.getId())); } @@ -658,56 +526,107 @@ public void testIfSavingsProgressAlreadyRunningWeeksBadRequestErr() { public void testIfSavingsProgressNotAuthUserForbiddenErr() { //given - ChallengeCategoryRepository mockChallengeCategoryRepository = Mockito.mock( - ChallengeCategoryRepository.class); - TargetItemRepository mockTargetItemRepository = Mockito.mock(TargetItemRepository.class); ChallengeRepository mockChallengeRepository = Mockito.mock(ChallengeRepository.class); ChallengeUserRepository mockChallengeUserRepository = Mockito.mock( ChallengeUserRepository.class); ProgressRepository mockProgressRepository = Mockito.mock(ProgressRepository.class); - FamilyUserRepository mockFamilyUserRepository = Mockito.mock(FamilyUserRepository.class); KidRepository mockKidRepository = Mockito.mock(KidRepository.class); - ParentRepository mockParentRepository = Mockito.mock(ParentRepository.class); - ChallengeRequest challengeRequest = new ChallengeRequest(true, "이자율 받기", "전자제품", "에어팟 사기", - 30L, - 30000L, 10000L, 3L, "test"); + son.setKid(sonKid); + mom.setParent(momParent); - User newUser = User.builder().id(1L).username("user1").isFemale(true).birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(true).refreshToken("token").build(); + Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) + .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); + + ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) + .member("parent").user(son).build(); - User newUser1 = User.builder().id(3L).username("user2").isFemale(true).birthday("19990213") - .authenticationCode("code2").provider("kakao").isKid(true).refreshToken("torken") + Progress newProgress = Progress.builder() + .id(1L) + .challenge(newChallenge) + .weeks(1L) + .isAchieved(true) + .build(); + + ReflectionTestUtils.setField( + newProgress, + AbstractTimestamp.class, + "createdAt", + Timestamp.valueOf(LocalDateTime.now().minusDays(15)), + Timestamp.class + ); + + Progress newProgress1 = Progress.builder() + .id(2L) + .challenge(newChallenge) + .weeks(2L) + .isAchieved(true) .build(); - User newParent = User.builder().id(2L).username("parent1").isFemale(true) - .birthday("19990521") - .authenticationCode("code").provider("kakao").isKid(false).refreshToken("token") + Progress newProgress2 = Progress.builder() + .id(3L) + .challenge(newChallenge) + .weeks(3L) + .isAchieved(true) .build(); - Kid newKid = Kid.builder().user(newUser).savings(0L).build(); - newUser.setKid(newKid); + List progressList = new ArrayList<>(); + progressList.add(newProgress); + progressList.add(newProgress1); + progressList.add(newProgress2); + + newChallenge.setProgressList(progressList); + + Mockito.when(mockChallengeRepository.findById(newChallenge.getId())) + .thenReturn(Optional.of(newChallenge)); + Mockito.when(mockChallengeUserRepository.findByChallengeId(newChallenge.getId())) + .thenReturn(Optional.of(newChallengeUser)); + Mockito.when(mockProgressRepository.findByChallengeIdAndWeeks(newChallenge.getId(), + newProgress2.getWeeks())).thenReturn(Optional.of(newProgress2)); - Parent parent = Parent.builder().user(newParent).build(); - newParent.setParent(parent); + //when + ProgressServiceImpl progressService = new ProgressServiceImpl(mockProgressRepository, + mockChallengeUserRepository, mockChallengeRepository, + mockKidRepository); + ProgressController progressController = new ProgressController(progressService); + + //then + Assertions.assertThrows(ForbiddenException.class, + () -> progressController.patchProgress(daughter, + newChallenge.getId())); + } - newParent.setParent(parent); + @Test + @DisplayName("돈길 걷기 요청 시, 주차가 한참 지났을 때, 400에러") + public void testIfSavingsProgressNotExistWalkingProgressBadRequestErr() { - ChallengeCategory newChallengeCategory = ChallengeCategory.builder().id(1L) - .category("이자율 받기").build(); + //given + ChallengeRepository mockChallengeRepository = Mockito.mock(ChallengeRepository.class); + ChallengeUserRepository mockChallengeUserRepository = Mockito.mock( + ChallengeUserRepository.class); + ProgressRepository mockProgressRepository = Mockito.mock(ProgressRepository.class); + KidRepository mockKidRepository = Mockito.mock(KidRepository.class); - TargetItem newTargetItem = TargetItem.builder().id(1L).name("전자제품").build(); + son.setKid(sonKid); + mom.setParent(momParent); - Challenge newChallenge = Challenge.builder().id(1L).title(challengeRequest.getTitle()) - .contractUser(newParent) - .isAchieved(1L).totalPrice(challengeRequest.getTotalPrice()) - .successWeeks(2L) + Challenge newChallenge = Challenge.builder().title(challengeRequest.getTitle()) + .contractUser(challengeRequest.getIsMom() ? mom : father) + .totalPrice(challengeRequest.getTotalPrice()) .weekPrice(challengeRequest.getWeekPrice()).weeks(challengeRequest.getWeeks()) - .challengeCategory(newChallengeCategory).targetItem(newTargetItem).status(2L) - .interestRate(challengeRequest.getInterestRate()).build(); + .challengeStatus(walking) + .interestRate(challengeRequest.getInterestRate()) + .challengeCategory(newChallengeCategory).targetItem(newTargetItem) + .filename(challengeRequest.getFileName()).build(); ChallengeUser newChallengeUser = ChallengeUser.builder().challenge(newChallenge) - .member("parent").user(newUser).build(); + .member("parent").user(son).build(); Progress newProgress = Progress.builder() .id(1L) @@ -720,7 +639,7 @@ public void testIfSavingsProgressNotAuthUserForbiddenErr() { newProgress, AbstractTimestamp.class, "createdAt", - Timestamp.valueOf(LocalDateTime.now().minusDays(15)), + Timestamp.valueOf(LocalDateTime.now().minusDays(40)), Timestamp.class ); @@ -735,7 +654,7 @@ public void testIfSavingsProgressNotAuthUserForbiddenErr() { .id(3L) .challenge(newChallenge) .weeks(3L) - .isAchieved(true) + .isAchieved(false) .build(); List progressList = new ArrayList<>(); @@ -754,14 +673,13 @@ public void testIfSavingsProgressNotAuthUserForbiddenErr() { //when ProgressServiceImpl progressService = new ProgressServiceImpl(mockProgressRepository, - mockChallengeUserRepository, mockChallengeRepository, mockFamilyUserRepository, - mockKidRepository, mockParentRepository); + mockChallengeUserRepository, mockChallengeRepository, + mockKidRepository); ProgressController progressController = new ProgressController(progressService); - ProgressDTO progressDTO = new ProgressDTO(newProgress2); //then - Assertions.assertThrows(ForbiddenException.class, - () -> progressController.patchProgress(newUser1, + Assertions.assertThrows(BadRequestException.class, + () -> progressController.patchProgress(son, newChallenge.getId())); } } From 24ce4da8e2a717dd911bbbebc71cddc9c984d490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A4=80?= Date: Tue, 9 Aug 2022 15:17:05 +0900 Subject: [PATCH 09/13] =?UTF-8?q?fix:=20challengeStatus=20=EC=BB=AC?= =?UTF-8?q?=EB=9F=BC=20default=20=EA=B0=92=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/ceos/bankids/domain/Challenge.java | 1 + .../ceos/bankids/unit/controller/ChallengeControllerTest.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/ceos/bankids/domain/Challenge.java b/src/main/java/com/ceos/bankids/domain/Challenge.java index a7b88b3d..6aafb74b 100644 --- a/src/main/java/com/ceos/bankids/domain/Challenge.java +++ b/src/main/java/com/ceos/bankids/domain/Challenge.java @@ -51,6 +51,7 @@ public class Challenge extends AbstractTimestamp { @Column(nullable = false) @Enumerated(EnumType.STRING) + @ColumnDefault("PENDING") private ChallengeStatus challengeStatus; @Column(nullable = false) diff --git a/src/test/java/com/ceos/bankids/unit/controller/ChallengeControllerTest.java b/src/test/java/com/ceos/bankids/unit/controller/ChallengeControllerTest.java index e41cdab0..a4971fb7 100644 --- a/src/test/java/com/ceos/bankids/unit/controller/ChallengeControllerTest.java +++ b/src/test/java/com/ceos/bankids/unit/controller/ChallengeControllerTest.java @@ -3046,7 +3046,7 @@ public void testIfReadKidWeekInfo() { mockProgressRepository, mockFamilyUserRepository, mockCommentRepository, mockKidRepository, mockParentRepository); ChallengeController challengeController = new ChallengeController(challengeService); - CommonResponse result = challengeController.getKidWeekInfo(mom, + CommonResponse result = challengeController.getKidWeekInfo(mom, son.getKid().getId()); //then From 3e68256f469de1deff16819a49ef71f80d40b7b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A4=80?= Date: Tue, 9 Aug 2022 15:25:43 +0900 Subject: [PATCH 10/13] =?UTF-8?q?chore:=20isAchieved,=20status=20=EC=BB=AC?= =?UTF-8?q?=EB=9F=BC=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/ceos/bankids/domain/Challenge.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/main/java/com/ceos/bankids/domain/Challenge.java b/src/main/java/com/ceos/bankids/domain/Challenge.java index 6aafb74b..3f4f2f6d 100644 --- a/src/main/java/com/ceos/bankids/domain/Challenge.java +++ b/src/main/java/com/ceos/bankids/domain/Challenge.java @@ -45,10 +45,6 @@ public class Challenge extends AbstractTimestamp { @Column(nullable = false, length = 20) private String title; - @Column(nullable = false) - @ColumnDefault("1") - private Long isAchieved; - @Column(nullable = false) @Enumerated(EnumType.STRING) @ColumnDefault("PENDING") @@ -63,10 +59,6 @@ public class Challenge extends AbstractTimestamp { @Column(nullable = false) private Long weeks; - @Column(nullable = false) - @ColumnDefault("1") - private Long status; - @Column(nullable = false) private Long interestRate; @@ -146,13 +138,11 @@ public Challenge( } this.id = id; - this.isAchieved = isAchieved; this.challengeStatus = challengeStatus; this.title = title; this.totalPrice = totalPrice; this.weekPrice = weekPrice; this.weeks = weeks; - this.status = status; this.interestRate = interestRate; this.successWeeks = successWeeks; this.fileName = filename; From 7790ff7f80356b9afcf70b48063a8f2a8fccfe2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A4=80?= Date: Tue, 9 Aug 2022 15:37:50 +0900 Subject: [PATCH 11/13] =?UTF-8?q?fix:=20challengeDTO=20/=20challengeReposi?= =?UTF-8?q?tory=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/com/ceos/bankids/dto/ChallengeDTO.java | 5 +++++ .../com/ceos/bankids/repository/ChallengeRepository.java | 5 +---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/ceos/bankids/dto/ChallengeDTO.java b/src/main/java/com/ceos/bankids/dto/ChallengeDTO.java index d3f8cb90..f0a17963 100644 --- a/src/main/java/com/ceos/bankids/dto/ChallengeDTO.java +++ b/src/main/java/com/ceos/bankids/dto/ChallengeDTO.java @@ -1,5 +1,6 @@ package com.ceos.bankids.dto; +import com.ceos.bankids.Enum.ChallengeStatus; import com.ceos.bankids.domain.Challenge; import com.ceos.bankids.domain.Comment; import com.fasterxml.jackson.annotation.JsonFormat; @@ -29,6 +30,9 @@ public class ChallengeDTO { @ApiModelProperty(example = "전자제품") private String itemName; + @ApiModelProperty(example = "PENDING") + private ChallengeStatus challengeStatus; + @ApiModelProperty(example = "부모와 함께 하기") private String challengeCategory; @@ -63,6 +67,7 @@ public ChallengeDTO(Challenge challenge, List progressDTOList, Comm this.id = challenge.getId(); this.isMom = challenge.getContractUser().getIsFemale(); this.title = challenge.getTitle(); + this.challengeStatus = challenge.getChallengeStatus(); this.itemName = challenge.getTargetItem().getName(); this.challengeCategory = challenge.getChallengeCategory().getCategory(); this.interestRate = challenge.getInterestRate(); diff --git a/src/main/java/com/ceos/bankids/repository/ChallengeRepository.java b/src/main/java/com/ceos/bankids/repository/ChallengeRepository.java index 7b41d7a9..4fbc95cb 100644 --- a/src/main/java/com/ceos/bankids/repository/ChallengeRepository.java +++ b/src/main/java/com/ceos/bankids/repository/ChallengeRepository.java @@ -1,13 +1,10 @@ package com.ceos.bankids.repository; import com.ceos.bankids.domain.Challenge; -import org.springframework.data.jpa.repository.JpaRepository; - import java.util.Optional; +import org.springframework.data.jpa.repository.JpaRepository; public interface ChallengeRepository extends JpaRepository { public Optional findById(Long id); - - public Optional findByStatus(Long status); } From 037108b9b4231a842c95b2a41cc2bb7d04dbb039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A4=80?= Date: Tue, 9 Aug 2022 16:23:32 +0900 Subject: [PATCH 12/13] =?UTF-8?q?fix:=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81=20=ED=95=9C=20=EC=BD=94=EB=93=9C=20=EC=8B=A4=20?= =?UTF-8?q?=EC=84=9C=EB=B2=84=EC=97=90=EC=84=9C=20=EC=95=88=EB=8F=8C?= =?UTF-8?q?=EC=95=84=EA=B0=80=EB=8A=94=20=EB=AC=B8=EC=A0=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/ceos/bankids/domain/Challenge.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/ceos/bankids/domain/Challenge.java b/src/main/java/com/ceos/bankids/domain/Challenge.java index 3f4f2f6d..2c04898f 100644 --- a/src/main/java/com/ceos/bankids/domain/Challenge.java +++ b/src/main/java/com/ceos/bankids/domain/Challenge.java @@ -95,12 +95,10 @@ public class Challenge extends AbstractTimestamp { public Challenge( Long id, String title, - Long isAchieved, ChallengeStatus challengeStatus, Long totalPrice, Long weekPrice, Long weeks, - Long status, Long interestRate, Long successWeeks, String filename, From a151771e4ec91ab5978b707d5f563ff9afe8044d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A4=80?= Date: Wed, 10 Aug 2022 20:39:35 +0900 Subject: [PATCH 13/13] =?UTF-8?q?fix:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95,=20dev=20=ED=99=98?= =?UTF-8?q?=EA=B2=BD=20=EC=84=9C=EB=B2=84=EC=97=90=EC=84=9C=20=EC=B9=B4?= =?UTF-8?q?=EC=B9=B4=EC=98=A4=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EC=8B=9C=20?= =?UTF-8?q?=EB=A6=AC=EB=8B=A4=EC=9D=B4=EB=A0=89=ED=8A=B8=20url=20=EC=98=A4?= =?UTF-8?q?=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 --- .../bankids/unit/controller/ChallengeControllerTest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/ceos/bankids/unit/controller/ChallengeControllerTest.java b/src/test/java/com/ceos/bankids/unit/controller/ChallengeControllerTest.java index a4971fb7..6ce98f1b 100644 --- a/src/test/java/com/ceos/bankids/unit/controller/ChallengeControllerTest.java +++ b/src/test/java/com/ceos/bankids/unit/controller/ChallengeControllerTest.java @@ -1807,7 +1807,7 @@ public void testIfGetListChallengeChallengeIsFailureTest() { middleProgress, AbstractTimestamp.class, "createdAt", - Timestamp.valueOf(LocalDateTime.now().minusDays(24)), + Timestamp.valueOf(LocalDateTime.now().minusDays(35)), Timestamp.class ); @@ -2437,8 +2437,6 @@ public void testIfUpdateChallengeStatusIsSuccess() { //then - newChallenge.setStatus(2L); - newChallenge1.setStatus(0L); newChallenge1.setComment(newComment); ChallengeDTO successChallengeDTO = new ChallengeDTO(newChallenge, progressDTOList, null); ChallengeDTO falseChallengeDTO = new ChallengeDTO(newChallenge1, null, newComment); @@ -3051,8 +3049,9 @@ public void testIfReadKidWeekInfo() { //then WeekDTO weekDTO1 = new WeekDTO(newChallenge.getWeekPrice(), newChallenge.getWeekPrice()); + KidWeekDTO kidWeekDTO = new KidWeekDTO(sonKid, weekDTO1); - Assertions.assertEquals(weekDTO1, result.getData()); + Assertions.assertEquals(kidWeekDTO, result.getData()); } @Test