Skip to content

Commit

Permalink
test: device-token-register-service integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
psychology50 committed Dec 4, 2024
1 parent 37657b5 commit 14db037
Showing 1 changed file with 106 additions and 0 deletions.
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());
}
}

0 comments on commit 14db037

Please sign in to comment.