AddOperationsToChain implements GraphHook { @Override public void preExecute(final OperationChain<?> opChain, final Context context) { final List<Operation> newOpList = new ArrayList<>(); boolean hasAuth = false; if (!authorisedOps.isEmpty() && !context.getUser().getOpAuths().isEmpty()) { for (final String auth : authorisedOps.keySet()) { if (context.getUser().getOpAuths().contains(auth)) { final AdditionalOperations additionalOperations = authorisedOps.get(auth); newOpList.addAll(additionalOperations.getStart()); newOpList.addAll(addOperationsToChain(opChain, additionalOperations)); newOpList.addAll(additionalOperations.getEnd()); hasAuth = true; break; } } } if (!hasAuth) { newOpList.addAll(defaultOperations.getStart()); newOpList.addAll(addOperationsToChain(opChain, defaultOperations)); newOpList.addAll(defaultOperations.getEnd()); } try { opChain.updateOperations(newOpList); } catch (final Exception e) { } }  @Override void preExecute(final OperationChain<?> opChain, final Context context); @Override T postExecute(final T result,
                             final OperationChain<?> opChain, final Context context); @Override T onFailure(final T result, final OperationChain<?> opChain, final Context context, final Exception e); List<Operation> getStart(); void setStart(final List<Operation> start); List<Operation> getEnd(); void setEnd(final List<Operation> end); Map<String, List<Operation>> getBefore(); void setBefore(final Map<String, List<Operation>> before); Map<String, List<Operation>> getAfter(); void setAfter(final Map<String, List<Operation>> after); LinkedHashMap<String, AdditionalOperations> getAuthorisedOps(); void setAuthorisedOps(final LinkedHashMap<String, AdditionalOperations> authorisedOps);  }
@Test public void shouldAddAllOperationsWithNoAuthsGivenPath() throws IOException { AddOperationsToChain hook = fromJson(ADD_OPERATIONS_TO_CHAIN_RESOURCE_PATH); Operation discardOutput = new DiscardOutput(); Operation splitStore = new SplitStoreFromFile(); Operation validate = new Validate(); Operation getAdjacentIds = new GetAdjacentIds(); Operation count = new Count<>(); Operation countGroups = new CountGroups(); Operation getElements = new GetElements(); Operation getAllElements = new GetAllElements(); Operation limit = new Limit<>(); final OperationChain opChain = new OperationChain.Builder() .first(getAdjacentIds) .then(getElements) .then(getAllElements) .build(); hook.preExecute(opChain, new Context(new User())); final OperationChain expectedOpChain = new OperationChain.Builder() .first(discardOutput) .then(splitStore) .then(validate) .then(getAdjacentIds) .then(count) .then(discardOutput) .then(countGroups) .then(getElements) .then(getAllElements) .then(limit) .then(validate) .then(count) .build(); JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain)); }
@Test public void shouldAddAllOperationsWithFirstAuthsGivenPath() throws IOException { AddOperationsToChain hook = fromJson(ADD_OPERATIONS_TO_CHAIN_RESOURCE_PATH); User user = new User.Builder().opAuths("auth1", "auth2").build(); Operation discardOutput = new DiscardOutput(); Operation splitStore = new SplitStoreFromFile(); Operation getAdjacentIds = new GetAdjacentIds(); Operation getElements = new GetElements(); Operation getAllElements = new GetAllElements(); final OperationChain opChain = new OperationChain.Builder() .first(getAdjacentIds) .then(getElements) .then(getAllElements) .build(); hook.preExecute(opChain, new Context(user)); final OperationChain expectedOpChain = new OperationChain.Builder() .first(discardOutput) .then(getAdjacentIds) .then(getElements) .then(getAllElements) .then(splitStore) .build(); JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain)); }
@Test public void shouldAddAllOperationsWithSecondAuthsGivenPath() throws IOException { AddOperationsToChain hook = fromJson(ADD_OPERATIONS_TO_CHAIN_RESOURCE_PATH); User user = new User.Builder().opAuths("auth2").build(); Operation splitStore = new SplitStoreFromFile(); Operation validate = new Validate(); Operation getAdjacentIds = new GetAdjacentIds(); Operation countGroups = new CountGroups(); Operation getElements = new GetElements(); Operation getAllElements = new GetAllElements(); final OperationChain opChain = new OperationChain.Builder() .first(getAdjacentIds) .then(getElements) .then(getAllElements) .build(); hook.preExecute(opChain, new Context(user)); final OperationChain expectedOpChain = new OperationChain.Builder() .first(validate) .then(getAdjacentIds) .then(countGroups) .then(getElements) .then(getAllElements) .then(splitStore) .build(); JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain)); }
@Test public void shouldAddAllOperationsGivenJson() throws IOException { final byte[] bytes; try (final InputStream inputStream = StreamUtil.openStream(getClass(), ADD_OPERATIONS_TO_CHAIN_RESOURCE_PATH)) { bytes = IOUtils.toByteArray(inputStream); } final AddOperationsToChain hook = fromJson(bytes); Operation discardOutput = new DiscardOutput(); Operation splitStore = new SplitStoreFromFile(); Operation validate = new Validate(); Operation getAdjacentIds = new GetAdjacentIds(); Operation count = new Count<>(); Operation countGroups = new CountGroups(); Operation getElements = new GetElements(); Operation getAllElements = new GetAllElements(); Operation limit = new Limit<>(); final OperationChain opChain = new OperationChain.Builder() .first(getAdjacentIds) .then(getElements) .then(getAllElements) .build(); hook.preExecute(opChain, new Context(new User())); final OperationChain expectedOpChain = new OperationChain.Builder() .first(discardOutput) .then(splitStore) .then(validate) .then(getAdjacentIds) .then(count) .then(discardOutput) .then(countGroups) .then(getElements) .then(getAllElements) .then(limit) .then(validate) .then(count) .build(); JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain)); }
@Test public void shouldHandleNestedOperationChain() throws SerialisationException { AddOperationsToChain hook = fromJson(ADD_OPERATIONS_TO_CHAIN_RESOURCE_PATH); Operation discardOutput = new DiscardOutput(); Operation splitStore = new SplitStoreFromFile(); Operation validate = new Validate(); Operation getAdjacentIds = new GetAdjacentIds(); Operation count = new Count<>(); Operation countGroups = new CountGroups(); Operation getElements = new GetElements(); If ifOp = new If.Builder<>() .conditional(new Conditional(new Exists(), new GetElements())) .then(new GetElements()) .otherwise(new GetAllElements()) .build(); Operation getAllElements = new GetAllElements(); Operation limit = new Limit<>(); final OperationChain opChain = new OperationChain.Builder() .first(getAdjacentIds) .then(new OperationChain.Builder() .first(getElements) .then(getAllElements) .build()) .then(ifOp) .build(); hook.preExecute(opChain, new Context(new User())); final OperationChain expectedOpChain = new OperationChain.Builder() .first(discardOutput) .then(splitStore) .then(validate) .then(getAdjacentIds) .then(count) .then(discardOutput) .then((Operation) new OperationChain.Builder() .first(countGroups) .then(getElements) .then(getAllElements) .then(limit) .then(validate) .build()) .then(new If.Builder<>() .conditional(new Conditional(new Exists(), new OperationChain<>(new CountGroups(), new GetElements()))) .then(new OperationChain<>(new CountGroups(), new GetElements())) .otherwise(new OperationChain<>(new GetAllElements(), new Limit<>(), new Validate())) .build()) .then(new Count()) .build(); JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain)); }
@Test public void shouldHandleIfOperationWithNoConditionalOrOtherwise() throws SerialisationException { AddOperationsToChain hook = fromJson(ADD_OPERATIONS_TO_CHAIN_RESOURCE_PATH); Operation discardOutput = new DiscardOutput(); Operation splitStore = new SplitStoreFromFile(); If ifOp = new If.Builder<>() .then(new GetElements()) .build(); final OperationChain opChain = new OperationChain.Builder() .first(ifOp) .build(); hook.preExecute(opChain, new Context(new User())); final OperationChain expectedOpChain = new OperationChain.Builder() .first(discardOutput) .then(splitStore) .then(new If.Builder<>() .then(new OperationChain<>(new CountGroups(), new GetElements())) .build()) .then(new Count()) .build(); JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain)); }
@Test public void shouldFailQuietlyIfNestedOperationsCannotBeModified() throws SerialisationException { AddOperationsToChain hook = fromJson(ADD_OPERATIONS_TO_CHAIN_RESOURCE_PATH); Operation discardOutput = new DiscardOutput(); Operation splitStore = new SplitStoreFromFile(); Operation validate = new Validate(); Operation getAdjacentIds = new GetAdjacentIds(); Operation count = new Count<>(); Operation getElements = new GetElements(); Operation getAllElements = new GetAllElements(); TestUnmodifiableOperationsImpl nestedUnmodifiableOps = new TestUnmodifiableOperationsImpl(Arrays.asList(getAllElements, getElements)); final OperationChain opChain = new OperationChain.Builder() .first(getAdjacentIds) .then(nestedUnmodifiableOps) .build(); hook.preExecute(opChain, new Context(new User())); final OperationChain expectedOpChain = new OperationChain.Builder() .first(discardOutput) .then(splitStore) .then(validate) .then(getAdjacentIds) .then(count) .then(discardOutput) .then(nestedUnmodifiableOps) .then(count) .build(); JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain)); }