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)
public class QnaServiceTest {
@Mock
private QuestionRepository questionRepository;
@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(11L, UserTest.JAVAJIGI, QuestionTest.Q1, "Answers Contents1");
question.addAnswer(answer);
}
@Test
public void delete_성공() throws Exception {
when(questionRepository.findByIdAndDeletedFalse(question.getId())).thenReturn(Optional.of(question));
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));
qnAService.deleteQuestion(UserTest.JAVAJIGI, question.getId());
assertThat(question.isDeleted()).isTrue();
assertThat(answer.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);
}
private void verifyDeleteHistories() {
List<DeleteHistory> deleteHistories = Arrays.asList(
new DeleteHistory(ContentType.QUESTION, question.getId(), question.getWriter(), LocalDateTime.now()),
new DeleteHistory(ContentType.ANSWER, answer.getId(), answer.getWriter(), LocalDateTime.now()));
verify(deleteHistoryService).saveAll(deleteHistories);
}
}
t0_54coder
- 粉丝: 3200
- 资源: 5642
最新资源
- MATLAB【面板】车辆检测.zip
- MATLAB【面板】车牌出入库计费系统.zip
- MATLAB【面板】车道线检测定位.zip
- MATLAB【面板】车牌识别.zip
- 微电网,下垂控制(三相交流) 传统阻感型下垂控制输出有功 无功 频率波形
- MATLAB【面板】车牌号码出入库管理.zip
- MATLAB【面板】车牌识别设计.zip
- MATLAB【面板】车牌识别GUI实现.zip
- MATLAB【面板】车牌识别GUI界面.zip
- MATLAB【面板】答题卡识别GUI.zip
- MATLAB【面板】虫害检测.zip
- MATLAB【面板】答题卡自动识别系统.zip
- MATLAB【面板】答题卡识别系统.zip
- MATLAB【面板】打印纸缺陷检测GUI设计.zip
- MATLAB【面板】道路桥梁裂缝检测.zip
- 八木天线计算器,如果您想制作天线,这个计算器非常好用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈