use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail in project Gaffer by gchq.
the class AddNamedOperationHandlerTest method shouldAddNamedOperationFieldsToNamedOperationDetailCorrectly.
@Test
public void shouldAddNamedOperationFieldsToNamedOperationDetailCorrectly() throws OperationException, CacheOperationFailedException {
final List<String> readAuths = asList("readAuth1", "readAuth2");
final List<String> writeAuths = asList("writeAuth1", "writeAuth2");
OperationChain opChain = new OperationChain.Builder().first(new AddElements()).build();
addNamedOperation.setOperationChain(opChain);
addNamedOperation.setScore(2);
addNamedOperation.setOperationName("testOp");
addNamedOperation.setLabels(asList("test label"));
addNamedOperation.setReadAccessRoles(readAuths);
addNamedOperation.setWriteAccessRoles(writeAuths);
handler.doOperation(addNamedOperation, context, store);
final NamedOperationDetail result = mockCache.getNamedOperation("testOp", new User(), EMPTY_ADMIN_AUTH);
assert cacheContains("testOp");
assertTrue(result.getScore() == 2);
assertEquals(asList("test label"), result.getLabels());
final AccessPredicate expectedReadAccessPredicate = new AccessPredicate(context.getUser(), readAuths);
assertEquals(expectedReadAccessPredicate, result.getOrDefaultReadAccessPredicate());
final AccessPredicate expectedWriteAccessPredicate = new AccessPredicate(context.getUser(), writeAuths);
assertEquals(expectedWriteAccessPredicate, result.getOrDefaultWriteAccessPredicate());
}
use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail in project Gaffer by gchq.
the class AddNamedOperationHandlerTest method before.
@BeforeEach
public void before() throws CacheOperationFailedException {
storedOperations.clear();
addNamedOperation.setOperationName(OPERATION_NAME);
doAnswer(invocationOnMock -> {
Object[] args = invocationOnMock.getArguments();
storedOperations.put(((NamedOperationDetail) args[0]).getOperationName(), (NamedOperationDetail) args[0]);
return null;
}).when(mockCache).addNamedOperation(any(NamedOperationDetail.class), anyBoolean(), any(User.class), eq(EMPTY_ADMIN_AUTH));
doAnswer(invocationOnMock -> new WrappedCloseableIterable<>(storedOperations.values())).when(mockCache).getAllNamedOperations(any(User.class), eq(EMPTY_ADMIN_AUTH));
doAnswer(invocationOnMock -> {
String name = (String) invocationOnMock.getArguments()[0];
NamedOperationDetail result = storedOperations.get(name);
if (result == null) {
throw new CacheOperationFailedException();
}
return result;
}).when(mockCache).getNamedOperation(anyString(), any(User.class), eq(EMPTY_ADMIN_AUTH));
given(store.getProperties()).willReturn(new StoreProperties());
}
use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail 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.named.operation.NamedOperationDetail 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.named.operation.NamedOperationDetail in project Gaffer by gchq.
the class GetAllNamedOperationsHandlerTest method shouldReturnNamedOperationWithNoInputType.
@Test
public void shouldReturnNamedOperationWithNoInputType() throws Exception {
// Given
AddNamedOperation addNamedOperation = new AddNamedOperation.Builder().name(expectedOperationDetailWithoutInputType.getOperationName()).description(expectedOperationDetailWithoutInputType.getDescription()).operationChain(expectedOperationDetailWithoutInputType.getOperationChainWithDefaultParams()).build();
addNamedOperationHandler.doOperation(addNamedOperation, context, store);
// When
CloseableIterable<NamedOperationDetail> allNamedOperationsList = getAllNamedOperationsHandler.doOperation(new GetAllNamedOperations(), context, store);
// Then
assertEquals(1, Iterables.size(allNamedOperationsList));
assertTrue(Iterables.contains(allNamedOperationsList, expectedOperationDetailWithoutInputType));
}
Aggregations