Search in sources :

Example 6 with Interceptor

use of org.apache.flume.interceptor.Interceptor in project apex-malhar by apache.

the class InterceptorTestHelper method testFiles.

public void testFiles() throws IOException, URISyntaxException {
    Properties properties = new Properties();
    properties.load(getClass().getResourceAsStream("/flume/conf/flume-conf.properties"));
    String interceptor = null;
    for (Entry<Object, Object> entry : properties.entrySet()) {
        logger.debug("{} => {}", entry.getKey(), entry.getValue());
        if (builder.getClass().getName().equals(entry.getValue().toString())) {
            String key = entry.getKey().toString();
            if (key.endsWith(".type")) {
                interceptor = key.substring(0, key.length() - "type".length());
                break;
            }
        }
    }
    assertNotNull(builder.getClass().getName(), interceptor);
    @SuppressWarnings({ "null", "ConstantConditions" }) final int interceptorLength = interceptor.length();
    HashMap<String, String> map = new HashMap<String, String>();
    for (Entry<Object, Object> entry : properties.entrySet()) {
        String key = entry.getKey().toString();
        if (key.startsWith(interceptor)) {
            map.put(key.substring(interceptorLength), entry.getValue().toString());
        }
    }
    builder.configure(new Context(map));
    Interceptor interceptorInstance = builder.build();
    URL url = getClass().getResource("/test_data/gentxns/");
    assertNotNull("Generated Transactions", url);
    int records = 0;
    File dir = new File(url.toURI());
    for (File file : dir.listFiles()) {
        records += processFile(file, interceptorInstance);
    }
    Assert.assertEquals("Total Records", 2200, records);
}
Also used : Context(org.apache.flume.Context) HashMap(java.util.HashMap) Properties(java.util.Properties) Interceptor(org.apache.flume.interceptor.Interceptor) File(java.io.File) URL(java.net.URL)

Example 7 with Interceptor

use of org.apache.flume.interceptor.Interceptor in project apex-malhar by apache.

the class InterceptorTestHelper method testIntercept_Event.

public void testIntercept_Event() {
    builder.configure(new Context(context));
    Interceptor interceptor = builder.build();
    assertArrayEquals("Empty Bytes", "\001\001\001".getBytes(), interceptor.intercept(new MyEvent("".getBytes())).getBody());
    assertArrayEquals("One Separator", "\001\001\001".getBytes(), interceptor.intercept(new MyEvent("\002".getBytes())).getBody());
    assertArrayEquals("Two Separators", "\001\001\001".getBytes(), interceptor.intercept(new MyEvent("\002\002".getBytes())).getBody());
    assertArrayEquals("One Field", "\001\001\001".getBytes(), interceptor.intercept(new MyEvent("First".getBytes())).getBody());
    assertArrayEquals("Two Fields", "First\001\001\001".getBytes(), interceptor.intercept(new MyEvent("\002First".getBytes())).getBody());
    assertArrayEquals("Two Fields", "\001\001\001".getBytes(), interceptor.intercept(new MyEvent("First\001".getBytes())).getBody());
    assertArrayEquals("Two Fields", "Second\001\001\001".getBytes(), interceptor.intercept(new MyEvent("First\002Second".getBytes())).getBody());
    assertArrayEquals("Three Fields", "Second\001\001\001".getBytes(), interceptor.intercept(new MyEvent("First\002Second\002".getBytes())).getBody());
    assertArrayEquals("Three Fields", "\001Second\001\001".getBytes(), interceptor.intercept(new MyEvent("First\002\002Second".getBytes())).getBody());
    assertArrayEquals("Four Fields", "\001Second\001\001".getBytes(), interceptor.intercept(new MyEvent("First\002\002Second\002".getBytes())).getBody());
    assertArrayEquals("Five Fields", "\001Second\001\001".getBytes(), interceptor.intercept(new MyEvent("First\002\002Second\002\002".getBytes())).getBody());
    assertArrayEquals("Six Fields", "\001Second\001\001".getBytes(), interceptor.intercept(new MyEvent("First\002\002Second\002\002\002".getBytes())).getBody());
}
Also used : Context(org.apache.flume.Context) Interceptor(org.apache.flume.interceptor.Interceptor)

Aggregations

Context (org.apache.flume.Context)7 Interceptor (org.apache.flume.interceptor.Interceptor)7 HashMap (java.util.HashMap)6 Test (org.junit.Test)5 File (java.io.File)1 URL (java.net.URL)1 Properties (java.util.Properties)1