use of uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException 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.cache.exception.CacheOperationFailedException in project Gaffer by gchq.
the class AddNamedOperationHandler method doOperation.
/**
* Adds a NamedOperation to a cache which must be specified in the operation declarations file. An
* NamedOperationDetail is built using the fields on the AddNamedOperation. The operation name and operation chain
* fields must be set and cannot be left empty, or the build() method will fail and a runtime exception will be
* thrown. The handler then adds/overwrites the NamedOperation according toa an overwrite flag.
*
* @param operation the {@link Operation} to be executed
* @param context the operation chain context, containing the user who executed the operation
* @param store the {@link Store} the operation should be run on
* @return null (since the output is void)
* @throws OperationException if the operation on the cache fails
*/
@Override
public Void doOperation(final AddNamedOperation operation, final Context context, final Store store) throws OperationException {
try {
final NamedOperationDetail namedOperationDetail = new NamedOperationDetail.Builder().operationChain(operation.getOperationChainAsString()).operationName(operation.getOperationName()).labels(operation.getLabels()).creatorId(context.getUser().getUserId()).readers(operation.getReadAccessRoles()).writers(operation.getWriteAccessRoles()).description(operation.getDescription()).parameters(operation.getParameters()).score(operation.getScore()).readAccessPredicate(operation.getReadAccessPredicate()).writeAccessPredicate(operation.getWriteAccessPredicate()).build();
validate(namedOperationDetail.getOperationChainWithDefaultParams(), namedOperationDetail);
cache.addNamedOperation(namedOperationDetail, operation.isOverwriteFlag(), context.getUser(), store.getProperties().getAdminAuth());
} catch (final CacheOperationFailedException e) {
throw new OperationException(e.getMessage(), e);
}
return null;
}
use of uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException in project Gaffer by gchq.
the class NamedViewCache method getAllNamedViews.
/**
* Gets all the {@link uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail}s from the cache for a user.
*
* @param user {@link uk.gov.gchq.gaffer.user.User} user requesting views
* @param adminAuth admin auths
* @return a {@link CloseableIterable} containing all of the {@link uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail}s in the cache
* @throws CacheOperationFailedException if the get operation fails
*/
public CloseableIterable<NamedViewDetail> getAllNamedViews(final User user, final String adminAuth) throws CacheOperationFailedException {
final Set<String> keys = CacheServiceLoader.getService().getAllKeysFromCache(CACHE_NAME);
final Set<NamedViewDetail> views = new HashSet<>();
for (final String key : keys) {
try {
final NamedViewDetail namedViewDetail = getFromCache(key);
if (namedViewDetail.hasReadAccess(user, adminAuth)) {
views.add(namedViewDetail);
}
} catch (final CacheOperationFailedException e) {
throw e;
}
}
return new WrappedCloseableIterable<>(views);
}
use of uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException in project Gaffer by gchq.
the class NamedViewCache method remove.
private void remove(final String name, final User user, final String adminAuth) throws CacheOperationFailedException {
if (null == name) {
throw new IllegalArgumentException("NamedView name cannot be null");
}
NamedViewDetail existing;
try {
existing = getFromCache(name);
} catch (final CacheOperationFailedException e) {
return;
}
if (user != null) {
if (existing.hasWriteAccess(user, adminAuth)) {
deleteFromCache(name);
} else {
throw new CacheOperationFailedException("User " + user + " does not have permission to delete named view: " + name);
}
}
deleteFromCache(name);
}
use of uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException in project Gaffer by gchq.
the class AddNamedViewHandler method doOperation.
/**
* Adds a {@link uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail} to the NamedViewCache. If no cache is specified it will created a new {@link NamedViewCache}.
* The {@link AddNamedView} name field must be set and cannot be left empty, or the build() method will fail and a runtime exception will be
* thrown. The handler then adds/overwrites the {@link uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail} according to an overwrite flag.
*
* @param operation the {@link uk.gov.gchq.gaffer.operation.Operation} containing the {@link uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView} to be added to cache
* @param context the {@link Context}
* @param store the {@link Store} the operation should be run on
* @return null (since the output is void)
* @throws OperationException if the addition to the cache fails
*/
@Override
public Object doOperation(final AddNamedView operation, final Context context, final Store store) throws OperationException {
if (null == operation.getName() || operation.getName().isEmpty()) {
throw new IllegalArgumentException("NamedView name must be set and not empty");
}
final NamedViewDetail namedViewDetail = new NamedViewDetail.Builder().name(operation.getName()).view(operation.getViewAsString()).description(operation.getDescription()).creatorId(context.getUser().getUserId()).writers(operation.getWriteAccessRoles()).parameters(operation.getParameters()).readAccessPredicate(operation.getReadAccessPredicate()).writeAccessPredicate(operation.getWriteAccessPredicate()).build();
validate(namedViewDetail.getViewWithDefaultParams(), namedViewDetail);
try {
cache.addNamedView(namedViewDetail, operation.isOverwriteFlag(), context.getUser(), store.getProperties().getAdminAuth());
} catch (final CacheOperationFailedException e) {
throw new OperationException(e.getMessage(), e);
}
return null;
}
Aggregations