package Ġcom . kin vey . java . store . request . save Ċ Ċ import Ġcom . kin vey . java . Abstract Client Ċ import Ġcom . kin vey . java . cache . IC ache Ċ import Ġcom . kin vey . java . model . Kin vey Batch Insert Error Ċ import Ġcom . kin vey . java . model . Kin vey Save Batch Response Ċ import Ġcom . kin vey . java . network . Network Manager Ċ import Ġcom . kin vey . java . store . Write Policy Ċ import Ġcom . kin vey . java . store . request . Person Ċ import Ġcom . kin vey . java . store . requests . data . save . Save List Batch Request Ċ import Ġcom . kin vey . java . sync . Sync Manager Ċ import Ġjunit . framework . TestCase Ċ import Ġorg . junit . Before Ċ import Ġorg . mockito . Argument Matchers .* Ċ import Ġorg . mockito . Mockito Ċ import Ġorg . mockito . Mockito . times Ċ import Ġorg . mockito . Mockito . verify Ċ import Ġorg . power mock . api . mockito . Power Mockito Ċ import Ġjava . io . IOException Ċ Ċ class ĠSave List Batch Request Test Ġ: ĠTestCase () Ġ{ ĊĊĠĠĠ Ġprivate Ġval ĠMAX _ POST _ ITEMS Ġ= Ġ 1 0 0 ĊĊĠĠĠ Ġlateinit Ġvar Ġspy Network Manager : ĠNetwork Manager < Person > ĊĠĠĠ Ġlateinit Ġvar Ġsync Manager : ĠSync Manager ĊĠĠĠ Ġlateinit Ġvar Ġclient : ĠAbstract Client < *> ĊĠĠĠ Ġlateinit Ġvar Ġcache : ĠI Cache < Person > ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġoverride Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ Ġclient Ġ= ĠMockito . mock ( Abstract Client :: class . java ) ĊĠĠĠĠĠĠĠ Ġspy Network Manager Ġ= ĠMockito . mock ( Network Manager :: class . java ) Ġas ĠNetwork Manager < Person > ĊĠĠĠĠĠĠĠ Ġsync Manager Ġ= ĠMockito . mock ( Sync Manager :: class . java ) ĊĠĠĠĠĠĠĠ Ġcache Ġ= ĠMockito . mock ( IC ache :: class . java ) Ġas ĠI Cache < Person > ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġtest Force Local () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġids Ġ= Ġ( 1 .. 5 ). map Ġ{ ĠPerson (" user Ġ$ it ") Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMockito . spy ( Save List Batch Request ( cache , Ġspy Network Manager , ĠWrite Policy . FORCE _ LOCAL , Ġids , Ġsync Manager )) ĊĠĠĠĠĠĠĠ Ġrequest . execute () ĊĠĠĠĠĠĠĠ Ġverify ( cache , Ġtimes ( 1 )) ?. save ( any Iterable ()) ĊĠĠĠĠĠĠĠ Ġverify ( sync Manager , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠĠ Ġ? . enqueue Save Requests ( any String (), Ġany ( Network Manager :: class . java ), Ġany List ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġtest Force Network () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġids Ġ= Ġ( 1 .. 5 ). map Ġ{ ĠPerson (" user Ġ$ it ") Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMockito . spy ( Save List Batch Request ( cache , Ġspy Network Manager , ĠWrite Policy . FORCE _ NETWORK , Ġids , Ġsync Manager )) ĊĠĠĠĠĠĠĠ ĠPower Mockito . do Return ( listOf ( Person ())). ` when `( request , Ġ" run Save Items Request ", Ġids , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġrequest . execute () ĊĠĠĠĠĠĠĠ ĠPower Mockito . verify Private ( request , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ? . invoke (" run Save Items Request ", Ġids , Ġfalse ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġtest Force Network _ Multiple Requests () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġids Ġ= Ġ( 1 .. 2 0 0 ). map Ġ{ ĠPerson (" user Ġ$ it ") Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMockito . spy ( Save List Batch Request ( cache , Ġspy Network Manager , ĠWrite Policy . FORCE _ NETWORK , Ġids , Ġsync Manager )) ĊĠĠĠĠĠĠĠ ĠPower Mockito . do Return ( listOf ( Person ())). ` when `( request , Ġ" run Save Items Request ", Ġids , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġrequest . execute () ĊĠĠĠĠĠĠĠ ĠPower Mockito . verify Private ( request , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ? . invoke (" run Save Items Request ", Ġids , Ġfalse ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġtest Local Then Network () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġids Ġ= Ġ( 1 .. 5 ). map Ġ{ ĠPerson (" user Ġ$ it ") Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ ĠMockito . spy ( Save List Batch Request ( cache , Ġspy Network Manager , ĠWrite Policy . LOCAL _ THEN _ NETWORK , Ġids , Ġsync Manager )) ĊĠĠĠĠĠĠĠ ĠPower Mockito . do Return ( ids ). ` when `( cache , Ġ" save ", Ġids ) ĊĠĠĠĠĠĠĠ ĠPower Mockito . do Return ( Kin vey Save Batch Response ( mutable ListOf ( Person ()), Ġnull )). ` when `( request , Ġ" post Save Batch Request ", Ġids , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf < Person >(), ĠlistOf < Kin vey Batch Insert Error >(), Ġtrue ) ĊĠĠĠĠĠĠĠ Ġrequest . execute () ĊĠĠĠĠĠĠĠ Ġverify ( cache , Ġtimes ( 2 )) ?. save ( any List ()) ĊĠĠĠĠĠĠĠ ĠPower Mockito . verify Private ( request , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ? . invoke (" post Save Batch Request ", Ġids , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf < Person >(), ĠlistOf < Kin vey Batch Insert Error >(), Ġtrue ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġtest Local Then Network _ Multiple Requests () Ġ{ ĊĊĠĠĠĠĠĠĠ Ġval Ġids Ġ= Ġ( 1 .. 2 0 0 ). map Ġ{ ĠPerson (" user Ġ$ it ") Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġchunks Ġ= Ġids . chunk ed ( MAX _ POST _ ITEMS ). to Typed Array () Ġ// Ġshould Ġbe Ġ 2 Ġchunks Ġfor Ġpost Ġitems ĊĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMockito . spy ( Save List Batch Request ( cache , Ġspy Network Manager , ĠWrite Policy . LOCAL _ THEN _ NETWORK , Ġids , Ġsync Manager )) ĊĠĠĠĠĠĠĠ ĠPower Mockito . do Return ( ids ). ` when `( cache , Ġ" save ", Ġids ) ĊĠĠĠĠĠĠĠ ĠPower Mockito . do Return ( Kin vey Save Batch Response ( mutable ListOf < Person >(), Ġmutable ListOf ())). ` when `( request , Ġ" post Save Batch Request ", Ġchunks [ 0 ], ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf < Person >(), ĠlistOf < Kin vey Batch Insert Error >(), Ġtrue ) ĊĠĠĠĠĠĠĠ ĠPower Mockito . do Return ( Kin vey Save Batch Response ( mutable ListOf < Person >(), Ġmutable ListOf ())). ` when `( request , Ġ" post Save Batch Request ", Ġchunks [ 1 ], ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf < Person >(), ĠlistOf < Kin vey Batch Insert Error >(), Ġtrue ) ĊĊĠĠĠĠĠĠĠ Ġrequest . execute () ĊĊĠĠĠĠĠĠĠ Ġverify ( cache , Ġtimes ( 2 )) ?. save ( any List ()) ĊĊĠĠĠĠĠĠĠ Ġ// Ġtest Ġof Ġsaving Ġfirst Ġchunk ĊĠĠĠĠĠĠĠ ĠPower Mockito . verify Private ( request , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ? . invoke (" post Save Batch Request ", Ġchunks [ 0 ], ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf < Person >(), ĠlistOf < Kin vey Batch Insert Error >(), Ġtrue ) ĊĠĠĠĠĠĠĠ Ġ// Ġtest Ġof Ġsaving Ġsecond Ġchunk ĊĠĠĠĠĠĠĠ ĠPower Mockito . verify Private ( request , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ? . invoke (" post Save Batch Request ", Ġchunks [ 1 ], ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf < Person >(), ĠlistOf < Kin vey Batch Insert Error >(), Ġtrue ) ĊĠĠĠ Ġ} Ċ }