Skip to content

Commit

Permalink
feat: 콕 찌르기 여부를 확인하는 기능 구현 및 테스트 (#53)
Browse files Browse the repository at this point in the history
* feat: 콕 찌르기 여부를 확인하는 기능 구현

* test: 콕 찌르기 여부를 확인하는 기능 테스트

* test: 콕 찌르기 여부를 확인하는 기능 테스트
  • Loading branch information
hongdosan authored Nov 8, 2023
1 parent c40b8bb commit 51ccfaa
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/moabam/api/application/NotificationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
Expand All @@ -15,6 +18,7 @@
import com.moabam.api.domain.entity.Participant;
import com.moabam.api.domain.repository.NotificationRepository;
import com.moabam.api.domain.repository.ParticipantSearchRepository;
import com.moabam.api.dto.KnockNotificationStatusResponse;
import com.moabam.api.dto.NotificationMapper;
import com.moabam.global.common.annotation.MemberTest;
import com.moabam.global.error.exception.ConflictException;
Expand Down Expand Up @@ -58,6 +62,24 @@ public void sendCertificationTimeNotification() {
});
}

/**
* TODO : 영명-재윤님 방 조회하실 때, 특정 사용자의 방 내 참여자들에 대한 콕 찌르기 여부를 반환해주는 메서드이니 사용하시기 바랍니다.
*/
public KnockNotificationStatusResponse checkMyKnockNotificationStatusInRoom(MemberTest member, Long roomId) {
List<Participant> participants = participantSearchRepository.findOtherParticipantsInRoom(member.memberId(),
roomId);

Predicate<Long> knockPredicate = targetId ->
notificationRepository.existsByKey(generateKnockKey(member.memberId(), targetId, roomId));

Map<Boolean, List<Long>> knockNotificationStatus = participants.stream()
.map(Participant::getMemberId)
.collect(Collectors.partitioningBy(knockPredicate));

return NotificationMapper
.toKnockNotificationStatusResponse(knockNotificationStatus.get(true), knockNotificationStatus.get(false));
}

private void sendAsyncFcm(Long fcmTokenKey, Notification notification) {
String fcmToken = notificationRepository.findFcmTokenByMemberId(fcmTokenKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public List<Participant> findParticipants(Long roomId) {
.fetch();
}

public List<Participant> findOtherParticipantsInRoom(Long memberId, Long roomId) {
return jpaQueryFactory
.selectFrom(participant)
.where(
participant.room.id.eq(roomId),
participant.memberId.ne(memberId)
)
.fetch();
}

public List<Participant> findAllByRoomCertifyTime(int certifyTime) {
return jpaQueryFactory
.selectFrom(participant)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.moabam.api.dto;

import java.util.List;

import lombok.Builder;

@Builder
public record KnockNotificationStatusResponse(
List<Long> knockedMembersId,
List<Long> notKnockedMembersId
) {

}
12 changes: 12 additions & 0 deletions src/main/java/com/moabam/api/dto/NotificationMapper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.moabam.api.dto;

import java.util.List;

import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;

Expand Down Expand Up @@ -33,4 +35,14 @@ public static Message toMessageEntity(Notification notification, String fcmToken
.setToken(fcmToken)
.build();
}

public static KnockNotificationStatusResponse toKnockNotificationStatusResponse(
List<Long> knockedMembersId,
List<Long> notKnockedMembersId
) {
return KnockNotificationStatusResponse.builder()
.knockedMembersId(knockedMembersId)
.notKnockedMembersId(notKnockedMembersId)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.moabam.api.domain.entity.Participant;
import com.moabam.api.domain.repository.NotificationRepository;
import com.moabam.api.domain.repository.ParticipantSearchRepository;
import com.moabam.api.dto.KnockNotificationStatusResponse;
import com.moabam.global.common.annotation.MemberTest;
import com.moabam.global.error.exception.ConflictException;
import com.moabam.global.error.exception.NotFoundException;
Expand Down Expand Up @@ -134,4 +135,40 @@ void notificationService_sendCertificationTimeNotification_NoFirebaseMessaging(L
// Then
verify(firebaseMessaging, times(0)).sendAsync(any(Message.class));
}

@DisplayName("특정 방에서 나 이외의 모든 사용자를 콕 찔렀을 때, - KnockNotificationStatusResponse")
@MethodSource("com.moabam.support.fixture.ParticipantFixture#provideParticipants")
@ParameterizedTest
void notificationService_knocked_checkMyKnockNotificationStatusInRoom(List<Participant> participants) {
// Given
given(participantSearchRepository.findOtherParticipantsInRoom(any(Long.class), any(Long.class)))
.willReturn(participants);
given(notificationRepository.existsByKey(any(String.class))).willReturn(true);

// When
KnockNotificationStatusResponse actual =
notificationService.checkMyKnockNotificationStatusInRoom(memberTest, 1L);

// Then
assertThat(actual.knockedMembersId()).hasSize(3);
assertThat(actual.notKnockedMembersId()).isEmpty();
}

@DisplayName("특정 방에서 나 이외의 모든 사용자를 콕 안 찔렀을 때, - KnockNotificationStatusResponse")
@MethodSource("com.moabam.support.fixture.ParticipantFixture#provideParticipants")
@ParameterizedTest
void notificationService_notKnocked_checkMyKnockNotificationStatusInRoom(List<Participant> participants) {
// Given
given(participantSearchRepository.findOtherParticipantsInRoom(any(Long.class), any(Long.class)))
.willReturn(participants);
given(notificationRepository.existsByKey(any(String.class))).willReturn(false);

// When
KnockNotificationStatusResponse actual =
notificationService.checkMyKnockNotificationStatusInRoom(memberTest, 1L);

// Then
assertThat(actual.knockedMembersId()).isEmpty();
assertThat(actual.notKnockedMembersId()).hasSize(3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ void participantSearchRepository_findAllByRoomCertifyTime(Room room, List<Partic
List<Participant> actual = participantSearchRepository.findAllByRoomCertifyTime(10);

// Then
assertThat(actual).hasSize(3);
assertThat(actual).hasSize(5);
}

@DisplayName("특정 방에서 본인을 제외한 참여자 조회를 성공적으로 했을 때, - List<Participant>")
@MethodSource("com.moabam.support.fixture.ParticipantFixture#provideRoomAndParticipants")
@ParameterizedTest
void participantSearchRepository_findOtherParticipantsInRoom(Room room, List<Participant> participants) {
// Given
roomRepository.save(room);
participantRepository.saveAll(participants);

// When
List<Participant> actual = participantSearchRepository.findOtherParticipantsInRoom(7L, room.getId());

// Then
assertThat(actual).hasSize(4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static Stream<Arguments> provideParticipants() {

return Stream.of(Arguments.of(List.of(
ParticipantFixture.participant(room, 1L),
ParticipantFixture.participant(room, 2L),
ParticipantFixture.participant(room, 3L)
ParticipantFixture.participant(room, 3L),
ParticipantFixture.participant(room, 7L)
)));
}

Expand All @@ -35,7 +35,9 @@ public static Stream<Arguments> provideRoomAndParticipants() {
List.of(
ParticipantFixture.participant(room, 1L),
ParticipantFixture.participant(room, 2L),
ParticipantFixture.participant(room, 3L)
ParticipantFixture.participant(room, 3L),
ParticipantFixture.participant(room, 5L),
ParticipantFixture.participant(room, 7L)
))
);
}
Expand Down

0 comments on commit 51ccfaa

Please sign in to comment.