Skip to content

Commit

Permalink
refactor: #690 Device 인프라 리팩토링 (#696)
Browse files Browse the repository at this point in the history
* feat: 도메인 영역 기기토큰 레포지토리 추가

* feat: 도메인 기기토큰 레포지토리의 구현체 추가

* feat: 기기토큰 레포지토리 메서드 중 누락된 메서드 추가

* refactor: 서비스 레이어에서 도메인 영역 기기토큰 레포지토리 사용하도록 변경

* refactor: 누락된 final 추가

* test: 테스트에서 도메인 영역의 기기토큰 레포지토리 사용하도록 변경

* refactor: 레포지토리명에 맞춰 변수명 변경

* refactor: 테스트 픽스처에서 도메인 영역의 사용자 레포지토리 사용하도록 변경

* test: 기기토큰 레포지토리 구현체 테스트 추가

* chore: 질문에 대한 todo 주석 추가

* style: 개행 추가

* refactor: jpaQueryFactory 파라미터명 통일

* refactor: 필드 순서 변경

* refactor: given절 추가

* refactor: jpa 레포지토리 저장 테스트 메서드에서 엔티티 매니저 사용하는 코드 삭제

* refactor: 불필요한 필드 삭제

* refactor: 테스트에서 도메인 레포지토리 사용하도록 변경

* refactor: 불필요한 todo 삭제
  • Loading branch information
swonny authored Oct 15, 2023
1 parent a58f961 commit 7507116
Show file tree
Hide file tree
Showing 36 changed files with 293 additions and 128 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.ddang.ddang.authentication.application;

import static com.ddang.ddang.image.domain.ProfileImage.DEFAULT_PROFILE_IMAGE_STORE_NAME;

import com.ddang.ddang.authentication.application.dto.LoginInformationDto;
import com.ddang.ddang.authentication.application.dto.LoginUserInformationDto;
import com.ddang.ddang.authentication.application.dto.TokenDto;
Expand All @@ -18,20 +16,23 @@
import com.ddang.ddang.authentication.infrastructure.oauth2.Oauth2Type;
import com.ddang.ddang.device.application.DeviceTokenService;
import com.ddang.ddang.device.application.dto.PersistDeviceTokenDto;
import com.ddang.ddang.device.infrastructure.persistence.JpaDeviceTokenRepository;
import com.ddang.ddang.device.domain.repository.DeviceTokenRepository;
import com.ddang.ddang.image.application.exception.ImageNotFoundException;
import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.image.domain.repository.ProfileImageRepository;
import com.ddang.ddang.user.domain.Reliability;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.domain.repository.UserRepository;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.ddang.ddang.image.domain.ProfileImage.DEFAULT_PROFILE_IMAGE_STORE_NAME;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
Expand All @@ -47,7 +48,7 @@ public class AuthenticationService {
private final TokenEncoder tokenEncoder;
private final TokenDecoder tokenDecoder;
private final BlackListTokenService blackListTokenService;
private final JpaDeviceTokenRepository deviceTokenRepository;
private final DeviceTokenRepository deviceTokenRepository;

@Transactional
public LoginInformationDto login(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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.device.domain.repository.DeviceTokenRepository;
import com.ddang.ddang.user.application.exception.UserNotFoundException;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.domain.repository.UserRepository;
Expand All @@ -15,7 +15,7 @@
@RequiredArgsConstructor
public class DeviceTokenService {

private final JpaDeviceTokenRepository deviceTokenRepository;
private final DeviceTokenRepository deviceTokenRepository;
private final UserRepository userRepository;

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ddang.ddang.device.domain.repository;

import com.ddang.ddang.device.domain.DeviceToken;

import java.util.Optional;

public interface DeviceTokenRepository {

DeviceToken save(final DeviceToken deviceToken);

Optional<DeviceToken> findByUserId(final Long userId);

void deleteByUserId(final Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.ddang.ddang.device.infrastructure.persistence;

import com.ddang.ddang.device.domain.DeviceToken;
import com.ddang.ddang.device.domain.repository.DeviceTokenRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
@RequiredArgsConstructor
public class DeviceTokenRepositoryImpl implements DeviceTokenRepository {

private final JpaDeviceTokenRepository jpaDeviceTokenRepository;

@Override
public DeviceToken save(final DeviceToken deviceToken) {
return jpaDeviceTokenRepository.save(deviceToken);
}

@Override
public Optional<DeviceToken> findByUserId(final Long userId) {
return jpaDeviceTokenRepository.findByUserId(userId);
}

@Override
public void deleteByUserId(final Long id) {
jpaDeviceTokenRepository.deleteByUserId(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public interface JpaDeviceTokenRepository extends JpaRepository<DeviceToken, Lon

Optional<DeviceToken> findByUserId(final Long userId);

void deleteByUserId(Long id);
void deleteByUserId(final Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.ddang.ddang.device.application.exception.DeviceTokenNotFoundException;
import com.ddang.ddang.device.domain.DeviceToken;
import com.ddang.ddang.device.infrastructure.persistence.JpaDeviceTokenRepository;
import com.ddang.ddang.device.domain.repository.DeviceTokenRepository;
import com.ddang.ddang.notification.application.dto.CreateNotificationDto;
import com.ddang.ddang.notification.domain.NotificationStatus;
import com.google.firebase.messaging.AndroidConfig;
Expand All @@ -27,7 +27,7 @@
public class FcmNotificationService implements NotificationService {

private final FirebaseMessaging firebaseMessaging;
private final JpaDeviceTokenRepository deviceTokenRepository;
private final DeviceTokenRepository deviceTokenRepository;

@Override
public NotificationStatus send(final CreateNotificationDto createNotificationDto) throws FirebaseMessagingException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.ddang.ddang.auction.infrastructure.persistence;

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

import com.ddang.ddang.auction.domain.Auction;
import com.ddang.ddang.auction.domain.repository.AuctionRepository;
import com.ddang.ddang.auction.infrastructure.persistence.fixture.AuctionRepositoryImplFixture;
import com.ddang.ddang.configuration.JpaConfiguration;
import com.ddang.ddang.configuration.QuerydslConfiguration;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.Optional;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
Expand All @@ -20,6 +17,10 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;

import java.util.Optional;

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

@DataJpaTest
@Import({JpaConfiguration.class, QuerydslConfiguration.class})
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
Expand All @@ -29,16 +30,13 @@ class AuctionRepositoryImplTest extends AuctionRepositoryImplFixture {
@Autowired
JpaAuctionRepository jpaAuctionRepository;

@Autowired
JPAQueryFactory queryFactory;

AuctionRepository auctionRepository;

@BeforeEach
void setUp() {
void setUp(@Autowired final JPAQueryFactory jpaQueryFactory) {
auctionRepository = new AuctionRepositoryImpl(
jpaAuctionRepository,
new QuerydslAuctionRepository(queryFactory)
new QuerydslAuctionRepository(jpaQueryFactory)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class JpaAuctionRepositoryTest extends JpaAuctionRepositoryFixture {
// when
final Auction actual = auctionRepository.save(저장하기_전_경매_엔티티);

// then
assertThat(actual.getId()).isPositive();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class QuerydslAuctionAndImageRepositoryTest extends QuerydslAuctionAndImageRepos
QuerydslAuctionAndImageRepository querydslAuctionAndImageRepository;

@BeforeEach
void setUp(@Autowired final JPAQueryFactory queryFactory) {
querydslAuctionAndImageRepository = new QuerydslAuctionAndImageRepository(queryFactory);
void setUp(@Autowired final JPAQueryFactory jpaQueryFactory) {
querydslAuctionAndImageRepository = new QuerydslAuctionAndImageRepository(jpaQueryFactory);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
@SuppressWarnings("NonAsciiCharacters")
public class AuctionRepositoryImplFixture {

@Autowired
private JpaCategoryRepository categoryRepository;

private AuctionRepository auctionRepository;

private UserRepository userRepository;

private RegionRepository regionRepository;

@Autowired
private JpaCategoryRepository categoryRepository;

private BidRepository bidRepository;

private Instant 시간 = Instant.parse("2023-07-08T22:21:20Z");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,47 @@
import com.ddang.ddang.image.infrastructure.persistence.JpaAuctionImageRepository;
import com.ddang.ddang.user.domain.Reliability;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.domain.repository.UserRepository;
import com.ddang.ddang.user.infrastructure.persistence.JpaUserRepository;
import com.ddang.ddang.user.infrastructure.persistence.UserRepositoryImpl;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import java.time.LocalDateTime;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@SuppressWarnings("NonAsciiCharacters")
public class QuerydslAuctionAndImageRepositoryFixture {

@PersistenceContext
private EntityManager em;

@Autowired
private JPAQueryFactory queryFactory;

@Autowired
private JpaAuctionRepository jpaAuctionRepository;

@Autowired
private JpaAuctionImageRepository auctionImageRepository;

private UserRepository userRepository;

@Autowired
private JpaUserRepository userRepository;
private AuctionRepository auctionRepository;

private User 사용자;
protected Auction 경매;
protected AuctionImage 경매_이미지;

@BeforeEach
void setUp() {
void fixtureSetUp(
@Autowired JPAQueryFactory jpaQueryFactory,
@Autowired JpaAuctionRepository jpaAuctionRepository,
@Autowired JpaUserRepository jpaUserRepository
) {
auctionRepository = new AuctionRepositoryImpl(
jpaAuctionRepository,
new QuerydslAuctionRepository(jpaQueryFactory)
);
userRepository = new UserRepositoryImpl(jpaUserRepository);

경매 = Auction.builder()
.title("경매 상품 1")
.description("이것은 경매 상품 1 입니다.")
Expand All @@ -63,10 +71,6 @@ void setUp() {
.oauthId("12345")
.build();

final AuctionRepository auctionRepository = new AuctionRepositoryImpl(
jpaAuctionRepository,
new QuerydslAuctionRepository(queryFactory)
);
auctionRepository.save(경매);
userRepository.save(사용자);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package com.ddang.ddang.authentication.application;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;

import com.ddang.ddang.authentication.application.dto.LoginInformationDto;
import com.ddang.ddang.authentication.application.dto.TokenDto;
import com.ddang.ddang.authentication.application.exception.InvalidWithdrawalException;
Expand All @@ -17,6 +12,8 @@
import com.ddang.ddang.authentication.infrastructure.oauth2.OAuth2UserInformationProvider;
import com.ddang.ddang.configuration.IsolateDatabase;
import com.ddang.ddang.device.application.DeviceTokenService;
import com.ddang.ddang.device.domain.repository.DeviceTokenRepository;
import com.ddang.ddang.device.infrastructure.persistence.DeviceTokenRepositoryImpl;
import com.ddang.ddang.device.infrastructure.persistence.JpaDeviceTokenRepository;
import com.ddang.ddang.image.application.exception.ImageNotFoundException;
import com.ddang.ddang.image.domain.repository.ProfileImageRepository;
Expand All @@ -32,6 +29,11 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;

@IsolateDatabase
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
Expand Down Expand Up @@ -63,15 +65,19 @@ class AuthenticationServiceTest extends AuthenticationServiceFixture {
@Autowired
BlackListTokenService blackListTokenService;

@Autowired
JpaDeviceTokenRepository deviceTokenRepository;
DeviceTokenRepository deviceTokenRepository;

AuthenticationService authenticationService;

AuthenticationService profileImageNotFoundAuthenticationService;

@BeforeEach
void fixtureSetUp(@Autowired final JpaProfileImageRepository jpaProfileImageRepository) {
void fixtureSetUp(
@Autowired final JpaProfileImageRepository jpaProfileImageRepository,
@Autowired final JpaDeviceTokenRepository jpaDeviceTokenRepository
) {
profileImageRepository = new ProfileImageRepositoryImpl(jpaProfileImageRepository);
deviceTokenRepository = new DeviceTokenRepositoryImpl(jpaDeviceTokenRepository);

authenticationService = new AuthenticationService(
deviceTokenService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.ddang.ddang.authentication.infrastructure.jwt.PrivateClaims;
import com.ddang.ddang.authentication.infrastructure.oauth2.Oauth2Type;
import com.ddang.ddang.device.domain.DeviceToken;
import com.ddang.ddang.device.domain.repository.DeviceTokenRepository;
import com.ddang.ddang.device.infrastructure.persistence.DeviceTokenRepositoryImpl;
import com.ddang.ddang.device.infrastructure.persistence.JpaDeviceTokenRepository;
import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.image.infrastructure.persistence.JpaProfileImageRepository;
Expand Down Expand Up @@ -58,11 +60,12 @@ public class AuthenticationServiceFixture {
@Autowired
private TokenEncoder tokenEncoder;

@Autowired
private JpaDeviceTokenRepository deviceTokenRepository;
private DeviceTokenRepository deviceTokenRepository;

@BeforeEach
void fixtureSetUp() {
void fixtureSetUp(@Autowired final JpaDeviceTokenRepository jpaDeviceTokenRepository) {
deviceTokenRepository = new DeviceTokenRepositoryImpl(jpaDeviceTokenRepository);

profileImageRepository.save(new ProfileImage("default_profile_image.png", "default_profile_image.png"));

사용자 = User.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
@SuppressWarnings("NonAsciiCharacters")
class JpaBidRepositoryTest extends JpaBidRepositoryFixture {

@PersistenceContext
EntityManager em;

@Autowired
JpaBidRepository bidRepository;

Expand All @@ -40,9 +37,6 @@ class JpaBidRepositoryTest extends JpaBidRepositoryFixture {
final Bid actual = bidRepository.save(bid);

// then
em.flush();
em.clear();

assertThat(actual.getId()).isPositive();
}

Expand Down
Loading

0 comments on commit 7507116

Please sign in to comment.