/** Ċ * ĠCopyright ĠS or am its u ĠCo ., ĠLtd . ĠAll ĠRights ĠReserved . Ċ * ĠSPDX - License - Identifier : ĠGPL - 3 . 0 Ċ */ Ċ Ċ package Ġjp . co . sor am its u . feature _ vot able _ impl . data . repository . datasource Ċ Ċ import Ġandroidx . arch . core . executor . testing . Instant Task Executor Rule Ċ import Ġio . reactivex . Single Ċ import Ġjp . co . sor am its u . common . data . Preferences Ċ import Ġjp . co . sor am its u . common . data . network . dto . Status Dto Ċ import Ġjp . co . sor am its u . feature _ vot able _ impl . data . local . Prefs Votes DataSource Ċ import Ġjp . co . sor am its u . feature _ vot able _ impl . data . network . Project Network Api Ċ import Ġjp . co . sor am its u . feature _ vot able _ impl . data . network . response . Get Project Votes Response Ċ import Ġjp . co . sor am its u . test _ shared . Rx Schedulers Rule Ċ import Ġjunit . framework . Assert . assertEquals Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . rules . Test Rule Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . mockito . Argument Matchers Ċ import Ġorg . mockito . BDD Mockito . any String Ċ import Ġorg . mockito . BDD Mockito . eq Ċ import Ġorg . mockito . BDD Mockito . given Ċ import Ġorg . mockito . BDD Mockito . never Ċ import Ġorg . mockito . BDD Mockito . verify Zero Interactions Ċ import Ġorg . mockito . Mock Ċ import Ġorg . mockito . Mockito Ċ import Ġorg . mockito . Mockito . verify Ċ import Ġorg . mockito . junit . Mockito JUnit Runner Ċ import Ġjava . math . BigDecimal Ċ Ċ @ RunWith ( Mockito JUnit Runner :: class ) Ċ class ĠP refs Votes DataSource Test Ġ{ ĊĊĠĠĠ Ġ@ Rule ĊĠĠĠ Ġ@ Jvm Field ĊĠĠĠ Ġval Ġrule : ĠTest Rule Ġ= ĠInstant Task Executor Rule () ĊĠĠĠ Ġ@ Rule ĊĠĠĠ Ġ@ Jvm Field ĊĠĠĠ Ġval Ġrx Scheduler Rule Ġ= ĠRx Schedulers Rule () ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġpreferences : ĠPreferences ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġnetwork Api : ĠProject Network Api ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġprefs Project DataSource : ĠP refs Votes DataSource ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ Ġgiven ( preferences . getString ( any String ())). willReturn (" 0 ") ĊĊĠĠĠĠĠĠĠ Ġval Ġvotes Ġ= ĠBigDecimal . TEN ĊĠĠĠĠĠĠĠ Ġval Ġstatus Ġ= ĠStatus Dto (" OK ", Ġ" Success ") ĊĊĠĠĠĠĠĠĠ Ġgiven ( network Api . get Votes ()). willReturn ( Single . just ( Get Project Votes Response ( votes , Ġvotes , Ġstatus ))) ĊĊĠĠĠĠĠĠĠ Ġprefs Project DataSource Ġ= ĠP refs Votes DataSource ( network Api , Ġpreferences ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` observed Ġvotes Ġshould Ġbe Ġtriggered Ġafter Ġsync `() Ġ{ ĊĠĠĠĠĠĠĠ Ġvar Ġsync H app ened Ġ= Ġfalse ĊĊĠĠĠĠĠĠĠ Ġprefs Project DataSource . observe Votes (). subscribe Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( sync H app ened ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals (" 1 0 ", Ġit ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġsync H app ened Ġ= Ġtrue ĊĊĠĠĠĠĠĠĠ Ġprefs Project DataSource . sync Votes () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` observed Ġvotes Ġshould Ġbe Ġtriggered Ġwith Ġcache Ġvalue Ġif Ġit Ġexists `() Ġ{ ĊĠĠĠĠĠĠĠ Ġgiven ( preferences . contains ( any String ())). willReturn ( true ) ĊĊĠĠĠĠĠĠĠ Ġprefs Project DataSource . observe Votes (). subscribe Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals (" 0 ", Ġit ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġverify ( network Api , Ġnever ()). get Votes () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` observed Ġvotes Ġshould Ġbe Ġtaken Ġfrom Ġnetwork Ġif Ġno Ġcache Ġis Ġfound `() Ġ{ ĊĠĠĠĠĠĠĠ Ġgiven ( preferences . contains ( any String ())). willReturn ( false ) ĊĊĠĠĠĠĠĠĠ Ġprefs Project DataSource . observe Votes (). subscribe Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals (" 1 0 ", Ġit ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġverify ( network Api ). get Votes () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` save Ġvotes Ġcalled `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġkey Votes Ġ= Ġ" key _ votes " ĊĠĠĠĠĠĠĠ Ġval Ġvotes Ġ= Ġ" 1 " ĊĊĠĠĠĠĠĠĠ Ġprefs Project DataSource . save Votes ( votes ) ĊĊĠĠĠĠĠĠĠ Ġverify ( preferences ). putString ( key Votes , Ġvotes ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` retrieve Ġvotes Ġcalled `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġkey Votes Ġ= Ġ" key _ votes " ĊĠĠĠĠĠĠĠ Ġval Ġvotes Ġ= Ġ" 1 " ĊĠĠĠĠĠĠĠ Ġgiven ( preferences . getString ( key Votes )). willReturn ( votes ) ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( votes , Ġprefs Project DataSource . retrieve Votes ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` save Ġlast Ġreceiver Ġvotes Ġcalled `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġkey Last Votes Ġ= Ġ" key _ projects _ last _ v oted " ĊĠĠĠĠĠĠĠ Ġval Ġvotes Ġ= Ġ" 1 " ĊĊĠĠĠĠĠĠĠ Ġprefs Project DataSource . save Last Received Votes ( votes ) ĊĊĠĠĠĠĠĠĠ Ġverify ( preferences ). putString ( key Last Votes , Ġvotes ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` retrieve Ġlast Ġreceiver Ġvotes Ġcalled `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġkey Last Votes Ġ= Ġ" key _ projects _ last _ v oted " ĊĠĠĠĠĠĠĠ Ġval Ġvotes Ġ= Ġ" 1 " ĊĠĠĠĠĠĠĠ Ġgiven ( preferences . getString ( key Last Votes )). willReturn ( votes ) ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( votes , Ġprefs Project DataSource . retrieve Last Received Votes ()) ĊĠĠĠ Ġ} Ċ } Ċ