Search in sources :

Example 16 with FederatedStore

use of uk.gov.gchq.gaffer.federatedstore.FederatedStore 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 17 with FederatedStore

use of uk.gov.gchq.gaffer.federatedstore.FederatedStore in project Gaffer by gchq.

the class FederatedOperationHandlerTest method shouldMergeResultsFromFieldObjectsWithGivenGraphIds.

@Test
public final void shouldMergeResultsFromFieldObjectsWithGivenGraphIds() throws Exception {
    // Given
    final Operation op = mock(Operation.class);
    final Operation opClone = mock(Operation.class);
    given(op.getOption(KEY_OPERATION_OPTIONS_GRAPH_IDS)).willReturn("1,3");
    given(op.shallowClone()).willReturn(opClone);
    given(opClone.shallowClone()).willReturn(opClone);
    final OperationChain<?> opChainClone = OperationChain.wrap(opClone);
    Schema unusedSchema = new Schema.Builder().build();
    StoreProperties storeProperties = new StoreProperties();
    Store mockStore1 = getMockStore(unusedSchema, storeProperties);
    Store mockStore2 = getMockStore(unusedSchema, storeProperties);
    Store mockStore3 = getMockStore(unusedSchema, storeProperties);
    Store mockStore4 = getMockStore(unusedSchema, storeProperties);
    Graph graph1 = getGraphWithMockStore(mockStore1);
    Graph graph3 = getGraphWithMockStore(mockStore3);
    FederatedStore mockStore = mock(FederatedStore.class);
    LinkedHashSet<Graph> filteredGraphs = Sets.newLinkedHashSet();
    filteredGraphs.add(graph1);
    filteredGraphs.add(graph3);
    when(mockStore.getGraphs(user, "1,3", op)).thenReturn(filteredGraphs);
    // When
    new FederatedOperationHandler().doOperation(op, context, mockStore);
    verify(mockStore1).execute(eq(opChainClone), any(Context.class));
    verify(mockStore2, never()).execute(eq(opChainClone), any(Context.class));
    verify(mockStore3).execute(eq(opChainClone), any(Context.class));
    verify(mockStore4, never()).execute(eq(opChainClone), any(Context.class));
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Graph(uk.gov.gchq.gaffer.graph.Graph) Schema(uk.gov.gchq.gaffer.store.schema.Schema) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore) Store(uk.gov.gchq.gaffer.store.Store) Operation(uk.gov.gchq.gaffer.operation.Operation) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore) Test(org.junit.jupiter.api.Test)

Example 18 with FederatedStore

use of uk.gov.gchq.gaffer.federatedstore.FederatedStore in project Gaffer by gchq.

the class FederatedOperationHandlerTest method shouldThrowException.

@Test
public void shouldThrowException() throws Exception {
    String message = "test exception";
    final Operation op = mock(Operation.class);
    final String graphID = "1,3";
    given(op.getOption(KEY_OPERATION_OPTIONS_GRAPH_IDS)).willReturn(graphID);
    given(op.shallowClone()).willReturn(op);
    Schema unusedSchema = new Schema.Builder().build();
    StoreProperties storeProperties = new StoreProperties();
    Store mockStoreInner = getMockStore(unusedSchema, storeProperties);
    given(mockStoreInner.execute(any(OperationChain.class), any(Context.class))).willThrow(new RuntimeException(message));
    FederatedStore mockStore = mock(FederatedStore.class);
    HashSet<Graph> filteredGraphs = Sets.newHashSet(getGraphWithMockStore(mockStoreInner));
    when(mockStore.getGraphs(user, graphID, op)).thenReturn(filteredGraphs);
    try {
        new FederatedOperationHandler().doOperation(op, context, mockStore);
        fail("Exception Not thrown");
    } catch (OperationException e) {
        assertEquals(message, e.getCause().getMessage());
    }
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Schema(uk.gov.gchq.gaffer.store.schema.Schema) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore) Store(uk.gov.gchq.gaffer.store.Store) Operation(uk.gov.gchq.gaffer.operation.Operation) Graph(uk.gov.gchq.gaffer.graph.Graph) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) OperationException(uk.gov.gchq.gaffer.operation.OperationException) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore) Test(org.junit.jupiter.api.Test)

Example 19 with FederatedStore

use of uk.gov.gchq.gaffer.federatedstore.FederatedStore in project Gaffer by gchq.

the class FederatedGetTraitsHandler method doOperation.

@Override
public Set<StoreTrait> doOperation(final GetTraits operation, final Context context, final Store store) throws OperationException {
    try {
        FederatedOperationChain<Void, StoreTrait> wrappedFedChain = new FederatedOperationChain.Builder<Void, StoreTrait>().operationChain(OperationChain.wrap(operation)).options(isNull(operation.getOptions()) ? new HashMap<>() : new HashMap<>(operation.getOptions())).build();
        final CloseableIterable<StoreTrait> concatResults = store.execute(wrappedFedChain, context);
        Map<StoreTrait, Integer> rtn;
        if (nonNull(concatResults) && nonNull(concatResults.iterator()) && concatResults.iterator().hasNext()) {
            rtn = Streams.toStream(concatResults).collect(Collectors.toMap(t -> t, ignore -> 1, (existing, replacement) -> existing + replacement));
            long graphIdsSize = ((FederatedStore) store).getGraphs(context.getUser(), operation.getOption(KEY_OPERATION_OPTIONS_GRAPH_IDS), operation).stream().count();
            rtn.values().removeIf(v -> v < graphIdsSize);
        } else {
            rtn = Collections.EMPTY_MAP;
        }
        return rtn.keySet();
    } catch (final Exception e) {
        throw new OperationException("Error getting federated traits.", e);
    }
}
Also used : StoreTrait(uk.gov.gchq.gaffer.store.StoreTrait) FederatedOperationChain(uk.gov.gchq.gaffer.federatedstore.operation.FederatedOperationChain) OperationException(uk.gov.gchq.gaffer.operation.OperationException) OperationException(uk.gov.gchq.gaffer.operation.OperationException) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore)

Example 20 with FederatedStore

use of uk.gov.gchq.gaffer.federatedstore.FederatedStore in project Gaffer by gchq.

the class FederatedChangeGraphAccessHandler method doOperation.

@Override
public Boolean doOperation(final ChangeGraphAccess operation, final Context context, final Store store) throws OperationException {
    try {
        final boolean userRequestingAdminUsage = FederatedStoreUtil.isUserRequestingAdminUsage(operation);
        final FederatedAccess federatedAccess = new FederatedAccess(operation.getGraphAuths(), operation.getOwnerUserId(), operation.getIsPublic(), operation.isDisabledByDefault());
        final User user = context.getUser();
        return ((FederatedStore) store).changeGraphAccess(user, operation.getGraphId(), federatedAccess, userRequestingAdminUsage);
    } catch (final Exception e) {
        throw new OperationException("Error changing graph access", e);
    }
}
Also used : User(uk.gov.gchq.gaffer.user.User) FederatedAccess(uk.gov.gchq.gaffer.federatedstore.FederatedAccess) OperationException(uk.gov.gchq.gaffer.operation.OperationException) OperationException(uk.gov.gchq.gaffer.operation.OperationException) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore)

Aggregations

FederatedStore (uk.gov.gchq.gaffer.federatedstore.FederatedStore)38 Test (org.junit.jupiter.api.Test)25 Context (uk.gov.gchq.gaffer.store.Context)25 Schema (uk.gov.gchq.gaffer.store.schema.Schema)19 Graph (uk.gov.gchq.gaffer.graph.Graph)16 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)12 OperationException (uk.gov.gchq.gaffer.operation.OperationException)12 StoreProperties (uk.gov.gchq.gaffer.store.StoreProperties)11 Store (uk.gov.gchq.gaffer.store.Store)10 FederatedOperationChain (uk.gov.gchq.gaffer.federatedstore.operation.FederatedOperationChain)8 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)7 FederatedStoreProperties (uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties)7 PredefinedFederatedStore (uk.gov.gchq.gaffer.federatedstore.PredefinedFederatedStore)7 Operation (uk.gov.gchq.gaffer.operation.Operation)7 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)6 User (uk.gov.gchq.gaffer.user.User)6 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Element (uk.gov.gchq.gaffer.data.element.Element)4 GraphConfig (uk.gov.gchq.gaffer.graph.GraphConfig)4 GraphSerialisable (uk.gov.gchq.gaffer.graph.GraphSerialisable)4