### Question:
StaticInterceptor implements Interceptor { @Override public Event intercept(Event event) { Map<String, String> headers = event.getHeaders(); String existValue = headers.get(key); if (preserveExisting && existValue != null && ! existValue.trim().equals("")) { return event; } headers.put(key, value); return event; } private  StaticInterceptor(boolean preserveExisting, String key,
      String value); @Override void initialize(); @Override Event intercept(Event event); @Override List<Event> intercept(List<Event> events); @Override void close();  }### Answer:
@Test public void testDefaultKeyValue() throws ClassNotFoundException, InstantiationException, IllegalAccessException { Interceptor.Builder builder = InterceptorBuilderFactory.newInstance( InterceptorType.STATIC.toString()); builder.configure(new Context()); Interceptor interceptor = builder.build(); Event event = EventBuilder.withBody("test", Charsets.UTF_8); Assert.assertNull(event.getHeaders().get(Constants.KEY)); event = interceptor.intercept(event); String val = event.getHeaders().get(Constants.KEY); Assert.assertNotNull(val); Assert.assertEquals(Constants.VALUE, val); }
@Test public void testCustomKeyValue() throws ClassNotFoundException, InstantiationException, IllegalAccessException { Interceptor.Builder builder = InterceptorBuilderFactory.newInstance( InterceptorType.STATIC.toString()); Context ctx = new Context(); ctx.put(Constants.KEY, "myKey"); ctx.put(Constants.VALUE, "myVal"); builder.configure(ctx); Interceptor interceptor = builder.build(); Event event = EventBuilder.withBody("test", Charsets.UTF_8); Assert.assertNull(event.getHeaders().get("myKey")); event = interceptor.intercept(event); String val = event.getHeaders().get("myKey"); Assert.assertNotNull(val); Assert.assertEquals("myVal", val); }
@Test public void testReplace() throws ClassNotFoundException, InstantiationException, IllegalAccessException { Interceptor.Builder builder = InterceptorBuilderFactory.newInstance( InterceptorType.STATIC.toString()); Context ctx = new Context(); ctx.put(Constants.PRESERVE, "false"); ctx.put(Constants.VALUE, "replacement value"); builder.configure(ctx); Interceptor interceptor = builder.build(); Event event = EventBuilder.withBody("test", Charsets.UTF_8); event.getHeaders().put(Constants.KEY, "incumbent value"); Assert.assertNotNull(event.getHeaders().get(Constants.KEY)); event = interceptor.intercept(event); String val = event.getHeaders().get(Constants.KEY); Assert.assertNotNull(val); Assert.assertEquals("replacement value", val); }
@Test public void testPreserve() throws ClassNotFoundException, InstantiationException, IllegalAccessException { Interceptor.Builder builder = InterceptorBuilderFactory.newInstance( InterceptorType.STATIC.toString()); Context ctx = new Context(); ctx.put(Constants.PRESERVE, "true"); ctx.put(Constants.VALUE, "replacement value"); builder.configure(ctx); Interceptor interceptor = builder.build(); Event event = EventBuilder.withBody("test", Charsets.UTF_8); event.getHeaders().put(Constants.KEY, "incumbent value"); Assert.assertNotNull(event.getHeaders().get(Constants.KEY)); event = interceptor.intercept(event); String val = event.getHeaders().get(Constants.KEY); Assert.assertNotNull(val); Assert.assertEquals("incumbent value", val); }