@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(); }
@Test public void testEvaluateContainmentContextOnce() { tested.evaluate(ruleSet, containmentContext); tested.evaluate(ruleSet, containmentContext); verify(ruleSet, times(1)).getRules(); verify(containmentHandler, times(2)).evaluate(eq(containmentRule), eq(containmentContext)); verify(connectionHandler, never()).evaluate(any(CanConnect.class), any(ConnectionContext.class)); }
@Before public void setup() throws Exception { tested = new ConnectionEvaluationHandler(); }
@Before public void setup() throws Exception { RegistryFactory registryFactory = mock(RegistryFactory.class); when(registryFactory.newRuleHandlerRegistry()).thenReturn(ruleHandlerRegistry); when(ruleHandlerRegistry.getHandlersByContext(eq(ContainmentContext.class))).thenReturn(Arrays.asList(containmentHandler)); when(ruleHandlerRegistry.getHandlersByContext(eq(ConnectionContext.class))).thenReturn(Arrays.asList(connectionHandler)); when(connectionHandler.getRuleType()).thenReturn(CanConnect.class); when(connectionHandler.getContextType()).thenReturn(ConnectionContext.class); when(connectionHandler.accepts(any(CanConnect.class), any(ConnectionContext.class))).thenReturn(true); when(containmentHandler.accepts(any(CanContain.class), any(ContainmentContext.class))).thenReturn(true); when(containmentHandler.getRuleType()).thenReturn(CanContain.class); when(containmentHandler.getContextType()).thenReturn(ContainmentContext.class); RuleManagerImpl delegate = new RuleManagerImpl(registryFactory); when(ruleSet.getName()).thenReturn("testRuleSet"); when(ruleSet.getRules()).thenReturn(Arrays.asList(containmentRule, connectionRule)); tested = new CachedRuleManager(delegate); tested.init(); }
@Override public RuleViolations evaluate(final CanConnect rule, final ConnectionContext context) { final List<CanConnect.PermittedConnection> permittedConnections = rule.getPermittedConnections(); final String currentConnectorRole = context.getConnectorRole(); final Set<String> incomingLabels = context.getTargetRoles().orElse(Collections.emptySet()); final Set<String> outgoingLabels = context.getSourceRoles().orElse(Collections.emptySet()); final DefaultRuleViolations results = new DefaultRuleViolations(); final Set<Pair<String, String>> couples = new LinkedHashSet<>(); for (CanConnect.PermittedConnection pc : permittedConnections) { final boolean startMatch = outgoingLabels.contains(pc.getStartRole()); final boolean endMatch = startMatch && incomingLabels.contains(pc.getEndRole()); if (endMatch) { return results; } couples.add(new Pair<>(pc.getStartRole(), pc.getEndRole())); } results.addViolation(new ConnectionRuleViolation(currentConnectorRole, serializeAllowedConnections(couples))); return results; }
@Test public void testEvaluateConnectionContextOnce() { tested.evaluate(ruleSet, connectionContext); tested.evaluate(ruleSet, connectionContext); verify(ruleSet, times(1)).getRules(); verify(connectionHandler, times(2)).evaluate(eq(connectionRule), eq(connectionContext)); verify(containmentHandler, never()).evaluate(any(CanContain.class), any(ContainmentContext.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 public void testEvaluateSuccess2() { final Set<String> sourceRoles = new HashSet<String>(1) {{ add("r2"); }}; final Set<String> targetRoles = new HashSet<String>(1) {{ add("o2"); }}; when(context.getConnectorRole()).thenReturn(EDGE_ID); when(context.getSourceRoles()).thenReturn(Optional.of(sourceRoles)); when(context.getTargetRoles()).thenReturn(Optional.of(targetRoles)); final RuleViolations violations = tested.evaluate(RULE, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Test public void testEvaluateSuccess1() { final Set<String> sourceRoles = new HashSet<String>(1) {{ add("r1"); }}; final Set<String> targetRoles = new HashSet<String>(1) {{ add("o1"); }}; when(context.getConnectorRole()).thenReturn(EDGE_ID); when(context.getSourceRoles()).thenReturn(Optional.of(sourceRoles)); when(context.getTargetRoles()).thenReturn(Optional.of(targetRoles)); final RuleViolations violations = tested.evaluate(RULE, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Test public void testEvaluateFailed1() { final Set<String> sourceRoles = new HashSet<String>(1) {{ add("r1"); }}; final Set<String> targetRoles = new HashSet<String>(1) {{ add("o2"); }}; when(context.getConnectorRole()).thenReturn(EDGE_ID); when(context.getSourceRoles()).thenReturn(Optional.of(sourceRoles)); when(context.getTargetRoles()).thenReturn(Optional.of(targetRoles)); final RuleViolations violations = tested.evaluate(RULE, context); assertNotNull(violations); assertTrue(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Test public void testEvaluateFailed2() { final Set<String> sourceRoles = new HashSet<String>(1) {{ add("r2"); }}; final Set<String> targetRoles = new HashSet<String>(1) {{ add("o1"); }}; when(context.getConnectorRole()).thenReturn(EDGE_ID); when(context.getSourceRoles()).thenReturn(Optional.of(sourceRoles)); when(context.getTargetRoles()).thenReturn(Optional.of(targetRoles)); final RuleViolations violations = tested.evaluate(RULE, context); assertNotNull(violations); assertTrue(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); } }