diff --git a/src/main/java/com/atwoz/member/application/info/hobby/HobbyService.java b/src/main/java/com/atwoz/member/application/info/hobby/HobbyService.java index dff75747..23bcaf34 100644 --- a/src/main/java/com/atwoz/member/application/info/hobby/HobbyService.java +++ b/src/main/java/com/atwoz/member/application/info/hobby/HobbyService.java @@ -17,7 +17,7 @@ public class HobbyService { @Transactional public void saveMemberHobbies(final Long memberId, final List hobbyNames) { - deleteBeforeMemberHobbies(memberId); + deleteMemberHobbies(memberId); List memberHobbies = HobbyName.findAllByNames(hobbyNames) .stream() .map(hobbyName -> new Hobby(memberId, hobbyName)) @@ -27,7 +27,7 @@ public void saveMemberHobbies(final Long memberId, final List hobbyNames } @Transactional - public void deleteBeforeMemberHobbies(final Long memberId) { + public void deleteMemberHobbies(final Long memberId) { hobbyRepository.deleteHobbiesByMemberId(memberId); } diff --git a/src/test/java/com/atwoz/member/application/info/HobbyServiceTest.java b/src/test/java/com/atwoz/member/application/info/HobbyServiceTest.java new file mode 100644 index 00000000..02cbd576 --- /dev/null +++ b/src/test/java/com/atwoz/member/application/info/HobbyServiceTest.java @@ -0,0 +1,72 @@ +package com.atwoz.member.application.info; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.atwoz.member.application.info.hobby.HobbyService; +import com.atwoz.member.domain.info.hobby.Hobby; +import com.atwoz.member.domain.info.hobby.HobbyRepository; +import com.atwoz.member.infrastructure.info.HobbyFakeRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import java.util.List; + +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +@SuppressWarnings("NonAsciiCharacters") +@ExtendWith(MockitoExtension.class) +public class HobbyServiceTest { + + private HobbyService hobbyService; + private HobbyRepository hobbyRepository; + + @BeforeEach + void init() { + hobbyRepository = new HobbyFakeRepository(); + hobbyService = new HobbyService(hobbyRepository); + } + + @Test + void 회원의_모든_취미를_저장한다() { + // given + Long memberId = 1L; + List hobbyNames = List.of("자전거", "애니메이션"); + + // when + hobbyService.saveMemberHobbies(memberId, hobbyNames); + + // then + List saveHobbies = hobbyRepository.findAllByMemberId(memberId); + assertThat(saveHobbies.size()).isEqualTo(2); + } + + @Test + void 회원의_모든_취미를_삭제한다() { + // given + Long memberId = 1L; + List hobbyNames = List.of("자전거", "애니메이션"); + hobbyService.saveMemberHobbies(memberId, hobbyNames); + + // when + hobbyService.deleteMemberHobbies(memberId); + + // then + assertThat(hobbyRepository.findAllByMemberId(memberId)).isEmpty(); + } + + @Test + void 회원의_모든_취미를_조회한다() { + // given + Long memberId = 1L; + List hobbyNames = List.of("자전거", "애니메이션"); + hobbyService.saveMemberHobbies(memberId, hobbyNames); + + // when + List memberHobbies = hobbyService.findMemberHobbies(memberId); + + // then + assertThat(memberHobbies.size()).isEqualTo(2); + } +} diff --git a/src/test/java/com/atwoz/member/infrastructure/info/HobbyFakeRepository.java b/src/test/java/com/atwoz/member/infrastructure/info/HobbyFakeRepository.java new file mode 100644 index 00000000..983a006a --- /dev/null +++ b/src/test/java/com/atwoz/member/infrastructure/info/HobbyFakeRepository.java @@ -0,0 +1,40 @@ +package com.atwoz.member.infrastructure.info; + +import com.atwoz.member.domain.info.hobby.Hobby; +import com.atwoz.member.domain.info.hobby.HobbyRepository; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class HobbyFakeRepository implements HobbyRepository { + + private final Map map = new HashMap<>(); + private long id = 1L; + + @Override + public void save(final Hobby hobby) { + Hobby newHobby = new Hobby(hobby.getMemberId(), hobby.getHobbyName()); + map.put(id, newHobby); + id++; + } + + @Override + public void saveAll(final List memberHobbies) { + memberHobbies.stream() + .map(hobby -> new Hobby(hobby.getMemberId(), hobby.getHobbyName())) + .forEach(hobby -> map.put(id++, hobby)); + } + + @Override + public void deleteHobbiesByMemberId(final Long memberId) { + map.values().removeIf(hobby -> memberId.equals(hobby.getMemberId())); + } + + @Override + public List findAllByMemberId(final Long memberId) { + return map.values() + .stream() + .filter(hobby -> memberId.equals(hobby.getMemberId())) + .toList(); + } +}