/
QnaServiceTest.java
97 lines (76 loc) · 3.6 KB
/
QnaServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package qna.service;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import qna.CannotDeleteException;
import qna.domain.*;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class QnaServiceTest {
@Mock
private QuestionRepository questionRepository;
@Mock
private AnswerRepository answerRepository;
@Mock
private DeleteHistoryService deleteHistoryService;
@InjectMocks
private QnaService qnaService;
private Question question;
private Answer answer;
@BeforeEach
public void setUp() throws Exception {
question = new Question(1L, "title1", "contents1").writeBy(UserTest.JAVAJIGI);
answer = new Answer(1L, UserTest.JAVAJIGI, question, "Answers Contents1");
question.addAnswer(answer);
}
@Test
public void delete_성공() throws Exception {
when(questionRepository.findByIdAndDeletedFalse(question.getId())).thenReturn(Optional.of(question));
when(answerRepository.findByQuestionIdAndDeletedFalse(question.getId())).thenReturn(Arrays.asList(answer));
assertThat(question.isDeleted()).isFalse();
qnaService.deleteQuestion(UserTest.JAVAJIGI, question.getId());
assertThat(question.isDeleted()).isTrue();
verifyDeleteHistories();
}
@Test
public void delete_다른_사람이_쓴_글() throws Exception {
when(questionRepository.findByIdAndDeletedFalse(question.getId())).thenReturn(Optional.of(question));
assertThatThrownBy(() -> qnaService.deleteQuestion(UserTest.SANJIGI, question.getId()))
.isInstanceOf(CannotDeleteException.class);
}
@Test
public void delete_성공_질문자_답변자_같음() throws Exception {
when(questionRepository.findByIdAndDeletedFalse(question.getId())).thenReturn(Optional.of(question));
when(answerRepository.findByQuestionIdAndDeletedFalse(question.getId())).thenReturn(Arrays.asList(answer));
qnaService.deleteQuestion(UserTest.JAVAJIGI, question.getId());
assertThat(question.isDeleted()).isTrue();
assertThat(answer.isDeleted()).isTrue();
verifyDeleteHistories();
}
@Test
public void delete_답변_중_다른_사람이_쓴_글() throws Exception {
Answer answer2 = new Answer(2L, UserTest.SANJIGI, QuestionTest.Q1, "Answers Contents1");
question.addAnswer(answer2);
when(questionRepository.findByIdAndDeletedFalse(question.getId())).thenReturn(Optional.of(question));
when(answerRepository.findByQuestionIdAndDeletedFalse(question.getId())).thenReturn(Arrays.asList(answer, answer2));
assertThatThrownBy(() -> qnaService.deleteQuestion(UserTest.JAVAJIGI, question.getId()))
.isInstanceOf(CannotDeleteException.class);
}
private void verifyDeleteHistories() {
List<DeleteHistory> deleteHistories = Arrays.asList(
new DeleteHistory(ContentType.QUESTION, question.getId(), question.getWriterId(), LocalDateTime.now()),
new DeleteHistory(ContentType.ANSWER, answer.getId(), answer.getWriterId(), LocalDateTime.now())
);
verify(deleteHistoryService).saveAll(deleteHistories);
}
}