Skip to content

Commit

Permalink
Revert "refactor: #480 디바이스 토큰 관련 테스트 코드 리팩토링 (#491)"
Browse files Browse the repository at this point in the history
This reverts commit d64c257.
  • Loading branch information
swonny authored Oct 6, 2023
1 parent ac37e1b commit ffc2e66
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 228 deletions.
Original file line number Diff line number Diff line change
@@ -1,68 +1,113 @@
package com.ddang.ddang.device.application;

import com.ddang.ddang.configuration.IsolateDatabase;
import com.ddang.ddang.device.application.fixture.DeviceTokenServiceFixture;
import com.ddang.ddang.device.application.dto.PersistDeviceTokenDto;
import com.ddang.ddang.device.domain.DeviceToken;
import com.ddang.ddang.device.infrastructure.persistence.JpaDeviceTokenRepository;
import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.user.application.exception.UserNotFoundException;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.infrastructure.persistence.JpaUserRepository;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@IsolateDatabase
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class DeviceTokenServiceTest extends DeviceTokenServiceFixture {
class DeviceTokenServiceTest {

@Autowired
DeviceTokenService deviceTokenService;

@Autowired
JpaDeviceTokenRepository deviceTokenRepository;

@Autowired
JpaUserRepository userRepository;

@Test
void 사용자의_디바이스_토큰이_존재하지_않는다면_저장한다() {
// given
final String deviceTokenValue = "deviceToken";
final User user = User.builder()
.name("사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
userRepository.save(user);

final PersistDeviceTokenDto persistDeviceTokenDto = new PersistDeviceTokenDto(deviceTokenValue);

// when & then
assertThatNoException().isThrownBy(
() -> deviceTokenService.persist(디바이스_토큰이_없는_사용자.getId(), 디바이스_토큰_저장을_위한_DTO)
);
assertThatNoException().isThrownBy(() -> deviceTokenService.persist(user.getId(), persistDeviceTokenDto));
}

@Test
void 사용자의_디바이스_토큰이_이미_존재하고_새로운_토큰이_주어진다면_토큰을_갱신한다() {
// given
final String deviceTokenValue = "deviceToken";
final User user = User.builder()
.name("사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
userRepository.save(user);

final DeviceToken deviceToken = new DeviceToken(user, deviceTokenValue);
deviceTokenRepository.save(deviceToken);

final String newDeviceTokenValue = "newDeviceToken";
final PersistDeviceTokenDto persistDeviceTokenDto = new PersistDeviceTokenDto(newDeviceTokenValue);

// when
deviceTokenService.persist(디바이스_토큰이_있는_사용자.getId(), 디바이스_토큰_갱신을_위한_DTO);
deviceTokenService.persist(user.getId(), persistDeviceTokenDto);

// then
final Optional<DeviceToken> deviceTokenResult = deviceTokenRepository.findByUserId(디바이스_토큰이_있는_사용자.getId());
final String actual = deviceTokenResult.get().getDeviceToken();

assertThat(actual).isEqualTo(갱신된_디바이스_토큰_값);
assertThat(deviceToken.getDeviceToken()).isEqualTo(newDeviceTokenValue);
}

@Test
void 사용자의_디바이스_토큰이_이미_존재하고_동일한_토큰이_주어진다면_토큰을_갱신하지_않는다() {
// given
final String deviceTokenValue = "deviceToken";
final User user = User.builder()
.name("사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
userRepository.save(user);

final DeviceToken deviceToken = new DeviceToken(user, deviceTokenValue);
deviceTokenRepository.save(deviceToken);

final String newDeviceTokenValue = deviceTokenValue;
final PersistDeviceTokenDto persistDeviceTokenDto = new PersistDeviceTokenDto(newDeviceTokenValue);

// when
deviceTokenService.persist(디바이스_토큰이_있는_사용자.getId(), 존재하는_디바이스_토큰과_동일한_토큰을_저장하려는_DTO);
deviceTokenService.persist(user.getId(), persistDeviceTokenDto);

// then
final Optional<DeviceToken> userDeviceToken = deviceTokenRepository.findByUserId(디바이스_토큰이_있는_사용자.getId());
final String actual = userDeviceToken.get().getDeviceToken();

assertThat(actual).isEqualTo(사용_중인_디바이스_토큰_값);
assertThat(deviceToken.getDeviceToken()).isEqualTo(deviceTokenValue);
}

@Test
void 사용자를_찾을_수_없다면_예외가_발생한다() {
// given
final Long invalidUserId = -999L;
final String deviceTokenValue = "deviceToken";
final PersistDeviceTokenDto persistDeviceTokenDto = new PersistDeviceTokenDto(deviceTokenValue);

// when & then
assertThatThrownBy(() -> deviceTokenService.persist(존재하지_않는_사용자_아이디, 디바이스_토큰_저장을_위한_DTO))
assertThatThrownBy(() -> deviceTokenService.persist(invalidUserId, persistDeviceTokenDto))
.isInstanceOf(UserNotFoundException.class)
.hasMessage("해당 사용자를 찾을 수 없습니다.");
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package com.ddang.ddang.device.domain;

import com.ddang.ddang.device.domain.fixture.DeviceTokenFixture;
import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.user.domain.User;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

@SuppressWarnings("NonAsciiCharacters")
class DeviceTokenTest extends DeviceTokenFixture {

class DeviceTokenTest {

@Test
void 디바이스_토큰이_다르다면_참을_반환한다() {
// given
final DeviceToken deviceToken = new DeviceToken(사용자, 디바이스_토큰);
final String deviceTokenValue = "deviceToken";
final User user = User.builder()
.name("사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
final DeviceToken deviceToken = new DeviceToken(user, deviceTokenValue);

final String targetDeviceTokenValue = "differentDeviceToken";

// when
final boolean actual = deviceToken.isDifferentToken(새로운_디바이스_토큰);
final boolean actual = deviceToken.isDifferentToken(targetDeviceTokenValue);

// then
assertThat(actual).isTrue();
Expand All @@ -24,10 +32,19 @@ class DeviceTokenTest extends DeviceTokenFixture {
@Test
void 디바이스_토큰이_같다면_거짓을_반환한다() {
// given
final DeviceToken deviceToken = new DeviceToken(사용자, 디바이스_토큰);
final String deviceTokenValue = "deviceToken";
final User user = User.builder()
.name("사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
final DeviceToken deviceToken = new DeviceToken(user, deviceTokenValue);

final String targetDeviceTokenValue = deviceTokenValue;

// when
final boolean actual = deviceToken.isDifferentToken(디바이스_토큰);
final boolean actual = deviceToken.isDifferentToken(targetDeviceTokenValue);

// then
assertThat(actual).isFalse();
Expand All @@ -36,12 +53,21 @@ class DeviceTokenTest extends DeviceTokenFixture {
@Test
void 디바이스_토큰을_갱신한다() {
// given
final DeviceToken deviceToken = new DeviceToken(사용자, 디바이스_토큰);
final String deviceTokenValue = "deviceToken";
final User user = User.builder()
.name("사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
final DeviceToken deviceToken = new DeviceToken(user, deviceTokenValue);

final String newDeviceTokenValue = "newDeviceToken";

// when
deviceToken.updateDeviceToken(새로운_디바이스_토큰);
deviceToken.updateDeviceToken(newDeviceTokenValue);

// then
assertThat(deviceToken.getDeviceToken()).isEqualTo(새로운_디바이스_토큰);
assertThat(deviceToken.getDeviceToken()).isEqualTo(newDeviceTokenValue);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import com.ddang.ddang.configuration.JpaConfiguration;
import com.ddang.ddang.configuration.QuerydslConfiguration;
import com.ddang.ddang.device.domain.DeviceToken;
import com.ddang.ddang.device.infrastructure.persistence.fixture.JpaDeviceTokenRepositoryFixture;
import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.infrastructure.persistence.JpaUserRepository;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
Expand All @@ -16,20 +20,43 @@
import static org.assertj.core.api.Assertions.assertThat;

@DataJpaTest
@Import({JpaConfiguration.class, QuerydslConfiguration.class})
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class JpaDeviceTokenRepositoryTest extends JpaDeviceTokenRepositoryFixture {
@Import({JpaConfiguration.class, QuerydslConfiguration.class})
class JpaDeviceTokenRepositoryTest {

@PersistenceContext
EntityManager em;

@Autowired
JpaUserRepository userRepository;

@Autowired
JpaDeviceTokenRepository userDeviceTokenRepository;

@Test
void 주어진_사용자_아이디에_해당하는_기기토큰을_조회한다() {
// given
final String deviceToken = "token1234";

final User user = User.builder()
.name("사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
userRepository.save(user);

final DeviceToken expect = new DeviceToken(user, deviceToken);
userDeviceTokenRepository.save(expect);

em.flush();
em.clear();

// when
final Optional<DeviceToken> actual = userDeviceTokenRepository.findByUserId(사용자.getId());
final Optional<DeviceToken> actual = userDeviceTokenRepository.findByUserId(user.getId());

// then
assertThat(actual).contains(사용자의_디바이스_토큰);
assertThat(actual).contains(expect);
}
}
Loading

0 comments on commit ffc2e66

Please sign in to comment.