Search in sources :

Example 6 with CacheOperationFailedException

use of uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException in project Gaffer by gchq.

the class NamedOperationCache method add.

private void add(final NamedOperationDetail namedOperation, final boolean overwrite, final User user, final String adminAuth) throws CacheOperationFailedException {
    String name;
    try {
        name = namedOperation.getOperationName();
    } catch (final NullPointerException e) {
        throw new CacheOperationFailedException("NamedOperation cannot be null", e);
    }
    if (null == name) {
        throw new CacheOperationFailedException("NamedOperation name cannot be null");
    }
    if (!overwrite) {
        addToCache(name, namedOperation, false);
        return;
    }
    NamedOperationDetail existing;
    try {
        existing = getFromCache(name);
    } catch (final CacheOperationFailedException e) {
        // if there is no existing named Operation add one
        addToCache(name, namedOperation, false);
        return;
    }
    if (existing.hasWriteAccess(user, adminAuth)) {
        addToCache(name, namedOperation, true);
    } else {
        throw new CacheOperationFailedException("User " + user.getUserId() + " does not have permission to overwrite");
    }
}
Also used : NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException)

Example 7 with CacheOperationFailedException

use of uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException in project Gaffer by gchq.

the class NamedViewResolverTest method shouldThrowExceptionWhenNamedViewToBeMergedIsNotInCache.

@Test
public void shouldThrowExceptionWhenNamedViewToBeMergedIsNotInCache() throws CacheOperationFailedException {
    // Given
    given(CACHE.getNamedView(NAMED_VIEW_NAME, CONTEXT.getUser())).willReturn(FULL_NAMED_VIEW_DETAIL);
    given(CACHE.getNamedView(NAMED_VIEW_NAME + 1, CONTEXT.getUser())).willThrow(new CacheOperationFailedException("No NamedView with the name namedViewName1 exists in the cache"));
    final OperationChain<?> opChain = new OperationChain.Builder().first(new GetElements.Builder().view(new NamedView.Builder().name(NAMED_VIEW_NAME).merge(new NamedView.Builder().name(NAMED_VIEW_NAME + 1).build()).build()).build()).build();
    // When / Then
    try {
        RESOLVER.preExecute(opChain, CONTEXT);
        fail("Exception expected");
    } catch (final RuntimeException e) {
        assert e.getMessage().contains("No NamedView with the name namedViewName1 exists in the cache");
    }
}
Also used : NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException) Test(org.junit.jupiter.api.Test)

Example 8 with CacheOperationFailedException

use of uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException in project Gaffer by gchq.

the class NamedViewResolver method resolveView.

private View resolveView(final String namedViewName, final Map<String, Object> parameters, final Context context) {
    final NamedViewDetail cachedNamedView;
    try {
        cachedNamedView = cache.getNamedView(namedViewName, context.getUser());
    } catch (final CacheOperationFailedException e) {
        throw new RuntimeException(e);
    }
    View resolvedView;
    if (null == cachedNamedView) {
        resolvedView = new View();
    } else {
        resolvedView = cachedNamedView.getView(parameters);
        if (resolvedView instanceof NamedView) {
            ((NamedView) resolvedView).setName(null);
            if (CollectionUtils.isNotEmpty(((NamedView) resolvedView).getMergedNamedViewNames())) {
                final View.Builder viewBuilder = new View.Builder();
                viewBuilder.merge(resolvedView);
                for (final String name : ((NamedView) resolvedView).getMergedNamedViewNames()) {
                    viewBuilder.merge(resolveView(name, parameters, context));
                }
                resolvedView = viewBuilder.build();
            }
        }
    }
    return resolvedView;
}
Also used : NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View)

Example 9 with CacheOperationFailedException

use of uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException in project Gaffer by gchq.

the class NamedOperationCacheTest method shouldThrowExceptionTryingToDeleteOperationConfiguredWithWriteNoAccessPredicate.

@Test
public void shouldThrowExceptionTryingToDeleteOperationConfiguredWithWriteNoAccessPredicate() throws CacheOperationFailedException {
    final NamedOperationDetail noWriteAccess = new NamedOperationDetail.Builder().creatorId(standardUser.getUserId()).description("an operation that does no allow read access").operationName("test").readers(readers).operationChain(standardOpChain).writeAccessPredicate(new NoAccessPredicate()).build();
    cache.addNamedOperation(noWriteAccess, false, standardUser);
    assertThatExceptionOfType(CacheOperationFailedException.class).isThrownBy(() -> cache.deleteNamedOperation("test", standardUser));
}
Also used : NoAccessPredicate(uk.gov.gchq.gaffer.access.predicate.NoAccessPredicate) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException) Test(org.junit.jupiter.api.Test)

Example 10 with CacheOperationFailedException

use of uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException in project Gaffer by gchq.

the class NamedOperationResolver method resolveNamedOperation.

private List<Operation> resolveNamedOperation(final NamedOperation namedOp, final User user) {
    final NamedOperationDetail namedOpDetail;
    try {
        namedOpDetail = cache.getNamedOperation(namedOp.getOperationName(), user);
    } catch (final CacheOperationFailedException e) {
        // Unable to find named operation - just return the original named operation
        return Collections.singletonList(namedOp);
    }
    final OperationChain<?> namedOperationChain = namedOpDetail.getOperationChain(namedOp.getParameters());
    updateOperationInput(namedOperationChain, namedOp.getInput());
    // Call resolveNamedOperations again to check there are no nested named operations
    resolveNamedOperations(namedOperationChain, user);
    return namedOperationChain.getOperations();
}
Also used : NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException)

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