use of uk.gov.gchq.gaffer.operation.impl.get.GetElements in project Gaffer by gchq.
the class OperationChainHandlerTest method shouldHandleNestedOperationChain.
@Test
public void shouldHandleNestedOperationChain() throws OperationException {
// Given
final OperationChainValidator opChainValidator = mock(OperationChainValidator.class);
final List<OperationChainOptimiser> opChainOptimisers = Collections.emptyList();
final OperationChainHandler opChainHandler = new OperationChainHandler(opChainValidator, opChainOptimisers);
final Context context = mock(Context.class);
final Store store = mock(Store.class);
final User user = mock(User.class);
final StoreProperties storeProperties = new StoreProperties();
final GetAdjacentIds op1 = mock(GetAdjacentIds.class);
final GetElements op2 = mock(GetElements.class);
final Limit op3 = mock(Limit.class);
final OperationChain opChain1 = new OperationChain(Arrays.asList(op1, op2));
final OperationChain opChain2 = new OperationChain(Arrays.asList(opChain1, op3));
final Entity entityA = new Entity.Builder().group(TestGroups.ENTITY).vertex("A").build();
final Entity entityB = new Entity.Builder().group(TestGroups.ENTITY).vertex("B").build();
given(context.getUser()).willReturn(user);
given(store.getProperties()).willReturn(storeProperties);
given(opChainValidator.validate(any(), any(), any())).willReturn(new ValidationResult());
given(store.handleOperation(op1, context)).willReturn(new WrappedCloseableIterable<>(Lists.newArrayList(new EntitySeed("A"), new EntitySeed("B"))));
given(store.handleOperation(op2, context)).willReturn(new WrappedCloseableIterable<>(Lists.newArrayList(entityA, entityB)));
given(store.handleOperation(op3, context)).willReturn(entityA);
// When
final Object result = opChainHandler.doOperation(opChain2, context, store);
// Then
assertSame(entityA, result);
}
use of uk.gov.gchq.gaffer.operation.impl.get.GetElements in project Gaffer by gchq.
the class ScoreOperationChainHandlerTest method shouldExecuteScoreOperationChainContainingNamedOperation.
@Test
public void shouldExecuteScoreOperationChainContainingNamedOperation() throws OperationException {
// Given
final ScoreOperationChainHandler handler = new ScoreOperationChainHandler();
final Map<Class<? extends Operation>, ScoreResolver> resolvers = new HashMap<>();
final ScoreResolver scoreResolver = mock(NamedOperationScoreResolver.class);
final Context context = mock(Context.class);
final Store store = mock(Store.class);
final User user = mock(User.class);
final ScoreOperationChain scoreOperationChain = mock(ScoreOperationChain.class);
final StoreProperties storeProperties = new StoreProperties();
final GetAdjacentIds op1 = mock(GetAdjacentIds.class);
final GetElements op2 = mock(GetElements.class);
final Limit op3 = mock(Limit.class);
final Map<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
opScores.put(Operation.class, 1);
opScores.put(GetAdjacentIds.class, 2);
opScores.put(GetElements.class, 1);
opScores.put(Limit.class, 1);
handler.setOpScores(opScores);
final String opName = "basicOp";
final NamedOperation<Iterable<? extends Element>, Iterable<? extends Element>> namedOp = mock(NamedOperation.class);
namedOp.setOperationName(opName);
resolvers.put(namedOp.getClass(), scoreResolver);
handler.setScoreResolvers(resolvers);
given(scoreResolver.getScore(eq(namedOp), any())).willReturn(3);
final OperationChain opChain = new OperationChain(Arrays.asList(op1, op2, op3, namedOp));
given(context.getUser()).willReturn(user);
Set<String> opAuths = new HashSet<>();
opAuths.add("TEST_USER");
given(user.getOpAuths()).willReturn(opAuths);
given(scoreOperationChain.getOperationChain()).willReturn(opChain);
given(store.getProperties()).willReturn(storeProperties);
// When
final Object result = handler.doOperation(new ScoreOperationChain.Builder().operationChain(opChain).build(), context, store);
// Then
assertEquals(7, result);
}
use of uk.gov.gchq.gaffer.operation.impl.get.GetElements in project Gaffer by gchq.
the class ScoreOperationChainHandlerTest method shouldCorrectlyExecuteScoreOperationChainWhenNamedOperationScoreIsNull.
@Test
public void shouldCorrectlyExecuteScoreOperationChainWhenNamedOperationScoreIsNull() throws OperationException {
// Given
final ScoreOperationChainHandler handler = new ScoreOperationChainHandler();
final Map<Class<? extends Operation>, ScoreResolver> resolvers = new HashMap<>();
final ScoreResolver scoreResolver = mock(NamedOperationScoreResolver.class);
final Context context = mock(Context.class);
final Store store = mock(Store.class);
final User user = mock(User.class);
final ScoreOperationChain scoreOperationChain = mock(ScoreOperationChain.class);
final StoreProperties storeProperties = new StoreProperties();
final GetAdjacentIds op1 = mock(GetAdjacentIds.class);
final GetElements op2 = mock(GetElements.class);
final Limit op3 = mock(Limit.class);
final Map<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
opScores.put(GetAdjacentIds.class, 3);
opScores.put(GetElements.class, 2);
opScores.put(Limit.class, 1);
handler.setOpScores(opScores);
final String opName = "basicOp";
final NamedOperation<Iterable<? extends Element>, Iterable<? extends Element>> namedOp = mock(NamedOperation.class);
namedOp.setOperationName(opName);
resolvers.put(namedOp.getClass(), scoreResolver);
handler.setScoreResolvers(resolvers);
given(scoreResolver.getScore(eq(namedOp), any())).willReturn(null);
final OperationChain opChain = new OperationChain(Arrays.asList(op1, op2, op3, namedOp));
given(context.getUser()).willReturn(user);
Set<String> opAuths = new HashSet<>();
opAuths.add("TEST_USER");
given(user.getOpAuths()).willReturn(opAuths);
given(scoreOperationChain.getOperationChain()).willReturn(opChain);
given(store.getProperties()).willReturn(storeProperties);
// When
final Object result = handler.doOperation(new ScoreOperationChain.Builder().operationChain(opChain).build(), context, store);
// Then
assertEquals(7, result);
}
use of uk.gov.gchq.gaffer.operation.impl.get.GetElements in project Gaffer by gchq.
the class WhileHandlerTest method shouldUpdateTransformInputAndTestAgainstPredicate.
@Test
public void shouldUpdateTransformInputAndTestAgainstPredicate() throws OperationException {
final Edge input = new Edge.Builder().group("testEdge").source("src").dest("dest").directed(true).property("count", 3).build();
final Map<Element, Object> transform = mock(Map.class);
final Map<Element, Object> transformClone = mock(Map.class);
given(transform.shallowClone()).willReturn(transformClone);
final Predicate predicate = new IsMoreThan(2);
final Conditional conditional = new Conditional(predicate, transform);
final Context context = mock(Context.class);
final Store store = mock(Store.class);
final GetElements getElements = mock(GetElements.class);
final While operation = new While.Builder<>().input(input).maxRepeats(1).conditional(conditional).operation(getElements).build();
final WhileHandler handler = new WhileHandler();
// When
handler.doOperation(operation, context, store);
// Then
verify(transformClone).setInput(input);
verify(store).execute(transformClone, context);
}
use of uk.gov.gchq.gaffer.operation.impl.get.GetElements in project Gaffer by gchq.
the class WhileHandlerTest method shouldRepeatWhileConditionIsTrue.
@Test
public void shouldRepeatWhileConditionIsTrue() throws OperationException {
// Given
final List<EntitySeed> input = Collections.singletonList(mock(EntitySeed.class));
final boolean condition = true;
final int maxRepeats = 3;
final GetElements delegate = mock(GetElements.class);
final GetElements delegateClone1 = mock(GetElements.class);
final GetElements delegateClone2 = mock(GetElements.class);
final GetElements delegateClone3 = mock(GetElements.class);
final Context context = mock(Context.class);
final Store store = mock(Store.class);
given(delegate.shallowClone()).willReturn(delegateClone1, delegateClone2, delegateClone3);
final CloseableIterable result1 = mock(CloseableIterable.class);
final CloseableIterable result2 = mock(CloseableIterable.class);
final CloseableIterable result3 = mock(CloseableIterable.class);
given(store.execute(delegateClone1, context)).willReturn(result1);
given(store.execute(delegateClone2, context)).willReturn(result2);
given(store.execute(delegateClone3, context)).willReturn(result3);
final While operation = new While.Builder<>().input(input).condition(condition).maxRepeats(maxRepeats).operation(delegate).build();
final WhileHandler handler = new WhileHandler();
// When
final Object result = handler.doOperation(operation, context, store);
// Then
verify(delegateClone1, times(1)).getInput();
verify(delegateClone2, times(1)).getInput();
verify(delegateClone3, times(1)).getInput();
verify(store).execute((Output) delegateClone1, context);
verify(store).execute((Output) delegateClone2, context);
verify(store).execute((Output) delegateClone3, context);
assertSame(result3, result);
}
Aggregations