RemoveHeaderInterceptor implements Interceptor { @Override public List<Event> intercept(final List<Event> events) { for (final Event event : events) { intercept(event); } return events; } private  RemoveHeaderInterceptor(final String withName, final String fromList,
                                  final String listSeparator, final Pattern matchRegex); @Override void initialize(); @Override void close(); @Override List<Event> intercept(final List<Event> events); @Override Event intercept(final Event event);  }
@Test public void testWithName() throws IllegalAccessException, ClassNotFoundException, InstantiationException { final Interceptor removeHeaderInterceptor = new RemoveHeaderIntBuilder() .withName(HEADER4).build(); final Event event1 = buildEventWithHeader(); Assert.assertEquals(HEADER1, event1.getHeaders().get(HEADER1)); Assert.assertEquals(HEADER2, event1.getHeaders().get(HEADER2)); Assert.assertEquals(HEADER3, event1.getHeaders().get(HEADER3)); Assert.assertEquals(HEADER4, event1.getHeaders().get(HEADER4)); Assert.assertEquals(HEADER5, event1.getHeaders().get(HEADER5)); removeHeaderInterceptor.intercept(event1); Assert.assertEquals(HEADER1, event1.getHeaders().get(HEADER1)); Assert.assertEquals(HEADER2, event1.getHeaders().get(HEADER2)); Assert.assertEquals(HEADER3, event1.getHeaders().get(HEADER3)); Assert.assertNull(event1.getHeaders().get(HEADER4)); Assert.assertEquals(HEADER5, event1.getHeaders().get(HEADER5)); final Event event2 = buildEventWithoutHeader(); Assert.assertTrue(event2.getHeaders().isEmpty()); removeHeaderInterceptor.intercept(event2); Assert.assertTrue(event2.getHeaders().isEmpty()); }
@Test public void testFromListWithDefaultSeparator1() throws Exception { final Interceptor removeHeaderInterceptor = new RemoveHeaderIntBuilder() .fromList(HEADER4 + MY_SEPARATOR + HEADER2).build(); final Event event1 = buildEventWithHeader(); Assert.assertEquals(HEADER1, event1.getHeaders().get(HEADER1)); Assert.assertEquals(HEADER2, event1.getHeaders().get(HEADER2)); Assert.assertEquals(HEADER3, event1.getHeaders().get(HEADER3)); Assert.assertEquals(HEADER4, event1.getHeaders().get(HEADER4)); Assert.assertEquals(HEADER5, event1.getHeaders().get(HEADER5)); removeHeaderInterceptor.intercept(event1); Assert.assertEquals(HEADER1, event1.getHeaders().get(HEADER1)); Assert.assertEquals(HEADER2, event1.getHeaders().get(HEADER2)); Assert.assertEquals(HEADER3, event1.getHeaders().get(HEADER3)); Assert.assertEquals(HEADER4, event1.getHeaders().get(HEADER4)); Assert.assertEquals(HEADER5, event1.getHeaders().get(HEADER5)); final Event event2 = buildEventWithoutHeader(); Assert.assertTrue(event2.getHeaders().isEmpty()); removeHeaderInterceptor.intercept(event2); Assert.assertTrue(event2.getHeaders().isEmpty()); }
@Test public void testFromListWithDefaultSeparator2() throws Exception { final Interceptor removeHeaderInterceptor = new RemoveHeaderIntBuilder() .fromList(HEADER4 + DEFAULT_SEPARATOR + HEADER2).build(); final Event event1 = buildEventWithHeader(); Assert.assertEquals(HEADER1, event1.getHeaders().get(HEADER1)); Assert.assertEquals(HEADER2, event1.getHeaders().get(HEADER2)); Assert.assertEquals(HEADER3, event1.getHeaders().get(HEADER3)); Assert.assertEquals(HEADER4, event1.getHeaders().get(HEADER4)); Assert.assertEquals(HEADER5, event1.getHeaders().get(HEADER5)); removeHeaderInterceptor.intercept(event1); Assert.assertEquals(HEADER1, event1.getHeaders().get(HEADER1)); Assert.assertNull(event1.getHeaders().get(HEADER2)); Assert.assertEquals(HEADER3, event1.getHeaders().get(HEADER3)); Assert.assertNull(event1.getHeaders().get(HEADER4)); Assert.assertEquals(HEADER5, event1.getHeaders().get(HEADER5)); final Event event2 = buildEventWithoutHeader(); Assert.assertTrue(event2.getHeaders().isEmpty()); removeHeaderInterceptor.intercept(event2); Assert.assertTrue(event2.getHeaders().isEmpty()); }
@Test public void testFromListWithCustomSeparator1() throws Exception { final Interceptor removeHeaderInterceptor = new RemoveHeaderIntBuilder() .fromList(HEADER4 + MY_SEPARATOR + HEADER2, MY_SEPARATOR).build(); final Event event1 = buildEventWithHeader(); Assert.assertEquals(HEADER1, event1.getHeaders().get(HEADER1)); Assert.assertEquals(HEADER2, event1.getHeaders().get(HEADER2)); Assert.assertEquals(HEADER3, event1.getHeaders().get(HEADER3)); Assert.assertEquals(HEADER4, event1.getHeaders().get(HEADER4)); Assert.assertEquals(HEADER5, event1.getHeaders().get(HEADER5)); removeHeaderInterceptor.intercept(event1); Assert.assertEquals(HEADER1, event1.getHeaders().get(HEADER1)); Assert.assertNull(event1.getHeaders().get(HEADER2)); Assert.assertEquals(HEADER3, event1.getHeaders().get(HEADER3)); Assert.assertNull(event1.getHeaders().get(HEADER4)); Assert.assertEquals(HEADER5, event1.getHeaders().get(HEADER5)); final Event event2 = buildEventWithoutHeader(); Assert.assertTrue(event2.getHeaders().isEmpty()); removeHeaderInterceptor.intercept(event2); Assert.assertTrue(event2.getHeaders().isEmpty()); }
@Test public void testFromListWithCustomSeparator2() throws Exception { final Interceptor removeHeaderInterceptor = new RemoveHeaderIntBuilder() .fromList(HEADER4 + DEFAULT_SEPARATOR + HEADER2, MY_SEPARATOR).build(); final Event event1 = buildEventWithHeader(); Assert.assertEquals(HEADER1, event1.getHeaders().get(HEADER1)); Assert.assertEquals(HEADER2, event1.getHeaders().get(HEADER2)); Assert.assertEquals(HEADER3, event1.getHeaders().get(HEADER3)); Assert.assertEquals(HEADER4, event1.getHeaders().get(HEADER4)); Assert.assertEquals(HEADER5, event1.getHeaders().get(HEADER5)); removeHeaderInterceptor.intercept(event1); Assert.assertEquals(HEADER1, event1.getHeaders().get(HEADER1)); Assert.assertEquals(HEADER2, event1.getHeaders().get(HEADER2)); Assert.assertEquals(HEADER3, event1.getHeaders().get(HEADER3)); Assert.assertEquals(HEADER4, event1.getHeaders().get(HEADER4)); Assert.assertEquals(HEADER5, event1.getHeaders().get(HEADER5)); final Event event2 = buildEventWithoutHeader(); Assert.assertTrue(event2.getHeaders().isEmpty()); removeHeaderInterceptor.intercept(event2); Assert.assertTrue(event2.getHeaders().isEmpty()); }
@Test public void testMatchRegex() throws Exception { final Interceptor removeHeaderInterceptor = new RemoveHeaderIntBuilder() .matchRegex("my-header1.*").build(); final Event event1 = buildEventWithHeader(); Assert.assertEquals(HEADER1, event1.getHeaders().get(HEADER1)); Assert.assertEquals(HEADER2, event1.getHeaders().get(HEADER2)); Assert.assertEquals(HEADER3, event1.getHeaders().get(HEADER3)); Assert.assertEquals(HEADER4, event1.getHeaders().get(HEADER4)); Assert.assertEquals(HEADER5, event1.getHeaders().get(HEADER5)); removeHeaderInterceptor.intercept(event1); Assert.assertNull(event1.getHeaders().get(HEADER1)); Assert.assertNull(event1.getHeaders().get(HEADER2)); Assert.assertNull(event1.getHeaders().get(HEADER3)); Assert.assertEquals(HEADER4, event1.getHeaders().get(HEADER4)); Assert.assertEquals(HEADER5, event1.getHeaders().get(HEADER5)); final Event event2 = buildEventWithoutHeader(); Assert.assertTrue(event2.getHeaders().isEmpty()); removeHeaderInterceptor.intercept(event2); Assert.assertTrue(event2.getHeaders().isEmpty()); }
@Test public void testAll() throws Exception { final Interceptor removeHeaderInterceptor = new RemoveHeaderIntBuilder() .matchRegex("my-header2.*") .fromList(HEADER1 + MY_SEPARATOR + HEADER3, MY_SEPARATOR) .withName(HEADER2).build(); final Event event1 = buildEventWithHeader(); Assert.assertEquals(HEADER1, event1.getHeaders().get(HEADER1)); Assert.assertEquals(HEADER2, event1.getHeaders().get(HEADER2)); Assert.assertEquals(HEADER3, event1.getHeaders().get(HEADER3)); Assert.assertEquals(HEADER4, event1.getHeaders().get(HEADER4)); Assert.assertEquals(HEADER5, event1.getHeaders().get(HEADER5)); removeHeaderInterceptor.intercept(event1); Assert.assertTrue(event1.getHeaders().isEmpty()); final Event event2 = buildEventWithoutHeader(); Assert.assertTrue(event2.getHeaders().isEmpty()); removeHeaderInterceptor.intercept(event2); Assert.assertTrue(event2.getHeaders().isEmpty()); }