Adding some tests on ignored cases

This commit is contained in:
Maxime NATUREL 2022-06-08 11:30:08 +02:00
parent dc95f4553e
commit 51b930147a
4 changed files with 201 additions and 7 deletions

View File

@ -36,16 +36,22 @@ import timber.log.Timber
import java.util.concurrent.TimeUnit
import javax.inject.Inject
// TODO add unit tests
/**
* Aggregates all live location sharing related events in local database.
*/
internal class LiveLocationAggregationProcessor @Inject constructor(
@SessionId private val sessionId: String,
private val workManagerProvider: WorkManagerProvider,
private val clock: Clock,
) {
fun handleBeaconInfo(realm: Realm, event: Event, content: MessageBeaconInfoContent, roomId: String, isLocalEcho: Boolean) {
/**
* Handle the content of a beacon info.
* @return true if it has been processed, false if ignored.
*/
fun handleBeaconInfo(realm: Realm, event: Event, content: MessageBeaconInfoContent, roomId: String, isLocalEcho: Boolean): Boolean {
if (event.senderId.isNullOrEmpty() || isLocalEcho) {
return
return false
}
val isLive = content.isLive.orTrue()
@ -58,7 +64,7 @@ internal class LiveLocationAggregationProcessor @Inject constructor(
if (targetEventId.isNullOrEmpty()) {
Timber.w("no target event id found for the beacon content")
return
return false
}
val aggregatedSummary = LiveLocationShareAggregatedSummaryEntity.getOrCreate(
@ -83,6 +89,8 @@ internal class LiveLocationAggregationProcessor @Inject constructor(
} else {
cancelDeactivationAfterTimeout(targetEventId, roomId)
}
return true
}
private fun scheduleDeactivationAfterTimeout(eventId: String, roomId: String, endOfLiveTimestampMillis: Long?) {
@ -110,6 +118,10 @@ internal class LiveLocationAggregationProcessor @Inject constructor(
workManagerProvider.workManager.cancelUniqueWork(workName)
}
/**
* Handle the content of a beacon location data.
* @return true if it has been processed, false if ignored.
*/
fun handleBeaconLocationData(
realm: Realm,
event: Event,
@ -117,14 +129,14 @@ internal class LiveLocationAggregationProcessor @Inject constructor(
roomId: String,
relatedEventId: String?,
isLocalEcho: Boolean
) {
): Boolean {
if (event.senderId.isNullOrEmpty() || isLocalEcho) {
return
return false
}
if (relatedEventId.isNullOrEmpty()) {
Timber.w("no related event id found for the live location content")
return
return false
}
val aggregatedSummary = LiveLocationShareAggregatedSummaryEntity.getOrCreate(
@ -143,6 +155,8 @@ internal class LiveLocationAggregationProcessor @Inject constructor(
Timber.d("updating last location of the summary of id=$relatedEventId")
aggregatedSummary.lastLocationContent = ContentMapper.map(content.toContent())
}
return true
}
private fun deactivateAllPreviousBeacons(realm: Realm, roomId: String, userId: String, currentEventId: String) {

View File

@ -0,0 +1,128 @@
/*
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.internal.session.room.aggregation.livelocation
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.room.model.message.MessageBeaconInfoContent
import org.matrix.android.sdk.api.session.room.model.message.MessageBeaconLocationDataContent
import org.matrix.android.sdk.test.fakes.FakeClock
import org.matrix.android.sdk.test.fakes.FakeRealm
import org.matrix.android.sdk.test.fakes.FakeWorkManagerProvider
private const val A_SESSION_ID = "session_id"
private const val A_SENDER_ID = "sender_id"
private const val AN_EVENT_ID = "event_id"
internal class LiveLocationAggregationProcessorTest {
private val fakeWorkManagerProvider = FakeWorkManagerProvider()
private val fakeClock = FakeClock()
private val fakeRealm = FakeRealm()
private val liveLocationAggregationProcessor = LiveLocationAggregationProcessor(
sessionId = A_SESSION_ID,
workManagerProvider = fakeWorkManagerProvider.instance,
clock = fakeClock
)
@Test
fun `given beacon info when it is local echo then it is ignored`() {
val event = Event(senderId = A_SENDER_ID)
val beaconInfo = MessageBeaconInfoContent()
val result = liveLocationAggregationProcessor.handleBeaconInfo(
realm = fakeRealm.instance,
event = event,
content = beaconInfo,
roomId = "",
isLocalEcho = true
)
result shouldBeEqualTo false
}
@Test
fun `given beacon info and event when senderId is null or empty then it is ignored`() {
val eventNoSenderId = Event(eventId = AN_EVENT_ID)
val eventEmptySenderId = Event(eventId = AN_EVENT_ID, senderId = "")
val beaconInfo = MessageBeaconInfoContent()
val resultNoSenderId = liveLocationAggregationProcessor.handleBeaconInfo(
realm = fakeRealm.instance,
event = eventNoSenderId,
content = beaconInfo,
roomId = "",
isLocalEcho = false
)
val resultEmptySenderId = liveLocationAggregationProcessor.handleBeaconInfo(
realm = fakeRealm.instance,
event = eventEmptySenderId,
content = beaconInfo,
roomId = "",
isLocalEcho = false
)
resultNoSenderId shouldBeEqualTo false
resultEmptySenderId shouldBeEqualTo false
}
@Test
fun `given beacon location data when it is local echo then it is ignored`() {
val event = Event(senderId = A_SENDER_ID)
val beaconLocationData = MessageBeaconLocationDataContent()
val result = liveLocationAggregationProcessor.handleBeaconLocationData(
realm = fakeRealm.instance,
event = event,
content = beaconLocationData,
roomId = "",
relatedEventId = "",
isLocalEcho = true
)
result shouldBeEqualTo false
}
@Test
fun `given beacon location data and event when senderId is null or empty then it is ignored`() {
val eventNoSenderId = Event(eventId = AN_EVENT_ID)
val eventEmptySenderId = Event(eventId = AN_EVENT_ID, senderId = "")
val beaconLocationData = MessageBeaconLocationDataContent()
val resultNoSenderId = liveLocationAggregationProcessor.handleBeaconLocationData(
realm = fakeRealm.instance,
event = eventNoSenderId,
content = beaconLocationData,
roomId = "",
relatedEventId = "",
isLocalEcho = false
)
val resultEmptySenderId = liveLocationAggregationProcessor.handleBeaconLocationData(
realm = fakeRealm.instance,
event = eventEmptySenderId,
content = beaconLocationData,
roomId = "",
relatedEventId = "",
isLocalEcho = false
)
resultNoSenderId shouldBeEqualTo false
resultEmptySenderId shouldBeEqualTo false
}
}

View File

@ -0,0 +1,27 @@
/*
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.test.fakes
import io.mockk.every
import io.mockk.mockk
import org.matrix.android.sdk.internal.util.time.Clock
internal class FakeClock : Clock by mockk() {
fun givenEpoch(epoch: Long) {
every { epochMillis() } returns epoch
}
}

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.test.fakes
import io.mockk.mockk
import org.matrix.android.sdk.internal.di.WorkManagerProvider
internal class FakeWorkManagerProvider {
val instance = mockk<WorkManagerProvider>()
}