|
| 1 | +package com.daedan.festabook.festival.infrastructure; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.daedan.festabook.device.domain.Device; |
| 6 | +import com.daedan.festabook.device.domain.DeviceFixture; |
| 7 | +import com.daedan.festabook.device.infrastructure.DeviceJpaRepository; |
| 8 | +import com.daedan.festabook.festival.domain.Festival; |
| 9 | +import com.daedan.festabook.festival.domain.FestivalFixture; |
| 10 | +import com.daedan.festabook.festival.domain.FestivalNotification; |
| 11 | +import com.daedan.festabook.festival.domain.FestivalNotificationFixture; |
| 12 | +import java.util.List; |
| 13 | +import org.junit.jupiter.api.DisplayNameGeneration; |
| 14 | +import org.junit.jupiter.api.DisplayNameGenerator; |
| 15 | +import org.junit.jupiter.api.Nested; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; |
| 19 | + |
| 20 | +@DataJpaTest |
| 21 | +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) |
| 22 | +class FestivalNotificationJpaRepositoryTest { |
| 23 | + |
| 24 | + @Autowired |
| 25 | + private FestivalJpaRepository festivalJpaRepository; |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private DeviceJpaRepository deviceJpaRepository; |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private FestivalNotificationJpaRepository festivalNotificationJpaRepository; |
| 32 | + |
| 33 | + @Nested |
| 34 | + class getExistsFlagByFestivalIdAndDeviceId { |
| 35 | + |
| 36 | + @Test |
| 37 | + void 성공_일치하는_알림이_있는_경우_1_반환() { |
| 38 | + // given |
| 39 | + Festival festival = festivalJpaRepository.save(FestivalFixture.create()); |
| 40 | + Device device = deviceJpaRepository.save(DeviceFixture.create()); |
| 41 | + |
| 42 | + FestivalNotification festivalNotification = FestivalNotificationFixture.create(festival, device); |
| 43 | + festivalNotificationJpaRepository.save(festivalNotification); |
| 44 | + |
| 45 | + // when |
| 46 | + int existsFlag = festivalNotificationJpaRepository.getExistsFlagByFestivalIdAndDeviceId( |
| 47 | + festival.getId(), |
| 48 | + device.getId() |
| 49 | + ); |
| 50 | + |
| 51 | + // then |
| 52 | + assertThat(existsFlag).isEqualTo(1); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void 성공_삭제된_알림인_경우_없다고_판단해_0_반환() { |
| 57 | + // given |
| 58 | + Festival festival = festivalJpaRepository.save(FestivalFixture.create()); |
| 59 | + Device device = deviceJpaRepository.save(DeviceFixture.create()); |
| 60 | + |
| 61 | + FestivalNotification festivalNotification = FestivalNotificationFixture.create(festival, device); |
| 62 | + FestivalNotification savedNotification = festivalNotificationJpaRepository.save(festivalNotification); |
| 63 | + festivalNotificationJpaRepository.delete(savedNotification); |
| 64 | + |
| 65 | + // when |
| 66 | + int existsFlag = festivalNotificationJpaRepository.getExistsFlagByFestivalIdAndDeviceId( |
| 67 | + festival.getId(), |
| 68 | + device.getId() |
| 69 | + ); |
| 70 | + |
| 71 | + // then |
| 72 | + assertThat(existsFlag).isZero(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void 성공_알림이_존재하지_않는_경우_0_반환() { |
| 77 | + // given |
| 78 | + Festival festival = festivalJpaRepository.save(FestivalFixture.create()); |
| 79 | + Device device = deviceJpaRepository.save(DeviceFixture.create()); |
| 80 | + |
| 81 | + // when |
| 82 | + int existsFlag = festivalNotificationJpaRepository.getExistsFlagByFestivalIdAndDeviceId( |
| 83 | + festival.getId(), |
| 84 | + device.getId() |
| 85 | + ); |
| 86 | + |
| 87 | + // then |
| 88 | + assertThat(existsFlag).isZero(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Nested |
| 93 | + class findAllWithFestivalByDeviceId { |
| 94 | + |
| 95 | + @Test |
| 96 | + void 성공_하나의_디바이스가_구독한_여러_축제_조회() { |
| 97 | + // given |
| 98 | + Device device = deviceJpaRepository.save(DeviceFixture.create()); |
| 99 | + |
| 100 | + Festival firstFestival = festivalJpaRepository.save(FestivalFixture.create()); |
| 101 | + Festival secondFestival = festivalJpaRepository.save(FestivalFixture.create()); |
| 102 | + |
| 103 | + FestivalNotification first = festivalNotificationJpaRepository.save( |
| 104 | + FestivalNotificationFixture.create(firstFestival, device) |
| 105 | + ); |
| 106 | + |
| 107 | + FestivalNotification second = festivalNotificationJpaRepository.save( |
| 108 | + FestivalNotificationFixture.create(secondFestival, device) |
| 109 | + ); |
| 110 | + |
| 111 | + // when |
| 112 | + List<FestivalNotification> result = festivalNotificationJpaRepository.findAllWithFestivalByDeviceId( |
| 113 | + device.getId()); |
| 114 | + |
| 115 | + // then |
| 116 | + assertThat(result) |
| 117 | + .extracting(FestivalNotification::getId) |
| 118 | + .containsExactlyInAnyOrder( |
| 119 | + first.getId(), |
| 120 | + second.getId() |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void 성공_다른_디바이스_알림은_제외하고_조회() { |
| 126 | + // given |
| 127 | + Festival targetFestival = festivalJpaRepository.save(FestivalFixture.create()); |
| 128 | + Device targetDevice = deviceJpaRepository.save(DeviceFixture.create()); |
| 129 | + |
| 130 | + FestivalNotification targetNotification = festivalNotificationJpaRepository.save( |
| 131 | + FestivalNotificationFixture.create(targetFestival, targetDevice) |
| 132 | + ); |
| 133 | + |
| 134 | + Festival otherFestival = festivalJpaRepository.save(FestivalFixture.create()); |
| 135 | + Device otherDevice = deviceJpaRepository.save(DeviceFixture.create()); |
| 136 | + |
| 137 | + festivalNotificationJpaRepository.save(FestivalNotificationFixture.create(otherFestival, otherDevice)); |
| 138 | + |
| 139 | + // when |
| 140 | + List<FestivalNotification> result = festivalNotificationJpaRepository.findAllWithFestivalByDeviceId( |
| 141 | + targetDevice.getId() |
| 142 | + ); |
| 143 | + |
| 144 | + // then |
| 145 | + assertThat(result).containsExactly(targetNotification); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void 성공_삭제된_알림은_조회_안됨() { |
| 150 | + // given |
| 151 | + Festival festival = festivalJpaRepository.save(FestivalFixture.create()); |
| 152 | + Device device = deviceJpaRepository.save(DeviceFixture.create()); |
| 153 | + |
| 154 | + FestivalNotification festivalNotification = festivalNotificationJpaRepository.save( |
| 155 | + FestivalNotificationFixture.create(festival, device) |
| 156 | + ); |
| 157 | + |
| 158 | + festivalNotificationJpaRepository.delete(festivalNotification); |
| 159 | + |
| 160 | + // when |
| 161 | + List<FestivalNotification> result = festivalNotificationJpaRepository.findAllWithFestivalByDeviceId( |
| 162 | + device.getId() |
| 163 | + ); |
| 164 | + |
| 165 | + // then |
| 166 | + assertThat(result).isEmpty(); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments