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()); }
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); }
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()); }
@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)); }
@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(); }
@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 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); }
@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 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 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 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 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 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 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 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 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)); }