diff --git a/backend/src/main/java/shook/shook/song/domain/InMemorySongs.java b/backend/src/main/java/shook/shook/song/domain/InMemorySongs.java index 6f5b5c6c..0bb8ce9d 100644 --- a/backend/src/main/java/shook/shook/song/domain/InMemorySongs.java +++ b/backend/src/main/java/shook/shook/song/domain/InMemorySongs.java @@ -19,11 +19,11 @@ @Repository public class InMemorySongs { - private static final Comparator COMPARATOR = Comparator.comparing(Song::getTotalLikeCount, - Comparator.reverseOrder()) - .thenComparing(Song::getId, Comparator.reverseOrder()); - private Map songsSortedInLikeCountById; - private List sortedIds; + private static final Comparator COMPARATOR = + Comparator.comparing(Song::getTotalLikeCount, Comparator.reverseOrder()) + .thenComparing(Song::getId, Comparator.reverseOrder()); + private Map songsSortedInLikeCountById = new HashMap<>(); + private List sortedIds = new ArrayList<>(); private final EntityManager entityManager; @@ -47,11 +47,13 @@ private static Map getSortedSong(final List songs) { } public List getSongs() { - return songsSortedInLikeCountById.values().stream().toList(); + return sortedIds.stream() + .map(songsSortedInLikeCountById::get) + .toList(); } public List getSongs(final int limit) { - final List topSongIds = this.sortedIds.subList(0, Math.min(limit, this.sortedIds.size())); + final List topSongIds = sortedIds.subList(0, Math.min(limit, sortedIds.size())); return topSongIds.stream() .map(songsSortedInLikeCountById::get) @@ -123,12 +125,89 @@ public void like(final KillingPart killingPart, final KillingPartLike likeOnKill final KillingPart killingPartById = findKillingPart(killingPart, song); final boolean updated = killingPartById.like(likeOnKillingPart); if (updated) { - sortSongIds(); + reorder(song); + } + } + + public void reorder(final Song updatedSong) { + int currentSongIndex = sortedIds.indexOf(updatedSong.getId()); + + if (currentSongIndex == -1) { + return; + } + + if (shouldMoveForward(updatedSong, currentSongIndex)) { + moveLeft(updatedSong, currentSongIndex); + } + + if (shouldMoveBackward(updatedSong, currentSongIndex)) { + moveRight(updatedSong, currentSongIndex); } } - private void sortSongIds() { - sortedIds.sort(Comparator.comparing(songsSortedInLikeCountById::get, COMPARATOR)); + private boolean shouldMoveForward(final Song song, final int index) { + if (index == 0) { + return false; + } + + final Long prevSongId = sortedIds.get(index - 1); + final Song prevSong = songsSortedInLikeCountById.get(prevSongId); + + return index > 0 && shouldSwapWithPrevious(song, prevSong); + } + + private boolean shouldMoveBackward(final Song song, final int index) { + if (index == sortedIds.size() - 1) { + return false; + } + + final Long nextSongId = sortedIds.get(index + 1); + final Song nextSong = songsSortedInLikeCountById.get(nextSongId); + + return index < sortedIds.size() - 1 && shouldSwapWithNext(song, nextSong); + } + + private void moveLeft(final Song changedSong, final int songIndex) { + int currentSongIndex = songIndex; + + while (currentSongIndex > 0 && currentSongIndex < sortedIds.size() && + shouldSwapWithPrevious(changedSong, + songsSortedInLikeCountById.get(sortedIds.get(currentSongIndex - 1)))) { + swap(currentSongIndex, currentSongIndex - 1); + currentSongIndex--; + } + } + + private boolean shouldSwapWithPrevious(final Song song, final Song prevSong) { + final boolean hasSameTotalLikeCountAndLargerIdThanPrevSong = + song.getTotalLikeCount() == prevSong.getTotalLikeCount() && song.getId() > prevSong.getId(); + final boolean hasLargerTotalLikeCountThanPrevSong = song.getTotalLikeCount() > prevSong.getTotalLikeCount(); + + return hasLargerTotalLikeCountThanPrevSong || hasSameTotalLikeCountAndLargerIdThanPrevSong; + } + + private void swap(final int currentIndex, final int otherIndex) { + final Long prevIndex = sortedIds.get(currentIndex); + sortedIds.set(currentIndex, sortedIds.get(otherIndex)); + sortedIds.set(otherIndex, prevIndex); + } + + private void moveRight(final Song changedSong, final int songIndex) { + int currentSongIndex = songIndex; + + while (currentSongIndex < sortedIds.size() - 1 && currentSongIndex > 0 + && shouldSwapWithNext(changedSong, songsSortedInLikeCountById.get(sortedIds.get(currentSongIndex - 1)))) { + swap(currentSongIndex, currentSongIndex + 1); + currentSongIndex++; + } + } + + private boolean shouldSwapWithNext(final Song song, final Song nextSong) { + final boolean hasSameTotalLikeCountAndSmallerIdThanNextSong = + song.getTotalLikeCount() == nextSong.getTotalLikeCount() && song.getId() < nextSong.getId(); + final boolean hasSmallerTotalLikeCountThanNextSong = song.getTotalLikeCount() < nextSong.getTotalLikeCount(); + + return hasSmallerTotalLikeCountThanNextSong || hasSameTotalLikeCountAndSmallerIdThanNextSong; } private static KillingPart findKillingPart(final KillingPart killingPart, final Song song) { @@ -145,7 +224,7 @@ public void unlike(final KillingPart killingPart, final KillingPartLike unlikeOn final KillingPart killingPartById = findKillingPart(killingPart, song); final boolean updated = killingPartById.unlike(unlikeOnKillingPart); if (updated) { - sortSongIds(); + reorder(song); } } } diff --git a/backend/src/test/java/shook/shook/song/application/SongServiceTest.java b/backend/src/test/java/shook/shook/song/application/SongServiceTest.java index e3a5064c..a83c6bdf 100644 --- a/backend/src/test/java/shook/shook/song/application/SongServiceTest.java +++ b/backend/src/test/java/shook/shook/song/application/SongServiceTest.java @@ -4,7 +4,6 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertAll; -import jakarta.persistence.EntityManager; import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -57,15 +56,13 @@ class SongServiceTest extends UsingJpaTest { @Autowired private ArtistRepository artistRepository; - @Autowired - private EntityManager entityManager; - - private final InMemorySongs inMemorySongs = new InMemorySongs(entityManager); + private InMemorySongs inMemorySongs; private SongService songService; @BeforeEach public void setUp() { + inMemorySongs = new InMemorySongs(entityManager); songService = new SongService( songRepository, killingPartRepository, @@ -121,8 +118,8 @@ void findById_exist_login_member() { //given final Member member = createAndSaveMember("email@naver.com", "email"); final Song song = registerNewSong("title"); + inMemorySongs.recreate(List.of(song)); addLikeToEachKillingParts(song, member); - inMemorySongs.recreate(songRepository.findAllWithKillingParts()); addMemberPartToSong(10, 5, song, member); //when @@ -166,7 +163,7 @@ private MemberPart addMemberPartToSong(final int startSecond, final int length, void findById_exist_not_login_member() { //given final Song song = registerNewSong("title"); - inMemorySongs.recreate(songRepository.findAllWithKillingParts()); + inMemorySongs.recreate(List.of(song)); //when인 saveAndClearEntityManager(); @@ -204,6 +201,7 @@ void findById_exist_not_login_member() { void findById_notExist() { //given final Member member = createAndSaveMember("email@naver.com", "email"); + inMemorySongs.recreate(List.of()); //when //then @@ -231,8 +229,8 @@ void showHighLikedSongs() { addLikeToEachKillingParts(thirdSong, member2); addLikeToEachKillingParts(fourthSong, member1); + inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); saveAndClearEntityManager(); - inMemorySongs.recreate(songRepository.findAllWithKillingParts()); // when final List result = songService.showHighLikedSongs(); @@ -308,7 +306,6 @@ void firstFindByMember() { // 4, 3, 5, 2, 1 addLikeToEachKillingParts(thirdSong, member); addLikeToEachKillingParts(fourthSong, member); - inMemorySongs.recreate(songRepository.findAllWithKillingParts()); // 1, 2, 3 노래에 memberPart 추가 addMemberPartToSong(10, 5, firstSong, member); @@ -317,6 +314,7 @@ void firstFindByMember() { addMemberPartToSong(10, 5, fourthSong, member); saveAndClearEntityManager(); + inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); // when final SongSwipeResponse result = @@ -355,6 +353,8 @@ void firstFindByAnonymous() { final Member member = createAndSaveMember("first@naver.com", "first"); final Long notExistSongId = Long.MAX_VALUE; + saveAndClearEntityManager(); + // when // then assertThatThrownBy( @@ -389,8 +389,6 @@ void findSongByIdForBeforeSwipe() { addLikeToEachKillingParts(fourthSong, member2); addLikeToEachKillingParts(firstSong, member2); - inMemorySongs.recreate(songRepository.findAllWithKillingParts()); - addMemberPartToSong(10, 5, firstSong, member); addMemberPartToSong(10, 5, secondSong, member); addMemberPartToSong(10, 5, standardSong, member); @@ -399,6 +397,7 @@ void findSongByIdForBeforeSwipe() { // 정렬 순서: 2L, 4L, 1L, 5L, 3L saveAndClearEntityManager(); + inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); // when final List beforeResponses = @@ -432,7 +431,6 @@ void findSongByIdForAfterSwipe() { addLikeToEachKillingParts(secondSong, member2); addLikeToEachKillingParts(standardSong, member2); addLikeToEachKillingParts(firstSong, member2); - inMemorySongs.recreate(songRepository.findAllWithKillingParts()); addMemberPartToSong(10, 5, firstSong, member); addMemberPartToSong(10, 5, secondSong, member); @@ -441,8 +439,8 @@ void findSongByIdForAfterSwipe() { addMemberPartToSong(10, 5, fifthSong, member); // 정렬 순서: 2L, 4L, 1L, 5L, 3L - saveAndClearEntityManager(); + inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); // when final List afterResponses = @@ -483,9 +481,9 @@ void findSongsByGenre() { addLikeToEachKillingParts(song1, member); addLikeToEachKillingParts(song1, secondMember); addLikeToEachKillingParts(song3, member); - inMemorySongs.recreate(songRepository.findAllWithKillingParts()); // 정렬 순서: 2L, 1L, 3L, 5L, 4L + inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); saveAndClearEntityManager(); // when @@ -508,9 +506,9 @@ void findSongById() { final Song song = registerNewSong("title"); final Member member = createAndSaveMember("email@email.com", "nickname"); addLikeToEachKillingParts(song, member); - inMemorySongs.recreate(songRepository.findAllWithKillingParts()); addMemberPartToSong(10, 5, song, member); saveAndClearEntityManager(); + inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); // when final SongResponse response = songService.findSongById(song.getId(), diff --git a/backend/src/test/java/shook/shook/song/application/killingpart/KillingPartLikeConcurrencyTest.java b/backend/src/test/java/shook/shook/song/application/killingpart/KillingPartLikeConcurrencyTest.java index c7002a77..4aef7e0d 100644 --- a/backend/src/test/java/shook/shook/song/application/killingpart/KillingPartLikeConcurrencyTest.java +++ b/backend/src/test/java/shook/shook/song/application/killingpart/KillingPartLikeConcurrencyTest.java @@ -19,9 +19,11 @@ import shook.shook.member.domain.repository.MemberRepository; import shook.shook.song.application.killingpart.dto.KillingPartLikeRequest; import shook.shook.song.domain.InMemorySongs; +import shook.shook.song.domain.Song; import shook.shook.song.domain.killingpart.KillingPart; import shook.shook.song.domain.killingpart.repository.KillingPartLikeRepository; import shook.shook.song.domain.killingpart.repository.KillingPartRepository; +import shook.shook.song.domain.repository.SongRepository; @Sql("classpath:/killingpart/initialize_killing_part_song.sql") @SpringBootTest @@ -29,6 +31,7 @@ class KillingPartLikeConcurrencyTest { private static KillingPart SAVED_KILLING_PART; private static Member SAVED_MEMBER; + private static Song SAVED_SONG; @Autowired private KillingPartRepository killingPartRepository; @@ -45,11 +48,15 @@ class KillingPartLikeConcurrencyTest { @Autowired private InMemorySongs inMemorySongs; + @Autowired + private SongRepository songRepository; + private KillingPartLikeService likeService; private TransactionTemplate transactionTemplate; @BeforeEach void setUp() { + SAVED_SONG = songRepository.findById(1L).get(); SAVED_KILLING_PART = killingPartRepository.findById(1L).get(); SAVED_MEMBER = memberRepository.findById(1L).get(); likeService = new KillingPartLikeService(killingPartRepository, memberRepository, killingPartLikeRepository, @@ -64,6 +71,7 @@ void likeByMultiplePeople() throws InterruptedException { // given final Member first = SAVED_MEMBER; final Member second = memberRepository.save(new Member("second@gmail.com", "second")); + inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); // when ExecutorService executorService = Executors.newFixedThreadPool(2); @@ -91,8 +99,10 @@ void likeByMultiplePeople() throws InterruptedException { Thread.sleep(1000); // then - final KillingPart killingPart = killingPartRepository.findById(SAVED_KILLING_PART.getId()).get(); - assertThat(killingPart.getLikeCount()).isEqualTo(2); + final KillingPart killingPart = inMemorySongs.getSongById(SAVED_SONG.getId()).getKillingParts().stream() + .filter(kp -> kp.getId().equals(SAVED_KILLING_PART.getId())) + .findAny().get(); + assertThat(killingPart.getAtomicLikeCount().get()).isEqualTo(2); } @Disabled("UPDATE + 1 사용 시 한 사용자의 동시에 도착하는 좋아요 요청 동시성 문제 발생") diff --git a/backend/src/test/java/shook/shook/song/application/killingpart/KillingPartLikeServiceTest.java b/backend/src/test/java/shook/shook/song/application/killingpart/KillingPartLikeServiceTest.java index 5a06fbe5..de22b50e 100644 --- a/backend/src/test/java/shook/shook/song/application/killingpart/KillingPartLikeServiceTest.java +++ b/backend/src/test/java/shook/shook/song/application/killingpart/KillingPartLikeServiceTest.java @@ -15,10 +15,12 @@ import shook.shook.member.exception.MemberException; import shook.shook.song.application.killingpart.dto.KillingPartLikeRequest; import shook.shook.song.domain.InMemorySongs; +import shook.shook.song.domain.Song; import shook.shook.song.domain.killingpart.KillingPart; import shook.shook.song.domain.killingpart.KillingPartLike; import shook.shook.song.domain.killingpart.repository.KillingPartLikeRepository; import shook.shook.song.domain.killingpart.repository.KillingPartRepository; +import shook.shook.song.domain.repository.SongRepository; import shook.shook.song.exception.killingpart.KillingPartException; import shook.shook.support.UsingJpaTest; @@ -29,6 +31,7 @@ class KillingPartLikeServiceTest extends UsingJpaTest { private static final long UNSAVED_KILLING_PART_ID = Long.MAX_VALUE; private static KillingPart SAVED_KILLING_PART; private static Member SAVED_MEMBER; + private static Song SAVED_SONG; @Autowired private KillingPartRepository killingPartRepository; @@ -39,14 +42,20 @@ class KillingPartLikeServiceTest extends UsingJpaTest { @Autowired private MemberRepository memberRepository; + @Autowired + private SongRepository songRepository; + private KillingPartLikeService likeService; + private InMemorySongs inMemorySongs; @BeforeEach void setUp() { + SAVED_SONG = songRepository.findById(1L).get(); SAVED_KILLING_PART = killingPartRepository.findById(1L).get(); SAVED_MEMBER = memberRepository.findById(1L).get(); - likeService = new KillingPartLikeService(killingPartRepository, memberRepository, - killingPartLikeRepository, new InMemorySongs(entityManager)); + inMemorySongs = new InMemorySongs(entityManager); + likeService = new KillingPartLikeService(killingPartRepository, memberRepository, killingPartLikeRepository, + inMemorySongs); } @DisplayName("킬링파트 좋아요를 누른다.") @@ -58,6 +67,7 @@ class Create { void create_newLike() { // given // when + inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); likeService.updateLikeStatus(SAVED_KILLING_PART.getId(), SAVED_MEMBER.getId(), new KillingPartLikeRequest(true)); saveAndClearEntityManager(); @@ -65,8 +75,10 @@ void create_newLike() { // then final Optional savedLike = killingPartLikeRepository. findByKillingPartAndMember(SAVED_KILLING_PART, SAVED_MEMBER); - final Optional updatedKillingPart = killingPartRepository.findById( - SAVED_KILLING_PART.getId()); + final Optional updatedKillingPart = inMemorySongs.getSongById(SAVED_SONG.getId()) + .getKillingParts().stream() + .filter(killingPart -> killingPart.getId().equals(SAVED_KILLING_PART.getId())) + .findAny(); assertThat(savedLike).isPresent() .get() @@ -81,6 +93,7 @@ void create_newLike() { @Test void create_updateLike_exist() { // given + inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); likeService.updateLikeStatus(SAVED_KILLING_PART.getId(), SAVED_MEMBER.getId(), new KillingPartLikeRequest(true)); likeService.updateLikeStatus(SAVED_KILLING_PART.getId(), SAVED_MEMBER.getId(), @@ -95,8 +108,10 @@ void create_updateLike_exist() { // then final Optional savedLike = killingPartLikeRepository. findByKillingPartAndMember(SAVED_KILLING_PART, SAVED_MEMBER); - final Optional updatedKillingPart = killingPartRepository.findById( - SAVED_KILLING_PART.getId()); + final Optional updatedKillingPart = inMemorySongs.getSongById(SAVED_SONG.getId()) + .getKillingParts().stream() + .filter(killingPart -> killingPart.getId().equals(SAVED_KILLING_PART.getId())) + .findAny(); assertThat(savedLike).isPresent() .get() @@ -111,6 +126,7 @@ void create_updateLike_exist() { @Test void create_noAction() { // given + inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); likeService.updateLikeStatus(SAVED_KILLING_PART.getId(), SAVED_MEMBER.getId(), new KillingPartLikeRequest(true)); saveAndClearEntityManager(); @@ -123,8 +139,10 @@ void create_noAction() { // then final Optional savedLike = killingPartLikeRepository. findByKillingPartAndMember(SAVED_KILLING_PART, SAVED_MEMBER); - final Optional updatedKillingPart = killingPartRepository.findById( - SAVED_KILLING_PART.getId()); + final Optional updatedKillingPart = inMemorySongs.getSongById(SAVED_SONG.getId()) + .getKillingParts().stream() + .filter(killingPart -> killingPart.getId().equals(SAVED_KILLING_PART.getId())) + .findAny(); assertThat(savedLike).isPresent() .get() @@ -187,6 +205,7 @@ void delete_noAction() { @Test void delete_alreadyDeleted_noAction() { // given + inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); likeService.updateLikeStatus(SAVED_KILLING_PART.getId(), SAVED_MEMBER.getId(), new KillingPartLikeRequest(true)); likeService.updateLikeStatus(SAVED_KILLING_PART.getId(), SAVED_MEMBER.getId(), @@ -217,6 +236,7 @@ void delete_alreadyDeleted_noAction() { @Test void create_noAction() { // given + inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); likeService.updateLikeStatus(SAVED_KILLING_PART.getId(), SAVED_MEMBER.getId(), new KillingPartLikeRequest(true)); saveAndClearEntityManager(); diff --git a/backend/src/test/java/shook/shook/song/domain/InMemorySongsTest.java b/backend/src/test/java/shook/shook/song/domain/InMemorySongsTest.java index 25273817..b9535c11 100644 --- a/backend/src/test/java/shook/shook/song/domain/InMemorySongsTest.java +++ b/backend/src/test/java/shook/shook/song/domain/InMemorySongsTest.java @@ -43,7 +43,7 @@ void setUp() { @Test void recreate() { // given - final List songs = songRepository.findAllWithKillingParts(); + final List songs = songRepository.findAllWithKillingPartsAndLikes(); likeAllKillingPartsInSong(songs.get(0)); likeAllKillingPartsInSong(songs.get(1)); @@ -71,14 +71,14 @@ private void likeAllKillingPartsInSong(final Song song) { @Test void getSongById() { // given - final List songs = songRepository.findAllWithKillingParts(); - inMemorySongs.recreate(songs); + inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); // when + final List allSongs = inMemorySongs.getSongs(); final Song foundSong = inMemorySongs.getSongById(4L); // then - final Song expectedSong = songs.get(0); + final Song expectedSong = allSongs.get(0); assertThat(foundSong).isEqualTo(expectedSong); } @@ -86,7 +86,7 @@ void getSongById() { @Test void getPrevLikedSongs() { // given - final List songs = songRepository.findAllWithKillingParts(); + final List songs = songRepository.findAllWithKillingPartsAndLikes(); final Song firstSong = songs.get(0); final Song secondSong = songs.get(1); final Song thirdSong = songs.get(2); @@ -110,7 +110,7 @@ void getPrevLikedSongs() { @Test void getNextLikedSongs() { // given - final List songs = songRepository.findAllWithKillingParts(); + final List songs = songRepository.findAllWithKillingPartsAndLikes(); final Song firstSong = songs.get(0); final Song secondSong = songs.get(1); final Song thirdSong = songs.get(2); @@ -133,7 +133,7 @@ void getNextLikedSongs() { @DisplayName("특정 장르 노래에 대해 1. 좋아요 수가 더 적거나 2. 좋아요 수가 같은 경우 id가 더 작은 노래 목록을 조회한다.") @Test void getSortedSongsByGenre() { - final List songs = songRepository.findAllWithKillingParts(); + final List songs = songRepository.findAllWithKillingPartsAndLikes(); final Song firstSong = songs.get(0); final Song secondSong = songs.get(1); final Song thirdSong = songs.get(2); @@ -155,7 +155,7 @@ void getSortedSongsByGenre() { @DisplayName("특정 장르 노래에 대해 1. 좋아요 수가 더 많거나 2. 좋아요 수가 같은 경우 id가 더 큰 노래 목록을 조회한다.") @Test void getPrevLikedSongByGenre() { - final List songs = songRepository.findAllWithKillingParts(); + final List songs = songRepository.findAllWithKillingPartsAndLikes(); final Song firstSong = songs.get(0); final Song secondSong = songs.get(1); final Song thirdSong = songs.get(2); diff --git a/backend/src/test/java/shook/shook/song/domain/killingpart/KillingPartLikesTest.java b/backend/src/test/java/shook/shook/song/domain/killingpart/KillingPartLikesTest.java index b4e68827..ab405e0a 100644 --- a/backend/src/test/java/shook/shook/song/domain/killingpart/KillingPartLikesTest.java +++ b/backend/src/test/java/shook/shook/song/domain/killingpart/KillingPartLikesTest.java @@ -2,7 +2,7 @@ import static org.assertj.core.api.Assertions.assertThat; -import java.util.List; +import java.util.Set; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -135,7 +135,7 @@ void getLikes() { KILLING_PART.unlike(newLike); // when - final List likes = LIKES.getLikes(); + final Set likes = LIKES.getLikes(); // then assertThat(LIKES.getSize()).isEqualTo(1); diff --git a/backend/src/test/java/shook/shook/song/ui/HighLikedSongControllerTest.java b/backend/src/test/java/shook/shook/song/ui/HighLikedSongControllerTest.java index 653d2995..f9cedc93 100644 --- a/backend/src/test/java/shook/shook/song/ui/HighLikedSongControllerTest.java +++ b/backend/src/test/java/shook/shook/song/ui/HighLikedSongControllerTest.java @@ -51,16 +51,15 @@ void setUp() { @Test void showHighLikedSongs() { //given + inMemorySongsScheduler.recreateCachedSong(); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_1, MEMBER_ID, - new KillingPartLikeRequest(true)); + new KillingPartLikeRequest(true)); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_2, MEMBER_ID, - new KillingPartLikeRequest(true)); + new KillingPartLikeRequest(true)); likeService.updateLikeStatus(SECOND_SONG_KILLING_PART_ID_1, MEMBER_ID, - new KillingPartLikeRequest(true)); + new KillingPartLikeRequest(true)); // 정렬 순서 1L 2L 4L 3L - inMemorySongsScheduler.recreateCachedSong(); - //when final List responses = RestAssured.given().log().all() .when().log().all() @@ -94,16 +93,15 @@ void showHighLikedSongs() { void showHighLikedSongsWithGenre() { // given final String genre = "DANCE"; + inMemorySongsScheduler.recreateCachedSong(); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_1, MEMBER_ID, - new KillingPartLikeRequest(true)); + new KillingPartLikeRequest(true)); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_2, MEMBER_ID, - new KillingPartLikeRequest(true)); + new KillingPartLikeRequest(true)); likeService.updateLikeStatus(SECOND_SONG_KILLING_PART_ID_1, MEMBER_ID, - new KillingPartLikeRequest(true)); + new KillingPartLikeRequest(true)); // 정렬 순서 1L 4L 3L - inMemorySongsScheduler.recreateCachedSong(); - // when final List responses = RestAssured.given().log().all() .queryParam("genre", genre) @@ -118,11 +116,11 @@ void showHighLikedSongsWithGenre() { assertAll( () -> assertThat(responses).hasSize(3), () -> assertThat(responses.stream() - .map(HighLikedSongResponse::getId) - .toList()) + .map(HighLikedSongResponse::getId) + .toList()) .containsExactly(1L, 4L, 3L), () -> assertThat(responses.stream() - .map(HighLikedSongResponse::getTotalLikeCount)) + .map(HighLikedSongResponse::getTotalLikeCount)) .containsExactly(2L, 0L, 0L) ); } diff --git a/backend/src/test/java/shook/shook/song/ui/SongControllerTest.java b/backend/src/test/java/shook/shook/song/ui/SongControllerTest.java index 276805da..bf77d037 100644 --- a/backend/src/test/java/shook/shook/song/ui/SongControllerTest.java +++ b/backend/src/test/java/shook/shook/song/ui/SongControllerTest.java @@ -57,6 +57,7 @@ void findSongById() { final String accessToken = tokenProvider.createAccessToken(MEMBER_ID, MEMBER_NICKNAME); final Long songId = 1L; + inMemorySongsScheduler.recreateCachedSong(); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_1, MEMBER_ID, new KillingPartLikeRequest(true)); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_1, MEMBER_ID, @@ -65,7 +66,6 @@ void findSongById() { new KillingPartLikeRequest(true)); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_3, MEMBER_ID, new KillingPartLikeRequest(true)); - inMemorySongsScheduler.recreateCachedSong(); memberPartService.register(songId, MEMBER_ID, new MemberPartRegisterRequest(0, 10)); diff --git a/backend/src/test/java/shook/shook/song/ui/SongSwipeControllerTest.java b/backend/src/test/java/shook/shook/song/ui/SongSwipeControllerTest.java index 494b106b..fe25d891 100644 --- a/backend/src/test/java/shook/shook/song/ui/SongSwipeControllerTest.java +++ b/backend/src/test/java/shook/shook/song/ui/SongSwipeControllerTest.java @@ -25,7 +25,6 @@ import shook.shook.song.application.killingpart.dto.HighLikedSongResponse; import shook.shook.song.application.killingpart.dto.KillingPartLikeRequest; -@SuppressWarnings("NonAsciiCharacters") @Sql("classpath:/killingpart/initialize_killing_part_song.sql") @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) class SongSwipeControllerTest { @@ -62,6 +61,7 @@ void showSongById() { final String accessToken = tokenProvider.createAccessToken(MEMBER_ID, "nickname"); final Long songId = 2L; + inMemorySongsScheduler.recreateCachedSong(); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_1, MEMBER_ID, new KillingPartLikeRequest(true)); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_2, MEMBER_ID, @@ -69,7 +69,6 @@ void showSongById() { likeService.updateLikeStatus(SECOND_SONG_KILLING_PART_ID_1, MEMBER_ID, new KillingPartLikeRequest(true)); - inMemorySongsScheduler.recreateCachedSong(); // 정렬 순서: 1L, 2L, 4L, 3L //when final SongSwipeResponse response = RestAssured.given().log().all() @@ -97,6 +96,8 @@ void showSongsBeforeSongWithId() { // given final Long songId = 2L; final String accessToken = tokenProvider.createAccessToken(MEMBER_ID, "nickname"); + + inMemorySongsScheduler.recreateCachedSong(); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_1, MEMBER_ID, new KillingPartLikeRequest(true)); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_2, MEMBER_ID, @@ -105,8 +106,6 @@ void showSongsBeforeSongWithId() { memberPartService.register(1L, MEMBER_ID, new MemberPartRegisterRequest(5, 5)); // 정렬 순서 1L, 4L, 3L, 2L - inMemorySongsScheduler.recreateCachedSong(); - //when final List response = RestAssured.given().log().all() .header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken) @@ -214,6 +213,7 @@ void findSongsByGenreForSwipe() { final String genre = "DANCE"; final String accessToken = tokenProvider.createAccessToken(MEMBER_ID, "nickname"); + inMemorySongsScheduler.recreateCachedSong(); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_1, MEMBER_ID, new KillingPartLikeRequest(true)); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_2, MEMBER_ID, @@ -224,8 +224,6 @@ void findSongsByGenreForSwipe() { memberPartService.register(1L, MEMBER_ID, new MemberPartRegisterRequest(5, 5)); // 정렬 순서 1L, 4L, 3L - inMemorySongsScheduler.recreateCachedSong(); - //when final SongSwipeResponse response = RestAssured.given().log().all() .queryParam("genre", genre) @@ -258,6 +256,7 @@ void showPrevSongsWithGenre() { final String genre = "DANCE"; final String accessToken = tokenProvider.createAccessToken(MEMBER_ID, "nickname"); + inMemorySongsScheduler.recreateCachedSong(); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_1, MEMBER_ID, new KillingPartLikeRequest(true)); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_2, MEMBER_ID, @@ -266,8 +265,6 @@ void showPrevSongsWithGenre() { memberPartService.register(1L, MEMBER_ID, new MemberPartRegisterRequest(5, 5)); // 정렬 순서 1L, 4L, 3L - inMemorySongsScheduler.recreateCachedSong(); - //when final List response = RestAssured.given().log().all() .queryParam("genre", genre) @@ -295,6 +292,7 @@ void showNextSongsWithGenre() { final String genre = "DANCE"; final String accessToken = tokenProvider.createAccessToken(MEMBER_ID, "nickname"); + inMemorySongsScheduler.recreateCachedSong(); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_1, MEMBER_ID, new KillingPartLikeRequest(true)); likeService.updateLikeStatus(FIRST_SONG_KILLING_PART_ID_2, MEMBER_ID, @@ -305,8 +303,6 @@ void showNextSongsWithGenre() { memberPartService.register(4L, MEMBER_ID, new MemberPartRegisterRequest(5, 5)); // 정렬 순서 1L, 4L, 3L - inMemorySongsScheduler.recreateCachedSong(); - //when final List response = RestAssured.given().log().all() .queryParam("genre", genre)