/**
* Test storing, querying and deleting triples
*/
@Test
public void testStoreTriples() throws Exception {
KiWiConnection connection = persistence.getConnection();
try {
KiWiUriResource stype = new KiWiUriResource(Namespaces.NS_XSD+"string");
KiWiUriResource subject = new KiWiUriResource("http://localhost/resource/"+RandomStringUtils.randomAlphanumeric(8));
KiWiUriResource pred_1 = new KiWiUriResource("http://localhost/predicate/P1");
KiWiUriResource pred_2 = new KiWiUriResource("http://localhost/predicate/P2");
KiWiUriResource object_1 = new KiWiUriResource("http://localhost/resource/"+RandomStringUtils.randomAlphanumeric(8));
KiWiStringLiteral object_2 = new KiWiStringLiteral(RandomStringUtils.randomAlphanumeric(32),null,stype);
KiWiUriResource context = new KiWiUriResource("http://localhost/context/"+RandomStringUtils.randomAlphanumeric(8));
connection.storeNode(stype, false);
connection.storeNode(subject, false);
connection.storeNode(pred_1, false);
connection.storeNode(pred_2, false);
connection.storeNode(object_1, false);
connection.storeNode(object_2, false);
connection.storeNode(context, false);
KiWiTriple triple1 = new KiWiTriple(subject,pred_1,object_1,context);
KiWiTriple triple2 = new KiWiTriple(subject,pred_2,object_2,context);
connection.storeTriple(triple1);
connection.storeTriple(triple2);
// check querying within transaction
List<Statement> result1 = connection.listTriples(subject,null,null,null,false, true).asList();
Assert.assertThat(result1,hasItems((Statement)triple1,(Statement)triple2));
Assert.assertEquals(2, connection.getSize());
Assert.assertEquals(2, connection.getSize(context));
Assert.assertEquals(0, connection.getSize(subject));
connection.commit();
List<Statement> result2 = connection.listTriples(subject,null,null,null,false, true).asList();
Assert.assertThat(result2,hasItems((Statement)triple1,(Statement)triple2));
Assert.assertEquals(2, connection.getSize());
Assert.assertEquals(2, connection.getSize(context));
Assert.assertEquals(0, connection.getSize(subject));
Assert.assertThat(Iterations.asList(connection.listContexts()), hasItem((KiWiResource)context));
// clear cache and test again
persistence.clearCache();
List<Statement> result3 = connection.listTriples(subject,null,null,null,false, true).asList();
Assert.assertThat(result3,hasItems((Statement)triple1,(Statement)triple2));
Assert.assertEquals(2, connection.getSize());
Assert.assertEquals(2, connection.getSize(context));
Assert.assertEquals(0, connection.getSize(subject));
// test database contents
PreparedStatement stmt = connection.getJDBCConnection().prepareStatement("SELECT * FROM triples WHERE deleted = false ORDER BY subject, predicate");
ResultSet dbResult1 = stmt.executeQuery();
Assert.assertTrue(dbResult1.next());
Assert.assertEquals((long) triple1.getId(), dbResult1.getLong("id"));
Assert.assertEquals((long) triple1.getSubject().getId(), dbResult1.getLong("subject"));
Assert.assertEquals((long) triple1.getPredicate().getId(), dbResult1.getLong("predicate"));
Assert.assertEquals((long)triple1.getObject().getId(),dbResult1.getLong("object"));
Assert.assertTrue(dbResult1.next());
Assert.assertEquals((long)triple2.getId(),dbResult1.getLong("id"));
Assert.assertEquals((long)triple2.getSubject().getId(),dbResult1.getLong("subject"));
Assert.assertEquals((long)triple2.getPredicate().getId(),dbResult1.getLong("predicate"));
Assert.assertEquals((long)triple2.getObject().getId(),dbResult1.getLong("object"));
dbResult1.close();
connection.commit();
} finally {
connection.close();
}
}