}
@Test
public void testDelete() throws Exception {
//Build the connection manager
ConnectionManager cm = getConnectionManager();
//Build our keyspace definition object
CKeyspaceDefinition definition = JsonUtil.objectFromJsonResource(CKeyspaceDefinition.class, this.getClass().getClassLoader(), "CKeyspaceTestData.js");
assertNotNull(definition);
//Rebuild the keyspace and get the object mapper
cm.buildKeyspace(definition, true);
cm.setDefaultKeyspace(definition);
ObjectMapper om = cm.getObjectMapper(definition.getName());
//Get a test object to insert
Map<String, Object> testObject = JsonUtil.rhombusMapFromJsonMap(TestHelpers.getTestObject(0), definition.getDefinitions().get("testtype"));
UUID key = (UUID) om.insert("testtype", testObject);
//Query to get back the object from the database
Map<String, Object> dbObject = om.getByKey("testtype", key);
for (String dbKey : dbObject.keySet()) {
//Verify that everything but the key is the same
if (!dbKey.equals("id")) {
assertEquals(testObject.get(dbKey), dbObject.get(dbKey));
}
}
//Query by foreign key
Criteria criteria = TestHelpers.getTestCriteria(0);
long foreignKey = ((Long) criteria.getIndexKeys().get("foreignid")).longValue();
criteria.getIndexKeys().put("foreignid", foreignKey);
List<Map<String, Object>> dbObjects = om.list("testtype", criteria);
assertEquals(1, dbObjects.size());
for (Map<String, Object> result : dbObjects) {
assertEquals(foreignKey, result.get("foreignid"));
}
//Remove the object we added
om.delete("testtype", key);
//Query to get back the object from the database
assertEquals(null, om.getByKey("testtype", key));
assertEquals(0, om.list("testtype", criteria).size());
//now try inserting an object that has a null for one of the index values
testObject.put("foreignid", null);
UUID key3 = (UUID) om.insert("testtype", testObject);
//Query to get back the object from the database
dbObject = om.getByKey("testtype", key3);
for (String dbKey : dbObject.keySet()) {
//Verify that everything but the key is the same
if (!dbKey.equals("id")) {
assertEquals(testObject.get(dbKey), dbObject.get(dbKey));
}
}
//Remove the object we added
om.delete("testtype", key3);
//Query to get back the object from the database
assertEquals(null, om.getByKey("testtype", key3));
cm.teardown();
}