Skip to content

Commit

Permalink
Fix FailureCollectorWrapper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amar3tto committed Oct 13, 2022
1 parent 7105687 commit e392e6b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.cdap.cdap.etl.api.validation.ValidationFailure;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;

/** Class FailureCollectorWrapper is a class for collecting ValidationFailure. */
Expand All @@ -46,12 +47,16 @@ public ValidationException getOrThrowException() throws ValidationException {

// We skip schema field validation errors because they are CDAP oriented and don't affect
// anything in our case
for (Iterator<ValidationFailure> iterator = failuresCollection.iterator();
iterator.hasNext(); ) {
String inputField =
iterator.next().getCauses().get(0).getAttribute(CauseAttributes.INPUT_SCHEMA_FIELD);
if (BatchContextImpl.DEFAULT_SCHEMA_FIELD_NAME.equals(inputField)) {
iterator.remove();
for (Iterator<ValidationFailure> failureIterator = failuresCollection.iterator();
failureIterator.hasNext(); ) {
List<ValidationFailure.Cause> causes = failureIterator.next().getCauses();
if (causes != null) {
for (ValidationFailure.Cause cause : causes) {
String inputField = cause.getAttribute(CauseAttributes.INPUT_SCHEMA_FIELD);
if (BatchContextImpl.DEFAULT_SCHEMA_FIELD_NAME.equals(inputField)) {
failureIterator.remove();
}
}
}
}
if (failuresCollection.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import io.cdap.cdap.etl.api.validation.CauseAttributes;
import io.cdap.cdap.etl.api.validation.ValidationException;
import io.cdap.cdap.etl.api.validation.ValidationFailure;
import java.util.ArrayList;
Expand Down Expand Up @@ -90,4 +91,27 @@ public void getValidationFailures() {
assertEquals(errorMessage, exceptionCollector.get(0).getMessage());
assertEquals(0, emptyExceptionCollector.size());
}

/**
* Should skip schema field validation errors because they are CDAP oriented and don't affect
* anything in our case.
*/
@Test
public void shouldNotThrowForSchemaFieldValidation() {
/** arrange */
FailureCollectorWrapper failureCollectorWrapper = new FailureCollectorWrapper();

/** act */
RuntimeException error = new RuntimeException("An error with cause has occurred");
failureCollectorWrapper.addFailure(error.getMessage(), null);
ValidationFailure failure = failureCollectorWrapper.getValidationFailures().get(0);
ValidationFailure.Cause cause = new ValidationFailure.Cause();
cause.addAttribute(
CauseAttributes.INPUT_SCHEMA_FIELD, BatchContextImpl.DEFAULT_SCHEMA_FIELD_NAME);
failure.getCauses().add(cause);

/** assert */
failureCollectorWrapper.getOrThrowException();
assertEquals(0, failureCollectorWrapper.getValidationFailures().size());
}
}

0 comments on commit e392e6b

Please sign in to comment.