Skip to content

Commit

Permalink
refactor: #480 디바이스 토큰 관련 테스트 코드 리팩토링 (#491)
Browse files Browse the repository at this point in the history
* test: DeviceTokenTest Fixture 추가 및 테스트 케이스 리팩토링

* test: JpaDeviceTokenRepositoryTest Fixture 추가 및 테스트 케이스 리팩토링

* test: DeviceTokenServiceTest Fixture 추가 및 테스트 케이스 리팩토링

* test: DeviceTokenControllerTest Fixture 추가 및 테스트 케이스 리팩토링

* test: 모든 픽스처 객체를 `@BeforeEach`로 세팅하도록 변경

* test: 테스트 픽스처 추가, 네이밍 변경, 코드 스타일 개선

* style: 불필요한 공백 제거

* rename: 테스트하려는 결과의 변수명을 actual로 통일
  • Loading branch information
kwonyj1022 authored Oct 2, 2023
1 parent 9920747 commit 5a4a81e
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 198 deletions.
Original file line number Diff line number Diff line change
@@ -1,113 +1,68 @@
package com.ddang.ddang.device.application;

import com.ddang.ddang.configuration.IsolateDatabase;
import com.ddang.ddang.device.application.dto.PersistDeviceTokenDto;
import com.ddang.ddang.device.application.fixture.DeviceTokenServiceFixture;
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 {
class DeviceTokenServiceTest extends DeviceTokenServiceFixture {

@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(user.getId(), persistDeviceTokenDto));
assertThatNoException().isThrownBy(
() -> deviceTokenService.persist(디바이스_토큰이_없는_사용자.getId(), 디바이스_토큰_저장을_위한_DTO)
);
}

@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(user.getId(), persistDeviceTokenDto);
deviceTokenService.persist(디바이스_토큰이_있는_사용자.getId(), 디바이스_토큰_갱신을_위한_DTO);

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

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

@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(user.getId(), persistDeviceTokenDto);
deviceTokenService.persist(디바이스_토큰이_있는_사용자.getId(), 존재하는_디바이스_토큰과_동일한_토큰을_저장하려는_DTO);

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

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

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

// when & then
assertThatThrownBy(() -> deviceTokenService.persist(invalidUserId, persistDeviceTokenDto))
assertThatThrownBy(() -> deviceTokenService.persist(존재하지_않는_사용자_아이디, 디바이스_토큰_저장을_위한_DTO))
.isInstanceOf(UserNotFoundException.class)
.hasMessage("해당 사용자를 찾을 수 없습니다.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.ddang.ddang.device.application.fixture;

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.domain.User;
import com.ddang.ddang.user.infrastructure.persistence.JpaUserRepository;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@SuppressWarnings("NonAsciiCharacters")
public class DeviceTokenServiceFixture {

@Autowired
private JpaDeviceTokenRepository deviceTokenRepository;

@Autowired
private JpaUserRepository userRepository;
private String 초기_디바이스_토큰_값 = "initialDeviceToken";
private DeviceToken 사용자의_디바이스_토큰;

protected String 사용_중인_디바이스_토큰_값 = "usingDeviceToken";
protected String 갱신된_디바이스_토큰_값 = "newDeviceToken";
protected Long 존재하지_않는_사용자_아이디 = -999L;
protected User 디바이스_토큰이_있는_사용자;
protected User 디바이스_토큰이_없는_사용자;
protected PersistDeviceTokenDto 디바이스_토큰_저장을_위한_DTO;
protected PersistDeviceTokenDto 디바이스_토큰_갱신을_위한_DTO;
protected PersistDeviceTokenDto 존재하는_디바이스_토큰과_동일한_토큰을_저장하려는_DTO;

@BeforeEach
void setUp() {
디바이스_토큰이_있는_사용자 = User.builder()
.name("디바이스 토큰이 있는 사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
디바이스_토큰이_없는_사용자 = User.builder()
.name("디바이스 토큰이 없는 사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12346")
.build();
사용자의_디바이스_토큰 = new DeviceToken(디바이스_토큰이_있는_사용자, 사용_중인_디바이스_토큰_값);
userRepository.saveAll(List.of(디바이스_토큰이_있는_사용자, 디바이스_토큰이_없는_사용자));
deviceTokenRepository.save(사용자의_디바이스_토큰);

디바이스_토큰_저장을_위한_DTO = new PersistDeviceTokenDto(초기_디바이스_토큰_값);
디바이스_토큰_갱신을_위한_DTO = new PersistDeviceTokenDto(갱신된_디바이스_토큰_값);
존재하는_디바이스_토큰과_동일한_토큰을_저장하려는_DTO = new PersistDeviceTokenDto(사용_중인_디바이스_토큰_값);
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
package com.ddang.ddang.device.domain;

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

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

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


@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();
final DeviceToken deviceToken = new DeviceToken(user, deviceTokenValue);

final String targetDeviceTokenValue = "differentDeviceToken";
final DeviceToken deviceToken = new DeviceToken(사용자, 디바이스_토큰);

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

// then
assertThat(actual).isTrue();
Expand All @@ -32,19 +24,10 @@ class DeviceTokenTest {
@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();
final DeviceToken deviceToken = new DeviceToken(user, deviceTokenValue);

final String targetDeviceTokenValue = deviceTokenValue;
final DeviceToken deviceToken = new DeviceToken(사용자, 디바이스_토큰);

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

// then
assertThat(actual).isFalse();
Expand All @@ -53,21 +36,12 @@ class DeviceTokenTest {
@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();
final DeviceToken deviceToken = new DeviceToken(user, deviceTokenValue);

final String newDeviceTokenValue = "newDeviceToken";
final DeviceToken deviceToken = new DeviceToken(사용자, 디바이스_토큰);

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

// then
assertThat(deviceToken.getDeviceToken()).isEqualTo(newDeviceTokenValue);
assertThat(deviceToken.getDeviceToken()).isEqualTo(새로운_디바이스_토큰);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.ddang.ddang.device.domain.fixture;

import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.user.domain.User;
import org.junit.jupiter.api.BeforeEach;

@SuppressWarnings("NonAsciiCharacters")
public class DeviceTokenFixture {

protected String 디바이스_토큰 = "deviceToken";
protected String 새로운_디바이스_토큰 = "newDeviceToken";
protected User 사용자;

@BeforeEach
void setUp() {
사용자 = User.builder()
.name("사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import com.ddang.ddang.configuration.JpaConfiguration;
import com.ddang.ddang.configuration.QuerydslConfiguration;
import com.ddang.ddang.device.domain.DeviceToken;
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 com.ddang.ddang.device.infrastructure.persistence.fixture.JpaDeviceTokenRepositoryFixture;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
Expand All @@ -20,43 +16,20 @@
import static org.assertj.core.api.Assertions.assertThat;

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

@PersistenceContext
EntityManager em;

@Autowired
JpaUserRepository userRepository;
class JpaDeviceTokenRepositoryTest extends JpaDeviceTokenRepositoryFixture {

@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(user.getId());
final Optional<DeviceToken> actual = userDeviceTokenRepository.findByUserId(사용자.getId());

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

0 comments on commit 5a4a81e

Please sign in to comment.