Search in sources :

Example 1 with BasicParser

use of org.apache.metron.parsers.BasicParser in project metron by apache.

the class ParserBoltTest method testFieldTransformationPriorToValidation.

@Test
public void testFieldTransformationPriorToValidation() {
    String sensorType = "dummy";
    RecordingWriter recordingWriter = new RecordingWriter();
    // create a parser which acts like a basic parser but returns no timestamp field.
    BasicParser dummyParser = new BasicParser() {

        @Override
        public void init() {
        }

        @Override
        public List<JSONObject> parse(byte[] rawMessage) {
            return ImmutableList.of(new JSONObject() {

                {
                    put("data", "foo");
                    put("timestampstr", "2016-01-05 17:02:30");
                    put("original_string", "blah");
                }
            });
        }

        @Override
        public void configure(Map<String, Object> config) {
        }
    };
    ParserBolt parserBolt = new ParserBolt("zookeeperUrl", sensorType, dummyParser, new WriterHandler(recordingWriter)) {

        @Override
        protected SensorParserConfig getSensorParserConfig() {
            try {
                return SensorParserConfig.fromBytes(Bytes.toBytes(csvWithFieldTransformations));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        protected ConfigurationsUpdater<ParserConfigurations> createUpdater() {
            return ParserBoltTest.createUpdater();
        }
    };
    parserBolt.setCuratorFramework(client);
    parserBolt.setZKCache(cache);
    parserBolt.prepare(new HashMap(), topologyContext, outputCollector);
    when(t1.getBinary(0)).thenReturn(new byte[] {});
    parserBolt.execute(t1);
    Assert.assertEquals(1, recordingWriter.getRecords().size());
    long expected = 1452013350000L;
    Assert.assertEquals(expected, recordingWriter.getRecords().get(0).get("timestamp"));
}
Also used : BasicParser(org.apache.metron.parsers.BasicParser) JSONObject(org.json.simple.JSONObject) ParserConfigurations(org.apache.metron.common.configuration.ParserConfigurations) IOException(java.io.IOException) BaseBoltTest(org.apache.metron.test.bolt.BaseBoltTest) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)1 ParserConfigurations (org.apache.metron.common.configuration.ParserConfigurations)1 BasicParser (org.apache.metron.parsers.BasicParser)1 BaseBoltTest (org.apache.metron.test.bolt.BaseBoltTest)1 JSONObject (org.json.simple.JSONObject)1 Test (org.junit.Test)1