Skip to content

Commit

Permalink
Add Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Jan 26, 2024
1 parent 340bb20 commit 859f2f0
Show file tree
Hide file tree
Showing 25 changed files with 851 additions and 88 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotl
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
ktor-client-mock = { module = "io.ktor:ktor-client-mock", version.ref = "ktor" }
ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }
ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor" }
ktor-http = { module = "io.ktor:ktor-http", version.ref = "ktor" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class SRGAnalyticsTest {
verifySequence {
commandersAct.sendEvent(commandersActEvent)
}
verify(exactly = 0) {
verify {
comScore wasNot Called
}
}
Expand Down Expand Up @@ -102,7 +102,7 @@ class SRGAnalyticsTest {
verifySequence {
comScore.getPersistentLabel(label)
}
verify(exactly = 0) {
verify {
commandersAct wasNot Called
}
}
Expand All @@ -116,7 +116,7 @@ class SRGAnalyticsTest {
verifySequence {
commandersAct.getPermanentDataLabel(label)
}
verify(exactly = 0) {
verify {
comScore wasNot Called
}
}
Expand Down
5 changes: 5 additions & 0 deletions pillarbox-core-business/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ dependencies {
api(libs.tagcommander.core)

testImplementation(libs.junit)
testImplementation(libs.kotlin.test)
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.ktor.client.mock)
testImplementation(libs.mockk)
testImplementation(libs.mockk.dsl)

androidTestImplementation(project(":pillarbox-player-testutils"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ package ch.srgssr.pillarbox.core.business

import android.content.Context
import ch.srgssr.pillarbox.core.business.integrationlayer.data.MediaComposition
import ch.srgssr.pillarbox.core.business.integrationlayer.service.DefaultHttpClient
import ch.srgssr.pillarbox.core.business.integrationlayer.service.MediaCompositionDataSource
import kotlinx.serialization.json.Json

class LocalMediaCompositionDataSource(context: Context) : MediaCompositionDataSource {
private val localData = HashMap<String, MediaComposition>()

init {
val jsonSerializer = Json { ignoreUnknownKeys = true }
val json = context.assets.open("media-compositions.json").bufferedReader().use { it.readText() }
val listMediaComposition: List<MediaComposition> = jsonSerializer.decodeFromString(json)
val listMediaComposition: List<MediaComposition> = DefaultHttpClient.jsonSerializer.decodeFromString(json)
for (mediaComposition in listMediaComposition) {
localData[mediaComposition.mainChapter.urn] = mediaComposition
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package ch.srgssr.pillarbox.core.business.akamai

import android.net.Uri
import android.text.TextUtils
import androidx.media3.datasource.DataSource
import androidx.media3.datasource.DataSpec
import androidx.media3.datasource.DefaultHttpDataSource
Expand Down Expand Up @@ -63,7 +62,7 @@ class AkamaiTokenDataSource private constructor(
val queryParametersNames = uri.queryParameterNames
val uriBuilder = uri.buildUpon().clearQuery().build().buildUpon()
for (name in queryParametersNames) {
if (!TextUtils.equals(MediaCompositionMediaItemSource.TOKEN_QUERY_PARAM, name)) {
if (MediaCompositionMediaItemSource.TOKEN_QUERY_PARAM != name) {
uriBuilder.appendQueryParameter(name, uri.getQueryParameter(name))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AkamaiTokenProvider(private val httpClient: HttpClient = DefaultHttpClient
}

private suspend fun getToken(acl: String): Result<Token> {
return Result.runCatching {
return runCatching {
httpClient.get(TOKEN_SERVICE_URL) {
url {
appendEncodedPathSegments("akahd/token")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package ch.srgssr.pillarbox.core.business.exception

import ch.srgssr.pillarbox.core.business.integrationlayer.data.BlockReason
import ch.srgssr.pillarbox.core.business.integrationlayer.data.Chapter
import ch.srgssr.pillarbox.core.business.integrationlayer.data.Segment
import java.io.IOException

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
*/
package ch.srgssr.pillarbox.core.business.exception

import ch.srgssr.pillarbox.core.business.integrationlayer.data.Chapter
import java.io.IOException

/**
* Resource not found exception is throw when :
* Resource not found exception is throw when:
* - [Chapter] doesn't have a playable resource
* - [Chapter.listResource] is empty or null
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package ch.srgssr.pillarbox.core.business.extension

import android.content.Context
import androidx.annotation.StringRes
import ch.srgssr.pillarbox.core.business.R
import ch.srgssr.pillarbox.core.business.integrationlayer.data.BlockReason

Expand All @@ -23,6 +24,7 @@ fun Context.getString(blockReason: BlockReason): String {
*
* @return The android string resource id of a [BlockReason]
*/
@StringRes
fun BlockReason.getStringResId(): Int {
return when (this) {
BlockReason.AGERATING12 -> R.string.blockReason_ageRating12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import okhttp3.logging.HttpLoggingInterceptor
* Default ktor HttpClient.
*/
object DefaultHttpClient {
private val jsonSerializer = Json {
internal val jsonSerializer = Json {
encodeDefaults = true
ignoreUnknownKeys = true
isLenient = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DefaultMediaCompositionDataSource(
) : MediaCompositionDataSource {

override suspend fun getMediaCompositionByUrn(urn: String): Result<MediaComposition> {
return Result.runCatching {
return runCatching {
httpClient.get(baseUrl) {
url {
appendEncodedPathSegments("integrationlayer/2.1/mediaComposition/byUrn")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds

/**
* Total playtime counter
* Total playtime counter.
*
* @param timeProvider A callback invoked whenever the current time is needed.
*/
class TotalPlaytimeCounter {
class TotalPlaytimeCounter(
private val timeProvider: () -> Long = { System.currentTimeMillis() },
) {
private var totalPlayTime: Duration = Duration.ZERO
private var lastPlayTime = 0L

Expand All @@ -20,7 +24,7 @@ class TotalPlaytimeCounter {
*/
fun play() {
pause()
lastPlayTime = System.currentTimeMillis()
lastPlayTime = timeProvider()
}

/**
Expand All @@ -32,7 +36,7 @@ class TotalPlaytimeCounter {
return if (lastPlayTime <= 0L) {
totalPlayTime
} else {
totalPlayTime + (System.currentTimeMillis() - lastPlayTime).milliseconds
totalPlayTime + (timeProvider() - lastPlayTime).milliseconds
}
}

Expand All @@ -41,7 +45,7 @@ class TotalPlaytimeCounter {
*/
fun pause() {
if (lastPlayTime > 0L) {
totalPlayTime += (System.currentTimeMillis() - lastPlayTime).milliseconds
totalPlayTime += (timeProvider() - lastPlayTime).milliseconds
lastPlayTime = 0L
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.core.business

import androidx.core.net.toUri
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

class MediaItemUrnTest {
@Test
fun `MediaItemUrn with all parameters`() {
val urn = "urn:rts:audio:3262363"
val title = "Media title"
val subtitle = "Media subtitle"
val artworkUri = "Artwork uri".toUri()
val mediaItem = MediaItemUrn(
urn = urn,
title = title,
subtitle = subtitle,
artworkUri = artworkUri,
)

assertEquals(urn, mediaItem.mediaId)
assertEquals(title, mediaItem.mediaMetadata.title)
assertEquals(subtitle, mediaItem.mediaMetadata.subtitle)
assertEquals(artworkUri, mediaItem.mediaMetadata.artworkUri)
}

@Test
fun `MediaItemUrn with urn only`() {
val urn = "urn:rts:audio:3262363"
val mediaItem = MediaItemUrn(
urn = urn,
)

assertEquals(urn, mediaItem.mediaId)
assertNull(mediaItem.mediaMetadata.title)
assertNull(mediaItem.mediaMetadata.subtitle)
assertNull(mediaItem.mediaMetadata.artworkUri)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ package ch.srgssr.pillarbox.core.business
import ch.srgssr.pillarbox.core.business.integrationlayer.data.BlockReason
import ch.srgssr.pillarbox.core.business.integrationlayer.data.Chapter
import ch.srgssr.pillarbox.core.business.integrationlayer.data.MediaComposition
import ch.srgssr.pillarbox.core.business.integrationlayer.service.DefaultHttpClient.jsonSerializer
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import org.junit.Assert
import org.junit.Test

class TestJsonSerialization {

private val jsonSerializer = Json { ignoreUnknownKeys = true }

@Test
fun testChapterValidJson() {
val json = "{\"urn\":\"urn:srf:video:12343\",\"title\":\"Chapter title\",\"imageUrl\":\"https://image.png\",\"blockReason\": \"UNKNOWN\"}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.core.business.exception

import ch.srgssr.pillarbox.core.business.integrationlayer.data.BlockReason
import kotlin.test.Test
import kotlin.test.assertEquals

class BlockReasonExceptionTest {
@Test
fun `BlockReasonException created with a BlockReason`() {
BlockReason.entries.forEach { blockReason ->
val exception = BlockReasonException(blockReason)

assertEquals(blockReason.name, exception.message)
}
}

@Test
fun `BlockReasonException created with a message matching a BlockReason`() {
BlockReason.entries.forEach { blockReason ->
val exception = BlockReasonException(blockReason.name)

assertEquals(blockReason.name, exception.message)
}
}

@Test
fun `BlockReasonException created with a message not matching a BlockReason`() {
val exception = BlockReasonException("FOO_BAR")

assertEquals(BlockReason.UNKNOWN.name, exception.message)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.core.business.extension

import ch.srgssr.pillarbox.core.business.R
import ch.srgssr.pillarbox.core.business.integrationlayer.data.BlockReason
import kotlin.test.Test
import kotlin.test.assertEquals

class BlockReasonTest {
@Test
fun `get string resId for BlockReason`() {
assertEquals(R.string.blockReason_ageRating12, BlockReason.AGERATING12.getStringResId())
assertEquals(R.string.blockReason_ageRating18, BlockReason.AGERATING18.getStringResId())
assertEquals(R.string.blockReason_commercial, BlockReason.COMMERCIAL.getStringResId())
assertEquals(R.string.blockReason_endDate, BlockReason.ENDDATE.getStringResId())
assertEquals(R.string.blockReason_geoBlock, BlockReason.GEOBLOCK.getStringResId())
assertEquals(R.string.blockReason_legal, BlockReason.LEGAL.getStringResId())
assertEquals(R.string.blockReason_startDate, BlockReason.STARTDATE.getStringResId())
assertEquals(R.string.blockReason_unknown, BlockReason.UNKNOWN.getStringResId())
}
}
Loading

0 comments on commit 859f2f0

Please sign in to comment.