Skip to content

Commit

Permalink
Merge pull request #108 from softeerbootcamp4th/feature/106-event-val…
Browse files Browse the repository at this point in the history
…idation-up

[feat] 추첨 이벤트 생성 및 수정 시 검증 기능 추가(#106)
  • Loading branch information
blaxsior authored Aug 20, 2024
2 parents 08c0286 + b56f882 commit 7bbc2b1
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/main/java/hyundai/softeer/orange/common/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public enum ErrorCode {

// 400 Bad Request
BAD_REQUEST(HttpStatus.BAD_REQUEST, false, "잘못된 요청입니다."),
INVALID_COMMENT(HttpStatus.BAD_REQUEST, false, "부정적인 표현을 사용하였습니다."),
Expand All @@ -19,7 +18,9 @@ public enum ErrorCode {
INVALID_EVENT_TYPE(HttpStatus.BAD_REQUEST, false, "이벤트 타입이 지원되지 않습니다."),
EVENT_NOT_ENDED(HttpStatus.BAD_REQUEST, false, "이벤트가 아직 종료되지 않았습니다."),
EVENT_IS_DRAWING(HttpStatus.BAD_REQUEST, false, "현재 추첨이 진행되고 있는 이벤트입니다."),

DUPLICATED_POLICIES(HttpStatus.BAD_REQUEST,false,"정책에서 중복된 액션이 존재합니다."),
DUPLICATED_GRADES(HttpStatus.BAD_REQUEST,false,"추첨 이벤트 정보에 중복된 등수가 존재합니다."),
CANNOT_PARTICIPATE(HttpStatus.BAD_REQUEST,false,"현재 유저가 참여할 수 없는 이벤트입니다."),

// 401 Unauthorized
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, false, "인증되지 않은 사용자입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import hyundai.softeer.orange.event.draw.entity.DrawEvent;
import hyundai.softeer.orange.event.draw.entity.DrawEventMetadata;
import hyundai.softeer.orange.event.draw.entity.DrawEventScorePolicy;
import hyundai.softeer.orange.event.draw.enums.DrawEventAction;
import hyundai.softeer.orange.event.draw.exception.DrawEventException;
import hyundai.softeer.orange.event.draw.repository.DrawEventMetadataRepository;
import hyundai.softeer.orange.event.draw.repository.DrawEventScorePolicyRepository;
import hyundai.softeer.orange.event.dto.EventDto;
Expand Down Expand Up @@ -36,8 +38,7 @@ public boolean canHandle(EventType eventType) {
@Override
public void fetchToEventEntity(EventMetadata metadata, EventDto eventDto) {
DrawEventDto dto = eventDto.getDraw();
// 비어 있으면 안됨.
if (dto == null) throw new EventException(ErrorCode.INVALID_JSON);
validateDrawEventDto(dto);

DrawEvent event = new DrawEvent();
metadata.updateDrawEvent(event);
Expand Down Expand Up @@ -67,7 +68,7 @@ public void fetchToEventEntity(EventMetadata metadata, EventDto eventDto) {
public void fetchToDto(EventMetadata metadata, EventDto eventDto) {
DrawEvent drawEvent = metadata.getDrawEvent();
// drawEvent 정보가 있으면 넣기
if(drawEvent == null) throw new EventException(ErrorCode.INVALID_JSON);

DrawEventDto drawEventDto = DrawEventDto
.builder()
.id(drawEvent.getId())
Expand Down Expand Up @@ -96,6 +97,7 @@ public void editEventField(EventMetadata metadata, EventDto dto) {
DrawEvent drawEvent = metadata.getDrawEvent();
if(drawEvent == null) throw new EventException(ErrorCode.EVENT_NOT_FOUND);
DrawEventDto drawEventDto = dto.getDraw();
validateDrawEventDto(drawEventDto);

editDrawEventMetadata(drawEvent, drawEventDto);
editDrawEventScorePolicy(drawEvent, drawEventDto);
Expand Down Expand Up @@ -184,4 +186,23 @@ else if(deleted.contains(policy.getId())) {
}
}

/**
* 입력된 정책 / 등수 중 중복된 값이 존재하지는 않는지 검증
* @param drawEventDto 검증 대상이 되는 객체
*/
protected void validateDrawEventDto(DrawEventDto drawEventDto) {
if(drawEventDto == null) throw new EventException(ErrorCode.INVALID_JSON);
Set<DrawEventAction> actionsSet = new HashSet<>();
Set<Long> gradeSet = new HashSet<>();

for(var policy: drawEventDto.getPolicies()) {
actionsSet.add(policy.getAction());
}
if(actionsSet.size() != drawEventDto.getPolicies().size()) throw new DrawEventException(ErrorCode.DUPLICATED_POLICIES);

for(var metadata: drawEventDto.getMetadata()) {
gradeSet.add(metadata.getGrade());
}
if(gradeSet.size() != drawEventDto.getMetadata().size()) throw new DrawEventException(ErrorCode.DUPLICATED_GRADES);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package hyundai.softeer.orange.event.dto.draw;

import hyundai.softeer.orange.event.dto.group.EventEditGroup;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.*;

/**
Expand All @@ -21,17 +22,19 @@ public class DrawEventMetadataDto {
* 추첨 이벤트 등수
*/
@NotNull
@Positive
private Long grade;

/**
* 현재 등수에 대한 최대 당첨 인원 수
*/
@NotNull
@Positive
private Long count;

/**
* 상품 정보를 기입하는 영역
*/
@NotNull
@NotBlank
private String prizeInfo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hyundai.softeer.orange.event.dto.group.EventEditGroup;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.*;

/**
Expand All @@ -29,5 +30,6 @@ public class DrawEventScorePolicyDto {
* action 1회 수행 시 증가하는 점수
*/
@NotNull
@Positive
private Integer score;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hyundai.softeer.orange.event.dto.validator.FcfsEventDtoTimeValidation;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -40,6 +41,7 @@ public class FcfsEventDto {
* 당첨 인원
*/
@NotNull
@Positive
private Long participantCount;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package hyundai.softeer.orange.event.common.component.eventFieldMapper.mapper;

import hyundai.softeer.orange.common.ErrorCode;
import hyundai.softeer.orange.event.common.entity.EventMetadata;
import hyundai.softeer.orange.event.common.enums.EventType;
import hyundai.softeer.orange.event.common.exception.EventException;
import hyundai.softeer.orange.event.draw.enums.DrawEventAction;
import hyundai.softeer.orange.event.draw.exception.DrawEventException;
import hyundai.softeer.orange.event.draw.repository.DrawEventMetadataRepository;
import hyundai.softeer.orange.event.draw.repository.DrawEventScorePolicyRepository;
import hyundai.softeer.orange.event.dto.EventDto;
Expand Down Expand Up @@ -55,7 +59,6 @@ void fetchToEventEntity_throwErrorIfFcfsDtoNotExists() {
});
}


@DisplayName("정상적인 EventDto가 들어오면 정상적으로 관계를 설정한다.")
@Test
void fetchToEventEntity_setRelationIfEventDtoIsValid() {
Expand Down Expand Up @@ -91,12 +94,74 @@ void editEventField_throwIfDrawEventNotExists() {
});
}

// TODO: repository 통합 테스트 코드 작성
// @DisplayName("규칙에 따라 정상적으로 DrawEvent를 갱신해야 한다.")
// @Test
// void editEventField_testOpIsValid() {
// // 1. DrawEvent
// EventMetadata metadata = new EventMetadata();
// EventDto dto = new EventDto();
// }
@DisplayName("validateDrawEventDto: 등수에 중복이 있다면 예외 반환")
@Test
void validateDrawEventDto_throwIfDuplicatedGrade() {
DrawEventDto dto = new DrawEventDto();
dto.setMetadata(
List.of(
DrawEventMetadataDto.builder().grade(1L).build(),
DrawEventMetadataDto.builder().grade(1L).build()
)
);

dto.setPolicies(List.of(
DrawEventScorePolicyDto.builder()
.action(DrawEventAction.ParticipateEvent)
.score(1)
.build(),
DrawEventScorePolicyDto.builder()
.action(DrawEventAction.WriteComment)
.build()
));

assertThatThrownBy(() -> {
mapper.validateDrawEventDto(dto);
}).isInstanceOf(DrawEventException.class)
.hasMessage(ErrorCode.DUPLICATED_GRADES.getMessage());
}

@DisplayName("validateDrawEventDto: 액션에 중복이 있다면 예외 반환")
@Test
void validateDrawEventDto_throwIfDuplicatedAction() {
DrawEventDto dto = new DrawEventDto();
dto.setMetadata(List.of(DrawEventMetadataDto.builder().grade(1L).build()));
dto.setPolicies(List.of(
DrawEventScorePolicyDto.builder()
.action(DrawEventAction.ParticipateEvent).score(1)
.build(),
DrawEventScorePolicyDto.builder()
.action(DrawEventAction.ParticipateEvent).score(5)
.build()
));

assertThatThrownBy(() -> {
mapper.validateDrawEventDto(dto);
}).isInstanceOf(DrawEventException.class)
.hasMessage(ErrorCode.DUPLICATED_POLICIES.getMessage());
}

@DisplayName("validateDrawEventDto: validation 성공")
@Test
void validateDrawEventDto_validateSuccessfully() {
DrawEventDto dto = new DrawEventDto();
dto.setMetadata(List.of(
DrawEventMetadataDto.builder().grade(1L).build(),
DrawEventMetadataDto.builder().grade(2L).build()
));

dto.setPolicies(List.of(
DrawEventScorePolicyDto.builder()
.action(DrawEventAction.ParticipateEvent)
.score(1)
.build(),
DrawEventScorePolicyDto.builder()
.action(DrawEventAction.ParticipateEvent)
.build()
));
assertThatThrownBy(() -> {
mapper.validateDrawEventDto(null);
}).isInstanceOf(EventException.class)
.hasMessage(ErrorCode.INVALID_JSON.getMessage());
}
}

0 comments on commit 7bbc2b1

Please sign in to comment.