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));
}
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));
}
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());
}
}
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);
}
}
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);
}
}
Aggregations