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() + } +}