package Ġcom . strings . attached . music library . album Ċ Ċ import Ġapp . cash . tur bine . test Ċ import Ġcom . strings . attached . music library . data . album . Album Repository Ċ import Ġcom . strings . attached . music library . data . album . Album Repository Impl Ċ import Ġcom . strings . attached . music library . model . album . Album Detail Ċ import Ġcom . strings . attached . music library . model . album . Album With Tracks Ċ import Ġcom . strings . attached . music library . model . album . Track Ċ import Ġcom . strings . attached . music library . model . album . Tracks Ċ import Ġkotlinx . coroutines . test . run Blocking Test Ċ import Ġorg . hamcrest . Core Matchers .` is ` Ċ import Ġorg . hamcrest . Core Matchers . null Value Ċ import Ġorg . hamcrest . Matcher Assert . assertThat Ċ import Ġorg . hamcrest . Matchers . equalTo Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġjava . io . IOException Ċ Ċ class ĠAlbum Repository Test Ġ{ ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġalbum Repository : ĠAlbum Repository ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġfake Album Dao : ĠFake Album Dao ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġfake Track Dao : ĠFake Track Dao ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġfake ApiService : ĠFake Album Service ĊĊĠĠĠ Ġ// region Ġprepare Ġtest Ġdata ĊĠĠĠ Ġprivate Ġval Ġtrack List Ġ= Ġmutable ListOf ( ĊĠĠĠĠĠĠĠ ĠTrack ( 1 , Ġ 1 , Ġ" track 1 ", Ġ" track Url 1 ", Ġ" 2 3 4 "), ĊĠĠĠĠĠĠĠ ĠTrack ( 2 , Ġ 1 , Ġ" track 2 ", Ġ" track Url 2 ", Ġ" 3 4 "), ĊĠĠĠĠĠĠĠ ĠTrack ( 3 , Ġ 2 , Ġ" track 3 ", Ġ" track Url 3 ", Ġ" 4 1 4 "), ĊĠĠĠĠĠĠĠ ĠTrack ( 4 , Ġ 3 , Ġ" track 4 ", Ġ" track Url 3 ", Ġ" 3 0 0 "), ĊĠĠĠ Ġ) ĊĠĠĠ Ġprivate Ġval Ġalbum List Ġ= Ġmutable ListOf ( ĊĠĠĠĠĠĠĠ ĠAlbum Detail ( ĊĠĠĠĠĠĠĠĠĠĠĠĠ 1 , Ġ" album 1 ", Ġ" artist 1 ", Ġ" url 1 ", Ġempty List (), Ġ" album Url 1 ", Ġ" 1 2 3 ", Ġ 1 2 , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠTrack s ( track List . take ( 2 )) ĊĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠ ĠAlbum Detail ( ĊĠĠĠĠĠĠĠĠĠĠĠĠ 2 , Ġ" album 2 ", Ġ" artist 2 ", Ġ" url 2 ", Ġempty List (), Ġ" album Url 2 ", Ġ" 1 2 ", Ġ 1 2 1 , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠTrack s ( listOf ( track List [ 2 ])) ĊĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠ ĠAlbum Detail ( ĊĠĠĠĠĠĠĠĠĠĠĠĠ 3 , Ġ" album 3 ", Ġ" artist 3 ", Ġ" url 3 ", Ġempty List (), Ġ" album Url 3 ", Ġ" 3 ", Ġ 1 2 4 , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠTrack s ( listOf ( track List [ 3 ])) ĊĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠ ĠAlbum Detail ( 4 , Ġ" album 4 ", Ġ" artist 4 ", Ġ" url 4 ", Ġempty List (), Ġ" album Url 4 ", Ġ" 2 3 ", Ġ 1 1 2 , Ġnull ), ĊĠĠĠĠĠĠĠ ĠAlbum Detail ( 5 , Ġ" album 5 ", Ġ" artist 5 ", Ġ" url 5 ", Ġempty List (), Ġ" album Url 5 ", Ġ" 1 3 ", Ġ 1 4 2 , Ġnull ), ĊĠĠĠ Ġ) ĊĠĠĠ Ġ// endregion ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun Ġsetup () Ġ{ ĊĠĠĠĠĠĠĠ Ġfake Album Dao Ġ= ĠFake Album Dao ( album List ) ĊĠĠĠĠĠĠĠ Ġfake Track Dao Ġ= ĠFake Track Dao ( track List ) ĊĠĠĠĠĠĠĠ Ġfake ApiService Ġ= ĠFake Album Service () ĊĠĠĠĠĠĠĠ Ġalbum Repository Ġ= ĠAlbum Repository Impl ( fake ApiService , Ġfake Album Dao , Ġfake Track Dao ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen Album Is Available In Db Then Return True () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġis Available Ġ= Ġalbum Repository . is Album Available Loc ally (" album 1 ", Ġ" artist 1 ") ĊĠĠĠĠĠĠĠ ĠassertThat ( is Available , Ġ` is `( true )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen Album Is Unavailable In Db Then Return False () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġis Available Ġ= Ġalbum Repository . is Album Available Loc ally (" album 1 2 3 ", Ġ" artist 1 2 3 ") ĊĠĠĠĠĠĠĠ ĠassertThat ( is Available , Ġ` is `( false )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen Save Album Called Then Save Album In Db () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Ġsave Ġa Ġnew Ġalbum Ġin Ġthe ĠDB Ġwith Ġits Ġtracks ĊĠĠĠĠĠĠĠ Ġval Ġtrack List Ġ= ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠTrack ( 1 2 3 , Ġ 3 2 1 , Ġ" track 1 ", Ġ" track Url 1 ", Ġ" 2 3 4 "), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠTrack ( 1 2 4 , Ġ 3 2 1 , Ġ" track 2 ", Ġ" track Url 2 ", Ġ" 3 4 "), ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġalbum With Track sTo Store Ġ= ĠAlbum With Tracks ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġalbum Ġ= ĠAlbum Detail ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ 3 2 1 , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" album Test ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" artist Test ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" url 1 ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġempty List (), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" album Url 1 ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" 1 2 3 ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ 1 2 , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠTrack s ( track List ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtracks Ġ= Ġtrack List ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġalbum Repository . save Album With Tracks Loc ally ( album With Track sTo Store ) ĊĠĠĠĠĠĠĠ Ġ// Ġfind Ġthe Ġsaved Ġalbum Ġin Ġthe ĠDB Ġand Ġverify ĊĠĠĠĠĠĠĠ Ġval Ġstored Album With Tracks Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ Ġfake Album Dao . get Album With Tracks Detail ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġalbum Name Ġ= Ġ" album Test ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġartist Name Ġ= Ġ" artist Test " ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ ĠassertThat ( stored Album With Tracks , Ġ` is `( equalTo ( album With Track sTo Store ))) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen Delete Album Called Then Delete Album From Db () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġalbum ToDelete Ġ= ĠAlbum With Tracks ( album List . first (), Ġtrack List . take ( 2 )) ĊĠĠĠĠĠĠĠ Ġalbum Repository . delete Local Album ( album ToDelete ) ĊĠĠĠĠĠĠĠ Ġval Ġfetch Deleted Album Ġ= Ġfake Album Dao . get Album With Tracks Detail ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġalbum Name Ġ= Ġalbum ToDelete . album . album Name , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġartist Name Ġ= Ġalbum ToDelete . album . artist Name ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ// Ġensure Ġthat Ġthe Ġalbum Ġis Ġdeleted Ġby Ġfetching Ġthe Ġdeleted Ġalbum Ġwhich Ġshould Ġreturn Ġnull ĊĠĠĠĠĠĠĠ ĠassertThat ( fetch Deleted Album , Ġnull Value ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen Fetch Saved Album s Then Return All Local Album s () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġalbum List . clear () Ġ// Ġclear Ġany Ġsaved Ġitems Ġto Ġkeep Ġthe Ġexample Ġsmall ĊĠĠĠĠĠĠĠ Ġval Ġalbum sTo Insert Ġ= ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠAlbum Detail ( 1 , Ġ" album 1 ", Ġ" artist 1 ", Ġ" url 1 ", Ġempty List (), Ġ" album Url 1 ", Ġ" 1 2 3 ", Ġ 1 2 , Ġnull ), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠAlbum Detail ( 2 , Ġ" album 2 ", Ġ" artist 2 ", Ġ" url 2 ", Ġempty List (), Ġ" album Url 2 ", Ġ" 1 2 ", Ġ 1 2 1 , Ġnull ), ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġfake Album Dao . insert ( album sTo Insert ) ĊĠĠĠĠĠĠĠ Ġ// Ġtest ĠKotlin ĠFlow Ġemission ĊĠĠĠĠĠĠĠ Ġalbum Repository . get Loc ally Saved Album s (). test Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( expect Item (). size , Ġ` is `( 2 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġexpect Complete () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen Fetch Album Details Then Return Db Album () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Ġsimulate Ġinternet Ġnot Ġavailable Ġenvironment Ġby Ġthrowing Ġan Ġexception ĊĠĠĠĠĠĠĠ Ġfake ApiService . exception Ġ= ĠIOException (" Internet Ġis Ġunavailable ") ĊĠĠĠĠĠĠĠ Ġval Ġdb Album Result Ġ= Ġalbum Repository . get Album Detail ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġartist Name Ġ= Ġalbum List [ 0 ]. artist Name , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġalbum Name Ġ= Ġalbum List [ 0 ]. album Name ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ ĠassertThat ( db Album Result . is Success , Ġ` is `( true )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen Fetch Album Details Then Return Network Album () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Ġadd Ġnetwork Ġdata ĊĠĠĠĠĠĠĠ Ġfake ApiService . album Detail Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ ĠAlbum Detail ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ 4 8 1 , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Ice D addy ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" G uc ci ĠM ane ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" url 1 ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġempty List (), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" album Url 1 ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" 1 2 3 ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ 1 2 , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠTrack s ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠTrack ( 6 7 8 , Ġ 4 8 1 , Ġ" Pop pin ", Ġ" Pop pin Url ", Ġ" 3 3 4 "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠTrack ( 6 7 9 , Ġ 4 8 1 , Ġ" I ĠGot Ġit ", Ġ" I ĠGot Ġit ĠUrl ", Ġ" 2 1 1 "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġnetwork Album Result Ġ= Ġalbum Repository . get Album Detail ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġartist Name Ġ= Ġalbum List [ 0 ]. artist Name , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġalbum Name Ġ= Ġalbum List [ 0 ]. album Name ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ ĠassertThat ( network Album Result . is Success , Ġ` is `( true )) ĊĠĠĠĠĠĠĠ Ġnetwork Album Result . on Success Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( it . album . album Id , Ġ` is `( 4 8 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( it . album . tracks ?. tracks ?. size , Ġ` is `( 2 )) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen Fetch Album Details Then Return Failure () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Ġsimulate Ġinternet Ġnot Ġavailable Ġenvironment Ġby Ġthrowing Ġan Ġexception ĊĠĠĠĠĠĠĠ Ġfake ApiService . exception Ġ= ĠIOException (" Internet Ġis Ġunavailable ") ĊĠĠĠĠĠĠĠ Ġ// Ġclear ĠDB Ġdata ĊĠĠĠĠĠĠĠ Ġalbum List . clear () ĊĠĠĠĠĠĠĠ Ġval Ġresult Ġ= Ġalbum Repository . get Album Detail (" artist 1 ", Ġ" album 1 ") ĊĠĠĠĠĠĠĠ ĠassertThat ( result . is Failure , Ġ` is `( true )) ĊĠĠĠ Ġ} Ċ }