Skip to content

Commit

Permalink
Merge pull request #158 from Bamdoliro/perf/#151
Browse files Browse the repository at this point in the history
[개선] 원서 반려시 재제출 가능하도록 변경
  • Loading branch information
cabbage16 authored Oct 13, 2024
2 parents 1e1ed60 + aeb552b commit d835b71
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@RequiredArgsConstructor
@UseCase
public class SubmitFormUseCase {
Expand Down Expand Up @@ -39,7 +41,12 @@ public void execute(User user, SubmitFormRequest request) {
}

private void validateOnlyOneFormPerUser(User user) {
if (formRepository.existsByUserId(user.getId())) {
Optional<Form> form = formRepository.findByUser(user);
if (form.isPresent()) {
if (form.get().isRejected()) {
formRepository.delete(form.get());
return;
}
throw new FormAlreadySubmittedException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface FormRepository extends JpaRepository<Form, Long>, FormRepositor

Optional<Form> findByUser(User user);

void deleteByUser(User user);

@Query("SELECT MAX(f.examinationNumber) " +
"FROM Form f " +
"WHERE f.examinationNumber BETWEEN :minValue AND :maxValue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
Expand Down Expand Up @@ -45,7 +47,8 @@ class SubmitFormUseCaseTest {
SubmitFormRequest request = FormFixture.createFormRequest(FormType.REGULAR);
User user = UserFixture.createUser();

given(formRepository.existsByUserId(user.getId())).willReturn(false);
given(formRepository.findByUser(user)).willReturn(Optional.empty());

willDoNothing().given(assignExaminationNumberService).execute(any(Form.class));
willDoNothing().given(calculateFormScoreService).execute(any(Form.class));

Expand All @@ -54,26 +57,49 @@ class SubmitFormUseCaseTest {

// then

verify(formRepository, times(1)).existsByUserId(user.getId());
verify(formRepository, times(1)).findByUser(user);
verify(calculateFormScoreService, times(1)).execute(any(Form.class));
verify(assignExaminationNumberService, times(1)).execute(any(Form.class));
verify(formRepository, times(1)).save(any(Form.class));
}

@Test
void 원서를_제출할__이미_제출한_원서가_있으면_에러가_발생한다() {
void 원서를_제출할__이미_제출한_원서가_반려상태가_아니면서_있으면_에러가_발생한다() {
// given
SubmitFormRequest request = FormFixture.createFormRequest(FormType.REGULAR);
User user = UserFixture.createUser();
Form form = FormFixture.createForm(FormType.REGULAR);

given(formRepository.existsByUserId(user.getId())).willReturn(true);
given(formRepository.findByUser(user)).willReturn(Optional.of(form));

// when and then
assertThrows(FormAlreadySubmittedException.class, () -> submitFormUseCase.execute(user, request));

verify(formRepository, times(1)).existsByUserId(user.getId());
verify(formRepository, times(1)).findByUser(user);
verify(calculateFormScoreService, never()).execute(any(Form.class));
verify(assignExaminationNumberService, never()).execute(any(Form.class));
verify(formRepository, never()).delete(any(Form.class));
verify(formRepository, never()).save(any(Form.class));
}

@Test
void 원서를_제출할__이미_제출한_원서가_반려상태면_다시_작성한다() {
//given
SubmitFormRequest request = FormFixture.createFormRequest(FormType.REGULAR);
User user = UserFixture.createUser();
Form form = FormFixture.createForm(FormType.REGULAR);
form.reject();

given(formRepository.findByUser(user)).willReturn(Optional.of(form));

//when
submitFormUseCase.execute(user, request);

//then
verify(formRepository, times(1)).findByUser(user);
verify(calculateFormScoreService, times(1)).execute(any(Form.class));
verify(assignExaminationNumberService, times(1)).execute(any(Form.class));
verify(formRepository, times(1)).delete(any(Form.class));
verify(formRepository, times(1)).save(any(Form.class));
}
}

0 comments on commit d835b71

Please sign in to comment.