### Question:
ProtoStorageClient { public <T extends AbstractMessageLite> Maybe<T> read(Parser<T> parser) { return Maybe.fromCallable( () -> { synchronized (this) { try (FileInputStream inputStream = application.openFileInput(fileName)) { return parser.parseFrom(inputStream); } catch (InvalidProtocolBufferException | FileNotFoundException e) { Logging.logi("Recoverable exception while reading cache: " + e.getMessage()); return null; } } }); } ProtoStorageClient(Application application, String fileName); Completable write(AbstractMessageLite messageLite); Maybe<T> read(Parser<T> parser);  }### Answer:
@Test public void read_noFailure_readsBytes() { TestObserver<FetchEligibleCampaignsResponse> subscriber = protoStorageClient.read(parser).test(); assertThat(subscriber.getEvents().get(0)).containsExactly(response); }
@Test public void read_onFailure_propagatesException() throws IOException { when(application.openFileInput(FILE_NAME)).thenThrow(new NullPointerException()); TestObserver<FetchEligibleCampaignsResponse> subscriber = protoStorageClient.read(parser).test(); subscriber.assertError(NullPointerException.class); }
@Test public void read_onFileNotFound_absorbsError() throws IOException { when(application.openFileInput(FILE_NAME)).thenThrow(new FileNotFoundException()); TestObserver<FetchEligibleCampaignsResponse> subscriber = protoStorageClient.read(parser).test(); subscriber.assertNoErrors(); subscriber.assertNoValues(); }
@Test public void read_onParsingException_absorbsError() throws IOException { when(parser.parseFrom(fileInputStream)).thenThrow(new InvalidProtocolBufferException("")); TestObserver<FetchEligibleCampaignsResponse> subscriber = protoStorageClient.read(parser).test(); subscriber.assertNoErrors(); subscriber.assertNoValues(); }
@Test public void read_noFailure_closesStream() throws IOException { protoStorageClient.read(parser).subscribe(); verify(fileInputStream).close(); }
@Test public void read_withFailureOpeningFile_doesNotCloseStream() throws IOException { when(application.openFileInput(FILE_NAME)).thenThrow(new FileNotFoundException()); protoStorageClient.read(parser).subscribe(); verify(fileInputStream, times(0)).close(); }
@Test public void read_withFailureReading_closesStream() throws IOException { doThrow(new InvalidProtocolBufferException("")).when(parser).parseFrom(fileInputStream); protoStorageClient.read(parser).subscribe(); verify(fileInputStream).close(); }