/**
   * Test method for {@link
   * RDFJSONUtility.kmr.scam.rest.util.RDFJSON#graphToRdfJsonPreordered(java.util.Set,
   * java.io.Writer)} .
   *
   * @throws JSONException
   */
  @Test
  public void testModelToRdfJsonPreorderedSetOfStatementWriter() throws Exception {
    // final Set<Statement> testStatements = new TreeSet<Statement>(new StatementComparator());
    final Model testStatements = new TreeModel();
    final ValueFactoryImpl vf = ValueFactoryImpl.getInstance();

    final BNode testBNode1 = vf.createBNode();

    final BNode testBNode2 = vf.createBNode();

    final URI testURI1 = vf.createURI("http://example.org/test/rdf/json/1");

    final URI testURI2 = vf.createURI("http://my.test.org/rdf/type/2");

    final URI testURI3 = vf.createURI("http://example.org/test/rdf/json/3");

    final URI testURI4 = vf.createURI("http://example.org/test/rdf/json/4");

    final URI testURI5 = vf.createURI("http://my.test.org/rdf/type/5");

    final Statement testStatement1 = vf.createStatement(testURI1, testURI2, testURI3);
    testStatements.add(testStatement1);
    final Statement testStatement2 = vf.createStatement(testURI1, testURI2, testBNode1);
    testStatements.add(testStatement2);
    final Statement testStatement3 = vf.createStatement(testURI1, testURI2, testBNode2);
    testStatements.add(testStatement3);

    final Statement testStatement4 = vf.createStatement(testURI4, testURI2, testURI3);
    testStatements.add(testStatement4);
    final Statement testStatement5 = vf.createStatement(testURI4, testURI2, testBNode2);
    testStatements.add(testStatement5);
    final Statement testStatement6 = vf.createStatement(testURI4, testURI2, testBNode1);
    testStatements.add(testStatement6);

    final Statement testStatement7 = vf.createStatement(testBNode1, testURI5, testBNode2);
    testStatements.add(testStatement7);
    final Statement testStatement8 = vf.createStatement(testBNode1, testURI5, testURI1);
    testStatements.add(testStatement8);
    final Statement testStatement9 = vf.createStatement(testBNode1, testURI5, testURI4);
    testStatements.add(testStatement9);

    // RDFJSONUnitTest.log.info("testStatements=" + testStatements);

    Assert.assertEquals(9, testStatements.size());

    // Verify that the statements are in an acceptable order (testStatement5 and testStatement6
    // can be legitimately swapped)

    final Iterator<Statement> testStatementIterator = testStatements.iterator();

    Assert.assertTrue(testStatementIterator.hasNext());

    // testStatement7 should always be first by virtue of the fact that it has two blank nodes
    // and no other statements have two blank nodes
    Assert.assertEquals(testStatement7, testStatementIterator.next());
    Assert.assertTrue(testStatementIterator.hasNext());

    // Then testStatement8
    Assert.assertEquals(testStatement8, testStatementIterator.next());
    Assert.assertTrue(testStatementIterator.hasNext());

    // Then testStatement9
    Assert.assertEquals(testStatement9, testStatementIterator.next());
    Assert.assertTrue(testStatementIterator.hasNext());

    Rio.write(testStatements, this.testWriter, RDFFormat.RDFJSON);
    // RDFJSONUtility.modelToRdfJson(testStatements, this.testWriter, this.testWriterConfig);

    this.testOutput = this.testWriter.toString();

    Assert.assertTrue(this.testOutput.length() > 0);

    Assert.assertTrue(this.testOutput.startsWith("{"));

    Assert.assertTrue(this.testOutput.endsWith("}"));

    // RDFJSONUnitTest.log.info("testOutput=" + this.testOutput);

    final int firstBlankNode = this.testOutput.indexOf("\"_:");

    // Test that a bnode exists after the opening brace
    Assert.assertTrue(firstBlankNode > 0);

    // The first value after the first blank node should be a blank node identifier
    final int firstValue = this.testOutput.indexOf("\"value\" : \"_:", firstBlankNode);

    Assert.assertTrue("A suitable blank node value was not found", firstValue > 0);

    // This should be guaranteed by the indexOf contract, but doing a quick check anyway
    Assert.assertTrue(firstValue > firstBlankNode);

    // Do a quick check to see if the testOutput is valid JSON

    // FIXME: TODO: Test using Jackson
    // Assert.fail("TODO: Implement me using Jackson");
    // final JSONObject testJSONObject = new JSONObject(this.testOutput);

    // Assert.assertNotNull(testJSONObject);
    // Assert.assertTrue(testJSONObject.length() > 0);
    // Assert.assertTrue(testJSONObject.names().length() > 0);
    // Assert.assertTrue(testJSONObject.keys().hasNext());
  }
  @Test
  public void testPostRdfBasic() throws Exception {
    // prepare: add an artifact
    final InferredOWLOntologyID testArtifact =
        this.loadTestArtifact(
            TestConstants.TEST_ARTIFACT_20130206, MediaType.APPLICATION_RDF_TURTLE);

    final ClientResource searchClientResource =
        new ClientResource(this.getUrl(PoddWebConstants.PATH_SEARCH));
    try {
      searchClientResource.addQueryParameter(
          PoddWebConstants.KEY_ARTIFACT_IDENTIFIER, testArtifact.getOntologyIRI().toString());

      // prepare: the test input
      final String[] objectUris = {
        "http://purl.org/podd/basic-1-20130206/object:2966",
        "http://purl.org/podd/basic-2-20130206/artifact:1#Demo-Genotype",
        "http://purl.org/podd/basic-2-20130206/artifact:1#SqueekeeMaterial",
        "http://purl.org/podd/ns/poddScience#WildType_NotApplicable",
        "http://purl.org/podd/ns/poddPlant#DeltaTporometer-63",
        "http://purl.org/podd/ns/poddBase#DisplayType_LongText"
      };

      final String[] expectedLabels = {
        "Project#2012-0006_ Cotton Leaf Morphology",
        "Demo genotype",
        "Squeekee material",
        "Not Applicable",
        "Delta-T porometer",
        null
      };

      final Model testModel = new LinkedHashModel();
      for (final String s : objectUris) {
        testModel.add(PODD.VF.createURI(s), RDFS.LABEL, PODD.VF.createLiteral("?blank"));
      }

      final RDFFormat inputFormat = RDFFormat.RDFXML;
      final MediaType inputMediaType = MediaType.valueOf(inputFormat.getDefaultMIMEType());

      // build input representation
      final ByteArrayOutputStream output = new ByteArrayOutputStream(8096);
      Rio.write(testModel, output, inputFormat);
      final Representation input = new StringRepresentation(output.toString(), inputMediaType);

      // invoke service
      final Representation results =
          this.doTestAuthenticatedRequest(
              searchClientResource,
              Method.POST,
              input,
              inputMediaType,
              Status.SUCCESS_OK,
              AbstractResourceImplTest.WITH_ADMIN);

      // verify: response
      final Model resultModel = this.assertRdf(results, RDFFormat.RDFXML, 5);

      // verify: each URI has the expected label
      for (int i = 0; i < objectUris.length; i++) {
        final String objectString =
            resultModel.filter(PODD.VF.createURI(objectUris[i]), RDFS.LABEL, null).objectString();
        Assert.assertEquals("Not the expected label", expectedLabels[i], objectString);
      }
    } finally {
      this.releaseClient(searchClientResource);
    }
  }