### Question:
HeaderBasedTrackerIdFetcher extends TrackerIdFetcher { @Override public String fetch(Http.Context ctx) { if ( config.hasPath(LOGTRACKER_TRACKER_PROVIDER_HEADER) && ctx.request().getHeaders().get(config.getString(LOGTRACKER_TRACKER_PROVIDER_HEADER)).isPresent() ) { return ctx.request() .getHeaders() .get(config.getString(LOGTRACKER_TRACKER_PROVIDER_HEADER)) .filter(StringUtils::isNotEmpty) .get(); } return null; } @Inject  HeaderBasedTrackerIdFetcher(Config config); @Override String fetch(Http.Context ctx);  }### Answer:
@Test public void should_return_the_header_value_when_exists() { when(config.hasPath(anyString())).thenReturn(true); when(headers.get(anyString())).thenReturn(Optional.of("123")); when(ctx.request().getHeaders().get(anyString()).filter(String::isEmpty)).thenReturn(Optional.of("123")); Assert.assertEquals(headerBasedTrackerIdFetcher.fetch(ctx), "123"); }
@Test public void should_return_null_when_header_is_not_configured_in_config() { when(config.hasPath(anyString())).thenReturn(false); Assert.assertNull(headerBasedTrackerIdFetcher.fetch(ctx)); }
@Test public void should_return_null_when_header_is_configured_in_config_but_header_does_not_exist() { when(config.hasPath(anyString())).thenReturn(true); when(headers.get(anyString())).thenReturn(Optional.ofNullable(null)); Assert.assertNull(headerBasedTrackerIdFetcher.fetch(ctx)); }