/* (non-Javadoc)
   * @see org.fcrepo.kernel.api.models.FedoraResource#updateProperties
   *     (org.fcrepo.kernel.api.identifiers.IdentifierConverter, java.lang.String, RdfStream)
   */
  @Override
  public void updateProperties(
      final IdentifierConverter<Resource, FedoraResource> idTranslator,
      final String sparqlUpdateStatement,
      final RdfStream originalTriples)
      throws MalformedRdfException, AccessDeniedException {

    if (!clean(sparqlUpdateStatement)) {
      throw new IllegalArgumentException(
          "Invalid SPARQL UPDATE statement:" + sparqlUpdateStatement);
    }

    final Model model = originalTriples.asModel();

    final JcrPropertyStatementListener listener =
        new JcrPropertyStatementListener(
            idTranslator, getSession(), idTranslator.reverse().convert(this).asNode());

    model.register(listener);

    final UpdateRequest request =
        create(sparqlUpdateStatement, idTranslator.reverse().convert(this).toString());
    model.setNsPrefixes(request.getPrefixMapping());
    execute(request, model);

    listener.assertNoExceptions();
  }
  @Test
  public void testChildren() throws RepositoryException {
    when(mockRes1.getPath()).thenReturn(RDF_PATH + "/res1");
    when(mockRes2.getPath()).thenReturn(RDF_PATH + "/res2");
    when(mockRes3.getPath()).thenReturn(RDF_PATH + "/res3");
    when(mockResourceNode.hasNodes()).thenReturn(true);
    final Stream<FedoraResource> first = of(mockRes1, mockRes2, mockRes3);
    final Stream<FedoraResource> second = of(mockRes1, mockRes2, mockRes3);
    when(mockResource.getChildren()).thenReturn(first).thenReturn(second);

    try (final ChildrenRdfContext context = new ChildrenRdfContext(mockResource, idTranslator)) {
      final Model results = context.collect(toModel());
      final Resource subject = idTranslator.reverse().convert(mockResource);

      final StmtIterator stmts =
          results.listStatements(subject, RdfLexicon.HAS_CHILD_COUNT, (RDFNode) null);

      assertTrue("There should have been a statement!", stmts.hasNext());
      final Statement stmt = stmts.nextStatement();
      assertTrue("Object should be a literal! " + stmt.getObject(), stmt.getObject().isLiteral());
      assertEquals(3, stmt.getInt());

      assertFalse("There should not have been a second statement!", stmts.hasNext());
    }
  }
  /* (non-Javadoc)
   * @see org.fcrepo.kernel.api.models.FedoraResource#replaceProperties
   *     (org.fcrepo.kernel.api.identifiers.IdentifierConverter, com.hp.hpl.jena.rdf.model.Model)
   */
  @Override
  public void replaceProperties(
      final IdentifierConverter<Resource, FedoraResource> idTranslator,
      final Model inputModel,
      final RdfStream originalTriples)
      throws MalformedRdfException {

    final RdfStream replacementStream =
        new RdfStream()
            .namespaces(inputModel.getNsPrefixMap())
            .topic(idTranslator.reverse().convert(this).asNode());

    final GraphDifferencingIterator differencer =
        new GraphDifferencingIterator(inputModel, originalTriples);

    final StringBuilder exceptions = new StringBuilder();
    try {
      new RdfRemover(idTranslator, getSession(), replacementStream.withThisContext(differencer))
          .consume();
    } catch (final ConstraintViolationException e) {
      throw e;
    } catch (final MalformedRdfException e) {
      exceptions.append(e.getMessage());
      exceptions.append("\n");
    }

    try {
      new RdfAdder(
              idTranslator,
              getSession(),
              replacementStream.withThisContext(differencer.notCommon()))
          .consume();
    } catch (final ConstraintViolationException e) {
      throw e;
    } catch (final MalformedRdfException e) {
      exceptions.append(e.getMessage());
    }

    if (exceptions.length() > 0) {
      throw new MalformedRdfException(exceptions.toString());
    }
  }
  @Test
  public void testNoChildren() throws RepositoryException {
    when(mockResourceNode.hasNodes()).thenReturn(false);

    try (final ChildrenRdfContext childrenRdfContext =
        new ChildrenRdfContext(mockResource, idTranslator)) {
      final Model results = childrenRdfContext.collect(toModel());
      final Resource subject = idTranslator.reverse().convert(mockResource);

      final StmtIterator stmts =
          results.listStatements(subject, RdfLexicon.HAS_CHILD_COUNT, (RDFNode) null);

      assertTrue("There should have been a statement!", stmts.hasNext());
      final Statement stmt = stmts.nextStatement();
      assertTrue("Object should be a literal! " + stmt.getObject(), stmt.getObject().isLiteral());
      assertEquals(0, stmt.getInt());

      assertFalse("There should not have been a second statement!", stmts.hasNext());
    }
  }