@Test
  public void testUpdate() {

    ds.begin(ReadWrite.WRITE);
    try {
      Ontology ont = Ontology.loadOntology(ds, testOntologyName);

      JSONObject values = new JSONObject();

      OntologyInstance instance = ont.createInstance(simpleClassId, values);

      JSONObject rep = instance.getJSONRepresentation();

      assertTrue(rep.length() == 0);

      // Add DataProperty

      values.put(idChar + dataPropId, new JSONArray("[test]"));

      instance.update(values);

      rep = instance.getJSONRepresentation();

      assertTrue(rep.length() == 1);
      assertTrue(rep.toString().equals(values.toString()));

      ds.commit();
    } catch (JSONException e) {
      ds.abort();
      fail(e.getMessage());
    } finally {
      ds.end();
    }
  }
  @Test
  public void test_CreateSimpleWithObjectProp() {
    String classId = simpleClassId;

    ds.begin(ReadWrite.WRITE);
    try {
      Ontology ont = Ontology.loadOntology(ds, testOntologyName);
      JSONObject values = new JSONObject();

      String[] propValue = new String[1];
      propValue[0] = existingInstanceId;
      values.put(idChar + objectPropId, new JSONArray(propValue));

      OntologyInstance instance = ont.createInstance(classId, values);

      JSONObject rep = instance.getJSONRepresentation();

      assertTrue("JSON Rep: " + rep, rep.length() == values.length());
      assertTrue(
          "Instance does not have expected data property and value: " + rep,
          rep.has(idChar + objectPropId)
              && rep.optJSONArray(idChar + objectPropId).length() == 1
              && rep.optJSONArray(idChar + objectPropId).optString(0).equals(propValue[0]));
      ds.commit();
    } catch (JSONException e) {
      ds.abort();
      fail(e.getMessage());
    } finally {
      ds.end();
    }
  }
  @Test
  public void test_ReadAllInstances() {
    ds.begin(ReadWrite.READ);
    try {
      Ontology ont = Ontology.loadOntology(ds, testOntologyName);

      Map<String, OntologyInstance> instanceMap = ont.getClass(simpleClassId).getAllInstances();

      assertTrue(instanceMap.containsKey(existingInstanceId));
    } finally {
      ds.end();
    }
  }
  @Test
  public void testRead() {

    ds.begin(ReadWrite.READ);
    try {
      Ontology ont = Ontology.loadOntology(ds, testOntologyName);

      OntologyInstance instance = ont.getInstance(existingInstanceId);

      JSONObject rep = instance.getJSONRepresentation();

      assertTrue("", rep.length() == 0);
    } finally {
      ds.end();
    }
  }
  @Test
  public void test_CreateSimple() {
    String classId = simpleClassId;

    ds.begin(ReadWrite.WRITE);
    try {
      Ontology ont = Ontology.loadOntology(ds, testOntologyName);

      JSONObject values = new JSONObject();

      OntologyInstance instance = ont.createInstance(classId, values);

      JSONObject rep = instance.getJSONRepresentation();

      assertTrue(rep.toString(), rep.length() == values.length());

      ds.commit();
    } finally {
      ds.end();
    }
  }
  @Test
  public void testDelete() {
    ds.begin(ReadWrite.WRITE);
    try {
      Ontology ont = Ontology.loadOntology(ds, testOntologyName);
      OntologyInstance instance = ont.getInstance(deleteInstanceId);

      instance.delete();

      assertTrue("", instance.getJSONRepresentation().length() == 0);

      ds.commit();
    } finally {
      ds.end();
    }

    ds.begin(ReadWrite.READ);
    try {
      Ontology ont = Ontology.loadOntology(ds, testOntologyName);
      ont.getInstance(deleteInstanceId);

      fail("Should throw RuntimeException when getting deleted Instance");
    } catch (RuntimeException e) {
    } finally {
      ds.end();
    }
  }
  @BeforeClass
  public static void setUpBeforeClass() throws Exception {

    ds = Ontology.setTDBLocation(localDirectory);

    ds.begin(ReadWrite.WRITE);
    try {
      Ontology ont;

      try {
        ont = Ontology.createOntology(ds, testOntologyName);
      } catch (RuntimeException e) {
        ont = Ontology.loadOntology(ds, testOntologyName);
        ont = Ontology.createOntology(ds, testOntologyName);
      }

      JSONObject values = new JSONObject();

      ont.createDataProperty(dataPropId, values);
      ont.createObjectProperty(objectPropId, values);

      ont.createClass(simpleClassId, values);

      existingInstanceId = ont.createInstance(simpleClassId, values).getId();
      deleteInstanceId = ont.createInstance(simpleClassId, values).getId();

      ds.commit();
    } finally {
      ds.end();
    }

    ds.begin(ReadWrite.READ);
    try {
      Ontology ont = Ontology.loadOntology(ds, testOntologyName);

      assertTrue(ont.propertyExists(dataPropId));
      assertTrue(ont.propertyExists(objectPropId));

      assertTrue(ont.classExists(simpleClassId));
    } finally {
      ds.end();
    }
  }