-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 후원 API 중복 요청을 방지한다. (따닥 방지) (#448)
* chore: DatabaseCleaner.clear 에 Redis 초기화 로직을 추가한다. * feat: DonationRedisRepository 를 생성한다. * feat: DonationService.registerDonation 에 중복 검증 로직을 추가한다. * refactor: ValueOperations.setIfAbsent() 의 NPE 를 방지한다. * refactor: Redis key prefix 를 상수로 선언한다. * refactor: valueOperations 를 DonationRedisRepository 생성 시점에서 초기화되도록 변경한다.
- Loading branch information
1 parent
4a18216
commit 31880d8
Showing
7 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/main/java/com/clova/anifriends/domain/donation/exception/DonationDuplicateException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.clova.anifriends.domain.donation.exception; | ||
|
||
import com.clova.anifriends.global.exception.BadRequestException; | ||
import com.clova.anifriends.global.exception.ErrorCode; | ||
|
||
public class DonationDuplicateException extends BadRequestException { | ||
|
||
public DonationDuplicateException(String message) { | ||
super(ErrorCode.BAD_REQUEST, message); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/clova/anifriends/domain/donation/repository/DonationCacheRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.clova.anifriends.domain.donation.repository; | ||
|
||
public interface DonationCacheRepository { | ||
|
||
boolean isDuplicateDonation(Long volunteerId); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/clova/anifriends/domain/donation/repository/DonationRedisRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.clova.anifriends.domain.donation.repository; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.ValueOperations; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public class DonationRedisRepository implements DonationCacheRepository { | ||
|
||
public static final int TIMEOUT = 1; | ||
public static final String DONATION_KEY = "donation:"; | ||
|
||
private final ValueOperations<String, Object> valueOperations; | ||
|
||
public DonationRedisRepository(RedisTemplate<String, Object> redisTemplate) { | ||
this.valueOperations = redisTemplate.opsForValue(); | ||
} | ||
|
||
public boolean isDuplicateDonation(Long volunteerId) { | ||
String key = DONATION_KEY + volunteerId; | ||
return Boolean.FALSE.equals( | ||
valueOperations.setIfAbsent(key, true, TIMEOUT, TimeUnit.SECONDS)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/test/java/com/clova/anifriends/domain/donation/service/DonationIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.clova.anifriends.domain.donation.service; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.catchException; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
import com.clova.anifriends.base.BaseIntegrationTest; | ||
import com.clova.anifriends.domain.donation.exception.DonationDuplicateException; | ||
import com.clova.anifriends.domain.shelter.Shelter; | ||
import com.clova.anifriends.domain.shelter.support.ShelterFixture; | ||
import com.clova.anifriends.domain.volunteer.Volunteer; | ||
import com.clova.anifriends.domain.volunteer.support.VolunteerFixture; | ||
import java.time.Duration; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
public class DonationIntegrationTest extends BaseIntegrationTest { | ||
|
||
@Autowired | ||
private DonationService donationService; | ||
|
||
@Nested | ||
@DisplayName("registerDonation 실행 시") | ||
class RegisterDonationTest { | ||
|
||
@Test | ||
@DisplayName("성공: 1초 간격으로 두 번 후원 요청 시") | ||
void registerDonation() { | ||
// given | ||
Volunteer volunteer = VolunteerFixture.volunteer(); | ||
Shelter shelter = ShelterFixture.shelter(); | ||
int amount = 100_000; | ||
|
||
volunteerRepository.save(volunteer); | ||
shelterRepository.save(shelter); | ||
|
||
// when && then | ||
donationService.registerDonation(volunteer.getVolunteerId(), shelter.getShelterId(), | ||
amount); | ||
|
||
await().pollDelay(Duration.ofSeconds(1)).untilAsserted(() -> { | ||
assertThat(catchException( | ||
() -> donationService.registerDonation(volunteer.getVolunteerId(), | ||
shelter.getShelterId(), amount))).isNull(); | ||
}); | ||
} | ||
|
||
@Test | ||
@DisplayName("예외(DonationDuplicateException): 1초 안에 연속 두 번 후원 요청 시") | ||
void exceptionWhenDuplicateDonation() { | ||
// given | ||
Volunteer volunteer = VolunteerFixture.volunteer(); | ||
Shelter shelter = ShelterFixture.shelter(); | ||
int amount = 100_000; | ||
|
||
volunteerRepository.save(volunteer); | ||
shelterRepository.save(shelter); | ||
|
||
// when | ||
Exception exception = catchException(() -> { | ||
donationService.registerDonation(volunteer.getVolunteerId(), shelter.getShelterId(), | ||
amount); | ||
donationService.registerDonation(volunteer.getVolunteerId(), shelter.getShelterId(), | ||
amount); | ||
}); | ||
|
||
// then | ||
assertThat(exception).isInstanceOf(DonationDuplicateException.class); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters