Search in sources :

Example 11 with NamedOperation

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

the class ScoreOperationChainHandlerTest method shouldCorrectlyExecuteScoreOperationChainWhenNamedOperationScoreIsNull.

@Test
public void shouldCorrectlyExecuteScoreOperationChainWhenNamedOperationScoreIsNull() throws OperationException {
    // Given
    final ScoreOperationChainHandler handler = new ScoreOperationChainHandler();
    final Map<Class<? extends Operation>, ScoreResolver> resolvers = new HashMap<>();
    final ScoreResolver scoreResolver = mock(NamedOperationScoreResolver.class);
    final Context context = mock(Context.class);
    final Store store = mock(Store.class);
    final User user = mock(User.class);
    final ScoreOperationChain scoreOperationChain = mock(ScoreOperationChain.class);
    final StoreProperties storeProperties = new StoreProperties();
    final GetAdjacentIds op1 = mock(GetAdjacentIds.class);
    final GetElements op2 = mock(GetElements.class);
    final Limit op3 = mock(Limit.class);
    final Map<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
    opScores.put(GetAdjacentIds.class, 3);
    opScores.put(GetElements.class, 2);
    opScores.put(Limit.class, 1);
    handler.setOpScores(opScores);
    final String opName = "basicOp";
    final NamedOperation<Iterable<? extends Element>, Iterable<? extends Element>> namedOp = mock(NamedOperation.class);
    namedOp.setOperationName(opName);
    resolvers.put(namedOp.getClass(), scoreResolver);
    handler.setScoreResolvers(resolvers);
    given(scoreResolver.getScore(eq(namedOp), any())).willReturn(null);
    final OperationChain opChain = new OperationChain(Arrays.asList(op1, op2, op3, namedOp));
    given(context.getUser()).willReturn(user);
    Set<String> opAuths = new HashSet<>();
    opAuths.add("TEST_USER");
    given(user.getOpAuths()).willReturn(opAuths);
    given(scoreOperationChain.getOperationChain()).willReturn(opChain);
    given(store.getProperties()).willReturn(storeProperties);
    // When
    final Object result = handler.doOperation(new ScoreOperationChain.Builder().operationChain(opChain).build(), context, store);
    // Then
    assertEquals(7, result);
}
Also used : User(uk.gov.gchq.gaffer.user.User) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Element(uk.gov.gchq.gaffer.data.element.Element) Store(uk.gov.gchq.gaffer.store.Store) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) LinkedHashMap(java.util.LinkedHashMap) NamedOperationScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.named.NamedOperationScoreResolver) DefaultScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver) ScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver) IfScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.IfScoreResolver) HashSet(java.util.HashSet) Context(uk.gov.gchq.gaffer.store.Context) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) Limit(uk.gov.gchq.gaffer.operation.impl.Limit) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Test(org.junit.jupiter.api.Test)

Example 12 with NamedOperation

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

the class GraphHooksIT method shouldResolveNamedViewWithinNamedOperation.

@Test
public void shouldResolveNamedViewWithinNamedOperation() throws OperationException {
    // Given
    final Edge edge1 = getEdges().get(new EdgeSeed(SOURCE_1, DEST_1, false)).emptyClone();
    edge1.putProperty(TestPropertyNames.INT, 100);
    final Edge edge2 = edge1.emptyClone();
    edge2.putProperty(TestPropertyNames.INT, 101);
    graph.execute(new AddElements.Builder().input(edge1, edge2).build(), getUser());
    final AddNamedView addNamedView = new AddNamedView.Builder().name("Test View").view(new View.Builder().edge(TestGroups.EDGE, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.INT).execute(new IsIn(Arrays.asList((Object) 100))).build()).build()).build()).overwrite(true).build();
    graph.execute(addNamedView, getUser());
    final AddNamedOperation addNamedOperation = new AddNamedOperation.Builder().operationChain(new OperationChain.Builder().first(new GetAllElements.Builder().view(new NamedView.Builder().name("Test View").build()).build()).build()).description("named operation GetAllElements test query").name("GetAllElements test").labels(Arrays.asList("label 1", "Label 2")).overwrite(true).build();
    graph.execute(addNamedOperation, getUser());
    final NamedOperation<EntityId, CloseableIterable<? extends Element>> operation = new NamedOperation.Builder<EntityId, CloseableIterable<? extends Element>>().name("GetAllElements test").input(new EntitySeed("10")).build();
    // When
    final CloseableIterable<? extends Element> results = graph.execute(operation, getUser());
    // Then
    final List<Element> resultList = Lists.newArrayList(results);
    assertThat(resultList).hasSize(1).contains((Element) edge1);
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) Element(uk.gov.gchq.gaffer.data.element.Element) AddNamedView(uk.gov.gchq.gaffer.named.view.AddNamedView) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) AddNamedView(uk.gov.gchq.gaffer.named.view.AddNamedView) IsIn(uk.gov.gchq.koryphe.impl.predicate.IsIn) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) Test(org.junit.Test)

Example 13 with NamedOperation

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

the class AddNamedOperationHandlerTest method shouldNotAllowUpdateToNamedOperationIfItCausesRecursion.

@Test
public void shouldNotAllowUpdateToNamedOperationIfItCausesRecursion() throws OperationException {
    String innocentOpName = "innocent";
    OperationChain innocent = new OperationChain.Builder().first(new GetElements<>()).build();
    addNamedOperation.setOperationName(innocentOpName);
    addNamedOperation.setOperationChain(innocent);
    handler.doOperation(addNamedOperation, context, store);
    OperationChain parent = new OperationChain.Builder().first(new NamedOperation(innocentOpName, "call down to currently innocent named op")).build();
    addNamedOperation.setOperationChain(parent);
    addNamedOperation.setOperationName(OPERATION_NAME);
    handler.doOperation(addNamedOperation, context, store);
    OperationChain recursive = new OperationChain.Builder().first(new NamedOperation(OPERATION_NAME, "")).build();
    addNamedOperation.setOperationName(innocentOpName);
    addNamedOperation.setOperationChain(recursive);
    addNamedOperation.setOverwriteFlag(true);
    exception.expect(OperationException.class);
    handler.doOperation(addNamedOperation, context, store);
}
Also used : OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Test(org.junit.Test)

Example 14 with NamedOperation

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

the class NamedOperationJCSCacheTest method shouldReturnSetOfNamedOperationsThatAUserCanExecute.

@Test
public void shouldReturnSetOfNamedOperationsThatAUserCanExecute() throws CacheOperationFailedException {
    cache.addNamedOperation(standard, false, standardUser);
    ExtendedNamedOperation alt = alternative;
    alt.setOperationName("alt");
    cache.addNamedOperation(alt, false, advancedUser);
    Set<NamedOperation> actual = Sets.newHashSet(cache.getAllNamedOperations(standardUser, true));
    assert (actual.contains(standard.getBasic()));
    assert (actual.contains(alt.getBasic()));
    assert (actual.size() == 2);
}
Also used : ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation) ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Test(org.junit.Test)

Example 15 with NamedOperation

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

the class NamedOperationJCSCacheTest method shouldNotReturnANamedOperationThatAUserCannotExecute.

@Test
public void shouldNotReturnANamedOperationThatAUserCannotExecute() throws CacheOperationFailedException {
    cache.addNamedOperation(standard, false, standardUser);
    ExtendedNamedOperation noReadAccess = new ExtendedNamedOperation.Builder().creatorId(advancedUser.getUserId()).description("an operation that a standard user cannot execute").operationName("test").readers(writers).writers(writers).operationChain(standardOpChain).build();
    cache.addNamedOperation(noReadAccess, false, advancedUser);
    Set<NamedOperation> actual = Sets.newHashSet(cache.getAllNamedOperations(standardUser, true));
    assert (actual.contains(standard.getBasic()));
    assert (actual.size() == 1);
}
Also used : ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation) ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Test(org.junit.Test)

Aggregations

NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)25 Operation (uk.gov.gchq.gaffer.operation.Operation)14 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)12 ExtendedNamedOperation (uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation)10 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)10 Test (org.junit.jupiter.api.Test)9 Element (uk.gov.gchq.gaffer.data.element.Element)9 LinkedHashMap (java.util.LinkedHashMap)8 Test (org.junit.Test)8 AddNamedOperation (uk.gov.gchq.gaffer.named.operation.AddNamedOperation)8 HashMap (java.util.HashMap)7 NamedOperationScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.named.NamedOperationScoreResolver)7 HashSet (java.util.HashSet)5 Limit (uk.gov.gchq.gaffer.operation.impl.Limit)5 ScoreOperationChain (uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain)5 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)5 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)5 DefaultScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver)5 ScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver)5 User (uk.gov.gchq.gaffer.user.User)5