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; } }
@Test @SuppressWarnings("unchecked") public void testNotAccepts() { when(connectionContext.getSource()).thenReturn(Optional.of(nodeA)); when(connectionContext.getTarget()).thenReturn(Optional.of(nodeB)); when(ruleExtension.getId()).thenReturn("anyOtherId"); when(ruleExtension.getTypeArguments()).thenReturn(new Class[]{ParentDefinition.class}); assertFalse(tested.accepts(ruleExtension, connectionContext)); }
@Test @SuppressWarnings("unchecked") public void testAccepts() { when(connectionContext.getSource()).thenReturn(Optional.of(nodeA)); when(connectionContext.getTarget()).thenReturn(Optional.of(nodeB)); when(ruleExtension.getTypeArguments()).thenReturn(new Class[]{ParentDefinition.class}); assertTrue(tested.accepts(ruleExtension, connectionContext)); }
@Test @SuppressWarnings("unchecked") public void testEvaluateFailed1() { when(context.getSource()).thenReturn(Optional.of(candidate)); when(context.getTarget()).thenReturn(Optional.of(parent)); final RuleViolations violations = tested.evaluate(RULE, context); assertNotNull(violations); assertTrue(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); } }
@Test @SuppressWarnings("unchecked") public void testNoParentDefinition() { when(connectionContext.getSource()).thenReturn(Optional.of(nodeA)); when(connectionContext.getTarget()).thenReturn(Optional.of(nodeC)); when(ruleExtension.getTypeArguments()).thenReturn(new Class[]{}); final RuleViolations violations = tested.evaluate(ruleExtension, connectionContext); assertNotNull(violations); assertViolations(violations, true); }
private void assertEvalSuccess(final Optional<Node<? extends View<?>, ? extends Edge>> source, final Optional<Node<? extends View<?>, ? extends Edge>> target) { when(connectionContext.getSource()).thenReturn(source); when(connectionContext.getTarget()).thenReturn(target); when(ruleExtension.getTypeArguments()).thenReturn(new Class[]{ParentDefinition.class}); final RuleViolations violations = tested.evaluate(ruleExtension, connectionContext); assertNotNull(violations); assertViolations(violations, true); }
@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)); }
@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 testFailEvaluateParentDefinition() { when(connectionContext.getSource()).thenReturn(Optional.of(nodeA)); when(connectionContext.getTarget()).thenReturn(Optional.of(nodeC)); when(ruleExtension.getTypeArguments()).thenReturn(new Class[]{ParentDefinition.class}); final RuleViolations violations = tested.evaluate(ruleExtension, connectionContext); assertNotNull(violations); assertViolations(violations, false); }
@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 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)); }
@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(); 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 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 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 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()); }
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()); } }
graph.addNode(node2); when(context.getSource()).thenReturn(Optional.of(node1)); when(context.getTarget()).thenReturn(Optional.of(node2)); when(context.getConnector()).thenReturn(connector);
graph.addNode(node2); when(context.getSource()).thenReturn(Optional.of(node2)); when(context.getTarget()).thenReturn(Optional.of(node1)); when(context.getConnector()).thenReturn(connector);
graphHandler.setChild(grandParentNode, nodeA); when(connectionContext.getSource()).thenReturn(Optional.of(nodeA)); when(connectionContext.getTarget()).thenReturn(Optional.of(nodeB)); when(ruleExtension.getTypeArguments()).thenReturn(new Class[]{ParentDefinition.class});