private boolean acceptsConnection(final RuleExtension rule, final GraphConnectionContext context) { final Edge<? extends View<?>, ? extends Node> connector = context.getConnector(); return evalUtils.getElementDefinitionId(connector).equals(rule.getId()); }
@Override public boolean accepts(final CanConnect rule, final GraphConnectionContext context) { final Edge<? extends View<?>, ? extends Node> connector = context.getConnector(); final Set<String> labels = evalUtils.getLabels(connector); return labels.stream() .filter(cr -> rule.getRole().equals(cr) && // As for acceptance the delegated handler only needs the connector id, no need // to calculate roles for current source/target nodes. connectionEvaluationHandler.accepts(rule, RuleContextBuilder.DomainContexts.connection(cr, Optional.empty(), Optional.empty()))) .findAny() .isPresent(); }
private RuleViolations evaluateConnection(final RuleExtension rule, final GraphConnectionContext context) { LOGGER.log(Level.INFO, "Evaluating rule handler [" + getClass().getName() + "]..."); final Optional<Node<? extends View<?>, ? extends Edge>> sourceNode = context.getSource(); final Optional<Node<? extends View<?>, ? extends Edge>> targetNode = context.getTarget(); final DefaultRuleViolations result = new DefaultRuleViolations(); boolean isValid = true; if (sourceNode.isPresent() && targetNode.isPresent()) { final Node<? extends View<?>, ? extends Edge> source = sourceNode.get(); final Node<? extends View<?>, ? extends Edge> target = targetNode.get(); final Element<? extends Definition> parentTarget = (Element<? extends Definition>) GraphUtils.getParent(target); final Element<? extends Definition> parentSource = (Element<? extends Definition>) GraphUtils.getParent(source); final Optional<Class<?>> parentType = getParentType(rule, parentTarget, parentSource); isValid = new ParentsTypeMatcher(definitionManager, parentType.orElse(null)) .test(source, target); } if (!isValid) { addViolation(context.getConnector().getUUID(), rule, result); } return result; } }
@Before @SuppressWarnings("unchecked") public void setup() { this.check = spy(getRule()); when(context.getGraph()).thenReturn(graph); when(context.getConnector()).thenReturn(connector); when(connector.getContent()).thenReturn(connectorView); when(connectorView.getDefinition()).thenReturn(new Definition()); }
@Override public RuleViolations evaluate(final CanConnect rule, final GraphConnectionContext context) { final Edge<? extends View<?>, ? extends Node> connector = context.getConnector(); final Node<? extends View<?>, ? extends Edge> source = context.getSource().orElse(null); final Node<? extends View<?>, ? extends Edge> target = context.getTarget().orElse(null); if (source == null || target == null) { return new DefaultRuleViolations(); } final Set<String> edgeLabels = evalUtils.getLabels(connector); final Optional<Set<String>> sourceLabels = Optional.of(evalUtils.getLabels(source)); final Optional<Set<String>> targetLabels = Optional.of(evalUtils.getLabels(target)); final DefaultRuleViolations result = new DefaultRuleViolations(); edgeLabels.stream() .filter(pr -> rule.getRole().equals(pr)) .forEach(pr -> result.addViolations(connectionEvaluationHandler .evaluate(rule, RuleContextBuilder.DomainContexts.connection(pr, sourceLabels, targetLabels)))); return GraphEvaluationHandlerUtils.addViolationsSourceUUID(connector.getUUID(), result); } }
@Test @SuppressWarnings("unchecked") public void checkMissingConnectionNodesDoesNotTriggerCheck() { when(context.getSource()).thenReturn(Optional.empty()); when(context.getTarget()).thenReturn(Optional.empty()); when(context.getConnector()).thenReturn(connector); final RuleViolations result = check.evaluate(rule, context); assertNotNull(result); assertFalse(result.violations().iterator().hasNext()); verify(check, never()).isConnectionAlreadyFormed(any(Node.class), any(Node.class), any(Edge.class)); }
@Test @SuppressWarnings("unchecked") public void checkMissingConnectionTargetNodeDoesNotTriggerGraphWalk() { final Node source = mock(Node.class); when(context.getSource()).thenReturn(Optional.of(source)); when(context.getTarget()).thenReturn(Optional.empty()); when(context.getConnector()).thenReturn(connector); when(check.getTreeWalker(any(Node.class), any(Node.class), any(Edge.class))).thenReturn(walker); final RuleViolations result = check.evaluate(rule, context); assertNotNull(result); assertFalse(result.violations().iterator().hasNext()); verify(walker, never()).traverse(any(Graph.class), any(TreeTraverseCallback.class)); }
@Test @SuppressWarnings("unchecked") public void checkMissingConnectionTargetNodeDoesNotTriggerCheck() { final Node source = mock(Node.class); when(context.getSource()).thenReturn(Optional.of(source)); when(context.getTarget()).thenReturn(Optional.empty()); when(context.getConnector()).thenReturn(connector); final RuleViolations result = check.evaluate(rule, context); assertNotNull(result); assertFalse(result.violations().iterator().hasNext()); verify(check, never()).isConnectionAlreadyFormed(any(Node.class), any(Node.class), any(Edge.class)); }
@Before @SuppressWarnings("unchecked") public void setup() throws Exception { super.setup(); this.graph = graphHandler.graph; when(graphHandler.definitionAdapter.getId(eq(connectorDef))).thenReturn(DefinitionId.build(DEF_EDGE_ID)); this.connector = graphHandler.newEdge(EDGE_UUID, Optional.of(connectorDef)); when(connectionContext.getGraph()).thenReturn(graph); when(connectionContext.getConnector()).thenReturn(connector); when(ruleExtension.getId()).thenReturn(DEF_EDGE_ID); when(ruleExtension.getArguments()).thenReturn(new String[]{"violation1"}); tested = new ConnectorParentsMatchConnectionHandler(graphHandler.definitionManager); }
@Test @SuppressWarnings("unchecked") public void checkCompleteConnectionDefinitionTriggersCheck() { final Node source = mock(Node.class); final Node target = mock(Node.class); when(context.getSource()).thenReturn(Optional.of(source)); when(context.getTarget()).thenReturn(Optional.of(target)); when(context.getConnector()).thenReturn(connector); final RuleViolations result = check.evaluate(rule, context); assertNotNull(result); assertFalse(result.violations().iterator().hasNext()); verify(check).isConnectionAlreadyFormed(any(Node.class), any(Node.class), any(Edge.class)); }
@Before @SuppressWarnings("unchecked") public void setup() throws Exception { super.setup(); final Set<String> edgeLabels = Collections.singleton(EDGE_ID); when(edge.getContent()).thenReturn(edgeContent); when(edge.getLabels()).thenReturn(edgeLabels); when(edgeContent.getDefinition()).thenReturn(edgeDefinition); when(definitionAdapter.getId(eq(edgeDefinition))).thenReturn(DefinitionId.build(EDGE_ID)); when(context.getConnector()).thenReturn(edge); when(context.getSource()).thenReturn(Optional.of(parent)); when(context.getTarget()).thenReturn(Optional.of(candidate)); tested = new GraphConnectionEvaluationHandler(definitionManager, HANDLER); }
@Test @SuppressWarnings("unchecked") public void checkMissingConnectionNodesDoesNotTriggerGraphWalk() { when(context.getSource()).thenReturn(Optional.empty()); when(context.getTarget()).thenReturn(Optional.empty()); when(context.getConnector()).thenReturn(connector); when(check.getTreeWalker(any(Node.class), any(Node.class), any(Edge.class))).thenReturn(walker); final RuleViolations result = check.evaluate(rule, context); assertNotNull(result); assertFalse(result.violations().iterator().hasNext()); verify(walker, never()).traverse(any(Graph.class), any(TreeTraverseCallback.class)); }
@Test @SuppressWarnings("unchecked") public void checkCompleteConnectionDefinitionTriggersGraphWalk() { final Node source = mock(Node.class); final Node target = mock(Node.class); when(context.getSource()).thenReturn(Optional.of(source)); when(context.getTarget()).thenReturn(Optional.of(target)); when(context.getConnector()).thenReturn(connector); when(check.getTreeWalker(any(Node.class), any(Node.class), any(Edge.class))).thenReturn(walker); final RuleViolations result = check.evaluate(rule, context); assertNotNull(result); assertFalse(result.violations().iterator().hasNext()); verify(walker).traverse(eq(graph), any(TreeTraverseCallback.class)); }
@Test @SuppressWarnings("unchecked") public void checkNoViolationsWhenNoExistingConnections() { final Node node1 = new NodeImpl<>("node1"); final Node node2 = new NodeImpl<>("node2"); graph.addNode(node1); graph.addNode(node2); when(context.getSource()).thenReturn(Optional.of(node1)); when(context.getTarget()).thenReturn(Optional.of(node2)); when(context.getConnector()).thenReturn(connector); final RuleViolations result = check.evaluate(rule, context); assertNotNull(result); assertFalse(result.violations().iterator().hasNext()); }
when(context.getConnector()).thenReturn(connector);
public static void verifyConnection(final GraphConnectionContext context, final Edge<? extends View<?>, ? extends Node> connector, final Node<? extends View<?>, ? extends Edge> sourceNode, final Node<? extends View<?>, ? extends Edge> targetNode) { assertNotNull(context); final Edge<? extends View<?>, ? extends Node> connector1 = context.getConnector(); final Optional<Node<? extends View<?>, ? extends Edge>> source = context.getSource(); final Optional<Node<? extends View<?>, ? extends Edge>> target = context.getTarget(); assertNotNull(connector1); assertEquals(connector, connector1); if (null != sourceNode) { assertEquals(sourceNode, source.get()); } if (null != targetNode) { assertEquals(targetNode, target.get()); } }
when(context.getConnector()).thenReturn(connector);