Skip to content

Commit

Permalink
refactor: Multicast Message Converter를 Object 클래스로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
GiJungPark committed Nov 1, 2024
1 parent 242822e commit 4c02d4b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ package com.dmforu.fcm

import com.dmforu.domain.message.MessageSender
import com.dmforu.domain.message.NoticeMessage
import com.google.firebase.FirebaseApp
import com.google.firebase.messaging.FirebaseMessaging
import org.springframework.stereotype.Component

@Component
internal class FirebaseMessageSender(
private val firebaseMessageConverter: FirebaseMessageConverter,
private val firebaseMessaging: FirebaseMessaging,
) : MessageSender {

override fun sendNoticeMessage(message: NoticeMessage, tokens: List<String>) {

val firebaseMessage = firebaseMessageConverter.buildMessageToNotice(message, tokens)
val firebaseMessage = MulticastMessageMapper.mapToMulticastMessage(message, tokens)

firebaseMessaging.sendEachForMulticast(firebaseMessage)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package com.dmforu.fcm
import com.dmforu.domain.message.NoticeMessage
import com.google.firebase.messaging.MulticastMessage
import com.google.firebase.messaging.Notification
import org.springframework.stereotype.Component

@Component
internal class FirebaseMessageConverter {
internal object MulticastMessageMapper {

fun buildMessageToNotice(message: NoticeMessage, tokens: List<String>): MulticastMessage {
fun mapToMulticastMessage(message: NoticeMessage, tokens: List<String>): MulticastMessage {
val notification = Notification.builder()
.setTitle(message.title)
.setBody(message.body)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
package com.dmforu.fcm

import com.dmforu.domain.message.NoticeMessage
import com.dmforu.domain.notice.Notice
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.MulticastMessage
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

import org.mockito.BDDMockito.given
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.eq
import org.mockito.kotlin.any
import org.mockito.kotlin.given
import java.time.LocalDate

@ExtendWith(MockitoExtension::class)
class FirebaseMessageSenderTest {

@Mock
private lateinit var firebaseMessageConverter: FirebaseMessageConverter

@Mock
private lateinit var firebaseMessaging: FirebaseMessaging

@InjectMocks
private lateinit var messageSender: FirebaseMessageSender

@BeforeEach
fun setUp() {
firebaseMessaging = mock()
messageSender = FirebaseMessageSender(firebaseMessaging)
}

@DisplayName("공지를 푸시 알림으로 전송한다.")
@Test
fun sendNoticeMessage() {
// given
val message = mock(NoticeMessage::class.java)
val multicastMessage = mock(MulticastMessage::class.java)
val notice = Notice.of(
number = 1,
type = "대학",
date = LocalDate.now(),
title = "공지사항입니다.",
author = "관리자",
url = "https://www.test.com"
)
val message = NoticeMessage.createDepartmentNoticeMessage(notice)
val tokens = listOf("토큰1", "토큰2")

given(firebaseMessageConverter.buildMessageToNotice(eq(message), eq(tokens))).willReturn(multicastMessage)

// when
messageSender.sendNoticeMessage(message, tokens)

// then
verify(firebaseMessageConverter).buildMessageToNotice(message, tokens)
verify(firebaseMessaging).sendEachForMulticast(multicastMessage)
verify(firebaseMessaging).sendEachForMulticast(any())
}
}

0 comments on commit 4c02d4b

Please sign in to comment.