### Question:
TimestampInterceptor implements Interceptor { @Override public Event intercept(Event event) { Map<String, String> headers = event.getHeaders(); if (preserveExisting && headers.containsKey(TIMESTAMP)) { } else { long now = System.currentTimeMillis(); headers.put(TIMESTAMP, Long.toString(now)); } return event; } private  TimestampInterceptor(boolean preserveExisting); @Override void initialize(); @Override Event intercept(Event event); @Override List<Event> intercept(List<Event> events); @Override void close();  }### Answer:
@Test public void testBasic() throws ClassNotFoundException, InstantiationException, IllegalAccessException { InterceptorBuilderFactory factory = new InterceptorBuilderFactory(); Interceptor.Builder builder = InterceptorBuilderFactory.newInstance( InterceptorType.TIMESTAMP.toString()); Interceptor interceptor = builder.build(); Event event = EventBuilder.withBody("test event", Charsets.UTF_8); Assert.assertNull(event.getHeaders().get(Constants.TIMESTAMP)); Long now = System.currentTimeMillis(); event = interceptor.intercept(event); String timestampStr = event.getHeaders().get(Constants.TIMESTAMP); Assert.assertNotNull(timestampStr); Assert.assertTrue(Long.parseLong(timestampStr) >= now); }
@Test public void testPreserve() throws ClassNotFoundException, InstantiationException, IllegalAccessException { Context ctx = new Context(); ctx.put("preserveExisting", "true"); InterceptorBuilderFactory factory = new InterceptorBuilderFactory(); Interceptor.Builder builder = InterceptorBuilderFactory.newInstance( InterceptorType.TIMESTAMP.toString()); builder.configure(ctx); Interceptor interceptor = builder.build(); long originalTs = 1L; Event event = EventBuilder.withBody("test event", Charsets.UTF_8); event.getHeaders().put(Constants.TIMESTAMP, Long.toString(originalTs)); Assert.assertEquals(Long.toString(originalTs), event.getHeaders().get(Constants.TIMESTAMP)); Long now = System.currentTimeMillis(); event = interceptor.intercept(event); String timestampStr = event.getHeaders().get(Constants.TIMESTAMP); Assert.assertNotNull(timestampStr); Assert.assertTrue(Long.parseLong(timestampStr) == originalTs); }
@Test public void testClobber() throws ClassNotFoundException, InstantiationException, IllegalAccessException { Context ctx = new Context(); ctx.put("preserveExisting", "false"); InterceptorBuilderFactory factory = new InterceptorBuilderFactory(); Interceptor.Builder builder = InterceptorBuilderFactory.newInstance( InterceptorType.TIMESTAMP.toString()); builder.configure(ctx); Interceptor interceptor = builder.build(); long originalTs = 1L; Event event = EventBuilder.withBody("test event", Charsets.UTF_8); event.getHeaders().put(Constants.TIMESTAMP, Long.toString(originalTs)); Assert.assertEquals(Long.toString(originalTs), event.getHeaders().get(Constants.TIMESTAMP)); Long now = System.currentTimeMillis(); event = interceptor.intercept(event); String timestampStr = event.getHeaders().get(Constants.TIMESTAMP); Assert.assertNotNull(timestampStr); Assert.assertTrue(Long.parseLong(timestampStr) >= now); }