@Override @SuppressWarnings("unchecked") public boolean accepts(final Occurrences rule, final ElementCardinalityContext context) { if (context.getCandidate().isPresent()) { final Set<String> candidateLabels = evalUtils.getLabels(context.getCandidate().get()); return cardinalityEvaluationHandler .accepts(rule, RuleContextBuilder.DomainContexts.cardinality( candidateLabels, -1, context.getOperation())); } return true; }
@Before public void setup() throws Exception { when(context.getRoles()).thenReturn(ROLES); tested = new CardinalityEvaluationHandler(); }
@Test public void testMaxOneSuccess() { when(context.getCandidateCount()).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 testMinOneFailed1() { when(context.getCandidateCount()).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 testMinOneSuccess() { when(context.getCandidateCount()).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 testAccepts() { Set<String> roles = Collections.singleton(ROLE); when(context.getRoles()).thenReturn(roles); assertTrue(tested.accepts(RULE_NO_LIMIT, context)); roles = Collections.singleton("anotherTheRole"); when(context.getRoles()).thenReturn(roles); assertFalse(tested.accepts(RULE_NO_LIMIT, context)); }
@Test public void testMaxOneFailed1() { when(context.getCandidateCount()).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()); } }
@Test public void testEvaluateNoLimit() { when(context.getCandidateCount()).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.getCandidateCount()).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()); }
@Override @SuppressWarnings("unchecked") public RuleViolations evaluate(final Occurrences rule, final ElementCardinalityContext context) { final DefaultRuleViolations results = new DefaultRuleViolations(); final Optional<Element<? extends View<?>>> candidate = context.getCandidate(); final String role = rule.getRole(); final Set<String> roles = Collections.singleton(role); final Map<String, Integer> graphLabelCount = countLabels(context.getGraph(), roles); // Ensure processing the role even if not used along the graph, so // cardinality min rules can be evaluated. final int count = graphLabelCount.isEmpty() ? 0 : graphLabelCount.get(role); final Optional<CardinalityContext.Operation> operation = context.getOperation(); results.addViolations( cardinalityEvaluationHandler .evaluate(rule, RuleContextBuilder.DomainContexts.cardinality(roles, count, operation)) ); if (candidate.isPresent()) { return GraphEvaluationHandlerUtils.addViolationsSourceUUID(candidate.get().getUUID(), results); } return results; }