mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 21:03:54 +01:00
Move notification API into main commons codebase (#5465)
* Moved the notification API calls out of the data client * Converted the NofificationClient to kotlin and improved its test
This commit is contained in:
parent
1948bab873
commit
3c1cdf18a1
8 changed files with 210 additions and 94 deletions
|
|
@ -1,35 +1,47 @@
|
|||
package fr.free.nrw.commons.notification
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import fr.free.nrw.commons.TestCommonsApplication
|
||||
import fr.free.nrw.commons.notification.models.NotificationType
|
||||
import io.reactivex.Observable
|
||||
import junit.framework.TestCase.assertEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.ArgumentMatchers
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.ArgumentMatchers.any
|
||||
import org.mockito.ArgumentMatchers.anyString
|
||||
import org.mockito.ArgumentMatchers.eq
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.verify
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.LooperMode
|
||||
import org.wikipedia.csrf.CsrfTokenClient
|
||||
import org.wikipedia.dataclient.Service
|
||||
import org.wikipedia.dataclient.mwapi.MwQueryResponse
|
||||
import org.wikipedia.dataclient.mwapi.MwQueryResult
|
||||
import org.wikipedia.json.GsonUtil
|
||||
import org.wikipedia.notifications.Notification
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
@Config(sdk = [21], application = TestCommonsApplication::class)
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class NotificationClientTest {
|
||||
|
||||
@Mock
|
||||
private lateinit var service: Service
|
||||
private lateinit var service: NotificationInterface
|
||||
|
||||
@Mock
|
||||
private lateinit var csrfTokenClient: CsrfTokenClient
|
||||
|
||||
@Mock
|
||||
private lateinit var mQueryResponse: MwQueryResponse
|
||||
|
||||
@Mock
|
||||
private lateinit var mQueryResult: MwQueryResult
|
||||
|
||||
@Mock
|
||||
private lateinit var mQueryResultNotificationsList: MwQueryResult.NotificationList
|
||||
@Mock
|
||||
private lateinit var notificationsList: List<Notification>
|
||||
|
||||
private lateinit var notificationClient: NotificationClient
|
||||
|
||||
|
|
@ -39,8 +51,8 @@ class NotificationClientTest {
|
|||
@Before
|
||||
@Throws(Exception::class)
|
||||
fun setUp() {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
notificationClient = NotificationClient(service, csrfTokenClient)
|
||||
MockitoAnnotations.openMocks(this)
|
||||
notificationClient = NotificationClient(csrfTokenClient, service)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -49,12 +61,40 @@ class NotificationClientTest {
|
|||
|
||||
@Test
|
||||
fun getNotificationTest() {
|
||||
Mockito.`when`(service.getAllNotifications(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.any())).thenReturn(Observable.just(mQueryResponse))
|
||||
Mockito.`when`(service.getAllNotifications(anyString(), anyString(), any()))
|
||||
.thenReturn(Observable.just(mQueryResponse))
|
||||
Mockito.`when`(mQueryResponse.query()).thenReturn(mQueryResult)
|
||||
Mockito.`when`(mQueryResult.notifications()).thenReturn(mQueryResultNotificationsList)
|
||||
Mockito.`when`(mQueryResultNotificationsList.list()).thenReturn(notificationsList)
|
||||
notificationClient.getNotifications(true)
|
||||
verify(service).getAllNotifications(eq("wikidatawiki|commonswiki|enwiki"), eq("read"), eq(null))
|
||||
Mockito.`when`(mQueryResultNotificationsList.list()).thenReturn(
|
||||
listOf(
|
||||
createWikimediaNotification(
|
||||
primaryUrl = "foo",
|
||||
compactHeader = "header",
|
||||
timestamp = "2024-01-22T10:12:00Z",
|
||||
notificationId = 1234L
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
val result = notificationClient.getNotifications(true).test().values()
|
||||
|
||||
verify(service).getAllNotifications(
|
||||
eq("wikidatawiki|commonswiki|enwiki"),
|
||||
eq("read"),
|
||||
eq(null)
|
||||
)
|
||||
|
||||
val notificationList = result.first()
|
||||
assertEquals(1, notificationList.size)
|
||||
|
||||
with(notificationList.first()) {
|
||||
assertEquals(NotificationType.UNKNOWN, notificationType)
|
||||
assertEquals("header", notificationText)
|
||||
assertEquals("January 22", date)
|
||||
assertEquals("foo", link)
|
||||
assertEquals("", iconUrl)
|
||||
assertEquals("1234", notificationId)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -63,10 +103,33 @@ class NotificationClientTest {
|
|||
@Test
|
||||
fun markNotificationAsReadTest() {
|
||||
Mockito.`when`(csrfTokenClient.tokenBlocking).thenReturn("test")
|
||||
Mockito.`when`(service.markRead(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString())).thenReturn(Observable.just(mQueryResponse))
|
||||
Mockito.`when`(service.markRead(anyString(), anyString(), anyString()))
|
||||
.thenReturn(Observable.just(mQueryResponse))
|
||||
Mockito.`when`(mQueryResponse.success()).thenReturn(true)
|
||||
notificationClient.markNotificationAsRead("test")
|
||||
verify(service).markRead(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString())
|
||||
verify(service).markRead(anyString(), anyString(), anyString())
|
||||
}
|
||||
|
||||
@Suppress("SameParameterValue")
|
||||
private fun createWikimediaNotification(
|
||||
primaryUrl: String, compactHeader: String, timestamp: String, notificationId: Long
|
||||
) = Notification().apply {
|
||||
setId(notificationId)
|
||||
|
||||
setTimestamp(Notification.Timestamp().apply {
|
||||
setUtciso8601(timestamp)
|
||||
})
|
||||
|
||||
contents = Notification.Contents().apply {
|
||||
setCompactHeader(compactHeader)
|
||||
|
||||
links = Notification.Links().apply {
|
||||
setPrimary(
|
||||
GsonUtil.getDefaultGson().toJsonTree(Notification.Link().apply {
|
||||
setUrl(primaryUrl)
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue