-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: device-token-register-service integration test
- Loading branch information
1 parent
37657b5
commit 14db037
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
...ennyway/domain/context/account/integration/DeviceTokenRegisterServiceIntegrationTest.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,106 @@ | ||
package kr.co.pennyway.domain.context.account.integration; | ||
|
||
import kr.co.pennyway.domain.common.repository.ExtendedRepositoryFactory; | ||
import kr.co.pennyway.domain.config.DomainServiceIntegrationProfileResolver; | ||
import kr.co.pennyway.domain.config.DomainServiceTestInfraConfig; | ||
import kr.co.pennyway.domain.config.JpaTestConfig; | ||
import kr.co.pennyway.domain.context.account.service.DeviceTokenRegisterService; | ||
import kr.co.pennyway.domain.context.common.fixture.UserFixture; | ||
import kr.co.pennyway.domain.domains.device.domain.DeviceToken; | ||
import kr.co.pennyway.domain.domains.device.exception.DeviceTokenErrorCode; | ||
import kr.co.pennyway.domain.domains.device.exception.DeviceTokenErrorException; | ||
import kr.co.pennyway.domain.domains.device.repository.DeviceTokenRepository; | ||
import kr.co.pennyway.domain.domains.device.service.DeviceTokenRdbService; | ||
import kr.co.pennyway.domain.domains.user.domain.User; | ||
import kr.co.pennyway.domain.domains.user.repository.UserRepository; | ||
import kr.co.pennyway.domain.domains.user.service.UserRdbService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.domain.EntityScan; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@Slf4j | ||
@EnableAutoConfiguration | ||
@SpringBootTest(classes = {DeviceTokenRegisterService.class, UserRdbService.class, DeviceTokenRdbService.class}) | ||
@EntityScan(basePackageClasses = {User.class, DeviceToken.class}) | ||
@EnableJpaRepositories(basePackageClasses = {UserRepository.class, DeviceTokenRepository.class}, repositoryFactoryBeanClass = ExtendedRepositoryFactory.class) | ||
@ActiveProfiles(resolver = DomainServiceIntegrationProfileResolver.class) | ||
@Import(value = {JpaTestConfig.class}) | ||
public class DeviceTokenRegisterServiceIntegrationTest extends DomainServiceTestInfraConfig { | ||
@Autowired | ||
private DeviceTokenRegisterService deviceTokenRegisterService; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private DeviceTokenRepository deviceTokenRepository; | ||
|
||
private User savedUser; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
savedUser = userRepository.save(UserFixture.GENERAL_USER.toUser()); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
@DisplayName("λλ°μ΄μ€ ν ν° λ±λ‘ μ κΈ°μ‘΄ νμ± ν ν°μ λΉνμ±νλ©λλ€") | ||
void shouldDeactivateExistingTokensWhenRegisteringNew() { | ||
// given | ||
String deviceId = "device1"; | ||
|
||
// when | ||
DeviceToken firstToken = deviceTokenRegisterService.execute(savedUser.getId(), deviceId, "Android", "token1"); | ||
DeviceToken secondToken = deviceTokenRegisterService.execute(savedUser.getId(), deviceId, "Android", "token2"); | ||
|
||
// then | ||
assertFalse(firstToken.isActivated()); | ||
assertTrue(secondToken.isActivated()); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
@DisplayName("νμ±νλ ν ν°μ΄ λ€λ₯Έ λλ°μ΄μ€μμ μ¬μ©λλ©΄ μμΈκ° λ°μν©λλ€") | ||
void shouldThrowExceptionWhenActiveTokenIsUsedOnDifferentDevice() { | ||
// given | ||
String token = "token1"; | ||
deviceTokenRegisterService.execute(savedUser.getId(), "device1", "Android", token); | ||
|
||
// when & then | ||
DeviceTokenErrorException exception = assertThrowsExactly( | ||
DeviceTokenErrorException.class, | ||
() -> deviceTokenRegisterService.execute(savedUser.getId(), "device2", "iPhone", token) | ||
); | ||
assertEquals(DeviceTokenErrorCode.DUPLICATED_DEVICE_TOKEN, exception.getBaseErrorCode()); | ||
} | ||
|
||
@Test | ||
@DisplayName("κ°μ deviceId, token / λ€λ₯Έ μ¬μ©μ κ°±μ μμ²μ΄λΌλ©΄, λλ°μ΄μ€ ν ν°μ μμ κΆμ΄ λ€λ₯Έ μ¬μ©μμκ² μ΄μ λ©λλ€") | ||
void shouldTransferTokenOwnership() { | ||
// given | ||
User anotherUser = userRepository.save(UserFixture.GENERAL_USER.toUser()); | ||
|
||
String deviceId = "device1"; | ||
String token = "token1"; | ||
|
||
// when | ||
DeviceToken firstUserToken = deviceTokenRegisterService.execute(savedUser.getId(), deviceId, "Android", token); | ||
DeviceToken secondUserToken = deviceTokenRegisterService.execute(anotherUser.getId(), deviceId, "Android", token); | ||
|
||
// then | ||
assertEquals(firstUserToken.getId(), secondUserToken.getId()); | ||
assertEquals(anotherUser.getId(), secondUserToken.getUser().getId()); | ||
assertTrue(secondUserToken.isActivated()); | ||
} | ||
} |