use of uk.gov.gchq.gaffer.operation.impl.Validate in project Gaffer by gchq.
the class CoreOperationChainOptimiserTest method shouldAddValidateOperationsForAllValidatableOperations.
@Test
public void shouldAddValidateOperationsForAllValidatableOperations() throws Exception {
// Given
final Store store = mock(Store.class);
final CoreOperationChainOptimiser optimiser = new CoreOperationChainOptimiser(store);
final CloseableIterable<Element> elements = mock(CloseableIterable.class);
final Validatable<Integer> validatable1 = mock(Validatable.class);
final Operation<Iterable<Element>, Iterable<Element>> nonValidatable1 = mock(Operation.class);
final Validatable<Iterable<Element>> validatable2 = mock(Validatable.class);
final Validatable<Iterable<Element>> validatable3 = mock(Validatable.class);
final Operation<Iterable<Element>, Iterable<Element>> nonValidatable2 = mock(Operation.class);
final boolean skipInvalidElements = true;
final OperationChain<Integer> opChain = new OperationChain.Builder().first(nonValidatable2).then(validatable3).then(validatable2).then(nonValidatable1).then(validatable1).build();
given(validatable1.getElements()).willReturn(elements);
given(validatable1.isSkipInvalidElements()).willReturn(skipInvalidElements);
given(validatable2.isSkipInvalidElements()).willReturn(skipInvalidElements);
given(validatable1.isValidate()).willReturn(true);
given(validatable2.isValidate()).willReturn(true);
given(validatable3.isValidate()).willReturn(false);
// When
final OperationChain<Integer> optimisedOpChain = optimiser.optimise(opChain);
// Then
assertEquals(7, optimisedOpChain.getOperations().size());
assertSame(nonValidatable2, optimisedOpChain.getOperations().get(0));
assertSame(validatable3, optimisedOpChain.getOperations().get(1));
assertTrue(optimisedOpChain.getOperations().get(2) instanceof Validate);
assertSame(validatable2, optimisedOpChain.getOperations().get(3));
assertSame(nonValidatable1, optimisedOpChain.getOperations().get(4));
assertTrue(optimisedOpChain.getOperations().get(5) instanceof Validate);
assertSame(elements, ((Validate) optimisedOpChain.getOperations().get(5)).getElements());
assertSame(validatable1, optimisedOpChain.getOperations().get(6));
verify(validatable2).setElements(null);
verify(validatable1).setElements(null);
verify(validatable3, never()).setElements(null);
}
use of uk.gov.gchq.gaffer.operation.impl.Validate in project Gaffer by gchq.
the class ValidateHandlerTest method shouldValidatedElements.
@Test
public void shouldValidatedElements() throws OperationException {
// Given
final ValidateHandler handler = new ValidateHandler();
final Store store = mock(Store.class);
final Validate validate = mock(Validate.class);
final Element elm1 = mock(Element.class);
final CloseableIterable<Element> elements = new WrappedCloseableIterable<>(Collections.singletonList(elm1));
final Schema schema = mock(Schema.class);
final Context context = new Context();
given(validate.getElements()).willReturn(elements);
given(validate.isSkipInvalidElements()).willReturn(false);
given(store.getSchema()).willReturn(schema);
final String group = "group";
given(elm1.getGroup()).willReturn(group);
final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class);
final ElementFilter validator = mock(ElementFilter.class);
given(validator.filter(elm1)).willReturn(true);
given(elementDef.getValidator(true)).willReturn(validator);
given(schema.getElement(group)).willReturn(elementDef);
// When
final Iterable<Element> result = handler.doOperation(validate, context, store);
// Then
final Iterator<Element> itr = result.iterator();
final Element elm1Result = itr.next();
assertSame(elm1, elm1Result);
assertFalse(itr.hasNext());
}
use of uk.gov.gchq.gaffer.operation.impl.Validate in project Gaffer by gchq.
the class ValidateHandlerTest method shouldReturnNullIfElementsAreNull.
@Test
public void shouldReturnNullIfElementsAreNull() throws OperationException {
// Given
final ValidateHandler handler = new ValidateHandler();
final Store store = mock(Store.class);
final Validate validate = mock(Validate.class);
given(validate.getElements()).willReturn(null);
final Context context = new Context();
// When
final Iterable<Element> result = handler.doOperation(validate, context, store);
// Then
assertNull(result);
}
use of uk.gov.gchq.gaffer.operation.impl.Validate in project Gaffer by gchq.
the class CoreOperationChainOptimiser method createValidateOperation.
private Validate createValidateOperation(final Validatable<?> currentOp) {
final Validate validate = new Validate(currentOp.isSkipInvalidElements());
validate.setOptions(currentOp.getOptions());
// Move input to new validate operation
validate.setElements(currentOp.getElements());
currentOp.setElements(null);
return validate;
}
Aggregations