Search in sources :

Example 1 with DefaultScoreResolver

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

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

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

the class ScoreOperationChainHandlerTest method shouldReAddDefaultScoreResolversWhenCallingSetMethod.

@Test
public void shouldReAddDefaultScoreResolversWhenCallingSetMethod() {
    // Given
    final ScoreOperationChainHandler handler = new ScoreOperationChainHandler();
    final Map<Class<? extends Operation>, ScoreResolver> defaultResolvers = ScoreOperationChainHandler.getDefaultScoreResolvers();
    final Map<Class<? extends Operation>, ScoreResolver> expectedMap = new HashMap<>();
    expectedMap.putAll(defaultResolvers);
    final Map<Class<? extends Operation>, ScoreResolver> inputMap = new HashMap<>();
    inputMap.put(GetElements.class, new DefaultScoreResolver(null));
    inputMap.put(GetAllElements.class, new DefaultScoreResolver(null));
    expectedMap.putAll(inputMap);
    // When
    handler.setScoreResolvers(inputMap);
    final Map<Class<? extends Operation>, ScoreResolver> results = handler.getScoreResolvers();
    // Then
    assertEquals(expectedMap.keySet(), results.keySet());
    assertTrue(results.get(NamedOperation.class) instanceof NamedOperationScoreResolver);
    assertTrue(results.get(If.class) instanceof IfScoreResolver);
    assertEquals(expectedMap.size(), results.size());
}
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) 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) DefaultScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver) Test(org.junit.jupiter.api.Test)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)3 Test (org.junit.jupiter.api.Test)3 DefaultScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver)3 ScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver)3 HashMap (java.util.HashMap)2 Element (uk.gov.gchq.gaffer.data.element.Element)2 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)2 NamedOperationDetail (uk.gov.gchq.gaffer.named.operation.NamedOperationDetail)2 Operation (uk.gov.gchq.gaffer.operation.Operation)2 NamedOperationCache (uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)1 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)1 IfScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.IfScoreResolver)1 NamedOperationScoreResolver (uk.gov.gchq.gaffer.store.operation.resolver.named.NamedOperationScoreResolver)1