Search in sources :

Example 6 with NamedOperationDetail

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

the class AddNamedOperationHandlerTest method shouldAddNamedOperationFieldsToNamedOperationDetailCorrectly.

@Test
public void shouldAddNamedOperationFieldsToNamedOperationDetailCorrectly() throws OperationException, CacheOperationFailedException {
    final List<String> readAuths = asList("readAuth1", "readAuth2");
    final List<String> writeAuths = asList("writeAuth1", "writeAuth2");
    OperationChain opChain = new OperationChain.Builder().first(new AddElements()).build();
    addNamedOperation.setOperationChain(opChain);
    addNamedOperation.setScore(2);
    addNamedOperation.setOperationName("testOp");
    addNamedOperation.setLabels(asList("test label"));
    addNamedOperation.setReadAccessRoles(readAuths);
    addNamedOperation.setWriteAccessRoles(writeAuths);
    handler.doOperation(addNamedOperation, context, store);
    final NamedOperationDetail result = mockCache.getNamedOperation("testOp", new User(), EMPTY_ADMIN_AUTH);
    assert cacheContains("testOp");
    assertTrue(result.getScore() == 2);
    assertEquals(asList("test label"), result.getLabels());
    final AccessPredicate expectedReadAccessPredicate = new AccessPredicate(context.getUser(), readAuths);
    assertEquals(expectedReadAccessPredicate, result.getOrDefaultReadAccessPredicate());
    final AccessPredicate expectedWriteAccessPredicate = new AccessPredicate(context.getUser(), writeAuths);
    assertEquals(expectedWriteAccessPredicate, result.getOrDefaultWriteAccessPredicate());
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AccessPredicate(uk.gov.gchq.gaffer.access.predicate.AccessPredicate) Test(org.junit.jupiter.api.Test)

Example 7 with NamedOperationDetail

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

the class AddNamedOperationHandlerTest method before.

@BeforeEach
public void before() throws CacheOperationFailedException {
    storedOperations.clear();
    addNamedOperation.setOperationName(OPERATION_NAME);
    doAnswer(invocationOnMock -> {
        Object[] args = invocationOnMock.getArguments();
        storedOperations.put(((NamedOperationDetail) args[0]).getOperationName(), (NamedOperationDetail) args[0]);
        return null;
    }).when(mockCache).addNamedOperation(any(NamedOperationDetail.class), anyBoolean(), any(User.class), eq(EMPTY_ADMIN_AUTH));
    doAnswer(invocationOnMock -> new WrappedCloseableIterable<>(storedOperations.values())).when(mockCache).getAllNamedOperations(any(User.class), eq(EMPTY_ADMIN_AUTH));
    doAnswer(invocationOnMock -> {
        String name = (String) invocationOnMock.getArguments()[0];
        NamedOperationDetail result = storedOperations.get(name);
        if (result == null) {
            throw new CacheOperationFailedException();
        }
        return result;
    }).when(mockCache).getNamedOperation(anyString(), any(User.class), eq(EMPTY_ADMIN_AUTH));
    given(store.getProperties()).willReturn(new StoreProperties());
}
Also used : User(uk.gov.gchq.gaffer.user.User) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with NamedOperationDetail

use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail 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 9 with NamedOperationDetail

use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail 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 10 with NamedOperationDetail

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

the class GetAllNamedOperationsHandlerTest method shouldReturnNamedOperationWithNoInputType.

@Test
public void shouldReturnNamedOperationWithNoInputType() throws Exception {
    // Given
    AddNamedOperation addNamedOperation = new AddNamedOperation.Builder().name(expectedOperationDetailWithoutInputType.getOperationName()).description(expectedOperationDetailWithoutInputType.getDescription()).operationChain(expectedOperationDetailWithoutInputType.getOperationChainWithDefaultParams()).build();
    addNamedOperationHandler.doOperation(addNamedOperation, context, store);
    // When
    CloseableIterable<NamedOperationDetail> allNamedOperationsList = getAllNamedOperationsHandler.doOperation(new GetAllNamedOperations(), context, store);
    // Then
    assertEquals(1, Iterables.size(allNamedOperationsList));
    assertTrue(Iterables.contains(allNamedOperationsList, expectedOperationDetailWithoutInputType));
}
Also used : GetAllNamedOperations(uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) Test(org.junit.jupiter.api.Test)

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