Search in sources :

Example 36 with NamedOperationDetail

use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail in project Gaffer by gchq.

the class NamedOperationResolverTest method shouldResolveNamedOperation.

@Test
public void shouldResolveNamedOperation() throws 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 namedOperationOpChain = new OperationChain(Arrays.asList(op1, op2));
    final Iterable<?> input = mock(CloseableIterable.class);
    final Map<String, Object> params = null;
    given(op1.getInput()).willReturn(null);
    given(cache.getNamedOperation(opName, user)).willReturn(extendedNamedOperation);
    given(extendedNamedOperation.getOperationChain(params)).willReturn(namedOperationOpChain);
    final OperationChain<Object> opChain = new OperationChain.Builder().first(new NamedOperation.Builder<>().name(opName).input(input).build()).build();
    // When
    resolver.preExecute(opChain, new Context(user));
    // Then
    assertEquals(namedOperationOpChain.getOperations(), opChain.getOperations());
    verify(op1).setInput((Iterable) input);
    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) 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 37 with NamedOperationDetail

use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail in project Gaffer by gchq.

the class NamedOperationResolverTest method shouldNotExecuteNamedOperationWithWrongParameterName.

@Test
public void shouldNotExecuteNamedOperationWithWrongParameterName() 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();
    // A parameter with the wrong name
    paramMap.put("param2", 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);
    // 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)

Example 38 with NamedOperationDetail

use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail in project Gaffer by gchq.

the class NamedOperationResolverTest method shouldResolveNestedNamedOperation.

@Test
public void shouldResolveNestedNamedOperation() 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 namedOperationOpChain = new OperationChain(Arrays.asList(op1, op2));
    final Iterable<?> input = mock(CloseableIterable.class);
    final Map<String, Object> params = null;
    given(op1.getInput()).willReturn(null);
    given(cache.getNamedOperation(opName, user)).willReturn(extendedNamedOperation);
    given(extendedNamedOperation.getOperationChain(params)).willReturn(namedOperationOpChain);
    final OperationChain<Object> opChain = new OperationChain.Builder().first(new OperationChain.Builder().first(new NamedOperation.Builder<>().name(opName).input(input).build()).build()).build();
    // When
    resolver.preExecute(opChain, new Context(user));
    // Then
    assertEquals(1, opChain.getOperations().size());
    final OperationChain<?> nestedOpChain = (OperationChain<?>) opChain.getOperations().get(0);
    assertEquals(namedOperationOpChain.getOperations(), nestedOpChain.getOperations());
    verify(op1).setInput((Iterable) input);
    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) 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 39 with NamedOperationDetail

use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail in project Gaffer by gchq.

the class NamedOperationScoreResolver method getScore.

@Override
public Integer getScore(final NamedOperation operation, final ScoreResolver defaultScoreResolver) {
    Integer namedOpScore = null;
    NamedOperationDetail namedOpDetail = null;
    if (null == operation) {
        return 0;
    }
    try {
        namedOpDetail = cache.getFromCache(operation.getOperationName());
    } catch (final CacheOperationFailedException e) {
        LOGGER.warn("Error accessing cache for Operation '{}': " + e.getMessage(), operation.getClass().getName());
    }
    if (null != namedOpDetail) {
        namedOpScore = namedOpDetail.getScore();
        if (null == namedOpScore && null != defaultScoreResolver) {
            namedOpScore = defaultScoreResolver.getScore(namedOpDetail.getOperationChain(operation.getParameters()));
        }
    }
    if (null != defaultScoreResolver) {
        if (null == namedOpScore) {
            namedOpScore = 0;
        }
        List parameterOperations = operation.getOperations();
        if (null != parameterOperations) {
            for (final Object objectOperation : parameterOperations) {
                Operation op = (Operation) objectOperation;
                Integer parameterOpScore = defaultScoreResolver.getScore(op);
                namedOpScore += parameterOpScore;
            }
        }
    }
    return namedOpScore;
}
Also used : NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) List(java.util.List) Operation(uk.gov.gchq.gaffer.operation.Operation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException)

Example 40 with NamedOperationDetail

use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail in project Gaffer by gchq.

the class NamedOperationCache method getAll.

private CloseableIterable<NamedOperationDetail> getAll(final User user, final String adminAuth) {
    final Set<String> keys = CacheServiceLoader.getService().getAllKeysFromCache(CACHE_NAME);
    final Set<NamedOperationDetail> executables = new HashSet<>();
    for (final String key : keys) {
        try {
            NamedOperationDetail op = getFromCache(key);
            if (op.hasReadAccess(user, adminAuth)) {
                executables.add(op);
            }
        } catch (final CacheOperationFailedException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
    return new WrappedCloseableIterable<>(executables);
}
Also used : WrappedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException) HashSet(java.util.HashSet)

Aggregations

NamedOperationDetail (uk.gov.gchq.gaffer.named.operation.NamedOperationDetail)40 Test (org.junit.jupiter.api.Test)27 User (uk.gov.gchq.gaffer.user.User)11 GetAllNamedOperations (uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations)10 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)10 NamedOperationCache (uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache)10 AddNamedOperation (uk.gov.gchq.gaffer.named.operation.AddNamedOperation)9 Context (uk.gov.gchq.gaffer.store.Context)9 CacheOperationFailedException (uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException)7 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)7 ParameterDetail (uk.gov.gchq.gaffer.named.operation.ParameterDetail)5 ArrayList (java.util.ArrayList)4 Element (uk.gov.gchq.gaffer.data.element.Element)4 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)4 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)4 Store (uk.gov.gchq.gaffer.store.Store)4 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)3 AddNamedOperationHandler (uk.gov.gchq.gaffer.store.operation.handler.named.AddNamedOperationHandler)3 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2