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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations