### Question:
AddChildNodeCommand extends AbstractGraphCompositeCommand { @Override @SuppressWarnings("unchecked") public CommandResult<RuleViolation> allow(final GraphCommandExecutionContext context) { ensureInitialized(context); final Element<? extends Definition<?>> parent = (Element<? extends Definition<?>>) getParent(context); final Collection<RuleViolation> containmentRuleViolations = evaluate(context, contextBuilder -> contextBuilder.containment(parent, candidate)); final Collection<RuleViolation> cardinalityRuleViolations = evaluate(context, contextBuilder -> contextBuilder.cardinality(Collections.singleton(candidate), CardinalityContext.Operation.ADD)); final Collection<RuleViolation> violations = new LinkedList<RuleViolation>(); violations.addAll(containmentRuleViolations); violations.addAll(cardinalityRuleViolations); return new GraphCommandResultBuilder(violations).build(); } AddChildNodeCommand(final @MapsTo("parentUUID") String parentUUID,
                               final @MapsTo("candidate") Node candidate,
                               final @MapsTo("location") Point2D location);  AddChildNodeCommand(final Node<?, Edge> parent,
                               final Node candidate,
                               final Point2D location);  AddChildNodeCommand(final Node<?, Edge> parent,
                               final Node candidate); @Override @SuppressWarnings("unchecked") CommandResult<RuleViolation> allow(final GraphCommandExecutionContext context); @Override @SuppressWarnings("unchecked") CommandResult<RuleViolation> undo(final GraphCommandExecutionContext context); Node<?, Edge> getParent(); Node getCandidate(); Point2D getLocation(); @Override String toString();  }### Answer:
@Test @SuppressWarnings("unchecked") public void testAllow() { CommandResult<RuleViolation> result = tested.allow(graphCommandExecutionContext); assertEquals(CommandResult.Type.INFO, result.getType()); List<Command<GraphCommandExecutionContext, RuleViolation>> commands = tested.getCommands(); assertNotNull(commands); assertEquals(3, commands.size()); final ArgumentCaptor<RuleEvaluationContext> contextCaptor = ArgumentCaptor.forClass(RuleEvaluationContext.class); verify(ruleManager, times(2)).evaluate(eq(ruleSet), contextCaptor.capture()); final List<RuleEvaluationContext> contexts = contextCaptor.getAllValues(); assertEquals(2, contexts.size()); verifyCardinality((ElementCardinalityContext) contexts.get(1), graph, candidate, CardinalityContext.Operation.ADD); verifyContainment((NodeContainmentContext) contexts.get(0), parent, candidate); }
@Test @SuppressWarnings("unchecked") public void testAllowNoRules() { useAllowedExecutionContext(); CommandResult<RuleViolation> result = tested.allow(graphCommandExecutionContext); assertEquals(CommandResult.Type.INFO, result.getType()); verify(ruleManager, times(0)).evaluate(eq(ruleSet), any(RuleEvaluationContext.class)); }
@Test @SuppressWarnings("unchecked") public void testNotAllowed() { final RuleViolations FAILED_VIOLATIONS = new DefaultRuleViolations() .addViolation(new ContainmentRuleViolation(graph.getUUID(), PARENT_UUID)); when(ruleManager.evaluate(any(RuleSet.class), any(RuleEvaluationContext.class))).thenReturn(FAILED_VIOLATIONS); CommandResult<RuleViolation> result = tested.allow(graphCommandExecutionContext); assertEquals(CommandResult.Type.ERROR, result.getType()); }