use of uk.gov.gchq.gaffer.operation.Operation in project Gaffer by gchq.
the class ScoreOperationChainHandlerTest method shouldReAddDefaultScoreResolversWhenCallingSetMethod.
@Test
public void shouldReAddDefaultScoreResolversWhenCallingSetMethod() {
// Given
final ScoreOperationChainHandler handler = new ScoreOperationChainHandler();
final Map<Class<? extends Operation>, ScoreResolver> defaultResolvers = ScoreOperationChainHandler.getDefaultScoreResolvers();
final Map<Class<? extends Operation>, ScoreResolver> expectedMap = new HashMap<>();
expectedMap.putAll(defaultResolvers);
final Map<Class<? extends Operation>, ScoreResolver> inputMap = new HashMap<>();
inputMap.put(GetElements.class, new DefaultScoreResolver(null));
inputMap.put(GetAllElements.class, new DefaultScoreResolver(null));
expectedMap.putAll(inputMap);
// When
handler.setScoreResolvers(inputMap);
final Map<Class<? extends Operation>, ScoreResolver> results = handler.getScoreResolvers();
// Then
assertEquals(expectedMap.keySet(), results.keySet());
assertTrue(results.get(NamedOperation.class) instanceof NamedOperationScoreResolver);
assertTrue(results.get(If.class) instanceof IfScoreResolver);
assertEquals(expectedMap.size(), results.size());
}
use of uk.gov.gchq.gaffer.operation.Operation in project Gaffer by gchq.
the class GraphTest method shouldCallAllGraphHooksOnExecuteFailureWhenRunningJob.
@Test
public void shouldCallAllGraphHooksOnExecuteFailureWhenRunningJob() throws OperationException {
// Given
final Operation operation = mock(Operation.class);
final OperationChain opChain = mock(OperationChain.class);
final OperationChain clonedOpChain = mock(OperationChain.class);
given(opChain.shallowClone()).willReturn(clonedOpChain);
given(clonedOpChain.getOperations()).willReturn(Lists.newArrayList(operation));
final GraphHook hook1 = mock(GraphHook.class);
final GraphHook hook2 = mock(GraphHook.class);
final Store store = mock(Store.class);
final Schema schema = new Schema();
final RuntimeException e = new RuntimeException("Store failed to execute operation chain");
given(store.getSchema()).willReturn(schema);
given(store.getProperties()).willReturn(new StoreProperties());
given(hook1.onFailure(null, clonedOpChain, clonedContext, e)).willThrow(new RuntimeException("Hook1 failed in onFailure"));
given(hook2.onFailure(null, clonedOpChain, clonedContext, e)).willReturn(null);
final Graph graph = new Graph.Builder().config(new GraphConfig.Builder().graphId(GRAPH_ID).addHook(hook1).addHook(hook2).build()).storeProperties(StreamUtil.storeProps(getClass())).store(store).addSchema(schema).build();
final ArgumentCaptor<OperationChain> captor = ArgumentCaptor.forClass(OperationChain.class);
given(store.executeJob(captor.capture(), eq(clonedContext))).willThrow(e);
// When / Then
try {
graph.executeJob(opChain, context);
fail("Exception expected");
} catch (final RuntimeException runtimeE) {
final InOrder inOrder = inOrder(context, hook1, hook2);
inOrder.verify(context).setOriginalOpChain(opChain);
inOrder.verify(hook1, never()).postExecute(any(), any(), any());
inOrder.verify(hook2, never()).postExecute(any(), any(), any());
inOrder.verify(hook1).onFailure(null, captor.getValue(), clonedContext, e);
inOrder.verify(hook2).onFailure(null, captor.getValue(), clonedContext, e);
final List<Operation> ops = captor.getValue().getOperations();
assertThat(ops).hasSize(1);
assertSame(operation, ops.get(0));
}
}
use of uk.gov.gchq.gaffer.operation.Operation in project Gaffer by gchq.
the class AddOperationsToChainTest method shouldAddIfOperation.
@Test
public void shouldAddIfOperation() throws SerialisationException {
// Given
final GetWalks getWalks = new GetWalks();
final uk.gov.gchq.gaffer.operation.impl.Map map = new uk.gov.gchq.gaffer.operation.impl.Map();
final ToVertices toVertices = new ToVertices();
final ToSet toSet = new ToSet();
final Exists exists = new Exists();
final Limit limit = new Limit();
final GetAllElements getAllElements = new GetAllElements();
final GetElements getElements = new GetElements();
final Conditional conditional = new Conditional();
conditional.setPredicate(exists);
final If ifOp = new If.Builder<>().conditional(conditional).then(getElements).otherwise(getAllElements).build();
final AddOperationsToChain hook = new AddOperationsToChain();
final Map<String, List<Operation>> after = new HashMap<>();
final List<Operation> afterOps = new LinkedList<>();
afterOps.add(ifOp);
afterOps.add(limit);
after.put("uk.gov.gchq.gaffer.operation.impl.output.ToSet", afterOps);
hook.setAfter(after);
final OperationChain opChain = new OperationChain.Builder().first(getWalks).then(map).then(toVertices).then(toSet).build();
// When
hook.preExecute(opChain, new Context());
// Then
final OperationChain expectedOpChain = new OperationChain.Builder().first(getWalks).then(map).then(toVertices).then(toSet).then(ifOp).then(limit).build();
JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain));
}
use of uk.gov.gchq.gaffer.operation.Operation in project Gaffer by gchq.
the class AddOperationsToChainTest method shouldFailQuietlyIfNestedOperationsCannotBeModified.
@Test
public void shouldFailQuietlyIfNestedOperationsCannotBeModified() throws SerialisationException {
// Given
AddOperationsToChain hook = fromJson(ADD_OPERATIONS_TO_CHAIN_RESOURCE_PATH);
Operation discardOutput = new DiscardOutput();
Operation splitStore = new SplitStoreFromFile();
Operation validate = new Validate();
Operation getAdjacentIds = new GetAdjacentIds();
Operation count = new Count<>();
Operation getElements = new GetElements();
Operation getAllElements = new GetAllElements();
TestUnmodifiableOperationsImpl nestedUnmodifiableOps = new TestUnmodifiableOperationsImpl(Arrays.asList(getAllElements, getElements));
final OperationChain opChain = new OperationChain.Builder().first(getAdjacentIds).then(nestedUnmodifiableOps).build();
// When
hook.preExecute(opChain, new Context(new User()));
// Then
final OperationChain expectedOpChain = new OperationChain.Builder().first(discardOutput).then(splitStore).then(validate).then(getAdjacentIds).then(count).then(discardOutput).then(nestedUnmodifiableOps).then(count).build();
JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain));
}
use of uk.gov.gchq.gaffer.operation.Operation in project Gaffer by gchq.
the class AddOperationsToChainTest method shouldHandleIfOperationWithNoConditionalOrOtherwise.
@Test
public void shouldHandleIfOperationWithNoConditionalOrOtherwise() throws SerialisationException {
// Given
AddOperationsToChain hook = fromJson(ADD_OPERATIONS_TO_CHAIN_RESOURCE_PATH);
Operation discardOutput = new DiscardOutput();
Operation splitStore = new SplitStoreFromFile();
If ifOp = new If.Builder<>().then(new GetElements()).build();
final OperationChain opChain = new OperationChain.Builder().first(ifOp).build();
// When
hook.preExecute(opChain, new Context(new User()));
// Then
final OperationChain expectedOpChain = new OperationChain.Builder().first(discardOutput).then(splitStore).then(new If.Builder<>().then(new OperationChain<>(new CountGroups(), new GetElements())).build()).then(new Count()).build();
JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain));
}
Aggregations