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));
}
}
//Add another object with the same foreign key
UUID key2 = (UUID) om.insert("testtype", JsonUtil.rhombusMapFromJsonMap(TestHelpers.getTestObject(1), definition.getDefinitions().get("testtype")));
//Query by foreign key
Criteria criteria = TestHelpers.getTestCriteria(0);
long foreignKey = ((Integer) criteria.getIndexKeys().get("foreignid")).longValue();
criteria.getIndexKeys().put("foreignid", foreignKey);
List<Map<String, Object>> dbObjects = om.list("testtype", criteria);
assertEquals(2, dbObjects.size());
for (Map<String, Object> result : dbObjects) {
assertEquals(foreignKey, result.get("foreignid"));
}
//Remove one of the objects we added
om.delete("testtype", key);
//Re-query by foreign key
dbObjects = om.list("testtype", criteria);
assertEquals(1, dbObjects.size());
//Update the values of one of the objects
Map<String, Object> testObject2 = JsonUtil.rhombusMapFromJsonMap(
TestHelpers.getTestObject(2),
definition.getDefinitions().get("testtype"));
UUID key3 = om.update("testtype", key2, testObject2);
//Get the updated object back and make sure it matches
Map<String, Object> dbObject2 = om.getByKey("testtype", key3);
testObject2.put("id", key2);
for (String dbKey : dbObject2.keySet()) {
//Verify that everything is the same
assertEquals(testObject2.get(dbKey), dbObject2.get(dbKey));
}
//Get from the original index
dbObjects = om.list("testtype", criteria);
assertEquals(0, dbObjects.size());
//Get from the new index
Criteria criteria2 = TestHelpers.getTestCriteria(1);
criteria2.getIndexKeys().put("foreignid", ((Integer) criteria2.getIndexKeys().get("foreignid")).longValue());
dbObjects = om.list("testtype", criteria2);
assertEquals(1, dbObjects.size());
//an imediate request should return null, because we didnt wait for consistency
assertEquals(null, om.getNextUpdateIndexRow(null));
//Do another update
Map<String, Object> testObject3 = Maps.newHashMap();
testObject3.put("type", Integer.valueOf(7));
UUID key4 = om.update("testtype", key2, testObject3);
//now wait for consistency
Thread.sleep(3000);
//Test that we can retrieve the proper update rows
IndexUpdateRow row = om.getNextUpdateIndexRow(null);
assertEquals("testtype", row.getObjectName());
assertEquals(2, row.getIndexValues().size());
//most recent should be at the front of the list
assertEquals(7, row.getIndexValues().get(0).get("type"));
assertEquals(5, row.getIndexValues().get(1).get("type"));
assertEquals(778L, row.getIndexValues().get(0).get("foreignid"));
assertEquals(778L, row.getIndexValues().get(1).get("foreignid"));
assertEquals(333333L, row.getIndexValues().get(0).get("instance"));
assertEquals(333333L, row.getIndexValues().get(1).get("instance"));
//verify that if we try to get the next row it returns null
assertEquals(null, om.getNextUpdateIndexRow(row.getRowKey()));
//Teardown connections
cm.teardown();
}