### Question:
ImpressionStorageClient { public Completable storeImpression(CampaignImpression impression) { return getAllImpressions() .defaultIfEmpty(EMPTY_IMPRESSIONS) .flatMapCompletable( storedImpressions -> { CampaignImpressionList appendedImpressions = appendImpression(storedImpressions, impression); return storageClient .write(appendedImpressions) .doOnComplete(() -> initInMemCache(appendedImpressions)); }); } @Inject  ImpressionStorageClient(@ImpressionStore ProtoStorageClient storageClient); Completable storeImpression(CampaignImpression impression); Maybe<CampaignImpressionList> getAllImpressions(); Single<Boolean> isImpressed(CampaignProto.ThickContent content); Completable clearImpressions(FetchEligibleCampaignsResponse response);  }### Answer:
@Test public void storeImpression_noErrors_writesToStorage() { impressionStorageClient.storeImpression(campaignImpression).subscribe(); assertThat(wasWritten).isTrue(); }
@Test public void storeImpression_noExistingImpressions_writesToStorage() { when(storageClient.read(any(CampaignImpressionsParser.class))).thenReturn(Maybe.empty()); impressionStorageClient.storeImpression(campaignImpression).subscribe(); assertThat(wasWritten).isTrue(); }
@Test public void storeImpression_noErrors_storesAppendedCampaigns() { when(storageClient.read(any(CampaignImpressionsParser.class))) .thenReturn( Maybe.just( CampaignImpressionList.newBuilder() .addAlreadySeenCampaigns(campaignImpression) .build())); ArgumentCaptor<CampaignImpressionList> campaignImpressionListArgumentCaptor = ArgumentCaptor.forClass(CampaignImpressionList.class); impressionStorageClient.storeImpression(campaignImpression).subscribe(); verify(storageClient).write(campaignImpressionListArgumentCaptor.capture()); assertThat(campaignImpressionListArgumentCaptor.getValue().getAlreadySeenCampaignsList()) .containsExactly(campaignImpression, campaignImpression); }
@Test public void storeImpression_noExistingCampaigns_storesAppendedCampaigns() { when(storageClient.read(any(CampaignImpressionsParser.class))).thenReturn(Maybe.empty()); ArgumentCaptor<CampaignImpressionList> campaignImpressionListArgumentCaptor = ArgumentCaptor.forClass(CampaignImpressionList.class); impressionStorageClient.storeImpression(campaignImpression).subscribe(); verify(storageClient).write(campaignImpressionListArgumentCaptor.capture()); assertThat(campaignImpressionListArgumentCaptor.getValue().getAlreadySeenCampaignsList()) .containsExactly(campaignImpression); }
@Test public void storeImpression_writeErrors_notifiesError() { when(storageClient.write(any(CampaignImpressionList.class))) .thenReturn(Completable.error(new IOException())); TestSubscriber<Object> subscriber = impressionStorageClient.storeImpression(campaignImpression).toFlowable().test(); subscriber.assertError(IOException.class); }
@Test public void storeImpression_readErrors_notifiesError() { when(storageClient.read(any(CampaignImpressionsParser.class))) .thenReturn(Maybe.error(new IOException())); TestSubscriber<Object> subscriber = impressionStorageClient.storeImpression(campaignImpression).toFlowable().test(); subscriber.assertError(IOException.class); }