@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; }
@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 @SuppressWarnings("unchecked") public void testAcceptsNoOp() { when(context.getCandidate()).thenReturn(Optional.empty()); assertTrue(tested.accepts(RULE_NO_LIMIT, context)); }
@Test @SuppressWarnings("unchecked") public void testAccepts() { when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); when(context.getCandidate()).thenReturn(Optional.of(candidate)); assertTrue(tested.accepts(RULE_NO_LIMIT, context)); when(context.getCandidate()).thenReturn(Optional.of(parent)); assertFalse(tested.accepts(RULE_NO_LIMIT, context)); }
public static void verifyCardinality(final ElementCardinalityContext context, final Graph graph) { assertNotNull(context); final Graph graph1 = context.getGraph(); final Optional<Element<? extends View<?>>> candidate1 = context.getCandidate(); final Optional<CardinalityContext.Operation> operation1 = context.getOperation(); assertNotNull(graph1); assertNotNull(operation1); assertEquals(graph, graph1); }
@Test @SuppressWarnings("unchecked") public void testMax0Failed() { final Map<String, Integer> count = new HashMap<String, Integer>(1) {{ put(CANDIDATE_ROLE1, 0); }}; doReturn(count).when(tested).countLabels(any(Graph.class), anySet()); when(context.getCandidate()).thenReturn(Optional.of(candidate)); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); final RuleViolations violations = tested.evaluate(RULE_MAX_0, context); assertNotNull(violations); assertTrue(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
public static void verifyCardinality(final ElementCardinalityContext context, final Graph graph, final Element<? extends View<?>> candidate, final CardinalityContext.Operation operation) { assertNotNull(context); final Graph graph1 = context.getGraph(); final Optional<Element<? extends View<?>>> candidate1 = context.getCandidate(); final Optional<CardinalityContext.Operation> operation1 = context.getOperation(); assertNotNull(graph1); assertNotNull(operation1); assertEquals(graph, graph1); assertEquals(candidate, candidate1.orElse(null)); assertEquals(operation, operation1.orElse(null)); }
@Test @SuppressWarnings("unchecked") public void testMax0EvaluateSuccess() { final Map<String, Integer> count = new HashMap<String, Integer>(1) {{ put(CANDIDATE_ROLE1, 0); }}; doReturn(count).when(tested).countLabels(any(Graph.class), anySet()); when(context.getCandidate()).thenReturn(Optional.empty()); when(context.getOperation()).thenReturn(Optional.empty()); final RuleViolations violations = tested.evaluate(RULE_MAX_0, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Test @SuppressWarnings("unchecked") public void testMax0EvaluateFailed() { final Map<String, Integer> count = new HashMap<String, Integer>(1) {{ put(CANDIDATE_ROLE1, 1); }}; doReturn(count).when(tested).countLabels(any(Graph.class), anySet()); when(context.getCandidate()).thenReturn(Optional.empty()); when(context.getOperation()).thenReturn(Optional.empty()); final RuleViolations violations = tested.evaluate(RULE_MAX_0, context); assertNotNull(violations); assertTrue(violations.violations(RuleViolation.Type.WARNING).iterator().hasNext()); } }
@Test @SuppressWarnings("unchecked") public void testMin1EvaluateFailed() { final Map<String, Integer> count = new HashMap<String, Integer>(1) {{ put(CANDIDATE_ROLE1, 0); }}; doReturn(count).when(tested).countLabels(any(Graph.class), anySet()); when(context.getCandidate()).thenReturn(Optional.empty()); when(context.getOperation()).thenReturn(Optional.empty()); final RuleViolations violations = tested.evaluate(RULE_MIN_1, context); assertNotNull(violations); assertTrue(violations.violations(RuleViolation.Type.WARNING).iterator().hasNext()); }
@Test @SuppressWarnings("unchecked") public void testMin1AddOk() { final Map<String, Integer> count = new HashMap<String, Integer>(1) {{ put(CANDIDATE_ROLE1, 0); }}; doReturn(count).when(tested).countLabels(any(Graph.class), anySet()); when(context.getCandidate()).thenReturn(Optional.of(candidate)); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); final RuleViolations violations = tested.evaluate(RULE_MIN_1, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Test @SuppressWarnings("unchecked") public void testMin1DeleteOk() { final Map<String, Integer> count = new HashMap<String, Integer>(1) {{ put(CANDIDATE_ROLE1, 2); }}; doReturn(count).when(tested).countLabels(any(Graph.class), anySet()); when(context.getCandidate()).thenReturn(Optional.of(candidate)); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.DELETE)); final RuleViolations violations = tested.evaluate(RULE_MIN_1, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Test @SuppressWarnings("unchecked") public void testEvaluateSuccess() { final Map<String, Integer> count = new HashMap<String, Integer>(2) {{ put(CANDIDATE_ROLE1, 0); put(CANDIDATE_ROLE2, 0); }}; doReturn(count).when(tested).countLabels(any(Graph.class), anySet()); when(context.getCandidate()).thenReturn(Optional.of(candidate)); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); final RuleViolations violations = tested.evaluate(RULE_NO_LIMIT, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Test @SuppressWarnings("unchecked") public void testMax1Failed() { final Map<String, Integer> count = new HashMap<String, Integer>(2) {{ put(CANDIDATE_ROLE1, 1); put(CANDIDATE_ROLE2, 0); }}; doReturn(count).when(tested).countLabels(any(Graph.class), anySet()); when(context.getCandidate()).thenReturn(Optional.of(candidate)); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); final RuleViolations violations = tested.evaluate(RULE_MAX_1, context); assertNotNull(violations); assertTrue(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Test @SuppressWarnings("unchecked") public void testMin1DeleteFailed() { final Map<String, Integer> count = new HashMap<String, Integer>(2) {{ put(CANDIDATE_ROLE1, 1); put(CANDIDATE_ROLE2, 0); }}; doReturn(count).when(tested).countLabels(any(Graph.class), anySet()); when(context.getCandidate()).thenReturn(Optional.of(candidate)); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.DELETE)); final RuleViolations violations = tested.evaluate(RULE_MIN_1, context); assertNotNull(violations); assertTrue(violations.violations(RuleViolation.Type.WARNING).iterator().hasNext()); }
@Test @SuppressWarnings("unchecked") public void testMax1Ok() { final Map<String, Integer> count = new HashMap<String, Integer>(2) {{ put(CANDIDATE_ROLE1, 0); put(CANDIDATE_ROLE2, 0); }}; doReturn(count).when(tested).countLabels(any(Graph.class), anySet()); when(context.getCandidate()).thenReturn(Optional.of(candidate)); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); final RuleViolations violations = tested.evaluate(RULE_MAX_1, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }
@Test @SuppressWarnings("unchecked") public void testEvaluateSuccessAgain() { final Map<String, Integer> count = new HashMap<String, Integer>(2) {{ put(CANDIDATE_ROLE1, 100); put(CANDIDATE_ROLE2, 0); }}; doReturn(count).when(tested).countLabels(any(Graph.class), anySet()); when(context.getCandidate()).thenReturn(Optional.of(candidate)); when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD)); final RuleViolations violations = tested.evaluate(RULE_NO_LIMIT, context); assertNotNull(violations); assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext()); }