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