Skip to content

Commit

Permalink
test: #20 answer 수정, 삭제 API 서비스 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
tnals2384 committed Mar 4, 2024
1 parent 8e99118 commit d9b1bde
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ public AnswerResponse.AnswerId updateAnswer(Member loginMember, Long answerId, A
Question question = answer.getQuestion();
checkPermission(loginMember, answer);

//현재 답변의 태그들
List<String> answerTags = Arrays.stream(answer.getTags().split(" "))
.filter(tag -> !tag.isEmpty()).toList();

//rdb에 태그 update
request.getTags().forEach(tagService::findOrCreateTag);

//현재 답변의 태그들
List<String> answerTags = getCurrentAnswerTags(answer);

//추가된 태그들
List<String> addedTags = new ArrayList<>(request.getTags());
addedTags.removeAll(answerTags);
Expand All @@ -84,6 +83,7 @@ public AnswerResponse.AnswerId updateAnswer(Member loginMember, Long answerId, A
if (question.getQuestionStatus().equals(QuestionStatus.ONGOING))
tagService.updateQuestionTags(question.getId(), addedTags, removedTags);

//답변 update
answer.update(request);

return new AnswerResponse.AnswerId(answerId);
Expand All @@ -97,8 +97,8 @@ public AnswerResponse.AnswerId deleteAnswer(Member loginMember, Long answerId) {
Question question = answer.getQuestion();
checkPermission(loginMember, answer);

List<String> answerTags = Arrays.stream(answer.getTags().split(" "))
.filter(tag -> !tag.isEmpty()).toList();
//현재 답변의 태그들
List<String> answerTags = getCurrentAnswerTags(answer);

//redis tag 삭제
tagService.deleteTags(answerTags);
Expand Down Expand Up @@ -133,4 +133,11 @@ private void checkPermission(Member loginMember, Answer answer) {
throw new RestApiException(AnswerErrorCode.ANSWER_UNAUTHORIZED);
}

private List<String> getCurrentAnswerTags(Answer answer) {
return Arrays.stream(answer.getTags().split(" "))
.filter(tag -> !tag.isEmpty()).toList();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

@DisplayName("Answer 서비스의 ")
@SpringBootTest
Expand All @@ -38,11 +37,60 @@ public void createAnswer() {

//then
assertEquals("나는 와플을 좋아하는 사람이 좋아", answer.getContent());
assertEquals("#와플유니버시티 #와플", answer.getTags());
assertEquals("#와플유니버시티 #와플 ", answer.getTags());
assertEquals(2, tags.size());
assertTrue(tags.contains("#와플"));
assertTrue(tags.contains("#와플유니버시티"));

}

@Test
@DisplayName("Answer 수정 테스트")
@Transactional
public void updateAnswer() {
//given
AnswerRequest request = getAnswerRequest();
Long answerId = answerService.createAnswer(member, question.getId(), request).getAnswerId();

AnswerRequest updateRequest = AnswerRequest.builder()
.answer("나는 동그랗고 와플 좋아하는 사람이 좋아")
.tags(List.of("#동그라미", "#와플", "#동글"))
.build();

//when
answerService.updateAnswer(member, answerId, updateRequest);
Answer answer = answerService.findAnswer(answerId);

//then
assertEquals("나는 동그랗고 와플 좋아하는 사람이 좋아", answer.getContent());
assertEquals("#동그라미 #와플 #동글 ", answer.getTags());
List<String> tags = tagService.getTagsByQuestion(question.getId()).getTags();

assertEquals(3, tags.size());
assertTrue(tags.contains("#와플"));
assertTrue(tags.contains("#동그라미"));
assertFalse(tags.contains("#와플유니버시티"));

}

@Test
@DisplayName("Answer 삭제 테스트")
@Transactional
public void deleteAnswer() {
//given
AnswerRequest request = getAnswerRequest();
Long answerId = answerService.createAnswer(member, question.getId(), request).getAnswerId();

//when
answerService.deleteAnswer(member, answerId);
Answer answer = answerService.findAnswer(answerId);

//then
List<String> tags = tagService.getTagsByQuestion(question.getId()).getTags();

assertEquals(0, tags.size());
assertNotNull(answer.getDeletedAt());

}

}
12 changes: 11 additions & 1 deletion src/test/java/com/server/capple/support/ServiceTestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import com.server.capple.domain.question.entity.Question;
import com.server.capple.domain.question.entity.QuestionStatus;
import com.server.capple.domain.question.repository.QuestionRepository;
import org.aspectj.lang.annotation.After;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

Expand All @@ -22,13 +25,18 @@ public abstract class ServiceTestConfig {
protected Member member;
protected Question question;

@Autowired
private RedisTemplate<String, Object> redisTemplate;

@BeforeEach
public void setUp() {
member = createMember();
question = createQuestion();
redisTemplate.getConnectionFactory().getConnection().flushAll();
}
@AfterEach
public void tearDown() {
}

protected Member createMember() {
return memberRepository.save(
Member.builder()
Expand All @@ -48,6 +56,8 @@ protected Question createQuestion() {
);
}



protected AnswerRequest getAnswerRequest() {
return AnswerRequest.builder()
.answer("나는 와플을 좋아하는 사람이 좋아")
Expand Down

0 comments on commit d9b1bde

Please sign in to comment.