diff --git a/app/src/main/java/com/antonioleiva/bandhookkotlin/data/CloudAlbumDataSet.kt b/app/src/main/java/com/antonioleiva/bandhookkotlin/data/CloudAlbumDataSource.kt similarity index 80% rename from app/src/main/java/com/antonioleiva/bandhookkotlin/data/CloudAlbumDataSet.kt rename to app/src/main/java/com/antonioleiva/bandhookkotlin/data/CloudAlbumDataSource.kt index 173ebcc..8a91f5e 100644 --- a/app/src/main/java/com/antonioleiva/bandhookkotlin/data/CloudAlbumDataSet.kt +++ b/app/src/main/java/com/antonioleiva/bandhookkotlin/data/CloudAlbumDataSource.kt @@ -23,15 +23,15 @@ import com.antonioleiva.bandhookkotlin.data.mapper.AlbumMapper import com.antonioleiva.bandhookkotlin.domain.entity.Album import com.antonioleiva.bandhookkotlin.domain.entity.BizException.AlbumNotFound import com.antonioleiva.bandhookkotlin.left -import com.antonioleiva.bandhookkotlin.repository.dataset.AlbumDataSet +import com.antonioleiva.bandhookkotlin.repository.datasource.AlbumDataSource import com.antonioleiva.bandhookkotlin.right -class CloudAlbumDataSet(val lastFmService: LastFmService) : AlbumDataSet { +class CloudAlbumDataSource(val lastFmService: LastFmService) : AlbumDataSource { - override fun requestAlbum(mbid: String): Result { - return lastFmService.requestAlbum(mbid).asResult { + override fun get(id: String): Result { + return lastFmService.requestAlbum(id).asResult { AlbumMapper().transform(album).fold( - { AlbumNotFound(mbid).left() }, + { AlbumNotFound(id).left() }, { it.right() } ) } @@ -49,4 +49,5 @@ class CloudAlbumDataSet(val lastFmService: LastFmService) : AlbumDataSet { return emptyList() } + } diff --git a/app/src/main/java/com/antonioleiva/bandhookkotlin/data/CloudArtistDataSet.kt b/app/src/main/java/com/antonioleiva/bandhookkotlin/data/CloudArtistDataSource.kt similarity index 79% rename from app/src/main/java/com/antonioleiva/bandhookkotlin/data/CloudArtistDataSet.kt rename to app/src/main/java/com/antonioleiva/bandhookkotlin/data/CloudArtistDataSource.kt index d93751a..93d36b6 100644 --- a/app/src/main/java/com/antonioleiva/bandhookkotlin/data/CloudArtistDataSet.kt +++ b/app/src/main/java/com/antonioleiva/bandhookkotlin/data/CloudArtistDataSource.kt @@ -23,18 +23,18 @@ import com.antonioleiva.bandhookkotlin.data.mapper.ArtistMapper import com.antonioleiva.bandhookkotlin.domain.entity.Artist import com.antonioleiva.bandhookkotlin.domain.entity.BizException.ArtistNotFound import com.antonioleiva.bandhookkotlin.left -import com.antonioleiva.bandhookkotlin.repository.dataset.ArtistDataSet +import com.antonioleiva.bandhookkotlin.repository.datasource.ArtistDataSource import com.antonioleiva.bandhookkotlin.right -class CloudArtistDataSet(val language: String, val lastFmService: LastFmService) : ArtistDataSet { +class CloudArtistDataSource(val language: String, val lastFmService: LastFmService) : ArtistDataSource { val coldplayMbid = "cc197bad-dc9c-440d-a5b5-d52ba2e14234" - override fun requestArtist(mbid: String): Result { - return lastFmService.requestArtistInfo(mbid, language).asResult { + return lastFmService.requestArtistInfo(id, language).asResult { ArtistMapper().transform(artist).fold( - { ArtistNotFound(mbid).left() }, + { ArtistNotFound(id).left() }, { it.right() } ) } diff --git a/app/src/main/java/com/antonioleiva/bandhookkotlin/di/RepositoryModule.kt b/app/src/main/java/com/antonioleiva/bandhookkotlin/di/RepositoryModule.kt index 5c755c9..98db48b 100644 --- a/app/src/main/java/com/antonioleiva/bandhookkotlin/di/RepositoryModule.kt +++ b/app/src/main/java/com/antonioleiva/bandhookkotlin/di/RepositoryModule.kt @@ -1,13 +1,13 @@ package com.antonioleiva.bandhookkotlin.di -import com.antonioleiva.bandhookkotlin.data.CloudAlbumDataSet -import com.antonioleiva.bandhookkotlin.data.CloudArtistDataSet +import com.antonioleiva.bandhookkotlin.data.CloudAlbumDataSource +import com.antonioleiva.bandhookkotlin.data.CloudArtistDataSource import com.antonioleiva.bandhookkotlin.data.lastfm.LastFmService import com.antonioleiva.bandhookkotlin.di.qualifier.LanguageSelection import com.antonioleiva.bandhookkotlin.domain.repository.AlbumRepository import com.antonioleiva.bandhookkotlin.domain.repository.ArtistRepository import com.antonioleiva.bandhookkotlin.repository.AlbumRepositoryImpl -import com.antonioleiva.bandhookkotlin.repository.ArtistRepositoryImp +import com.antonioleiva.bandhookkotlin.repository.ArtistRepositoryImpl import dagger.Module import dagger.Provides import javax.inject.Singleton @@ -17,9 +17,9 @@ class RepositoryModule { @Provides @Singleton fun provideArtistRepo(@LanguageSelection language: String, lastFmService: LastFmService): ArtistRepository - = ArtistRepositoryImp(listOf(CloudArtistDataSet(language, lastFmService))) + = ArtistRepositoryImpl(listOf(CloudArtistDataSource(language, lastFmService))) @Provides @Singleton fun provideAlbumRepo(lastFmService: LastFmService): AlbumRepository - = AlbumRepositoryImpl(listOf(CloudAlbumDataSet(lastFmService))) + = AlbumRepositoryImpl(listOf(CloudAlbumDataSource(lastFmService))) } \ No newline at end of file diff --git a/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetAlbumDetailInteractor.kt b/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetAlbumDetailInteractor.kt index 683c558..57f4b36 100644 --- a/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetAlbumDetailInteractor.kt +++ b/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetAlbumDetailInteractor.kt @@ -20,6 +20,6 @@ import com.antonioleiva.bandhookkotlin.domain.repository.AlbumRepository class GetAlbumDetailInteractor(val albumRepository: AlbumRepository) { - fun getAlbum(albumId: String) = albumRepository.getAlbum(albumId) + fun getAlbum(albumId: String) = albumRepository.get(albumId) } diff --git a/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetArtistDetailInteractor.kt b/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetArtistDetailInteractor.kt index 0f43347..7b9f258 100644 --- a/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetArtistDetailInteractor.kt +++ b/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetArtistDetailInteractor.kt @@ -20,6 +20,6 @@ import com.antonioleiva.bandhookkotlin.domain.repository.ArtistRepository class GetArtistDetailInteractor(val artistRepository: ArtistRepository) { - fun getArtist(artistId: String) = artistRepository.getArtist(artistId) + fun getArtist(artistId: String) = artistRepository.get(artistId) } \ No newline at end of file diff --git a/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/repository/AlbumRepository.kt b/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/repository/AlbumRepository.kt index d9bc9a9..df045a6 100644 --- a/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/repository/AlbumRepository.kt +++ b/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/repository/AlbumRepository.kt @@ -16,11 +16,9 @@ package com.antonioleiva.bandhookkotlin.domain.repository -import com.antonioleiva.bandhookkotlin.Result import com.antonioleiva.bandhookkotlin.domain.entity.Album import com.antonioleiva.bandhookkotlin.domain.entity.BizException.AlbumNotFound -interface AlbumRepository { +interface AlbumRepository : Repository { fun getTopAlbums(artistId: String?, artistName: String?): List - fun getAlbum(id: String): Result } diff --git a/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/repository/ArtistRepository.kt b/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/repository/ArtistRepository.kt index 85e9d7e..57dd184 100644 --- a/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/repository/ArtistRepository.kt +++ b/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/repository/ArtistRepository.kt @@ -16,11 +16,9 @@ package com.antonioleiva.bandhookkotlin.domain.repository -import com.antonioleiva.bandhookkotlin.Result import com.antonioleiva.bandhookkotlin.domain.entity.Artist import com.antonioleiva.bandhookkotlin.domain.entity.BizException.ArtistNotFound -interface ArtistRepository { +interface ArtistRepository : Repository { fun getRecommendedArtists(): List - fun getArtist(id: String): Result } diff --git a/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/repository/Repository.kt b/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/repository/Repository.kt new file mode 100644 index 0000000..12b5e30 --- /dev/null +++ b/app/src/main/java/com/antonioleiva/bandhookkotlin/domain/repository/Repository.kt @@ -0,0 +1,38 @@ +package com.antonioleiva.bandhookkotlin.domain.repository + +import com.antonioleiva.bandhookkotlin.Result +import com.antonioleiva.bandhookkotlin.recoverWith +import com.antonioleiva.bandhookkotlin.repository.datasource.DataSource +import org.funktionale.collections.tail + +/** + * + */ +interface Repository { + val dataSources: List> + + /** + * Trampolined (async) recursion until all DataSources are exhausted or a right result is found + */ + fun recurseDataSources(currentDataSources: List>, + acc: Result, f: + (DataSource) -> Result): Result = + if (currentDataSources.isEmpty()) { + acc + } else { + val current = currentDataSources.get(0) + f(current).recoverWith { + recurseDataSources(currentDataSources.tail(), f(current), f) + } + } + + // TODO: The only missing bit to provide a default implementation for get is a constructor / + // factory for the error + fun get(id: ID): Result +// = recurseDataSources( +// currentDataSources = dataSources, +// acc = ResultT.raiseError(E(id)), +// f = { it.get(id) } +// ) + +} diff --git a/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/AlbumRepositoryImpl.kt b/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/AlbumRepositoryImpl.kt index 127fffe..8fc9613 100644 --- a/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/AlbumRepositoryImpl.kt +++ b/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/AlbumRepositoryImpl.kt @@ -21,35 +21,20 @@ import com.antonioleiva.bandhookkotlin.ResultT import com.antonioleiva.bandhookkotlin.domain.entity.Album import com.antonioleiva.bandhookkotlin.domain.entity.BizException.AlbumNotFound import com.antonioleiva.bandhookkotlin.domain.repository.AlbumRepository -import com.antonioleiva.bandhookkotlin.recoverWith -import com.antonioleiva.bandhookkotlin.repository.dataset.AlbumDataSet -import org.funktionale.collections.tail +import com.antonioleiva.bandhookkotlin.repository.datasource.AlbumDataSource -class AlbumRepositoryImpl(val albumDataSets: List) : AlbumRepository { +class AlbumRepositoryImpl(override val dataSources: List) : AlbumRepository { - /** Trampolined (async) recursion until all DataSets are exhausted or a right result is found */ - fun recurseDataSets(currentDataSets: List, acc: Result, f: (AlbumDataSet) -> Result): Result = - if (currentDataSets.isEmpty()) { - acc - } else { - val current = currentDataSets.get(0) - f(current).recoverWith { - recurseDataSets(currentDataSets.tail(), f(current), f) - } - } - - - override fun getAlbum(id: String): Result = - recurseDataSets( - currentDataSets = albumDataSets, + override fun get(id: String): Result = + recurseDataSources( + currentDataSources = dataSources, acc = ResultT.raiseError(AlbumNotFound(id)), - f = { it.requestAlbum(id) } + f = { it.get(id) } ) - override fun getTopAlbums(artistId: String?, artistName: String?): List { - for (dataSet in albumDataSets) { - val result = dataSet.requestTopAlbums(artistId, artistName) + for (dataSource in dataSources) { + val result = dataSource.requestTopAlbums(artistId, artistName) if (result.isNotEmpty()) { return result } diff --git a/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/ArtistRepositoryImp.kt b/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/ArtistRepositoryImpl.kt similarity index 53% rename from app/src/main/java/com/antonioleiva/bandhookkotlin/repository/ArtistRepositoryImp.kt rename to app/src/main/java/com/antonioleiva/bandhookkotlin/repository/ArtistRepositoryImpl.kt index cf49966..a868425 100644 --- a/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/ArtistRepositoryImp.kt +++ b/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/ArtistRepositoryImpl.kt @@ -21,34 +21,20 @@ import com.antonioleiva.bandhookkotlin.ResultT import com.antonioleiva.bandhookkotlin.domain.entity.Artist import com.antonioleiva.bandhookkotlin.domain.entity.BizException.ArtistNotFound import com.antonioleiva.bandhookkotlin.domain.repository.ArtistRepository -import com.antonioleiva.bandhookkotlin.recoverWith -import com.antonioleiva.bandhookkotlin.repository.dataset.ArtistDataSet -import org.funktionale.collections.tail +import com.antonioleiva.bandhookkotlin.repository.datasource.ArtistDataSource -class ArtistRepositoryImp(val artistDataSets: List) : ArtistRepository { +class ArtistRepositoryImpl(override val dataSources: List) : ArtistRepository { - /** Trampolined (async) recursion until all DataSets are exhausted or a right result is found */ - fun recurseDataSets(currentDataSets: List, acc: Result, f: - (ArtistDataSet) -> Result): Result = - if (currentDataSets.isEmpty()) { - acc - } else { - val current = currentDataSets.get(0) - f(current).recoverWith { - recurseDataSets(currentDataSets.tail(), f(current), f) - } - } - - override fun getArtist(id: String): Result = - recurseDataSets( - currentDataSets = artistDataSets, + override fun get(id: String): Result = + recurseDataSources( + currentDataSources = dataSources, acc = ResultT.raiseError(ArtistNotFound(id)), - f = { it.requestArtist(id) } + f = { it.get(id) } ) override fun getRecommendedArtists(): List { - for (dataSet in artistDataSets) { - val result = dataSet.requestRecommendedArtists() + for (dataSource in dataSources) { + val result = dataSource.requestRecommendedArtists() if (result.isNotEmpty()) { return result } diff --git a/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/dataset/AlbumDataSet.kt b/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/datasource/AlbumDataSource.kt similarity index 80% rename from app/src/main/java/com/antonioleiva/bandhookkotlin/repository/dataset/AlbumDataSet.kt rename to app/src/main/java/com/antonioleiva/bandhookkotlin/repository/datasource/AlbumDataSource.kt index e238d36..a2c2c28 100644 --- a/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/dataset/AlbumDataSet.kt +++ b/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/datasource/AlbumDataSource.kt @@ -14,15 +14,13 @@ * limitations under the License. */ -package com.antonioleiva.bandhookkotlin.repository.dataset +package com.antonioleiva.bandhookkotlin.repository.datasource -import com.antonioleiva.bandhookkotlin.Result import com.antonioleiva.bandhookkotlin.domain.entity.Album import com.antonioleiva.bandhookkotlin.domain.entity.BizException.AlbumNotFound -interface AlbumDataSet { +interface AlbumDataSource : DataSource { fun requestTopAlbums(artistId: String?, artistName: String?): List - fun requestAlbum(mbid: String): Result } diff --git a/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/dataset/ArtistDataSet.kt b/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/datasource/ArtistDataSource.kt similarity index 80% rename from app/src/main/java/com/antonioleiva/bandhookkotlin/repository/dataset/ArtistDataSet.kt rename to app/src/main/java/com/antonioleiva/bandhookkotlin/repository/datasource/ArtistDataSource.kt index 959cf4c..4fd18ca 100644 --- a/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/dataset/ArtistDataSet.kt +++ b/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/datasource/ArtistDataSource.kt @@ -14,15 +14,13 @@ * limitations under the License. */ -package com.antonioleiva.bandhookkotlin.repository.dataset +package com.antonioleiva.bandhookkotlin.repository.datasource -import com.antonioleiva.bandhookkotlin.Result import com.antonioleiva.bandhookkotlin.domain.entity.Artist import com.antonioleiva.bandhookkotlin.domain.entity.BizException.ArtistNotFound -interface ArtistDataSet { +interface ArtistDataSource : DataSource { fun requestRecommendedArtists(): List - fun requestArtist(mbid: String): Result } diff --git a/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/datasource/DataSource.kt b/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/datasource/DataSource.kt new file mode 100644 index 0000000..76717f6 --- /dev/null +++ b/app/src/main/java/com/antonioleiva/bandhookkotlin/repository/datasource/DataSource.kt @@ -0,0 +1,10 @@ +package com.antonioleiva.bandhookkotlin.repository.datasource + +import com.antonioleiva.bandhookkotlin.Result + +/** + * + */ +interface DataSource { + fun get(id: ID): Result +} diff --git a/app/src/test/java/com/antonioleiva/bandhookkotlin/data/CloudAlbumDataSetTest.kt b/app/src/test/java/com/antonioleiva/bandhookkotlin/data/CloudAlbumDataSourceTest.kt similarity index 86% rename from app/src/test/java/com/antonioleiva/bandhookkotlin/data/CloudAlbumDataSetTest.kt rename to app/src/test/java/com/antonioleiva/bandhookkotlin/data/CloudAlbumDataSourceTest.kt index e3646e4..94939b9 100644 --- a/app/src/test/java/com/antonioleiva/bandhookkotlin/data/CloudAlbumDataSetTest.kt +++ b/app/src/test/java/com/antonioleiva/bandhookkotlin/data/CloudAlbumDataSourceTest.kt @@ -26,23 +26,23 @@ import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mock import org.mockito.Mockito.* -import org.mockito.runners.MockitoJUnitRunner +import org.mockito.junit.MockitoJUnitRunner import retrofit2.Response @RunWith(MockitoJUnitRunner::class) -class CloudAlbumDataSetTest { +class CloudAlbumDataSourceTest { @Mock lateinit var lastFmService: LastFmService - lateinit var cloudAlbumDataSet: CloudAlbumDataSet + lateinit var cloudAlbumDataSource: CloudAlbumDataSource lateinit var lastFmResponse: LastFmResponse lateinit var knownAlbumDetail: LastFmAlbumDetail lateinit var unknownAlbumDetail: LastFmAlbumDetail lateinit var album: LastFmAlbum lateinit var artist: LastFmArtist lateinit var topAlbums: LastFmTopAlbums - lateinit var albums: List + lateinit var albums: List private val albumMapper = AlbumMapper() private val albumMbid = "album mbid" @@ -51,7 +51,7 @@ class CloudAlbumDataSetTest { @Before fun setUp() { - cloudAlbumDataSet = CloudAlbumDataSet(lastFmService) + cloudAlbumDataSource = CloudAlbumDataSource(lastFmService) artist = LastFmArtist(artistName, artistMbid, "artist url", emptyList(), null, null) album = LastFmAlbum("album name", albumMbid, "album url", @@ -73,7 +73,7 @@ class CloudAlbumDataSetTest { @Test fun testRequestAlbum_knownAlbum() { // When - val album = cloudAlbumDataSet.requestAlbum(albumMbid) + val album = cloudAlbumDataSource.get(albumMbid) // Then verify(lastFmService).requestAlbum(albumMbid) @@ -88,7 +88,7 @@ class CloudAlbumDataSetTest { `when`(lastFmService.requestAlbum(albumMbid)).thenReturn(FakeCall(Response.success(unknownAlbumResponse), null)) // When - val album = cloudAlbumDataSet.requestAlbum(albumMbid) + val album = cloudAlbumDataSource.get(albumMbid) // Then verify(lastFmService).requestAlbum(albumMbid) @@ -98,7 +98,7 @@ class CloudAlbumDataSetTest { @Test fun testRequestTopAlbums_byArtistMbid() { // When - val albums = cloudAlbumDataSet.requestTopAlbums(artistMbid, null) + val albums = cloudAlbumDataSource.requestTopAlbums(artistMbid, null) // Then verify(lastFmService).requestAlbums(artistMbid, "") @@ -108,7 +108,7 @@ class CloudAlbumDataSetTest { @Test fun testRequestTopAlbums_byArtistName() { // When - val albums = cloudAlbumDataSet.requestTopAlbums(null, artistName) + val albums = cloudAlbumDataSource.requestTopAlbums(null, artistName) // Then verify(lastFmService).requestAlbums("", artistName) @@ -118,7 +118,7 @@ class CloudAlbumDataSetTest { @Test fun testRequestTopAlbums_byArtistNameAndMbid() { // When - val albums = cloudAlbumDataSet.requestTopAlbums(artistMbid, artistName) + val albums = cloudAlbumDataSource.requestTopAlbums(artistMbid, artistName) // Then verify(lastFmService).requestAlbums(artistMbid, artistName) @@ -128,10 +128,10 @@ class CloudAlbumDataSetTest { @Test fun testRequestTopAlbums_noArguments() { // When - val albums = cloudAlbumDataSet.requestTopAlbums(null, null) + val albums = cloudAlbumDataSource.requestTopAlbums(null, null) // Then verify(lastFmService, never()).requestAlbums(anyString(), anyString()) assertTrue(albums.isEmpty()) } -} \ No newline at end of file +} diff --git a/app/src/test/java/com/antonioleiva/bandhookkotlin/data/CloudArtistDataSetTest.kt b/app/src/test/java/com/antonioleiva/bandhookkotlin/data/CloudArtistDataSourceTest.kt similarity index 83% rename from app/src/test/java/com/antonioleiva/bandhookkotlin/data/CloudArtistDataSetTest.kt rename to app/src/test/java/com/antonioleiva/bandhookkotlin/data/CloudArtistDataSourceTest.kt index 0c2ce92..b567a92 100644 --- a/app/src/test/java/com/antonioleiva/bandhookkotlin/data/CloudArtistDataSetTest.kt +++ b/app/src/test/java/com/antonioleiva/bandhookkotlin/data/CloudArtistDataSourceTest.kt @@ -28,11 +28,11 @@ import org.junit.runner.RunWith import org.mockito.Mock import org.mockito.Mockito.`when` import org.mockito.Mockito.verify -import org.mockito.runners.MockitoJUnitRunner +import org.mockito.junit.MockitoJUnitRunner import retrofit2.Response @RunWith(MockitoJUnitRunner::class) -class CloudArtistDataSetTest { +class CloudArtistDataSourceTest { @Mock lateinit var lastFmService: LastFmService @@ -43,7 +43,7 @@ class CloudArtistDataSetTest { val artistMapper = ArtistMapper() - lateinit var cloudArtistDataSet: CloudArtistDataSet + lateinit var cloudArtistDataSource: CloudArtistDataSource private val artistMbid = "artist mbid" private val language = "lang" @@ -60,26 +60,26 @@ class CloudArtistDataSetTest { lastFmArtist, LastFmTopAlbums(emptyList()), LastFmArtistList(recommendedArtistList), lastFmAlbumDetail) - cloudArtistDataSet = CloudArtistDataSet(language, lastFmService) + cloudArtistDataSource = CloudArtistDataSource(language, lastFmService) - `when`(lastFmService.requestSimilar(cloudArtistDataSet.coldplayMbid)).thenReturn(FakeCall(Response.success(lastFmResponse), null)) + `when`(lastFmService.requestSimilar(cloudArtistDataSource.coldplayMbid)).thenReturn(FakeCall(Response.success(lastFmResponse), null)) `when`(lastFmService.requestArtistInfo(artistMbid, language)).thenReturn(FakeCall(Response.success(lastFmResponse), null)) } @Test fun testRequestRecommendedArtists() { // When - val recommendedArtists = cloudArtistDataSet.requestRecommendedArtists() + val recommendedArtists = cloudArtistDataSource.requestRecommendedArtists() // Then - verify(lastFmService).requestSimilar(cloudArtistDataSet.coldplayMbid) + verify(lastFmService).requestSimilar(cloudArtistDataSource.coldplayMbid) assertEquals(artistMapper.transform(recommendedArtistList), recommendedArtists) } @Test fun testRequestArtist() { // When - val requestedArtist = cloudArtistDataSet.requestArtist(artistMbid) + val requestedArtist = cloudArtistDataSource.get(artistMbid) // Then verify(lastFmService).requestArtistInfo(artistMbid, language) @@ -97,10 +97,10 @@ class CloudArtistDataSetTest { `when`(lastFmService.requestArtistInfo(unknownArtisMbid, language)).thenReturn(FakeCall(Response.success(unknownArtistResponse), null)) // When - val requestedArtist = cloudArtistDataSet.requestArtist(unknownArtisMbid) + val requestedArtist = cloudArtistDataSource.get(unknownArtisMbid) // Then verify(lastFmService).requestArtistInfo(unknownArtisMbid, language) assertNull(requestedArtist) } -} \ No newline at end of file +} diff --git a/app/src/test/java/com/antonioleiva/bandhookkotlin/data/mapper/AlbumMapperTest.kt b/app/src/test/java/com/antonioleiva/bandhookkotlin/data/mapper/AlbumMapperTest.kt index bac0cbd..c4a2be7 100644 --- a/app/src/test/java/com/antonioleiva/bandhookkotlin/data/mapper/AlbumMapperTest.kt +++ b/app/src/test/java/com/antonioleiva/bandhookkotlin/data/mapper/AlbumMapperTest.kt @@ -110,16 +110,19 @@ class AlbumMapperTest { @Test fun testTransformAlbumDetail_validAlbumDetail() { // When - val album = albumMapper.transform(lastFmAlbumDetail) + val albumOption = albumMapper.transform(lastFmAlbumDetail) // Then - assertNotNull(album) - assertEquals(lastFmAlbumDetail.mbid, album?.id) - assertEquals(lastFmAlbumDetail.name, album?.name) - assertNotNull(album?.url) - assertEquals(lastFmAlbumDetail.artist, album?.artist?.name) - assertEquals(lastFmAlbumDetail.tracks.tracks[0].name, album?.tracks?.get(0)?.name) - assertEquals(lastFmAlbumDetail.tracks.tracks[0].duration, album?.tracks?.get(0)?.duration) + assertTrue(albumOption.isDefined()) + albumOption.forEach { + assertEquals(lastFmAlbumDetail.mbid, it.id) + assertEquals(lastFmAlbumDetail.name, it.name) + assertNotNull(it.url) + assertTrue(it.artist.isDefined()) + it.artist.forEach { assertEquals(lastFmAlbumDetail.artist, it.name) } + assertEquals(lastFmAlbumDetail.tracks.tracks[0].name, it.tracks.get(0)?.name) + assertEquals(lastFmAlbumDetail.tracks.tracks[0].duration, it.tracks.get(0)?.duration) + } } @Test @@ -141,8 +144,10 @@ class AlbumMapperTest { assertEquals(lastFmAlbum.mbid, album?.id) assertEquals(lastFmAlbum.name, album?.name) assertNotNull(album?.url) - assertEquals(lastFmAlbum.artist.name, album?.artist?.name) - assertEquals(lastFmAlbum.artist.mbid, album?.artist?.id) + album?.artist?.forEach { + assertEquals(lastFmAlbum.artist.name, it.name) + assertEquals(lastFmAlbum.artist.mbid, it.id) + } assertEquals(lastFmAlbum.tracks?.tracks?.get(0)?.name, album?.tracks?.get(0)?.name) assertEquals(lastFmAlbum.tracks?.tracks?.get(0)?.duration, album?.tracks?.get(0)?.duration) } diff --git a/app/src/test/java/com/antonioleiva/bandhookkotlin/data/mapper/ArtistMapperTest.kt b/app/src/test/java/com/antonioleiva/bandhookkotlin/data/mapper/ArtistMapperTest.kt index 4848986..988251c 100644 --- a/app/src/test/java/com/antonioleiva/bandhookkotlin/data/mapper/ArtistMapperTest.kt +++ b/app/src/test/java/com/antonioleiva/bandhookkotlin/data/mapper/ArtistMapperTest.kt @@ -20,8 +20,7 @@ import com.antonioleiva.bandhookkotlin.data.lastfm.model.LastFmArtist import com.antonioleiva.bandhookkotlin.data.lastfm.model.LastFmBio import com.antonioleiva.bandhookkotlin.data.lastfm.model.LastFmImage import com.antonioleiva.bandhookkotlin.data.lastfm.model.LastFmImageType -import org.junit.Assert.assertEquals -import org.junit.Assert.assertNull +import org.junit.Assert.* import org.junit.Before import org.junit.Test @@ -51,8 +50,8 @@ class ArtistMapperTest { listOf(megaLastFmImage, smallLastFmImage), null, null) artistsList = listOf(validLastFmArtistWithMegaImage, - invalidLastFmArtist, - validLastFmArtistWithoutMegaImage) + invalidLastFmArtist, + validLastFmArtistWithoutMegaImage) artistMapper = ArtistMapper() } @@ -71,25 +70,31 @@ class ArtistMapperTest { @Test fun testTransformArtist_ValidArtistWithMegaImage() { // When - val artist = artistMapper.transform(validLastFmArtistWithMegaImage) + val artistOption = artistMapper.transform(validLastFmArtistWithMegaImage) // Then - assertEquals(validLastFmArtistWithMegaImage.mbid, artist?.id) - assertEquals(validLastFmArtistWithMegaImage.name, artist?.name) - assertEquals(validLastFmArtistWithMegaImage.bio?.content, artist?.bio) - assertEquals(megaImageUrl, artist?.url) + assertTrue(artistOption.isDefined()) + artistOption.forEach { + assertEquals(validLastFmArtistWithMegaImage.mbid, it.id) + assertEquals(validLastFmArtistWithMegaImage.name, it.name) + assertEquals(validLastFmArtistWithMegaImage.bio?.content, it.bio) + assertEquals(megaImageUrl, it.url) + } } @Test fun testTransformArtist_ValidArtistWithoutMegaImage() { // When - val artist = artistMapper.transform(validLastFmArtistWithoutMegaImage) + val artistOption = artistMapper.transform(validLastFmArtistWithMegaImage) // Then - assertEquals(validLastFmArtistWithoutMegaImage.mbid, artist?.id) - assertEquals(validLastFmArtistWithoutMegaImage.name, artist?.name) - assertEquals(validLastFmArtistWithoutMegaImage.bio?.content, artist?.bio) - assertEquals(smallImageUrl, artist?.url) + assertTrue(artistOption.isDefined()) + artistOption.forEach { + assertEquals(validLastFmArtistWithoutMegaImage.mbid, it.id) + assertEquals(validLastFmArtistWithoutMegaImage.name, it.name) + assertEquals(validLastFmArtistWithoutMegaImage.bio?.content, it.bio) + assertEquals(smallImageUrl, it.url) + } } @Test diff --git a/app/src/test/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetAlbumDetailInteractorTest.kt b/app/src/test/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetAlbumDetailInteractorTest.kt index f8ae8a3..1c77b9e 100644 --- a/app/src/test/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetAlbumDetailInteractorTest.kt +++ b/app/src/test/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetAlbumDetailInteractorTest.kt @@ -26,7 +26,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mock import org.mockito.Mockito.`when` -import org.mockito.runners.MockitoJUnitRunner +import org.mockito.junit.MockitoJUnitRunner @RunWith(MockitoJUnitRunner::class) class GetAlbumDetailInteractorTest { @@ -40,7 +40,7 @@ class GetAlbumDetailInteractorTest { @Before fun setUp() { - `when`(albumRepository.getAlbum(albumId)).thenReturn(Album("album id", "album name", + `when`(albumRepository.get(albumId)).thenReturn(Album("album id", "album name", Artist("artist id", "artist name", null, null, null), "album url", emptyList())) getAlbumDetailInteractor = GetAlbumDetailInteractor(albumRepository) diff --git a/app/src/test/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetTopAlbumsInteractorTest.kt b/app/src/test/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetTopAlbumsInteractorTest.kt index 032a1b5..3184219 100644 --- a/app/src/test/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetTopAlbumsInteractorTest.kt +++ b/app/src/test/java/com/antonioleiva/bandhookkotlin/domain/interactor/GetTopAlbumsInteractorTest.kt @@ -26,7 +26,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mock import org.mockito.Mockito.`when` -import org.mockito.runners.MockitoJUnitRunner +import org.mockito.junit.MockitoJUnitRunner @RunWith(MockitoJUnitRunner::class) class GetTopAlbumsInteractorTest { diff --git a/app/src/test/java/com/antonioleiva/bandhookkotlin/repository/AlbumRepositoryImplTest.kt b/app/src/test/java/com/antonioleiva/bandhookkotlin/repository/AlbumRepositoryImplTest.kt index 3a72455..5889720 100644 --- a/app/src/test/java/com/antonioleiva/bandhookkotlin/repository/AlbumRepositoryImplTest.kt +++ b/app/src/test/java/com/antonioleiva/bandhookkotlin/repository/AlbumRepositoryImplTest.kt @@ -19,7 +19,7 @@ package com.antonioleiva.bandhookkotlin.repository import com.antonioleiva.bandhookkotlin.domain.entity.Album import com.antonioleiva.bandhookkotlin.domain.entity.Artist import com.antonioleiva.bandhookkotlin.domain.repository.AlbumRepository -import com.antonioleiva.bandhookkotlin.repository.dataset.AlbumDataSet +import com.antonioleiva.bandhookkotlin.repository.datasource.AlbumDataSource import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Before @@ -27,95 +27,95 @@ import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mock import org.mockito.Mockito.`when` -import org.mockito.runners.MockitoJUnitRunner +import org.mockito.junit.MockitoJUnitRunner @RunWith(MockitoJUnitRunner::class) class AlbumRepositoryImplTest { @Mock - lateinit var firstAlbumDataSet: AlbumDataSet + lateinit var firstAlbumDataSource: AlbumDataSource @Mock - lateinit var secondAlbumDataSet: AlbumDataSet + lateinit var secondAlbumDataSource: AlbumDataSource - lateinit var albumInBothDataSets: Album - lateinit var albumInSecondDataSet: Album + lateinit var albumInBothDataSources: Album + lateinit var albumInSecondDataSource: Album lateinit var albumRepository: AlbumRepository - lateinit var albumsInBothDataSets: List - lateinit var albumsInSecondDataSet: List + lateinit var albumsInBothDataSources: List + lateinit var albumsInSecondDataSource: List - private val albumIdInBothDataSets = "album id" - private val albumIdInSecondDataSet = "second album id" - private val artistIdInBothDataSets = "artist id" - private val artistIdInSecondDataSet = "second artist id" + private val albumIdInBothDataSources = "album id" + private val albumIdInSecondDataSource = "second album id" + private val artistIdInBothDataSources = "artist id" + private val artistIdInSecondDataSource = "second artist id" private val artistName = "artist name" @Before fun setUp() { - albumInBothDataSets = Album(albumIdInBothDataSets, "album name", Artist(artistIdInBothDataSets, artistName), "album url", emptyList()) - albumInSecondDataSet = Album(albumIdInSecondDataSet, "album name", Artist(artistIdInBothDataSets, artistName), "album url", emptyList()) + albumInBothDataSources = Album(albumIdInBothDataSources, "album name", Artist(artistIdInBothDataSources, artistName), "album url", emptyList()) + albumInSecondDataSource = Album(albumIdInSecondDataSource, "album name", Artist(artistIdInBothDataSources, artistName), "album url", emptyList()) mockRequestAlbumReturns() - albumsInBothDataSets = listOf(albumInBothDataSets) - albumsInSecondDataSet = listOf(albumInSecondDataSet) + albumsInBothDataSources = listOf(albumInBothDataSources) + albumsInSecondDataSource = listOf(albumInSecondDataSource) mockRequestTopAlbumsReturns() - albumRepository = AlbumRepositoryImpl(listOf(firstAlbumDataSet, secondAlbumDataSet)) + albumRepository = AlbumRepositoryImpl(listOf(firstAlbumDataSources, secondAlbumDataSource)) } private fun mockRequestTopAlbumsReturns() { - `when`(firstAlbumDataSet.requestTopAlbums(artistIdInBothDataSets, artistName)).thenReturn(albumsInBothDataSets) - `when`(firstAlbumDataSet.requestTopAlbums(null, artistName)).thenReturn(albumsInBothDataSets) - `when`(firstAlbumDataSet.requestTopAlbums(artistIdInBothDataSets, null)).thenReturn(albumsInBothDataSets) - `when`(secondAlbumDataSet.requestTopAlbums(artistIdInBothDataSets, artistName)).thenReturn(albumsInBothDataSets) - `when`(secondAlbumDataSet.requestTopAlbums(null, artistName)).thenReturn(albumsInBothDataSets) - `when`(secondAlbumDataSet.requestTopAlbums(artistIdInBothDataSets, null)).thenReturn(albumsInBothDataSets) - `when`(secondAlbumDataSet.requestTopAlbums(artistIdInSecondDataSet, null)).thenReturn(albumsInSecondDataSet) + `when`(firstAlbumDataSources.requestTopAlbums(artistIdInBothDataSources, artistName)).thenReturn(albumsInBothDataSources) + `when`(firstAlbumDataSources.requestTopAlbums(null, artistName)).thenReturn(albumsInBothDataSources) + `when`(firstAlbumDataSources.requestTopAlbums(artistIdInBothDataSources, null)).thenReturn(albumsInBothDataSources) + `when`(secondAlbumDataSource.requestTopAlbums(artistIdInBothDataSources, artistName)).thenReturn(albumsInBothDataSources) + `when`(secondAlbumDataSource.requestTopAlbums(null, artistName)).thenReturn(albumsInBothDataSources) + `when`(secondAlbumDataSource.requestTopAlbums(artistIdInBothDataSources, null)).thenReturn(albumsInBothDataSources) + `when`(secondAlbumDataSource.requestTopAlbums(artistIdInSecondDataSource, null)).thenReturn(albumsInSecondDataSource) } private fun mockRequestAlbumReturns() { - `when`(firstAlbumDataSet.requestAlbum(albumIdInBothDataSets)).thenReturn(albumInBothDataSets) - `when`(secondAlbumDataSet.requestAlbum(albumIdInBothDataSets)).thenReturn(albumInBothDataSets) - `when`(secondAlbumDataSet.requestAlbum(albumIdInSecondDataSet)).thenReturn(albumInSecondDataSet) + `when`(firstAlbumDataSources.requestAlbum(albumIdInBothDataSources)).thenReturn(albumInBothDataSources) + `when`(secondAlbumDataSources.requestAlbum(albumIdInBothDataSources)).thenReturn(albumInBothDataSources) + `when`(secondAlbumDataSources.requestAlbum(albumIdInSecondDataSource)).thenReturn(albumInSecondDataSource) } @Test - fun testGetAlbum_existingInBothDataSets() { + fun testGetAlbum_existingInBothDataSources() { // When - val album = albumRepository.getAlbum(albumIdInBothDataSets) + val album = albumRepository.getAlbum(albumIdInBothDataSources) // Then - assertEquals(albumInBothDataSets, album) + assertEquals(albumInBothDataSources, album) } @Test - fun testGetAlbum_existingOnlyInSecondDataSet() { + fun testGetAlbum_existingOnlyInSecondDataSource() { // When - val album = albumRepository.getAlbum(albumIdInSecondDataSet) + val album = albumRepository.getAlbum(albumIdInSecondDataSource) // Then - assertEquals(albumInSecondDataSet, album) + assertEquals(albumInSecondDataSource, album) } @Test fun testGetTopAlbums_withArtistId() { // When - val albums = albumRepository.getTopAlbums(artistIdInBothDataSets, null) + val albums = albumRepository.getTopAlbums(artistIdInBothDataSources, null) // Then - assertEquals(albumsInBothDataSets, albums) + assertEquals(albumsInBothDataSources, albums) } @Test - fun testGetTopAlbums_withArtistIdExistingOnlyInSecondDataSet() { + fun testGetTopAlbums_withArtistIdExistingOnlyInSecondDataSource() { // When - val albums = albumRepository.getTopAlbums(artistIdInSecondDataSet, null) + val albums = albumRepository.getTopAlbums(artistIdInSecondDataSource, null) // Then - assertEquals(albumsInSecondDataSet, albums) + assertEquals(albumsInSecondDataSource, albums) } @Test @@ -124,7 +124,7 @@ class AlbumRepositoryImplTest { val albums = albumRepository.getTopAlbums(null, artistName) // Then - assertEquals(albumsInBothDataSets, albums) + assertEquals(albumsInBothDataSources, albums) } @Test @@ -135,4 +135,4 @@ class AlbumRepositoryImplTest { // Then assertTrue(albums.isEmpty()) } -} \ No newline at end of file +} diff --git a/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/adapter/ArtistDetailPagerAdapterTest.kt b/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/adapter/ArtistDetailPagerAdapterTest.kt index ee5c7c9..0e250ed 100644 --- a/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/adapter/ArtistDetailPagerAdapterTest.kt +++ b/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/adapter/ArtistDetailPagerAdapterTest.kt @@ -22,7 +22,7 @@ import org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mock -import org.mockito.runners.MockitoJUnitRunner +import org.mockito.junit.MockitoJUnitRunner @RunWith(MockitoJUnitRunner::class) class ArtistDetailPagerAdapterTest { diff --git a/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/adapter/TracksAdapterTest.kt b/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/adapter/TracksAdapterTest.kt index e9ad4e4..8dc7c60 100644 --- a/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/adapter/TracksAdapterTest.kt +++ b/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/adapter/TracksAdapterTest.kt @@ -28,7 +28,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mock import org.mockito.Mockito.* -import org.mockito.runners.MockitoJUnitRunner +import org.mockito.junit.MockitoJUnitRunner @RunWith(MockitoJUnitRunner::class) class TracksAdapterTest { diff --git a/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/presenter/AlbumPresenterTest.kt b/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/presenter/AlbumPresenterTest.kt index 2956e7b..2fca06c 100644 --- a/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/presenter/AlbumPresenterTest.kt +++ b/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/presenter/AlbumPresenterTest.kt @@ -31,7 +31,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mock import org.mockito.Mockito -import org.mockito.runners.MockitoJUnitRunner +import org.mockito.junit.MockitoJUnitRunner @RunWith(MockitoJUnitRunner::class) class AlbumPresenterTest { diff --git a/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/presenter/ArtistPresenterTest.kt b/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/presenter/ArtistPresenterTest.kt index 619c0c6..bdcf99f 100644 --- a/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/presenter/ArtistPresenterTest.kt +++ b/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/presenter/ArtistPresenterTest.kt @@ -36,7 +36,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mock import org.mockito.Mockito.verify -import org.mockito.runners.MockitoJUnitRunner +import org.mockito.junit.MockitoJUnitRunner @RunWith(MockitoJUnitRunner::class) class ArtistPresenterTest { diff --git a/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/screens/detail/AlbumsFragmentTest.kt b/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/screens/detail/AlbumsFragmentTest.kt index 2168c04..cafe291 100644 --- a/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/screens/detail/AlbumsFragmentTest.kt +++ b/app/src/test/java/com/antonioleiva/bandhookkotlin/ui/screens/detail/AlbumsFragmentTest.kt @@ -30,7 +30,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mock import org.mockito.Mockito.* -import org.mockito.runners.MockitoJUnitRunner +import org.mockito.junit.MockitoJUnitRunner @RunWith(MockitoJUnitRunner::class) class AlbumsFragmentTest {