Search in sources :

Example 6 with ScoreResolver

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

the class ScoreOperationChainHandlerTest method shouldCorrectlyResolveScoreForNestedOperationWithNullOperationList.

@Test
public void shouldCorrectlyResolveScoreForNestedOperationWithNullOperationList() 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 properties = mock(StoreProperties.class);
    final GetAllElements op1 = mock(GetAllElements.class);
    final Transform op2 = mock(Transform.class);
    final Map<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
    opScores.put(GetAllElements.class, 2);
    opScores.put(Transform.class, 1);
    handler.setOpScores(opScores);
    final String opName = "namedOp";
    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 List<? extends Operation> opList = null;
    final OperationChain nestedOpChain = new OperationChain(opList);
    final OperationChain opChain = new OperationChain(Arrays.asList(op1, op2, nestedOpChain, namedOp));
    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(6, result);
}
Also used : User(uk.gov.gchq.gaffer.user.User) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Element(uk.gov.gchq.gaffer.data.element.Element) 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) 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) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) 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) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Transform(uk.gov.gchq.gaffer.operation.impl.function.Transform) Test(org.junit.jupiter.api.Test)

Example 7 with ScoreResolver

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

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

the class ScoreOperationChainHandlerTest method shouldResolveScoreOperationChainWithMultipleScoreResolvers.

@Test
public void shouldResolveScoreOperationChainWithMultipleScoreResolvers() throws OperationException {
    // Given
    final ScoreOperationChainHandler handler = new ScoreOperationChainHandler();
    final Map<Class<? extends Operation>, ScoreResolver> resolvers = new HashMap<>();
    final ScoreResolver scoreResolver = mock(NamedOperationScoreResolver.class);
    final ScoreResolver scoreResolver1 = mock(DefaultScoreResolver.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 = mock(StoreProperties.class);
    final GetAdjacentIds op1 = new GetAdjacentIds();
    final AddElements op2 = new AddElements();
    final Map<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
    opScores.put(GetAdjacentIds.class, 2);
    handler.setOpScores(opScores);
    final String opName = "namedOp";
    final NamedOperation<Iterable<? extends Element>, Iterable<? extends Element>> namedOp = mock(NamedOperation.class);
    namedOp.setOperationName(opName);
    resolvers.put(namedOp.getClass(), scoreResolver);
    resolvers.put(op2.getClass(), scoreResolver1);
    handler.setScoreResolvers(resolvers);
    given(scoreResolver.getScore(eq(namedOp), any())).willReturn(3);
    given(scoreResolver1.getScore(eq(op2), any())).willReturn(5);
    final OperationChain opChain = new OperationChain(Arrays.asList(op1, op2, 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(10, result);
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) 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) 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) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Test(org.junit.jupiter.api.Test)

Example 9 with ScoreResolver

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