From 1215a1a319aefdad2bbfe7093eddae4a534c7b62 Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Mon, 6 Feb 2023 16:39:16 +0100 Subject: [PATCH] Add unit tests for VoteToPollUseCase --- .../room/detail/poll/VoteToPollUseCase.kt | 7 +- .../room/detail/poll/VoteToPollUseCaseTest.kt | 118 ++++++++++++++++++ 2 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 vector/src/test/java/im/vector/app/features/home/room/detail/poll/VoteToPollUseCaseTest.kt diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/poll/VoteToPollUseCase.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/poll/VoteToPollUseCase.kt index 244614493f..62f8006988 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/poll/VoteToPollUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/poll/VoteToPollUseCase.kt @@ -18,11 +18,11 @@ package im.vector.app.features.home.room.detail.poll import im.vector.app.core.di.ActiveSessionHolder import org.matrix.android.sdk.api.session.events.model.LocalEcho +import org.matrix.android.sdk.api.session.getRoom import org.matrix.android.sdk.api.session.room.getTimelineEvent import timber.log.Timber import javax.inject.Inject -// TODO add unit tests class VoteToPollUseCase @Inject constructor( private val activeSessionHolder: ActiveSessionHolder, ) { @@ -32,10 +32,7 @@ class VoteToPollUseCase @Inject constructor( if (LocalEcho.isLocalEchoId(pollEventId)) return runCatching { - val room = activeSessionHolder.getActiveSession() - .roomService() - .getRoom(roomId) - + val room = activeSessionHolder.getActiveSession().getRoom(roomId) room?.getTimelineEvent(pollEventId)?.let { pollTimelineEvent -> val currentVote = pollTimelineEvent .annotations diff --git a/vector/src/test/java/im/vector/app/features/home/room/detail/poll/VoteToPollUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/home/room/detail/poll/VoteToPollUseCaseTest.kt new file mode 100644 index 0000000000..aaa591cc37 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/home/room/detail/poll/VoteToPollUseCaseTest.kt @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2023 New Vector Ltd + * + * 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 im.vector.app.features.home.room.detail.poll + +import im.vector.app.test.fakes.FakeActiveSessionHolder +import im.vector.app.test.fixtures.RoomPollFixture +import io.mockk.every +import io.mockk.verify +import org.junit.Test +import org.matrix.android.sdk.api.session.getRoom +import org.matrix.android.sdk.api.session.room.getTimelineEvent + +private const val A_LOCAL_EVENT_ID = "\$local.event-id" +private const val AN_EVENT_ID = "event-id" +private const val A_ROOM_ID = "room-id" +private const val AN_EXISTING_OPTION_ID = "an-existing-option-id" +private const val AN_OPTION_ID = "option-id" +private const val AN_EVENT_TIMESTAMP = 123L + +internal class VoteToPollUseCaseTest { + + private val fakeActiveSessionHolder = FakeActiveSessionHolder() + private val voteToPollUseCase = VoteToPollUseCase(fakeActiveSessionHolder.instance) + + @Test + fun `given a local echo poll event when voting for an option then the vote is aborted`() { + // Given + val aPollEventId = A_LOCAL_EVENT_ID + val aVoteId = AN_OPTION_ID + givenAPollTimelineEvent(aPollEventId) + + // When + voteToPollUseCase.execute(A_ROOM_ID, aPollEventId, aVoteId) + + // Then + verify(exactly = 0) { + fakeActiveSessionHolder.fakeSession + .getRoom(A_ROOM_ID) + ?.sendService() + ?.voteToPoll(aPollEventId, aVoteId) + } + } + + @Test + fun `given a poll event when voting for a different option then the vote is sent`() { + // Given + val aPollEventId = AN_EVENT_ID + val aVoteId = AN_OPTION_ID + givenAPollTimelineEvent(aPollEventId) + + // When + voteToPollUseCase.execute(A_ROOM_ID, aPollEventId, aVoteId) + + // Then + verify(exactly = 1) { + fakeActiveSessionHolder.fakeSession + .getRoom(A_ROOM_ID) + ?.sendService() + ?.voteToPoll(aPollEventId, aVoteId) + } + } + + @Test + fun `given a poll event when voting for the same option then the vote is aborted`() { + // Given + val aPollEventId = AN_EVENT_ID + val aVoteId = AN_EXISTING_OPTION_ID + givenAPollTimelineEvent(aPollEventId) + + // When + voteToPollUseCase.execute(A_ROOM_ID, aPollEventId, aVoteId) + + // Then + verify(exactly = 0) { + fakeActiveSessionHolder.fakeSession + .getRoom(A_ROOM_ID) + ?.sendService() + ?.voteToPoll(aPollEventId, aVoteId) + } + } + + private fun givenAPollTimelineEvent(eventId: String) { + val pollCreationInfo = RoomPollFixture.givenPollCreationInfo("pollTitle") + val messageContent = RoomPollFixture.givenAMessagePollContent(pollCreationInfo) + val timelineEvent = RoomPollFixture.givenATimelineEvent( + eventId, + A_ROOM_ID, + AN_EVENT_TIMESTAMP, + messageContent + ) + every { + timelineEvent.annotations + ?.pollResponseSummary + ?.aggregatedContent + ?.myVote + } returns AN_EXISTING_OPTION_ID + + every { + fakeActiveSessionHolder.fakeSession + .getRoom(A_ROOM_ID) + ?.getTimelineEvent(eventId) + } returns timelineEvent + } +}