diff --git a/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java b/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java index 84eed6e4..5342a9ec 100644 --- a/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java +++ b/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java @@ -61,13 +61,12 @@ public AnswerResponse.AnswerId updateAnswer(Member loginMember, Long answerId, A Question question = answer.getQuestion(); checkPermission(loginMember, answer); - //현재 답변의 태그들 - List answerTags = Arrays.stream(answer.getTags().split(" ")) - .filter(tag -> !tag.isEmpty()).toList(); - //rdb에 태그 update request.getTags().forEach(tagService::findOrCreateTag); + //현재 답변의 태그들 + List answerTags = getCurrentAnswerTags(answer); + //추가된 태그들 List addedTags = new ArrayList<>(request.getTags()); addedTags.removeAll(answerTags); @@ -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); @@ -97,8 +97,8 @@ public AnswerResponse.AnswerId deleteAnswer(Member loginMember, Long answerId) { Question question = answer.getQuestion(); checkPermission(loginMember, answer); - List answerTags = Arrays.stream(answer.getTags().split(" ")) - .filter(tag -> !tag.isEmpty()).toList(); + //현재 답변의 태그들 + List answerTags = getCurrentAnswerTags(answer); //redis tag 삭제 tagService.deleteTags(answerTags); @@ -133,4 +133,11 @@ private void checkPermission(Member loginMember, Answer answer) { throw new RestApiException(AnswerErrorCode.ANSWER_UNAUTHORIZED); } + private List getCurrentAnswerTags(Answer answer) { + return Arrays.stream(answer.getTags().split(" ")) + .filter(tag -> !tag.isEmpty()).toList(); + } + + + } diff --git a/src/test/java/com/server/capple/domain/answer/service/AnswerServiceTest.java b/src/test/java/com/server/capple/domain/answer/service/AnswerServiceTest.java index 080bcb3e..8453671a 100644 --- a/src/test/java/com/server/capple/domain/answer/service/AnswerServiceTest.java +++ b/src/test/java/com/server/capple/domain/answer/service/AnswerServiceTest.java @@ -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 @@ -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 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 tags = tagService.getTagsByQuestion(question.getId()).getTags(); + + assertEquals(0, tags.size()); + assertNotNull(answer.getDeletedAt()); + + } + } diff --git a/src/test/java/com/server/capple/support/ServiceTestConfig.java b/src/test/java/com/server/capple/support/ServiceTestConfig.java index b380cc55..bdf086ce 100644 --- a/src/test/java/com/server/capple/support/ServiceTestConfig.java +++ b/src/test/java/com/server/capple/support/ServiceTestConfig.java @@ -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; @@ -22,13 +25,18 @@ public abstract class ServiceTestConfig { protected Member member; protected Question question; + @Autowired + private RedisTemplate 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() @@ -48,6 +56,8 @@ protected Question createQuestion() { ); } + + protected AnswerRequest getAnswerRequest() { return AnswerRequest.builder() .answer("나는 와플을 좋아하는 사람이 좋아")