From 8e895685fc42b55dd266b5e63eacd7d5a41094c1 Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Thu, 14 Sep 2017 22:32:24 +0200 Subject: [PATCH] Add method to map string type to AlbumListType enum. Signed-off-by: Yahor Berdnikau --- .../api/subsonic/models/AlbumListType.kt | 21 +++++++++- .../api/subsonic/models/AlbumListTypeTest.kt | 41 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 subsonic-api/src/test/kotlin/org/moire/ultrasonic/api/subsonic/models/AlbumListTypeTest.kt diff --git a/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/models/AlbumListType.kt b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/models/AlbumListType.kt index bd29f3e8..8ab52824 100644 --- a/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/models/AlbumListType.kt +++ b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/models/AlbumListType.kt @@ -19,6 +19,25 @@ enum class AlbumListType(val typeName: String) { BY_GENRE("byGenre"); override fun toString(): String { - return super.toString().toLowerCase() + return typeName + } + + companion object { + @JvmStatic + fun fromName(typeName: String): AlbumListType = when (typeName) { + in RANDOM.typeName -> RANDOM + in NEWEST.typeName -> NEWEST + in HIGHEST.typeName -> HIGHEST + in FREQUENT.typeName -> FREQUENT + in RECENT.typeName -> RECENT + in SORTED_BY_NAME.typeName -> SORTED_BY_NAME + in SORTED_BY_ARTIST.typeName -> SORTED_BY_ARTIST + in STARRED.typeName -> STARRED + in BY_YEAR.typeName -> BY_YEAR + in BY_GENRE.typeName -> BY_GENRE + else -> throw IllegalArgumentException("Unknown type: $typeName") + } + + private operator fun String.contains(other: String) = this.equals(other, true) } } diff --git a/subsonic-api/src/test/kotlin/org/moire/ultrasonic/api/subsonic/models/AlbumListTypeTest.kt b/subsonic-api/src/test/kotlin/org/moire/ultrasonic/api/subsonic/models/AlbumListTypeTest.kt new file mode 100644 index 00000000..ef0948d7 --- /dev/null +++ b/subsonic-api/src/test/kotlin/org/moire/ultrasonic/api/subsonic/models/AlbumListTypeTest.kt @@ -0,0 +1,41 @@ +package org.moire.ultrasonic.api.subsonic.models + +import org.amshove.kluent.`should equal to` +import org.amshove.kluent.`should equal` +import org.amshove.kluent.`should throw` +import org.junit.Test + +/** + * Unit test for [AlbumListType] class. + */ +class AlbumListTypeTest { + @Test + fun `Should create type from string ignoring case`() { + val type = AlbumListType.SORTED_BY_NAME.typeName.toLowerCase() + + val albumListType = AlbumListType.fromName(type) + + albumListType `should equal` AlbumListType.SORTED_BY_NAME + } + + @Test + fun `Should throw IllegalArgumentException for unknown type`() { + val failCall = { + AlbumListType.fromName("some-not-existing-type") + } + + failCall `should throw` IllegalArgumentException::class + } + + @Test + fun `Should convert type string to corresponding AlbumListType`() { + AlbumListType.values().forEach { + AlbumListType.fromName(it.typeName) `should equal` it + } + } + + @Test + fun `Should return type name for toString call`() { + AlbumListType.STARRED.typeName `should equal to` AlbumListType.STARRED.toString() + } +}