use of org.apache.metron.test.error.MetronErrorJSONMatcher in project metron by apache.
the class ErrorUtilsTest method handleErrorShouldEmitAndReportError.
@Test
public void handleErrorShouldEmitAndReportError() throws Exception {
Throwable e = new Exception("error");
MetronError error = new MetronError().withMessage("error message").withThrowable(e);
OutputCollector collector = mock(OutputCollector.class);
ErrorUtils.handleError(collector, error);
verify(collector, times(1)).emit(eq(Constants.ERROR_STREAM), argThat(new MetronErrorJSONMatcher(error.getJSONObject())));
verify(collector, times(1)).reportError(any());
}
use of org.apache.metron.test.error.MetronErrorJSONMatcher in project metron by apache.
the class ParserBoltTest method testInvalid.
@Test
public void testInvalid() throws Exception {
String sensorType = "yaf";
ParserBolt parserBolt = new ParserBolt("zookeeperUrl", sensorType, parser, new WriterHandler(writer)) {
@Override
protected ConfigurationsUpdater<ParserConfigurations> createUpdater() {
return ParserBoltTest.createUpdater();
}
};
buildGlobalConfig(parserBolt);
parserBolt.setCuratorFramework(client);
parserBolt.setZKCache(cache);
parserBolt.prepare(new HashMap(), topologyContext, outputCollector);
byte[] sampleBinary = "some binary message".getBytes();
when(tuple.getBinary(0)).thenReturn(sampleBinary);
JSONObject parsedMessage = new JSONObject();
parsedMessage.put("field", "invalidValue");
parsedMessage.put("guid", "this-is-unique-identifier-for-tuple");
List<JSONObject> messageList = new ArrayList<>();
messageList.add(parsedMessage);
when(parser.parseOptional(sampleBinary)).thenReturn(Optional.of(messageList));
when(parser.validate(parsedMessage)).thenReturn(true);
parserBolt.execute(tuple);
MetronError error = new MetronError().withErrorType(Constants.ErrorType.PARSER_INVALID).withSensorType(sensorType).withErrorFields(new HashSet<String>() {
{
add("field");
}
}).addRawMessage(new JSONObject() {
{
put("field", "invalidValue");
put("source.type", "yaf");
put("guid", "this-is-unique-identifier-for-tuple");
}
});
verify(outputCollector, times(1)).emit(eq(Constants.ERROR_STREAM), argThat(new MetronErrorJSONMatcher(error.getJSONObject())));
}
use of org.apache.metron.test.error.MetronErrorJSONMatcher in project metron by apache.
the class WriterBoltTest method testNonBatchErrorPathErrorInWrite.
@Test
public void testNonBatchErrorPathErrorInWrite() throws Exception {
ParserConfigurations configurations = getConfigurations(1);
String sensorType = "test";
Tuple t = mock(Tuple.class);
when(t.toString()).thenReturn("tuple");
when(t.getValueByField(eq("message"))).thenReturn(new JSONObject());
WriterBolt bolt = new WriterBolt(new WriterHandler(writer), configurations, sensorType);
bolt.prepare(new HashMap(), topologyContext, outputCollector);
doThrow(new Exception("write error")).when(writer).write(any(), any(), any());
verify(writer, times(1)).init();
bolt.execute(t);
verify(outputCollector, times(1)).ack(t);
verify(writer, times(1)).write(eq(sensorType), any(), any());
verify(outputCollector, times(1)).reportError(any());
verify(outputCollector, times(0)).fail(any());
MetronError error = new MetronError().withErrorType(Constants.ErrorType.INDEXING_ERROR).withThrowable(new Exception("write error")).withSensorType(Collections.singleton(sensorType)).addRawMessage(new JSONObject());
verify(outputCollector, times(1)).emit(eq(Constants.ERROR_STREAM), argThat(new MetronErrorJSONMatcher(error.getJSONObject())));
}
use of org.apache.metron.test.error.MetronErrorJSONMatcher in project metron by apache.
the class AckTuplesPolicyTest method shouldOnlyReportErrorsOncePerBatch.
@Test
public void shouldOnlyReportErrorsOncePerBatch() {
AckTuplesPolicy ackTuplesPolicy = new AckTuplesPolicy(collector, messageGetStrategy);
JSONObject rawMessage1 = new JSONObject();
JSONObject rawMessage2 = new JSONObject();
rawMessage1.put("value", "rawMessage1");
rawMessage2.put("value", "rawMessage2");
String messageId1 = "messageId1";
String messageId2 = "messageId2";
String messageId3 = "messageId3";
JSONObject message1 = new JSONObject();
JSONObject message2 = new JSONObject();
JSONObject message3 = new JSONObject();
message1.put("value", "message1");
message2.put("value", "message2");
message3.put("value", "message3");
Throwable e1 = new Exception("test exception 1");
Throwable e2 = new Exception("test exception 2");
MetronError expectedError1 = new MetronError().withSensorType(Collections.singleton(sensorType)).withErrorType(Constants.ErrorType.INDEXING_ERROR).withThrowable(e1).withRawMessages(Collections.singletonList(rawMessage1));
MetronError expectedError2 = new MetronError().withSensorType(Collections.singleton(sensorType)).withErrorType(Constants.ErrorType.INDEXING_ERROR).withThrowable(e2).withRawMessages(Collections.singletonList(rawMessage1));
MetronError expectedError3 = new MetronError().withSensorType(Collections.singleton(sensorType)).withErrorType(Constants.ErrorType.INDEXING_ERROR).withThrowable(e1).withRawMessages(Collections.singletonList(rawMessage2));
when(messageGetStrategy.get(tuple1)).thenReturn(rawMessage1);
when(messageGetStrategy.get(tuple2)).thenReturn(rawMessage2);
ackTuplesPolicy.addTupleMessageIds(tuple1, Arrays.asList(messageId1, messageId2));
ackTuplesPolicy.addTupleMessageIds(tuple2, Collections.singletonList(messageId3));
BulkWriterResponse response = new BulkWriterResponse();
response.addError(e1, new MessageId(messageId1));
ackTuplesPolicy.onFlush(sensorType, response);
assertEquals(2, ackTuplesPolicy.getTupleMessageMap().size());
assertEquals(1, ackTuplesPolicy.getTupleErrorMap().size());
verify(collector, times(0)).ack(any());
verify(collector, times(0)).reportError(any());
verify(collector, times(1)).emit(eq(Constants.ERROR_STREAM), new Values(argThat(new MetronErrorJSONMatcher(expectedError1.getJSONObject()))));
response = new BulkWriterResponse();
response.addError(e2, new MessageId(messageId2));
response.addError(e1, new MessageId(messageId3));
ackTuplesPolicy.onFlush(sensorType, response);
assertEquals(0, ackTuplesPolicy.getTupleMessageMap().size());
assertEquals(0, ackTuplesPolicy.getTupleErrorMap().size());
verify(collector, times(1)).ack(tuple1);
verify(collector, times(1)).ack(tuple2);
verify(collector, times(1)).reportError(e1);
verify(collector, times(1)).reportError(e2);
verify(collector, times(1)).emit(eq(Constants.ERROR_STREAM), new Values(argThat(new MetronErrorJSONMatcher(expectedError2.getJSONObject()))));
verify(collector, times(1)).emit(eq(Constants.ERROR_STREAM), new Values(argThat(new MetronErrorJSONMatcher(expectedError3.getJSONObject()))));
verifyNoMoreInteractions(collector);
}
use of org.apache.metron.test.error.MetronErrorJSONMatcher in project metron by apache.
the class ParserBoltTest method testEmpty.
@Test
public void testEmpty() throws Exception {
String sensorType = "yaf";
ParserBolt parserBolt = new ParserBolt("zookeeperUrl", sensorType, parser, new WriterHandler(writer)) {
@Override
protected ConfigurationsUpdater<ParserConfigurations> createUpdater() {
return ParserBoltTest.createUpdater();
}
};
parserBolt.setCuratorFramework(client);
parserBolt.setZKCache(cache);
parserBolt.prepare(new HashMap(), topologyContext, outputCollector);
verify(parser, times(1)).init();
verify(writer, times(1)).init();
byte[] sampleBinary = "some binary message".getBytes();
when(tuple.getBinary(0)).thenReturn(sampleBinary);
when(parser.parseOptional(sampleBinary)).thenReturn(null);
parserBolt.execute(tuple);
verify(parser, times(0)).validate(any());
verify(writer, times(0)).write(eq(sensorType), any(ParserWriterConfiguration.class), eq(tuple), any());
verify(outputCollector, times(1)).ack(tuple);
MetronError error = new MetronError().withErrorType(Constants.ErrorType.PARSER_ERROR).withThrowable(new NullPointerException()).withSensorType(sensorType).addRawMessage(sampleBinary);
verify(outputCollector, times(1)).emit(eq(Constants.ERROR_STREAM), argThat(new MetronErrorJSONMatcher(error.getJSONObject())));
}
Aggregations