Search in sources :

Example 1 with Validate

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);
}
Also used : CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) Element(uk.gov.gchq.gaffer.data.element.Element) Store(uk.gov.gchq.gaffer.store.Store) Validate(uk.gov.gchq.gaffer.operation.impl.Validate) Test(org.junit.Test)

Example 2 with Validate

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());
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) WrappedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable) Element(uk.gov.gchq.gaffer.data.element.Element) Schema(uk.gov.gchq.gaffer.store.schema.Schema) Store(uk.gov.gchq.gaffer.store.Store) Validate(uk.gov.gchq.gaffer.operation.impl.Validate) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) Test(org.junit.Test)

Example 3 with Validate

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);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Validate(uk.gov.gchq.gaffer.operation.impl.Validate) Element(uk.gov.gchq.gaffer.data.element.Element) Store(uk.gov.gchq.gaffer.store.Store) Test(org.junit.Test)

Example 4 with Validate

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;
}
Also used : Validate(uk.gov.gchq.gaffer.operation.impl.Validate)

Aggregations

Validate (uk.gov.gchq.gaffer.operation.impl.Validate)4 Test (org.junit.Test)3 Element (uk.gov.gchq.gaffer.data.element.Element)3 Store (uk.gov.gchq.gaffer.store.Store)3 Context (uk.gov.gchq.gaffer.store.Context)2 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)1 WrappedCloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable)1 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)1 Schema (uk.gov.gchq.gaffer.store.schema.Schema)1 SchemaElementDefinition (uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)1