Search in sources :

Example 1 with NamedOperationCache

use of uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache in project Gaffer by gchq.

the class NamedOperationScoreResolverTest method shouldGetScoreFromOperationsInParameters.

@Test
public void shouldGetScoreFromOperationsInParameters() throws CacheOperationFailedException {
    // Given
    final Integer expectedScore = 8;
    final String opName = "otherOp";
    final NamedOperation<Element, Iterable<? extends Element>> namedOp = mock(NamedOperation.class);
    namedOp.setOperationName(opName);
    Operation operation = new GetAllElements();
    Map<String, Object> paramMap = Maps.newHashMap();
    paramMap.put("test param", operation);
    namedOp.setParameters(paramMap);
    final Map<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
    opScores.put(GetAllElements.class, 3);
    final ScoreResolver scoreResolver = new DefaultScoreResolver(opScores);
    final NamedOperationDetail namedOpDetail = mock(NamedOperationDetail.class);
    final NamedOperationCache cache = mock(NamedOperationCache.class);
    final NamedOperationScoreResolver resolver = new NamedOperationScoreResolver(cache);
    given(cache.getFromCache(namedOpDetail.getOperationName())).willReturn(namedOpDetail);
    given(namedOpDetail.getOperationName()).willReturn(opName);
    given(namedOpDetail.getScore()).willReturn(5);
    final List<Operation> operations = new ArrayList<>();
    operations.add(operation);
    given(namedOp.getOperations()).willReturn(operations);
    // When
    final Integer result = resolver.getScore(namedOp, scoreResolver);
    // Then
    assertEquals(expectedScore, result);
}
Also used : NamedOperationCache(uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) DefaultScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver) LinkedHashMap(java.util.LinkedHashMap) ScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver) DefaultScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) Test(org.junit.jupiter.api.Test)

Example 2 with NamedOperationCache

use of uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache in project Gaffer by gchq.

the class NamedOperationScoreResolverTest method shouldDefaultScoreFromOpChainIfNamedOpScoreEmpty.

@Test
public void shouldDefaultScoreFromOpChainIfNamedOpScoreEmpty() throws CacheOperationFailedException {
    // Given
    final String namedOpName = "namedOp";
    final Map parametersMap = new HashMap<>();
    final Integer expectedScore = 7;
    final NamedOperation<Element, Iterable<? extends Element>> namedOp = mock(NamedOperation.class);
    final NamedOperationCache cache = mock(NamedOperationCache.class);
    final ScoreResolver defaultScoreResolver = mock(DefaultScoreResolver.class);
    final NamedOperationDetail namedOpDetail = mock(NamedOperationDetail.class);
    final NamedOperationScoreResolver resolver = new NamedOperationScoreResolver(cache);
    final OperationChain opChain = new OperationChain();
    namedOp.setOperationName(namedOpName);
    namedOp.setParameters(parametersMap);
    given(namedOpDetail.getOperationChain(parametersMap)).willReturn(opChain);
    given(namedOpDetail.getScore()).willReturn(null);
    given(cache.getFromCache(namedOpDetail.getOperationName())).willReturn(namedOpDetail);
    given(defaultScoreResolver.getScore(opChain)).willReturn(7);
    // When
    final Integer result = resolver.getScore(namedOp, defaultScoreResolver);
    // Then
    assertEquals(expectedScore, result);
}
Also used : NamedOperationCache(uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver) DefaultScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver) Element(uk.gov.gchq.gaffer.data.element.Element) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 3 with NamedOperationCache

use of uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache in project Gaffer by gchq.

the class NamedOperationResolverTest method shouldExecuteNamedOperationWithoutOverridingInput.

@Test
public void shouldExecuteNamedOperationWithoutOverridingInput() throws OperationException, CacheOperationFailedException {
    // Given
    final String opName = "opName";
    final NamedOperationCache cache = mock(NamedOperationCache.class);
    final NamedOperationResolver resolver = new NamedOperationResolver(cache);
    final User user = mock(User.class);
    final NamedOperationDetail extendedNamedOperation = mock(NamedOperationDetail.class);
    final GetAdjacentIds op1 = mock(GetAdjacentIds.class);
    final GetElements op2 = mock(GetElements.class);
    final OperationChain namedOpChain = new OperationChain(Arrays.asList(op1, op2));
    final Iterable<?> input = mock(CloseableIterable.class);
    final Map<String, Object> params = null;
    given(op1.getInput()).willReturn(mock(CloseableIterable.class));
    given(cache.getNamedOperation(opName, user)).willReturn(extendedNamedOperation);
    given(extendedNamedOperation.getOperationChain(params)).willReturn(namedOpChain);
    // When
    final OperationChain<Object> opChain = new OperationChain.Builder().first(new NamedOperation.Builder<>().name(opName).input(input).build()).build();
    resolver.preExecute(opChain, new Context(user));
    // Then
    assertSame(op1, opChain.getOperations().get(0));
    verify(op1, never()).setInput((Iterable) input);
    assertSame(op2, opChain.getOperations().get(1));
    verify(op2, never()).setInput((Iterable) input);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) User(uk.gov.gchq.gaffer.user.User) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) NamedOperationCache(uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Test(org.junit.jupiter.api.Test)

Example 4 with NamedOperationCache

use of uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache in project Gaffer by gchq.

the class NamedOperationResolverTest method shouldResolveNamedOperationWithParameter.

@Test
public void shouldResolveNamedOperationWithParameter() throws OperationException, CacheOperationFailedException {
    // Given
    final String opName = "opName";
    final NamedOperationCache cache = mock(NamedOperationCache.class);
    final NamedOperationResolver resolver = new NamedOperationResolver(cache);
    final User user = mock(User.class);
    Map<String, Object> paramMap = Maps.newHashMap();
    paramMap.put("param1", 1L);
    ParameterDetail param = new ParameterDetail.Builder().defaultValue(1L).description("Limit param").valueClass(Long.class).build();
    Map<String, ParameterDetail> paramDetailMap = Maps.newHashMap();
    paramDetailMap.put("param1", param);
    // Make a real NamedOperationDetail with a parameter
    final NamedOperationDetail extendedNamedOperation = new NamedOperationDetail.Builder().operationName(opName).description("standard operation").operationChain("{ \"operations\": [ { \"class\":\"uk.gov.gchq.gaffer.operation.impl.get.GetAllElements\" }, { \"class\":\"uk.gov.gchq.gaffer.operation.impl.Limit\", \"resultLimit\": \"${param1}\" } ] }").parameters(paramDetailMap).build();
    given(cache.getNamedOperation(opName, user)).willReturn(extendedNamedOperation);
    final OperationChain<Object> opChain = new OperationChain.Builder().first(new NamedOperation.Builder<>().name(opName).parameters(paramMap).build()).build();
    // When
    resolver.preExecute(opChain, new Context(user));
    // Then
    assertEquals(opChain.getOperations().get(0).getClass(), GetAllElements.class);
    assertEquals(opChain.getOperations().get(1).getClass(), Limit.class);
    // Check the parameter has been inserted
    assertEquals((long) ((Limit) opChain.getOperations().get(1)).getResultLimit(), 1L);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) User(uk.gov.gchq.gaffer.user.User) NamedOperationCache(uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache) ParameterDetail(uk.gov.gchq.gaffer.named.operation.ParameterDetail) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) Limit(uk.gov.gchq.gaffer.operation.impl.Limit) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Test(org.junit.jupiter.api.Test)

Example 5 with NamedOperationCache

use of uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache in project Gaffer by gchq.

the class NamedOperationResolverTest method shouldNotExecuteNamedOperationWithMissingRequiredArg.

@Test
public void shouldNotExecuteNamedOperationWithMissingRequiredArg() throws OperationException, CacheOperationFailedException {
    // Given
    final String opName = "opName";
    final NamedOperationCache cache = mock(NamedOperationCache.class);
    final NamedOperationResolver resolver = new NamedOperationResolver(cache);
    final User user = mock(User.class);
    // Don't set any parameters
    Map<String, Object> paramMap = Maps.newHashMap();
    ParameterDetail param = new ParameterDetail.Builder().description("Limit param").valueClass(Long.class).required(true).build();
    Map<String, ParameterDetail> paramDetailMap = Maps.newHashMap();
    paramDetailMap.put("param1", param);
    // Make a real NamedOperationDetail with a parameter
    final NamedOperationDetail extendedNamedOperation = new NamedOperationDetail.Builder().operationName(opName).description("standard operation").operationChain("{ \"operations\": [ { \"class\":\"uk.gov.gchq.gaffer.operation.impl.get.GetAllElements\" }, { \"class\":\"uk.gov.gchq.gaffer.operation.impl.Limit\", \"resultLimit\": \"${param1}\" } ] }").parameters(paramDetailMap).build();
    given(cache.getNamedOperation(opName, user)).willReturn(extendedNamedOperation);
    // When
    assertThatIllegalArgumentException().isThrownBy(() -> resolver.preExecute(new OperationChain.Builder().first(new NamedOperation.Builder<>().name(opName).parameters(paramMap).build()).build(), new Context(user)));
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) User(uk.gov.gchq.gaffer.user.User) NamedOperationCache(uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache) ParameterDetail(uk.gov.gchq.gaffer.named.operation.ParameterDetail) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)10 NamedOperationDetail (uk.gov.gchq.gaffer.named.operation.NamedOperationDetail)10 NamedOperationCache (uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache)10 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)8 Context (uk.gov.gchq.gaffer.store.Context)7 User (uk.gov.gchq.gaffer.user.User)7 ParameterDetail (uk.gov.gchq.gaffer.named.operation.ParameterDetail)4 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)4 Element (uk.gov.gchq.gaffer.data.element.Element)3 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)3 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)3 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 DefaultScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver)2 ScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)1 Operation (uk.gov.gchq.gaffer.operation.Operation)1 Limit (uk.gov.gchq.gaffer.operation.impl.Limit)1