AddNamedOperationHandler implements OperationHandler<AddNamedOperation> { @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; } AddNamedOperationHandler();  AddNamedOperationHandler(final NamedOperationCache cache); @Override Void doOperation(final AddNamedOperation operation, final Context context, final Store store);  }
@Test public void shouldNotAllowForNonRecursiveNamedOperationsToBeNested() throws OperationException { OperationChain child = new OperationChain.Builder().first(new AddElements()).build(); addNamedOperation.setOperationChain(child); addNamedOperation.setOperationName("child"); handler.doOperation(addNamedOperation, context, store); OperationChain parent = new OperationChain.Builder() .first(new NamedOperation.Builder().name("child").build()) .then(new GetElements()) .build(); addNamedOperation.setOperationChain(parent); addNamedOperation.setOperationName("parent"); assertThrows(OperationException.class, () -> handler.doOperation(addNamedOperation, context, store)); }
@Test public void shouldAllowForOperationChainJSONWithParameter() { try { final String opChainJSON = "{ \"operations\": [ { \"class\":\"uk.gov.gchq.gaffer.operation.impl.get.GetAllElements\" }, { \"class\":\"uk.gov.gchq.gaffer.operation.impl.Limit\", \"resultLimit\": \"${param1}\" } ] }"; addNamedOperation.setOperationChain(opChainJSON); addNamedOperation.setOperationName("namedop"); ParameterDetail param = new ParameterDetail.Builder() .defaultValue(1L) .description("Limit param") .valueClass(Long.class) .build(); Map<String, ParameterDetail> paramMap = Maps.newHashMap(); paramMap.put("param1", param); addNamedOperation.setParameters(paramMap); handler.doOperation(addNamedOperation, context, store); assert cacheContains("namedop"); } catch (final Exception e) { fail("Expected test to pass without error. Exception " + e.getMessage()); } }
@Test public void shouldNotAllowForOperationChainWithParameterNotInOperationString() throws OperationException { final String opChainJSON = "{ \"operations\": [ { \"class\":\"uk.gov.gchq.gaffer.operation.impl.get.GetAllElements\" }, { \"class\":\"uk.gov.gchq.gaffer.operation.impl.export.set.ExportToSet\", \"key\": \"${param1}\" } ] }"; addNamedOperation.setOperationChain(opChainJSON); addNamedOperation.setOperationName("namedop"); ParameterDetail param = new ParameterDetail.Builder() .defaultValue("setKey") .description("key param") .valueClass(String.class) .build(); Map<String, ParameterDetail> paramMap = Maps.newHashMap(); paramMap.put("param2", param); addNamedOperation.setParameters(paramMap); assertThrows(OperationException.class, () -> handler.doOperation(addNamedOperation, context, store)); }
@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.getReadAccessPredicate()); final AccessPredicate expectedWriteAccessPredicate = new AccessPredicate(context.getUser(), writeAuths); assertEquals(expectedWriteAccessPredicate, result.getWriteAccessPredicate()); }
@Test public void shouldCustomAccessPredicateAddNamedOperationFieldsToNamedOperationDetailCorrectly() throws OperationException, CacheOperationFailedException { final AccessPredicate readAccessPredicate = new AccessPredicate(new CustomUserPredicate()); final AccessPredicate writeAccessPredicate = new AccessPredicate(new CustomUserPredicate()); OperationChain opChain = new OperationChain.Builder().first(new AddElements()).build(); addNamedOperation.setOperationChain(opChain); addNamedOperation.setScore(2); addNamedOperation.setOperationName("testOp"); addNamedOperation.setLabels(asList("test label")); addNamedOperation.setReadAccessPredicate(readAccessPredicate); addNamedOperation.setWriteAccessPredicate(writeAccessPredicate); 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()); assertEquals(readAccessPredicate, result.getReadAccessPredicate()); assertEquals(writeAccessPredicate, result.getWriteAccessPredicate()); }