Search in sources :

Example 41 with ElementFilter

use of uk.gov.gchq.gaffer.data.element.function.ElementFilter in project Gaffer by gchq.

the class ElementValidatorTest method shouldReturnTrueWhenViewValidateWithValidElement.

@Test
public void shouldReturnTrueWhenViewValidateWithValidElement() {
    // Given
    final View view = mock(View.class);
    final String group = TestGroups.EDGE;
    final Element elm = mock(Element.class);
    final ViewElementDefinition elementDef = mock(ViewElementDefinition.class);
    final ElementFilter filter = mock(ElementFilter.class);
    final ElementValidator validator = new ElementValidator(view);
    given(elm.getGroup()).willReturn(group);
    given(view.getElement(group)).willReturn(elementDef);
    given(elementDef.getPreAggregationFilter()).willReturn(filter);
    given(filter.test(elm)).willReturn(true);
    // When
    final boolean isValid = validator.validate(elm);
    // Then
    assertTrue(isValid);
}
Also used : Element(uk.gov.gchq.gaffer.data.element.Element) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Test(org.junit.jupiter.api.Test)

Example 42 with ElementFilter

use of uk.gov.gchq.gaffer.data.element.function.ElementFilter in project Gaffer by gchq.

the class FederatedOperationHandlerTest method shouldPassGlobalsOnToSubstores.

@Test
public final void shouldPassGlobalsOnToSubstores() throws Exception {
    // Given
    final ElementFilter filter = mock(ElementFilter.class);
    final GlobalViewElementDefinition globalEntitiesAggregate = new GlobalViewElementDefinition.Builder().postAggregationFilter(filter).build();
    final View view = new View.Builder().globalEntities(globalEntitiesAggregate).build();
    final Operation operation = new GetElements.Builder().input("input").view(view).build();
    final OperationChain op = new OperationChain.Builder().first(operation).build();
    Schema unusedSchema = new Schema.Builder().build();
    final Schema concreteSchema = new Schema.Builder().entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().vertex(TestTypes.ID_STRING).aggregate(false).build()).entity(TestGroups.ENTITY + "2", new SchemaEntityDefinition.Builder().vertex(TestTypes.ID_STRING).aggregate(false).build()).type(TestTypes.ID_STRING, new TypeDefinition.Builder().clazz(String.class).build()).build();
    StoreProperties storeProperties = new StoreProperties();
    Store mockStore1 = getMockStore(unusedSchema, storeProperties);
    Store mockStore2 = getMockStore(concreteSchema, storeProperties);
    Graph graph1 = getGraphWithMockStore(mockStore1);
    Graph graph2 = getGraphWithMockStore(mockStore2);
    FederatedStore mockStore = mock(FederatedStore.class);
    LinkedHashSet<Graph> linkedGraphs = Sets.newLinkedHashSet();
    linkedGraphs.add(graph1);
    linkedGraphs.add(graph2);
    when(mockStore.getGraphs(user, null, op)).thenReturn(linkedGraphs);
    final ArgumentCaptor<OperationChain> capturedOperation = ArgumentCaptor.forClass(OperationChain.class);
    // When
    new FederatedOperationHandler().doOperation(op, context, mockStore);
    verify(mockStore2).execute(capturedOperation.capture(), any(Context.class));
    assertEquals(1, capturedOperation.getAllValues().size());
    final OperationChain transformedOpChain = capturedOperation.getAllValues().get(0);
    assertEquals(1, transformedOpChain.getOperations().size());
    assertEquals(GetElements.class, transformedOpChain.getOperations().get(0).getClass());
    final View mergedView = ((GetElements) transformedOpChain.getOperations().get(0)).getView();
    assertTrue(mergedView.getGlobalEntities() == null);
    assertEquals(2, mergedView.getEntities().size());
    assertTrue(mergedView.getEntities().entrySet().stream().allMatch(x -> x.getValue().getPostAggregationFilter() != null));
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE(uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE) StoreUser.testUser(uk.gov.gchq.gaffer.user.StoreUser.testUser) User(uk.gov.gchq.gaffer.user.User) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore) GraphConfig(uk.gov.gchq.gaffer.graph.GraphConfig) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) Graph(uk.gov.gchq.gaffer.graph.Graph) SchemaEntityDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition) BDDMockito.given(org.mockito.BDDMockito.given) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TestGroups(uk.gov.gchq.gaffer.commonutil.TestGroups) LinkedHashSet(java.util.LinkedHashSet) GlobalViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.GlobalViewElementDefinition) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Mockito.when(org.mockito.Mockito.when) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Sets(com.google.common.collect.Sets) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Store(uk.gov.gchq.gaffer.store.Store) Mockito.never(org.mockito.Mockito.never) Operation(uk.gov.gchq.gaffer.operation.Operation) Context(uk.gov.gchq.gaffer.store.Context) Schema(uk.gov.gchq.gaffer.store.schema.Schema) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TestTypes(uk.gov.gchq.gaffer.store.TestTypes) OperationException(uk.gov.gchq.gaffer.operation.OperationException) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) Mockito.mock(org.mockito.Mockito.mock) KEY_OPERATION_OPTIONS_GRAPH_IDS(uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS) Schema(uk.gov.gchq.gaffer.store.schema.Schema) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore) Store(uk.gov.gchq.gaffer.store.Store) Operation(uk.gov.gchq.gaffer.operation.Operation) SchemaEntityDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition) GlobalViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.GlobalViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Graph(uk.gov.gchq.gaffer.graph.Graph) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore) Test(org.junit.jupiter.api.Test)

Example 43 with ElementFilter

use of uk.gov.gchq.gaffer.data.element.function.ElementFilter in project Gaffer by gchq.

the class RetrieveElementsFromFile method call.

@Override
public OperationException call() throws Exception {
    if (null == elementFilter) {
        elementFilter = new ViewElementDefinition.Builder().json(elementDefinitionJson).build().getPreAggregationFilter();
    }
    if (null == schemaUtils) {
        schemaUtils = new SchemaUtils(Schema.fromJson(jsonGafferSchema));
    }
    try {
        final ParquetReader<Element> fileReader = openParquetReader();
        Element e = fileReader.read();
        while (null != e) {
            if (!visibility.isEmpty()) {
                if (isVisible(e)) {
                    if (needsValidatorsAndFiltersApplying) {
                        final String group = e.getGroup();
                        final ElementFilter validatorFilter = gafferSchema.getElement(group).getValidator(false);
                        if (skipValidation || validatorFilter == null || validatorFilter.test(e)) {
                            if (elementFilter == null || elementFilter.test(e)) {
                                ViewUtil.removeProperties(view, e);
                                queue.add(e);
                            }
                        }
                    } else {
                        ViewUtil.removeProperties(view, e);
                        queue.add(e);
                    }
                }
            } else if (needsValidatorsAndFiltersApplying) {
                final String group = e.getGroup();
                final ElementFilter validatorFilter = gafferSchema.getElement(group).getValidator(false);
                if (skipValidation || validatorFilter == null || validatorFilter.test(e)) {
                    if (elementFilter == null || elementFilter.test(e)) {
                        ViewUtil.removeProperties(view, e);
                        queue.add(e);
                    }
                }
            } else {
                ViewUtil.removeProperties(view, e);
                queue.add(e);
            }
            e = fileReader.read();
        }
        fileReader.close();
    } catch (final IOException ignore) {
        LOGGER.error("IOException reading file", ignore);
    // ignore as this file does not exist
    }
    return null;
}
Also used : SchemaUtils(uk.gov.gchq.gaffer.parquetstore.utils.SchemaUtils) Element(uk.gov.gchq.gaffer.data.element.Element) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) IOException(java.io.IOException)

Aggregations

ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)43 Test (org.junit.jupiter.api.Test)35 Schema (uk.gov.gchq.gaffer.store.schema.Schema)11 Element (uk.gov.gchq.gaffer.data.element.Element)10 ValidationResult (uk.gov.gchq.koryphe.ValidationResult)10 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)8 JSONSerialisationTest (uk.gov.gchq.gaffer.JSONSerialisationTest)6 Context (uk.gov.gchq.gaffer.store.Context)6 Store (uk.gov.gchq.gaffer.store.Store)6 Exists (uk.gov.gchq.koryphe.impl.predicate.Exists)6 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)5 ElementTransformer (uk.gov.gchq.gaffer.data.element.function.ElementTransformer)5 GlobalViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.GlobalViewElementDefinition)5 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)5 ExampleFilterFunction (uk.gov.gchq.gaffer.function.ExampleFilterFunction)5 SchemaElementDefinition (uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)5 HashMap (java.util.HashMap)4 TestStore (uk.gov.gchq.gaffer.integration.store.TestStore)4 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)4 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)4