### Question:
PartiallyFailableResponseCallback extends ResponseCallback<T, RESP> { @Override public void onFailedResponse(InputStream responseInputStream, int responseCode) { if (responseCode == FAILABLE_HTTP_CODE) { onFailableResponse(responseInputStream, responseCode); } else { super.onFailedResponse(responseInputStream, responseCode); } } PartiallyFailableResponseCallback(StreamObserver<T> observer, Serializer serializer, Reader<T, RESP> reader); @VisibleForTesting  PartiallyFailableResponseCallback(StreamObserver<T> observer, Serializer serializer,
            Reader<T, RESP> reader, ErrorsReader errorsReader); @Override void onFailedResponse(InputStream responseInputStream, int responseCode);  }### Answer:
@Test @DisplayName("Parse partially failed response correctly") void onFailableResponsePartiallyFailedTest() throws IOException { InputStream inputStream = mock(InputStream.class); when(inputStream.read(any(byte[].class))).thenReturn(-1); Parser parser = mock(Parser.class); when(serializer.newParser(any(InputStream.class))).thenReturn(parser); TestFailableRestResponse item = newItem(); when(reader.readNextItem()).thenReturn(item); when(reader.hasMoreItems()).thenReturn(true, false); unit.onFailedResponse(inputStream, 400); verify(streamObserver).onNext(item); verify(streamObserver).onCompleted(); }
@Test @DisplayName("Parse failed response correctly if not partially failed") void onFailableResponseFailedTest() throws IOException { InputStream inputStream = mock(InputStream.class); when(inputStream.read(any(byte[].class))).thenReturn(1, -1); Parser parser = mock(Parser.class); when(serializer.newParser(any(InputStream.class))).thenReturn(parser); when(reader.hasMoreItems()).thenReturn(false); when(errorsReader.readFullResponse()).thenReturn(null); unit.onFailedResponse(inputStream, 400); verify(streamObserver).onError(any(MatchbookSDKHttpException.class)); }
@Test @DisplayName("Parse failed response correctly if parsing error in first parse") void onFailableResponseParserInitialisationErrorTest() throws IOException { InputStream inputStream = mock(InputStream.class); when(inputStream.read(any(byte[].class))).thenReturn(-1); Parser parser = mock(Parser.class); when(serializer.newParser(any(InputStream.class))).thenReturn(parser); doThrow(MatchbookSDKParsingException.class).when(reader).init(any(Parser.class)); when(errorsReader.readFullResponse()).thenReturn(null); unit.onFailedResponse(inputStream, 400); verify(streamObserver).onError(any(MatchbookSDKHttpException.class)); }
@Test @DisplayName("I/O error while parsing failable response") void onFailableResponseInputErrorTest() throws IOException { InputStream inputStream = mock(InputStream.class); when(inputStream.read(any(byte[].class))).thenReturn(1, -1); doThrow(IOException.class).when(serializer).newParser(any(InputStream.class)); unit.onFailedResponse(inputStream, 400); verify(streamObserver).onError(any(MatchbookSDKParsingException.class)); }
@Test @DisplayName("I/O error while copying input stream on failable response") void onFailableResponseInputCopyErrorTest() throws IOException { InputStream inputStream = mock(InputStream.class); when(inputStream.read(any(byte[].class))).thenThrow(IOException.class); unit.onFailedResponse(inputStream, 400); verify(streamObserver).onError(any(MatchbookSDKParsingException.class)); }