@SuppressWarnings("unchecked")
@Test
public void testUpdate() throws Exception {
DataAccessObject<WeatherReport, Integer> dao = (DataAccessObject<WeatherReport, Integer>) getDao(getGeneratedClass());
WeatherReport original = getPersistentInstance(dao);
WeatherReport updated = dao.get(original.getReportId());
GeneratedInstanceInterceptor.setProperty(updated, "latitude", new Double(30));
GeneratedInstanceInterceptor.setProperty(updated, "longitude", new Double(30));
/* TODO this fails because we currently don't support one-to-many relationships. We need to reenable the
deleteOrphanItems in our BaseDataAccessObject to support this
*/
/*
// Test collection item updates
List<WeatherEvent> weatherEvents = new ArrayList<WeatherEvent>(original.getWeatherEvents());
// Delete the first
weatherEvents.remove(0);
// Update the second
weatherEvents.get(0).setName("foobar");
// Add a third
weatherEvents.add(new GenerateInstance<WeatherEvent>(WeatherEvent.class).generate());
GeneratedInstanceInterceptor.setProperty(updated, "weatherEvents", weatherEvents);
*/
dao.save(updated);
//final WeatherReport persisted = dao.get(updated.getReportId());
assertFalse(updated.getLatitude().equals(original.getLatitude()));
// Check the updated collection
// size should be equal
//assertEquals(original.getWeatherEvents().size(), persisted.getWeatherEvents().size());
// first item should be removed
//assertFalse(Filter.grepItemAgainstList(Atom.getFirst(original.getWeatherEvents()), persisted.getWeatherEvents()));
// should be an updated item named foobar
/* assertTrue(Filter.grepItemAgainstList("foobar",
Transform.map(new Unary<WeatherEvent,String>() {
public String f(WeatherEvent weatherEvent) {
return weatherEvent.getName();
}}, persisted.getWeatherEvents()))); */
// new item should exist
//assertTrue(Filter.grepItemAgainstList(Atom.getLast(weatherEvents), persisted.getWeatherEvents()));
String newPrimaryIndexKey = findPrimaryKeyOnDifferentNode(original);
GeneratedInstanceInterceptor.setProperty(updated, "continent", new Integer(newPrimaryIndexKey).toString());
Assert.assertEquals(getHive().directory().getNodeIdsOfResourceId("WeatherReport", original.getReportId()).size(), 1);
dao.save(updated);
final Collection<Integer> nodeIdsOfResourceId = getHive().directory().getNodeIdsOfResourceId("WeatherReport", updated.getReportId());
// Make sure the resource is only on 1 hive node
Assert.assertEquals(nodeIdsOfResourceId.size(), 1);
// Make sure the node matches the new node
Assert.assertEquals(Atom.getFirstOrThrow(nodeIdsOfResourceId), Atom.getFirstOrThrow(getHive().directory().getNodeIdsOfPrimaryIndexKey(newPrimaryIndexKey)));
// Make sure the entity can be fetched on the new node
Assert.assertNotNull(dao.get(updated.getReportId()));
}