Skip to content

Commit

Permalink
refactor: Survey 생성시 이미 존재하는 survey가 있다면 예외를 던진다.
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb committed Mar 6, 2024
1 parent 7bb89f7 commit 5fc2e04
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.nalab.survey.application.exception;

public class DuplicateSurveyException extends RuntimeException {

public DuplicateSurveyException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
package me.nalab.survey.application.service.create;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.MessageFormat;
import lombok.RequiredArgsConstructor;
import me.nalab.core.idgenerator.idcore.IdGenerator;
import me.nalab.survey.application.common.survey.dto.SurveyDto;
import me.nalab.survey.application.common.survey.mapper.SurveyDtoMapper;
import me.nalab.survey.application.exception.DuplicateSurveyException;
import me.nalab.survey.application.exception.TargetDoesNotExistException;
import me.nalab.survey.application.port.in.web.CreateSurveyUseCase;
import me.nalab.survey.application.port.out.persistence.SurveyCreatePort;
import me.nalab.survey.application.port.out.persistence.TargetExistCheckPort;
import me.nalab.survey.application.port.out.persistence.existsurvey.SurveyExistPort;
import me.nalab.survey.domain.survey.Survey;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
class SurveyCreateService implements CreateSurveyUseCase {

private final SurveyCreatePort surveyCreatePort;
private final SurveyExistPort surveyExistPort;
private final TargetExistCheckPort targetExistCheckPort;
private final IdGenerator idGenerator;

@Override
@Transactional
public void createSurvey(Long targetId, SurveyDto surveyDto) {
throwIfDoesNotExistTarget(targetId);
if (surveyExistPort.isSurveyExistByTargetId(targetId)) {
throw new DuplicateSurveyException(MessageFormat.format("이미 Survey를 등록한 Target \"{0}\" 입니다.", targetId));
}
Survey survey = SurveyDtoMapper.toSurvey(surveyDto);
survey.withId(idGenerator::generate);
surveyCreatePort.createSurvey(targetId, survey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import me.nalab.survey.application.port.out.persistence.existsurvey.SurveyExistPort;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -43,6 +46,15 @@ class SurveyCreateServiceTest {
@MockBean
private TargetExistCheckPort findTargetPort;

@MockBean
private SurveyExistPort surveyExistPort;

@BeforeEach
void mockingSurveyExistPort() {
when(surveyExistPort.isSurveyExistByTargetId(anyLong())).thenReturn(false);
}


@ParameterizedTest
@MethodSource("surveyDtoLargeNullIdSources")
void CREATE_NEW_SURVEY_SUCCESS(SurveyDto surveyDto) {
Expand Down

0 comments on commit 5fc2e04

Please sign in to comment.