FederatedStoreUtil { public static <OP extends Operation> OP updateOperationForGraph(final OP operation, final Graph graph) { OP resultOp = operation; if (operation instanceof Operations) { resultOp = (OP) operation.shallowClone(); final Operations<Operation> operations = (Operations) resultOp; final List<Operation> resultOperations = new ArrayList<>(); for (final Operation nestedOp : operations.getOperations()) { final Operation updatedNestedOp = updateOperationForGraph(nestedOp, graph); if (null == updatedNestedOp) { resultOp = null; break; } resultOperations.add(updatedNestedOp); } operations.updateOperations(resultOperations); } else if (operation instanceof OperationView) { final View view = ((OperationView) operation).getView(); if (null != view && view.hasGroups()) { final View validView = createValidView(view, graph.getSchema()); if (view != validView) { resultOp = (OP) operation.shallowClone(); if (validView.hasGroups()) { ((OperationView) resultOp).setView(validView); } else { resultOp = null; } } } } else if (operation instanceof AddElements) { final AddElements addElements = ((AddElements) operation); if (null == addElements.getInput()) { if (!addElements.isValidate() || !addElements.isSkipInvalidElements()) { LOGGER.debug("Invalid elements will be skipped when added to {}", graph.getGraphId()); resultOp = (OP) addElements.shallowClone(); ((AddElements) resultOp).setValidate(true); ((AddElements) resultOp).setSkipInvalidElements(true); } } else { resultOp = (OP) addElements.shallowClone(); final Set<String> graphGroups = graph.getSchema().getGroups(); final Iterable<? extends Element> filteredInput = Iterables.filter( addElements.getInput(), element -> graphGroups.contains(null != element ? element.getGroup() : null) ); ((AddElements) resultOp).setInput(filteredInput); } } return resultOp; } private  FederatedStoreUtil(); static String createOperationErrorMsg(final Operation operation, final String graphId, final Exception e); static List<String> getGraphIds(final Map<String, String> config); static List<String> getCleanStrings(final String value); static OP updateOperationForGraph(final OP operation, final Graph graph); static boolean isUserRequestingAdminUsage(final Operation operation); static final Collection<String> STRINGS_TO_REMOVE; }
@Test public void shouldNotUpdateOperationViewIfNotRequired() { final Graph graph = createGraph(); final GetElements operation = new GetElements.Builder() .view(new View.Builder() .edge(TestGroups.EDGE) .build()) .build(); final GetElements updatedOp = FederatedStoreUtil.updateOperationForGraph(operation, graph); assertSame(operation, updatedOp); assertSame(operation.getView(), updatedOp.getView()); }
@Test public void shouldUpdateOperationView() { final Graph graph = createGraph(); final GetElements operation = new GetElements.Builder() .view(new View.Builder() .edge(TestGroups.EDGE, new ViewElementDefinition()) .edge(TestGroups.EDGE_2, new ViewElementDefinition()) .entity(TestGroups.ENTITY, new ViewElementDefinition()) .entity(TestGroups.ENTITY_2, new ViewElementDefinition()) .build()) .build(); final GetElements updatedOp = FederatedStoreUtil.updateOperationForGraph(operation, graph); assertNotSame(operation, updatedOp); assertNotSame(operation.getView(), updatedOp.getView()); assertEquals(Sets.newHashSet(TestGroups.ENTITY), updatedOp.getView().getEntityGroups()); assertEquals(Sets.newHashSet(TestGroups.EDGE), updatedOp.getView().getEdgeGroups()); assertSame(operation.getView().getEntity(TestGroups.ENTITY), updatedOp.getView().getEntity(TestGroups.ENTITY)); assertSame(operation.getView().getEdge(TestGroups.EDGE), updatedOp.getView().getEdge(TestGroups.EDGE)); }
@Test public void shouldUpdateOperationViewAndReturnNullIfViewHasNoGroups() { final Graph graph = createGraph(); final GetElements operation = new GetElements.Builder() .view(new View.Builder() .edge(TestGroups.EDGE_2, new ViewElementDefinition()) .entity(TestGroups.ENTITY_2, new ViewElementDefinition()) .build()) .build(); final GetElements updatedOp = FederatedStoreUtil.updateOperationForGraph(operation, graph); assertNull(updatedOp); }
@Test public void shouldUpdateOperationChainAndReturnNullIfNestedOperationViewHasNoGroups() { final Graph graph = createGraph(); final OperationChain<?> operation = new OperationChain.Builder() .first(new GetElements.Builder() .view(new View.Builder() .edge(TestGroups.EDGE_2, new ViewElementDefinition()) .entity(TestGroups.ENTITY_2, new ViewElementDefinition()) .build()) .build()) .build(); final OperationChain<?> updatedOp = FederatedStoreUtil.updateOperationForGraph(operation, graph); assertNull(updatedOp); }
@Test public void shouldUpdateNestedOperations() { final Graph graph = createGraph(); final HashMap<String, String> options = new HashMap<>(); options.put("key", "value"); final HashMap<String, String> options2 = new HashMap<>(); options2.put("key", "value"); final GetElements operation = new GetElements.Builder() .view(new View.Builder() .edge(TestGroups.EDGE, new ViewElementDefinition()) .edge(TestGroups.EDGE_2, new ViewElementDefinition()) .entity(TestGroups.ENTITY, new ViewElementDefinition()) .entity(TestGroups.ENTITY_2, new ViewElementDefinition()) .build()) .options(options2) .build(); final OperationChain opChain = new OperationChain.Builder() .first(operation) .options(options) .build(); final OperationChain<?> updatedOpChain = FederatedStoreUtil.updateOperationForGraph(opChain, graph); assertNotSame(opChain, updatedOpChain); assertEquals(options, updatedOpChain.getOptions()); assertEquals(1, updatedOpChain.getOperations().size()); final GetElements updatedOperation = (GetElements) updatedOpChain.getOperations().get(0); assertNotSame(operation, updatedOperation); assertEquals(options2, updatedOperation.getOptions()); assertNotSame(operation.getView(), updatedOperation.getView()); assertEquals(Sets.newHashSet(TestGroups.ENTITY), updatedOperation.getView().getEntityGroups()); assertEquals(Sets.newHashSet(TestGroups.EDGE), updatedOperation.getView().getEdgeGroups()); assertSame(operation.getView().getEntity(TestGroups.ENTITY), updatedOperation.getView().getEntity(TestGroups.ENTITY)); assertSame(operation.getView().getEdge(TestGroups.EDGE), updatedOperation.getView().getEdge(TestGroups.EDGE)); }
@Test public void shouldNotUpdateAddElementsFlagsWhenNotRequired() { final Graph graph = createGraph(); final AddElements operation = new AddElements.Builder() .validate(true) .skipInvalidElements(true) .build(); final AddElements updatedOp = FederatedStoreUtil.updateOperationForGraph(operation, graph); assertSame(operation, updatedOp); assertNull(updatedOp.getInput()); }
@Test public void shouldUpdateAddElementsFlagsWhenNullInputAndValidateFalse() { final Graph graph = createGraph(); final AddElements operation = new AddElements.Builder() .validate(false) .skipInvalidElements(true) .build(); final AddElements updatedOp = FederatedStoreUtil.updateOperationForGraph(operation, graph); assertNotSame(operation, updatedOp); assertNull(updatedOp.getInput()); assertTrue(updatedOp.isValidate()); assertTrue(updatedOp.isSkipInvalidElements()); }
@Test public void shouldUpdateAddElementsFlagsWhenNullInputAndSkipFalse() { final Graph graph = createGraph(); final AddElements operation = new AddElements.Builder() .validate(true) .skipInvalidElements(false) .build(); final AddElements updatedOp = FederatedStoreUtil.updateOperationForGraph(operation, graph); assertNotSame(operation, updatedOp); assertNull(updatedOp.getInput()); assertTrue(updatedOp.isValidate()); assertTrue(updatedOp.isSkipInvalidElements()); }
@Test public void shouldUpdateAddElementsInput() { final Graph graph = createGraph(); final AddElements operation = new AddElements.Builder() .input(new Entity.Builder() .group(TestGroups.ENTITY) .build(), new Entity.Builder() .group(TestGroups.ENTITY_2) .build(), new Edge.Builder() .group(TestGroups.EDGE) .build(), new Edge.Builder() .group(TestGroups.EDGE_2) .build()) .build(); final AddElements updatedOp = FederatedStoreUtil.updateOperationForGraph(operation, graph); assertNotSame(operation, updatedOp); assertNotSame(operation.getInput(), updatedOp.getInput()); final List<Element> updatedInput = Lists.newArrayList(updatedOp.getInput()); assertEquals( Arrays.asList(new Entity.Builder() .group(TestGroups.ENTITY) .build(), new Edge.Builder() .group(TestGroups.EDGE) .build()), updatedInput); }