Search in sources :

Example 1 with CacheOperationFailedException

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());
}
Also used : User(uk.gov.gchq.gaffer.user.User) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with CacheOperationFailedException

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;
}
Also used : NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException) OperationException(uk.gov.gchq.gaffer.operation.OperationException)

Example 3 with CacheOperationFailedException

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);
}
Also used : WrappedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail) HashSet(java.util.HashSet)

Example 4 with CacheOperationFailedException

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);
}
Also used : CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail)

Example 5 with CacheOperationFailedException

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;
}
Also used : CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail) OperationException(uk.gov.gchq.gaffer.operation.OperationException)

Aggregations

CacheOperationFailedException (uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException)13 NamedOperationDetail (uk.gov.gchq.gaffer.named.operation.NamedOperationDetail)7 NamedViewDetail (uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail)5 HashSet (java.util.HashSet)2 Test (org.junit.jupiter.api.Test)2 WrappedCloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable)2 NamedView (uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView)2 OperationException (uk.gov.gchq.gaffer.operation.OperationException)2 List (java.util.List)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 NoAccessPredicate (uk.gov.gchq.gaffer.access.predicate.NoAccessPredicate)1 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)1 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)1 Operation (uk.gov.gchq.gaffer.operation.Operation)1 OperationView (uk.gov.gchq.gaffer.operation.graph.OperationView)1 StoreProperties (uk.gov.gchq.gaffer.store.StoreProperties)1 User (uk.gov.gchq.gaffer.user.User)1