### Question:
BufferedJestHttpObjectFactory extends JestHttpObjectFactory { protected JestResultHandler<JestResult> createResultHandler(Bulk bulk, Function<Bulk, Boolean> failureHandler) { return new JestResultHandler<JestResult>() { @Override public void completed(JestResult result) { backoffPolicy.deregister(bulk); if (!result.isSucceeded()) { LOG.warn(result.getErrorMessage()); failureHandler.apply(bulk); } ((BufferedBulk)bulk).completed(); } @Override public void failed(Exception ex) { LOG.warn(ex.getMessage(), ex); backoffPolicy.deregister(bulk); failureHandler.apply(bulk); ((BufferedBulk)bulk).completed(); } }; } protected  BufferedJestHttpObjectFactory(Builder builder); @Override Function<Bulk, Boolean> createFailureHandler(FailoverPolicy failover); @Override BatchOperations<Bulk> createBatchOperations(); @PluginBuilderFactory static Builder newBuilder(); @Override void start(); @Override void stop(); @Override boolean isStarted(); @Override boolean isStopped(); static final String PLUGIN_NAME; }### Answer:
@Test public void failoverIsExecutedAfterNonSuccessfulRequest() { BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder(); BufferedJestHttpObjectFactory config = spy(builder.build()); ItemSource<ByteBuf> payload1 = createDefaultTestBuffereItemSource("test1"); ItemSource<ByteBuf> payload2 = createDefaultTestBuffereItemSource("test2"); Bulk bulk = createTestBatch(payload1, payload2); Function<Bulk, Boolean> failoverHandler = mock(Function.class); JestResultHandler<JestResult> resultHandler = config.createResultHandler(bulk, failoverHandler); JestResult result = mock(JestResult.class); when(result.isSucceeded()).thenReturn(false); resultHandler.completed(result); ArgumentCaptor<Bulk> captor = ArgumentCaptor.forClass(Bulk.class); verify(failoverHandler, times(1)).apply(captor.capture()); verify((BufferedBulk)bulk, times(1)).completed(); assertEquals(bulk, captor.getValue()); }
@Test public void failoverIsNotExecutedAfterSuccessfulRequest() { BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder(); BufferedJestHttpObjectFactory config = spy(builder.build()); ItemSource<ByteBuf> payload1 = createDefaultTestBuffereItemSource("test1"); ItemSource<ByteBuf> payload2 = createDefaultTestBuffereItemSource("test2"); Bulk bulk = createTestBatch(payload1, payload2); Function<Bulk, Boolean> failoverHandler = mock(Function.class); JestResultHandler<JestResult> resultHandler = config.createResultHandler(bulk, failoverHandler); JestResult result = mock(JestResult.class); when(result.isSucceeded()).thenReturn(true); resultHandler.completed(result); verify(failoverHandler, never()).apply(Mockito.any(Bulk.class)); }
@Test public void failoverIsExecutedAfterFailedRequest() { BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder(); BufferedJestHttpObjectFactory config = spy(builder.build()); ItemSource<ByteBuf> payload1 = createDefaultTestBuffereItemSource("test1"); ItemSource<ByteBuf> payload2 = createDefaultTestBuffereItemSource("test2"); Bulk bulk = createTestBatch(payload1, payload2); Function<Bulk, Boolean> failoverHandler = mock(Function.class); JestResultHandler<JestResult> resultHandler = config.createResultHandler(bulk, failoverHandler); resultHandler.failed(new IOException()); ArgumentCaptor<Bulk> captor = ArgumentCaptor.forClass(Bulk.class); verify(failoverHandler, times(1)).apply(captor.capture()); verify((BufferedBulk)bulk, times(1)).completed(); assertEquals(bulk, captor.getValue()); }