### Question:
ConnectionEvaluationHandler implements RuleEvaluationHandler<CanConnect, ConnectionContext> { @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; }  @Override Class<CanConnect> getRuleType(); @Override Class<ConnectionContext> getContextType(); @Override boolean accepts(final CanConnect rule,
                           final ConnectionContext context); @Override RuleViolations evaluate(final CanConnect rule,
                                   final ConnectionContext context);  }### Answer:
@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 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 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()); }