### Question:
AppenderRefFailoverPolicy implements FailoverPolicy<String> { @Override public final void deliver(String failedPayload) { resolveAppender(); doDeliver(failedPayload); } protected  AppenderRefFailoverPolicy(AppenderRef appenderRef, Configuration configuration); @Override final void deliver(String failedPayload); @Override void deliver(ItemSource failedPayload); @Override void deliver(FailedItemSource failedPayload); @PluginBuilderFactory static AppenderRefFailoverPolicy.Builder newBuilder();  }### Answer:
@Test public void deliverStringDeliversToAppenderRef() { Appender appender = mock(Appender.class); when(appender.isStarted()).thenReturn(true); Configuration configuration = mock(Configuration.class); String testAppenderRef = "testAppenderRef"; when(configuration.getAppender(testAppenderRef)).thenReturn(appender); FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration); String failedMessage = UUID.randomUUID().toString(); failoverPolicy.deliver(failedMessage); verify(appender, times(1)).append(any(LogEvent.class)); }
@Test public void deliverItemSourceDelegates() { Appender appender = mock(Appender.class); when(appender.isStarted()).thenReturn(true); Configuration configuration = mock(Configuration.class); String testAppenderRef = "testAppenderRef"; when(configuration.getAppender(testAppenderRef)).thenReturn(appender); FailoverPolicy<String> failoverPolicy = spy(createTestFailoverPolicy(testAppenderRef, configuration)); String failedMessage = UUID.randomUUID().toString(); ItemSource<String> itemSource = new StringItemSource(failedMessage); failoverPolicy.deliver(itemSource); verify(failoverPolicy).deliver(eq(failedMessage)); }
@Test public void deliverFailedItemSourceDelegates() { Appender appender = mock(Appender.class); when(appender.isStarted()).thenReturn(true); Configuration configuration = mock(Configuration.class); String testAppenderRef = "testAppenderRef"; when(configuration.getAppender(testAppenderRef)).thenReturn(appender); FailoverPolicy<String> failoverPolicy = spy(createTestFailoverPolicy(testAppenderRef, configuration)); String failedMessage = UUID.randomUUID().toString(); ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(); buffer.writeBytes(failedMessage.getBytes()); FailedItemSource itemSource = new FailedItemSource<>( new ByteBufItemSource(buffer, source -> {}), new FailedItemInfo(UUID.randomUUID().toString()) ); failoverPolicy.deliver(itemSource); verify(failoverPolicy).deliver(eq(failedMessage)); }
@Test public void resolvesAppenderRefOnlyOnce() { Appender appender = mock(Appender.class); when(appender.isStarted()).thenReturn(true); Configuration configuration = mock(Configuration.class); String testAppenderRef = "testAppenderRef"; when(configuration.getAppender(testAppenderRef)).thenReturn(appender); FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration); String failedMessage = "test failed message"; failoverPolicy.deliver(failedMessage); failoverPolicy.deliver(failedMessage); verify(configuration, times(1)).getAppender(anyString()); verify(appender, times(2)).append(any(LogEvent.class)); }
@Test(expected = ConfigurationException.class) public void throwsExceptionOnUnresolvedAppender() { Appender appender = mock(Appender.class); when(appender.isStarted()).thenReturn(true); Configuration configuration = mock(Configuration.class); String testAppenderRef = "testAppenderRef"; when(configuration.getAppender(testAppenderRef)).thenReturn(null); FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration); String failedMessage = "test failed message"; failoverPolicy.deliver(failedMessage); }