FunctionAuthoriser implements GraphHook { @Override public void preExecute(final OperationChain<?> opChain, final Context context) { if (unauthorisedFunctions == null || unauthorisedFunctions.isEmpty()) { return; } Object input = null; if (opChain.getOperations().size() > 0 && opChain.getOperations().get(0) instanceof Input) { input = ((Input) opChain.getOperations().get(0)).getInput(); ((Input) opChain.getOperations().get(0)).setInput(null); } SimpleClassNameCache.setUseFullNameForSerialisation(true); String chainString; try { chainString = new String(JSONSerialiser.serialise(opChain)); } catch (final SerialisationException e) { LOGGER.warn("Failed to serialise operation chain: " + opChain + " due to " + e.getMessage()); return; } finally { if (input != null) { ((Input) opChain.getOperations().get(0)).setInput(input); } } checkNoUnauthorisedFunctionsArePresent(chainString); } FunctionAuthoriser();  FunctionAuthoriser(final List<Class<? extends Function>> unauthorisedFunctions); @Override void preExecute(final OperationChain<?> opChain, final Context context); List<Class<? extends Function>> getUnauthorisedFunctions(); void setUnauthorisedFunctions(final List<Class<? extends Function>> unauthorisedFunctions);  }
@Test public void shouldAllowEmptyOperationChain() { OperationChain chain = new OperationChain(); FunctionAuthoriser authoriser = new FunctionAuthoriser(Lists.newArrayList(Identity.class)); authoriser.preExecute(chain, new Context()); }
@Test public void shouldAllowAllFunctionsWhenUnauthorisedFunctionsAreNull() { OperationChain chain = generateOperation(Identity.class, ToString.class); FunctionAuthoriser authoriser = new FunctionAuthoriser(null); authoriser.preExecute(chain, new Context()); }
@Test public void shouldAllowAllFunctionsWhenUnauthorisedFunctionsAreEmpty() { OperationChain chain = generateOperation(Identity.class, ToString.class); FunctionAuthoriser authoriser = new FunctionAuthoriser(new ArrayList<>()); authoriser.preExecute(chain, new Context()); }
@Test public void shouldNotErrorIfFirstOperationIsNotInput() { OperationChain chain = new OperationChain.Builder() .first(new GetAllElements()).build(); FunctionAuthoriser authoriser = new FunctionAuthoriser(Lists.newArrayList(Identity.class)); authoriser.preExecute(chain, new Context()); }
@Test public void shouldMaintainOperationChainIfItFailsToSerialise() { FunctionAuthoriser authoriser = new FunctionAuthoriser(Lists.newArrayList(Identity.class)); List fakeInput = Lists.newArrayList(new EntitySeed(1), new EntitySeed(2), new EntitySeed(3)); GetElements getElements = new GetElements(); getElements.setInput(fakeInput); getElements = spy(getElements); final OperationChain chain = new OperationChain.Builder() .first(getElements) .then(generateOperation(ToEntityId.class)) .build(); authoriser.preExecute(chain, new Context()); assertEquals(fakeInput, ((Input) chain.getOperations().get(0)).getInput()); }
@Test public void shouldMaintainOperationChainInputIfItSerialises() { FunctionAuthoriser authoriser = new FunctionAuthoriser(Lists.newArrayList(Identity.class)); List fakeInput = Lists.newArrayList(new EntitySeed(1), new EntitySeed(2), new EntitySeed(3)); GetElements getElements = new GetElements(); getElements.setInput(fakeInput); final OperationChain chain = new OperationChain.Builder() .first(getElements) .then(generateOperation(ToEntityId.class)) .build(); authoriser.preExecute(chain, new Context()); assertEquals(fakeInput, ((Input) chain.getOperations().get(0)).getInput()); }