use of uk.gov.gchq.gaffer.operation.impl.If in project Gaffer by gchq.
the class IfScoreResolverTest method shouldGetScoreForNestedOperations.
@Test
public void shouldGetScoreForNestedOperations() {
// Given
final Map map = mock(Map.class);
final Conditional conditional = mock(Conditional.class);
given(conditional.getTransform()).willReturn(map);
final GetWalks getWalks = mock(GetWalks.class);
given(getWalks.getOperations()).willReturn(Collections.singletonList(new OperationChain<>(new GetAdjacentIds(), new GetAdjacentIds())));
final GetAllElements getAllElements = new GetAllElements();
final If operation = new If.Builder<>().conditional(conditional).then(getWalks).otherwise(getAllElements).build();
final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
opScores.put(Operation.class, 1);
opScores.put(Map.class, 3);
opScores.put(GetAdjacentIds.class, 2);
opScores.put(GetAllElements.class, 3);
final IfScoreResolver resolver = new IfScoreResolver();
final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores);
// When
final int score = resolver.getScore(operation, defaultResolver);
// Then
assertEquals(7, score);
}
use of uk.gov.gchq.gaffer.operation.impl.If in project Gaffer by gchq.
the class IfScoreResolverTest method shouldGetDefaultScoreWithNoOperationScores.
@Test
public void shouldGetDefaultScoreWithNoOperationScores() {
// Given
final IfScoreResolver resolver = new IfScoreResolver();
final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(new LinkedHashMap<>());
final If operation = new If();
// When
final int score = resolver.getScore(operation, defaultResolver);
// Then
assertEquals(2, score);
}
use of uk.gov.gchq.gaffer.operation.impl.If in project Gaffer by gchq.
the class IfScoreResolverTest method shouldGetScoreWithOperationChainAsAnOperation.
@Test
public void shouldGetScoreWithOperationChainAsAnOperation() {
// Given
final GetElements getElements = mock(GetElements.class);
final ToMap toMap = mock(ToMap.class);
final Map map = mock(Map.class);
final OperationChain conditionalChain = mock(OperationChain.class);
final List<Operation> conditionalOps = new LinkedList<>();
conditionalOps.add(getElements);
conditionalOps.add(toMap);
conditionalOps.add(map);
given(conditionalChain.getOperations()).willReturn(conditionalOps);
final Conditional conditional = mock(Conditional.class);
given(conditional.getTransform()).willReturn(conditionalChain);
final GetAdjacentIds getAdjacentIds = mock(GetAdjacentIds.class);
final GetAllElements getAllElements = mock(GetAllElements.class);
final If operation = new If.Builder<>().conditional(conditional).then(getAdjacentIds).otherwise(getAllElements).build();
final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
opScores.put(Operation.class, 1);
opScores.put(GetElements.class, 2);
opScores.put(ToMap.class, 2);
opScores.put(Map.class, 3);
opScores.put(GetAdjacentIds.class, 3);
opScores.put(GetAllElements.class, 4);
final IfScoreResolver resolver = new IfScoreResolver();
final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores);
// When
final int score = resolver.getScore(operation, defaultResolver);
// Then
assertEquals(11, score);
}
use of uk.gov.gchq.gaffer.operation.impl.If in project Gaffer by gchq.
the class IfScoreResolverTest method shouldThrowErrorWhenNoDefaultResolverConfigured.
@Test
public void shouldThrowErrorWhenNoDefaultResolverConfigured() {
// Given
final IfScoreResolver resolver = new IfScoreResolver();
final If operation = new If.Builder<>().conditional(new Conditional()).then(new GetAllElements()).build();
// When / Then
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> resolver.getScore(operation)).withMessage("Default Score Resolver has not been provided.");
}
use of uk.gov.gchq.gaffer.operation.impl.If in project Gaffer by gchq.
the class AddOperationsToChainTest method shouldHandleNestedOperationChain.
@Test
public void shouldHandleNestedOperationChain() 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 countGroups = new CountGroups();
Operation getElements = new GetElements();
If ifOp = new If.Builder<>().conditional(new Conditional(new Exists(), new GetElements())).then(new GetElements()).otherwise(new GetAllElements()).build();
Operation getAllElements = new GetAllElements();
Operation limit = new Limit<>();
final OperationChain opChain = new OperationChain.Builder().first(getAdjacentIds).then(new OperationChain.Builder().first(getElements).then(getAllElements).build()).then(ifOp).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((Operation) new OperationChain.Builder().first(countGroups).then(getElements).then(getAllElements).then(limit).then(validate).build()).then(new If.Builder<>().conditional(new Conditional(new Exists(), new OperationChain<>(new CountGroups(), new GetElements()))).then(new OperationChain<>(new CountGroups(), new GetElements())).otherwise(new OperationChain<>(new GetAllElements(), new Limit<>(), new Validate())).build()).then(new Count()).build();
JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain));
}
Aggregations