Search in sources :

Example 1 with ScoreResolver

use of uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver 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 ScoreResolver

use of uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver 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 ScoreResolver

use of uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver in project Gaffer by gchq.

the class ScoreOperationChainHandler method addDefaultScoreResolvers.

/**
 * Adds Gaffer's native {@link ScoreResolver} implementations to the list of available <code>ScoreResolver</code>s.
 * Any new implementations should be added to the map in this method, along with their respective class.
 *
 * @return a map of Operation class to ScoreResolver implementation
 */
private static Map<Class<? extends Operation>, ScoreResolver> addDefaultScoreResolvers() {
    final Map<Class<? extends Operation>, ScoreResolver> defaultResolvers = new HashMap<>();
    defaultResolvers.put(NamedOperation.class, new NamedOperationScoreResolver());
    defaultResolvers.put(If.class, new IfScoreResolver());
    defaultResolvers.put(While.class, new WhileScoreResolver());
    return Collections.unmodifiableMap(defaultResolvers);
}
Also used : 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) WhileScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.WhileScoreResolver) IfScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.IfScoreResolver) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IfScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.IfScoreResolver) NamedOperationScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.named.NamedOperationScoreResolver) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) WhileScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.WhileScoreResolver)

Example 4 with ScoreResolver

use of uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver in project Gaffer by gchq.

the class ScoreOperationChainHandlerTest method shouldCorrectlyResolveScoreForNullListOfOperations.

@Test
public void shouldCorrectlyResolveScoreForNullListOfOperations() throws OperationException {
    // Given
    final ScoreOperationChainHandler handler = new ScoreOperationChainHandler();
    final Map<Class<? extends Operation>, ScoreResolver> resolvers = new HashMap<>();
    handler.setScoreResolvers(resolvers);
    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 properties = mock(StoreProperties.class);
    final List<? extends Operation> opList = null;
    final OperationChain opChain = new OperationChain(opList);
    given(scoreOperationChain.getOperationChain()).willReturn(opChain);
    given(context.getUser()).willReturn(user);
    Set<String> opAuths = new HashSet<>();
    opAuths.add("TEST_USER");
    given(user.getOpAuths()).willReturn(opAuths);
    given(store.getProperties()).willReturn(properties);
    // When
    final Object result = handler.doOperation(new ScoreOperationChain.Builder().operationChain(opChain).build(), context, store);
    // Then
    assertEquals(0, result);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) User(uk.gov.gchq.gaffer.user.User) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Store(uk.gov.gchq.gaffer.store.Store) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) 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) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with ScoreResolver

use of uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver in project Gaffer by gchq.

the class ScoreOperationChainHandlerTest method shouldExecuteScoreOperationChainContainingNamedOperation.

@Test
public void shouldExecuteScoreOperationChainContainingNamedOperation() 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(Operation.class, 1);
    opScores.put(GetAdjacentIds.class, 2);
    opScores.put(GetElements.class, 1);
    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(3);
    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)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)9 DefaultScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver)9 ScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver)9 HashMap (java.util.HashMap)8 Test (org.junit.jupiter.api.Test)8 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)8 Operation (uk.gov.gchq.gaffer.operation.Operation)8 IfScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.IfScoreResolver)7 NamedOperationScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.named.NamedOperationScoreResolver)7 Element (uk.gov.gchq.gaffer.data.element.Element)6 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)6 HashSet (java.util.HashSet)5 ScoreOperationChain (uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain)5 Context (uk.gov.gchq.gaffer.store.Context)5 Store (uk.gov.gchq.gaffer.store.Store)5 StoreProperties (uk.gov.gchq.gaffer.store.StoreProperties)5 User (uk.gov.gchq.gaffer.user.User)5 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)3 NamedOperationDetail (uk.gov.gchq.gaffer.named.operation.NamedOperationDetail)2 Limit (uk.gov.gchq.gaffer.operation.impl.Limit)2