### Question:
CardinalityEvaluationHandler implements RuleEvaluationHandler<Occurrences, CardinalityContext> { @Override public RuleViolations evaluate(final Occurrences rule, final CardinalityContext context) { final DefaultRuleViolations results = new DefaultRuleViolations(); final int minOccurrences = rule.getMinOccurrences(); final int maxOccurrences = rule.getMaxOccurrences(); final int currentCount = context.getCurrentCount(); final int candidateCount = context.getCandidateCount(); final Optional<CardinalityContext.Operation> operation = context.getOperation(); final Violation.Type type = operation .filter(CardinalityContext.Operation.ADD::equals) .isPresent() ? Violation.Type.ERROR : Violation.Type.WARNING; final int count = !operation.isPresent() ? currentCount : (operation.get().equals(CardinalityContext.Operation.ADD) ? currentCount + candidateCount : currentCount - candidateCount); if (count < minOccurrences) { results.addViolation(new CardinalityMinRuleViolation(context.getRoles().toString(), minOccurrences, currentCount, type)); } else if (maxOccurrences > -1 && count > maxOccurrences) { results.addViolation(new CardinalityMaxRuleViolation(context.getRoles().toString(), maxOccurrences, currentCount, type)); } return results; }  @Override Class<Occurrences> getRuleType(); @Override Class<CardinalityContext> getContextType(); @Override boolean accepts(final Occurrences rule,
                           final CardinalityContext context); @Override RuleViolations evaluate(final Occurrences rule,
                                   final CardinalityContext context);  }### Answer:
@Test public void testEvaluateNoLimit() { when(context.getCurrentCount()).thenReturn(0); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); RuleViolations violations = tested.evaluate(RULE_NO_LIMIT, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); when(context.getCurrentCount()).thenReturn(100); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); violations = tested.evaluate(RULE_NO_LIMIT, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Test public void testMinOneSuccess() { when(context.getCurrentCount()).thenReturn(0); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); RuleViolations violations = tested.evaluate(RULE_MIN_1, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Test public void testMinOneFailed1() { when(context.getCurrentCount()).thenReturn(1); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.DELETE)); RuleViolations violations = tested.evaluate(RULE_MIN_1, context); assertNotNull(violations); assertTrue(violations.violations(RuleViolation.Type.WARNING).iterator().hasNext()); }
@Test public void testMaxOneSuccess() { when(context.getCurrentCount()).thenReturn(0); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); RuleViolations violations = tested.evaluate(RULE_MAX_1, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Test public void testMaxOneFailed1() { when(context.getCurrentCount()).thenReturn(1); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); RuleViolations violations = tested.evaluate(RULE_MAX_1, context); assertNotNull(violations); assertTrue(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }