Search in sources :

Example 1 with While

use of uk.gov.gchq.gaffer.operation.impl.While in project Gaffer by gchq.

the class WhileScoreResolverTest method shouldGetScoreWithFullyPopulatedOperation.

@Test
public void shouldGetScoreWithFullyPopulatedOperation() {
    // Given
    final Object input = new EntitySeed(2);
    final Count count = mock(Count.class);
    final int repeats = 5;
    final GetElements getElements = mock(GetElements.class);
    final Conditional conditional = mock(Conditional.class);
    given(conditional.getTransform()).willReturn(count);
    final While operation = new While.Builder<>().input(input).conditional(conditional).maxRepeats(repeats).operation(getElements).build();
    final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
    opScores.put(Count.class, 1);
    opScores.put(GetElements.class, 2);
    final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores);
    final WhileScoreResolver resolver = new WhileScoreResolver();
    // When
    final int score = resolver.getScore(operation, defaultResolver);
    // Then
    assertEquals(15, score);
}
Also used : GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Count(uk.gov.gchq.gaffer.operation.impl.Count) While(uk.gov.gchq.gaffer.operation.impl.While) Operation(uk.gov.gchq.gaffer.operation.Operation) LinkedHashMap(java.util.LinkedHashMap) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Test(org.junit.jupiter.api.Test)

Example 2 with While

use of uk.gov.gchq.gaffer.operation.impl.While in project Gaffer by gchq.

the class WhileScoreResolverTest method shouldThrowErrorWhenNoDefaultResolverConfigured.

@Test
public void shouldThrowErrorWhenNoDefaultResolverConfigured() {
    // Given
    final WhileScoreResolver resolver = new WhileScoreResolver();
    final While operation = new While.Builder<>().conditional(new Conditional()).operation(new GetAllElements()).build();
    // When / Then
    assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> resolver.getScore(operation)).withMessage("Default Score Resolver has not been provided.");
}
Also used : GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) While(uk.gov.gchq.gaffer.operation.impl.While) Test(org.junit.jupiter.api.Test)

Example 3 with While

use of uk.gov.gchq.gaffer.operation.impl.While in project Gaffer by gchq.

the class WhileScoreResolverTest method shouldGetDefaultScoreWithNoOperationScores.

@Test
public void shouldGetDefaultScoreWithNoOperationScores() {
    // Given
    final WhileScoreResolver resolver = new WhileScoreResolver();
    final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(new LinkedHashMap<>());
    final While operation = new While();
    // When
    final int score = resolver.getScore(operation, defaultResolver);
    // Then
    assertEquals(0, score);
}
Also used : While(uk.gov.gchq.gaffer.operation.impl.While) Test(org.junit.jupiter.api.Test)

Example 4 with While

use of uk.gov.gchq.gaffer.operation.impl.While 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);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Element(uk.gov.gchq.gaffer.data.element.Element) Store(uk.gov.gchq.gaffer.store.Store) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) While(uk.gov.gchq.gaffer.operation.impl.While) Predicate(java.util.function.Predicate) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Edge(uk.gov.gchq.gaffer.data.element.Edge) Test(org.junit.jupiter.api.Test)

Example 5 with While

use of uk.gov.gchq.gaffer.operation.impl.While 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);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Store(uk.gov.gchq.gaffer.store.Store) While(uk.gov.gchq.gaffer.operation.impl.While) Test(org.junit.jupiter.api.Test)

Aggregations

While (uk.gov.gchq.gaffer.operation.impl.While)15 Test (org.junit.jupiter.api.Test)12 Context (uk.gov.gchq.gaffer.store.Context)8 Store (uk.gov.gchq.gaffer.store.Store)8 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)7 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)7 Conditional (uk.gov.gchq.gaffer.operation.util.Conditional)6 Operation (uk.gov.gchq.gaffer.operation.Operation)4 Predicate (java.util.function.Predicate)3 Edge (uk.gov.gchq.gaffer.data.element.Edge)3 LinkedHashMap (java.util.LinkedHashMap)2 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)2 Element (uk.gov.gchq.gaffer.data.element.Element)2 OperationException (uk.gov.gchq.gaffer.operation.OperationException)2 IsMoreThan (uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)2 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1 ExtractProperty (uk.gov.gchq.gaffer.data.element.function.ExtractProperty)1 UnwrapEntityId (uk.gov.gchq.gaffer.data.element.function.UnwrapEntityId)1 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)1