Skip to content

Commit

Permalink
[TEST] repository stub 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Mar 14, 2024
1 parent 079ace0 commit e5ee9dc
Show file tree
Hide file tree
Showing 60 changed files with 728 additions and 421 deletions.
4 changes: 3 additions & 1 deletion backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ dependencies {
implementation 'org.flywaydb:flyway-core'
implementation 'org.flywaydb:flyway-mysql'

implementation 'com.github.f4b6a3:ulid-creator:5.2.3'
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
implementation 'org.springframework.cloud:spring-cloud-gcp-starter:1.2.5.RELEASE'
implementation 'org.springframework.cloud:spring-cloud-gcp-storage:1.2.5.RELEASE'
Expand All @@ -72,7 +73,7 @@ dependencies {

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
testRuntimeOnly 'com.h2database:h2'
testRuntimeOnly 'com.mysql:mysql-connector-j'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
Expand All @@ -91,6 +92,7 @@ dependencies {
testImplementation "org.testcontainers:junit-jupiter:1.19.7"
testImplementation "com.redis:testcontainers-redis:2.0.1"
testImplementation "org.testcontainers:rabbitmq:1.19.7"
testImplementation "org.testcontainers:mysql:1.17.2"
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
}
Expand Down
37 changes: 0 additions & 37 deletions backend/src/main/java/com/twtw/backend/domain/TestController.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,45 +1,29 @@
package com.twtw.backend.domain.friend.entity;

import com.github.f4b6a3.ulid.UlidCreator;
import com.twtw.backend.domain.friend.exception.InvalidFriendMemberException;
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.global.audit.AuditListener;
import com.twtw.backend.global.audit.Auditable;
import com.twtw.backend.global.audit.BaseTime;

import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.Where;

import java.time.LocalDateTime;
import java.util.UUID;

@Getter
@Entity
@EqualsAndHashCode(of = "id")
@Where(clause = "deleted_at is null and friend_status != 'EXPIRED'")
@EntityListeners(AuditListener.class)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Friend implements Auditable {

@Id
@GeneratedValue(generator = "uuid2")
@Column(name = "id", columnDefinition = "BINARY(16)")
private UUID id;
@Column(columnDefinition = "BINARY(16)")
private UUID id = UlidCreator.getMonotonicUlid().toUuid();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(columnDefinition = "BINARY(16)")
Expand Down Expand Up @@ -104,4 +88,8 @@ private boolean isRequestNotExpired() {
.getCreatedAt()
.isAfter(LocalDateTime.now().minusMinutes(30L)));
}

public boolean nicknameContains(final String nickname) {
return this.fromMember.nicknameContains(nickname) || this.toMember.nicknameContains(nickname);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.twtw.backend.domain.friend.repository;

import com.twtw.backend.domain.friend.entity.Friend;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

@Repository
public interface FriendCommandRepository {
Friend save(final Friend friend);
List<Friend> findByMemberAndMemberNickname(final UUID memberId, final String nickname);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.twtw.backend.domain.friend.repository;

import com.twtw.backend.domain.friend.entity.Friend;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

@Repository
public interface FriendCommandRepositoryImpl extends JpaRepository<Friend, UUID>, FriendCommandRepository {

@Query(value = "SELECT f.* FROM friend f JOIN member fm ON f.from_member_id = fm.id JOIN member tm ON f.to_member_id = tm.id WHERE f.friend_status = 'ACCEPTED' AND ((f.to_member_id = UNHEX(REPLACE(:memberId, '-', '')) AND MATCH(fm.nickname) AGAINST(:nickname IN BOOLEAN MODE)) OR (f.from_member_id = UNHEX(REPLACE(:memberId, '-', '')) AND MATCH(tm.nickname) AGAINST(:nickname IN BOOLEAN MODE)))", nativeQuery = true)
List<Friend> findByMemberAndMemberNickname(@Param("memberId") final UUID memberId, @Param("nickname") final String nickname);
Friend save(final Friend friend);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import com.twtw.backend.domain.friend.entity.Friend;
import com.twtw.backend.domain.friend.entity.FriendStatus;
import com.twtw.backend.domain.member.entity.Member;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Repository
public interface FriendQueryRepository {
Optional<Friend> findByTwoMemberId(final UUID loginMemberId, final UUID memberId);

List<Friend> findByMember(final Member member);

List<Friend> findByMemberAndFriendStatus(final Member member, final FriendStatus friendStatus);

List<Friend> findByMemberAndMemberNicknameContaining(final UUID memberId, final String nickname);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,15 @@ public List<Friend> findByMemberAndFriendStatus(
.and(friend.friendStatus.eq(friendStatus)))
.fetch();
}

@Override
public List<Friend> findByMemberAndMemberNicknameContaining(final UUID memberId, final String nickname) {
return jpaQueryFactory.selectFrom(friend)
.where(
(friend.friendStatus.eq(FriendStatus.ACCEPTED))
.and
(friend.toMember.id.eq(memberId).and(friend.fromMember.nickname.contains(nickname)))
.or(friend.fromMember.id.eq(memberId).and(friend.toMember.nickname.contains(nickname))))
.fetch();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import com.twtw.backend.domain.friend.entity.Friend;
import com.twtw.backend.domain.friend.entity.FriendStatus;
import com.twtw.backend.domain.friend.mapper.FriendMapper;
import com.twtw.backend.domain.friend.repository.FriendRepository;
import com.twtw.backend.domain.friend.repository.FriendCommandRepository;
import com.twtw.backend.domain.friend.repository.FriendQueryRepository;
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.domain.member.service.AuthService;
import com.twtw.backend.domain.member.service.MemberService;
Expand All @@ -15,7 +16,7 @@
import com.twtw.backend.global.constant.NotificationBody;
import com.twtw.backend.global.constant.NotificationTitle;
import com.twtw.backend.global.exception.EntityNotFoundException;
import com.twtw.backend.utils.StringParseUtils;
import com.twtw.backend.utils.QueryParseUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
Expand All @@ -28,7 +29,8 @@
@Service
@RequiredArgsConstructor
public class FriendService {
private final FriendRepository friendRepository;
private final FriendQueryRepository friendQueryRepository;
private final FriendCommandRepository friendCommandRepository;
private final FriendMapper friendMapper;
private final MemberService memberService;
private final AuthService authService;
Expand All @@ -54,7 +56,7 @@ private void createFriendRequestByNicknameOrder(final Member loginMember, final
}

public void createFriendRequest(final Member fromMember, final Member toMember) {
friendRepository.save(friendMapper.toEntity(fromMember, toMember));
friendCommandRepository.save(friendMapper.toEntity(fromMember, toMember));
}

private void sendNotification(final String deviceToken, final String nickname, final UUID id) {
Expand All @@ -75,7 +77,7 @@ public void updateStatus(final FriendUpdateRequest friendUpdateRequest) {
}

private Friend getFriendById(final UUID loginMemberId, final UUID memberId) {
return friendRepository
return friendQueryRepository
.findByTwoMemberId(loginMemberId, memberId)
.orElseThrow(EntityNotFoundException::new);
}
Expand Down Expand Up @@ -106,7 +108,7 @@ public List<FriendResponse> getFriendsWithCache() {
private List<FriendResponse> getFriendResponses() {
final Member loginMember = authService.getMemberByJwt();
final List<Member> friends =
friendRepository.findByMember(loginMember).stream()
friendQueryRepository.findByMember(loginMember).stream()
.map(friend -> friend.getFriendMember(loginMember))
.toList();

Expand Down Expand Up @@ -137,7 +139,7 @@ public List<FriendResponse> getFriendsByStatusWithCache(final FriendStatus frien
private List<FriendResponse> getFriendResponsesByStatus(final FriendStatus friendStatus) {
final Member loginMember = authService.getMemberByJwt();
final List<Member> friends =
friendRepository.findByMemberAndFriendStatus(loginMember, friendStatus).stream()
friendQueryRepository.findByMemberAndFriendStatus(loginMember, friendStatus).stream()
.map(friend -> friend.getFriendMember(loginMember))
.toList();

Expand Down Expand Up @@ -165,11 +167,20 @@ public List<FriendResponse> getFriendByNicknameWithCache(final String nickname)

private List<FriendResponse> getFriendResponsesByNickname(final String nickname) {
final Member loginMember = authService.getMemberByJwt();
final List<Member> friends =
friendRepository.findByMemberAndMemberNickname(loginMember, StringParseUtils.parse(nickname)).stream()
.map(friend -> friend.getFriendMember(loginMember))
.toList();
final List<Member> friends = findFriendsByNickname(loginMember, nickname);

return friendMapper.toResponses(friends);
}

private List<Member> findFriendsByNickname(final Member loginMember, final String nickname) {
if (nickname.length() < 2) {
return friendQueryRepository.findByMemberAndMemberNicknameContaining(loginMember.getId(), nickname).stream()
.map(friend -> friend.getFriendMember(loginMember))
.toList();
}
return friendCommandRepository.findByMemberAndMemberNickname(loginMember.getId(), QueryParseUtils.parse(nickname)).stream()
.filter(friend -> friend.nicknameContains(nickname))
.map(friend -> friend.getFriendMember(loginMember))
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
package com.twtw.backend.domain.group.entity;

import com.github.f4b6a3.ulid.UlidCreator;
import com.twtw.backend.domain.group.exception.IllegalGroupMemberException;
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.domain.plan.entity.Plan;
import com.twtw.backend.global.audit.AuditListener;
import com.twtw.backend.global.audit.Auditable;
import com.twtw.backend.global.audit.BaseTime;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;

import jakarta.persistence.*;
import lombok.*;

import org.hibernate.annotations.Where;

import java.util.ArrayList;
Expand All @@ -30,10 +21,10 @@
@EntityListeners(AuditListener.class)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Group implements Auditable {

@Id
@GeneratedValue(generator = "uuid2")
@Column(name = "id", columnDefinition = "BINARY(16)")
private UUID id;
@Column(columnDefinition = "BINARY(16)")
private UUID id = UlidCreator.getMonotonicUlid().toUuid();

private String name;
private String groupImage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
package com.twtw.backend.domain.group.entity;

import com.github.f4b6a3.ulid.UlidCreator;
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.domain.place.entity.Coordinate;
import com.twtw.backend.global.audit.AuditListener;
import com.twtw.backend.global.audit.Auditable;
import com.twtw.backend.global.audit.BaseTime;

import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.hibernate.annotations.Where;

import java.time.LocalDateTime;
Expand All @@ -35,9 +24,8 @@
public class GroupMember implements Auditable {

@Id
@GeneratedValue(generator = "uuid2")
@Column(name = "id", columnDefinition = "BINARY(16)")
private UUID id;
@Column(columnDefinition = "BINARY(16)")
private UUID id = UlidCreator.getMonotonicUlid().toUuid();

@ManyToOne
@JoinColumn(name = "group_id")
Expand Down
Loading

0 comments on commit e5ee9dc

Please sign in to comment.