logger.info("Run test");
//
// allocate and begin persistence manager transaction
//
IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
persistenceManager.begin();
//
// test findAll method for IWidget
//
IWidget [] allWidgets = persistenceManager.findAll(IWidget.class);
assertNotNull(allWidgets);
assertEquals(1, allWidgets.length);
//
// test findById for IWidget
//
Object widgetId = allWidgets[0].getId();
IWidget widgetById = persistenceManager.findById(IWidget.class, widgetId);
assertNotNull(widgetById);
assertEquals(allWidgets[0], widgetById);
//
// test findByValue method for IWidget
//
String widgetGuid = allWidgets[0].getIdentifier();
IWidget [] widgetsByValue = persistenceManager.findByValue(IWidget.class, "guid", widgetGuid);
assertNotNull(widgetsByValue);
assertEquals(1, widgetsByValue.length);
assertEquals(allWidgets[0], widgetsByValue[0]);
//
// test findByValues methods for IWidget
//
Map<String,Object> values = new HashMap<String,Object>();
values.put("height", allWidgets[0].getHeight());
values.put("width", allWidgets[0].getWidth());
//
// removed for now as this is a deprecated method, and can't be called
// at this point due to transaction boundaries.
//
//values.put("widgetAuthor", allWidgets[0].getWidgetAuthor());
IWidget [] widgetsByValues = persistenceManager.findByValues(IWidget.class, values);
assertNotNull(widgetsByValues);
assertEquals(1, widgetsByValues.length);
assertEquals(allWidgets[0], widgetsByValues[0]);
//
// test custom widget query methods
//
IWidget widgetByGuid = persistenceManager.findWidgetByGuid(widgetGuid);
assertNotNull(widgetByGuid);
assertEquals(allWidgets[0], widgetByGuid);
//
// rollback and close persistence manager transaction
//
persistenceManager.rollback();
PersistenceManagerFactory.closePersistenceManager();
//
// allocate and begin persistence manager transaction
//
persistenceManager = PersistenceManagerFactory.getPersistenceManager();
persistenceManager.begin();
//
// Get the first existing API key
// and first existing Widget to use as the basis of a
// new Widget Instance
//
String apiKey = "TEST";
IWidget [] widgets = persistenceManager.findAll(IWidget.class);
IWidget widget = widgets[0];
widgetGuid = widget.getIdentifier();
//
// check that the Widget Instance does not yet exist
//
IWidgetInstance widgetInstance = persistenceManager.findWidgetInstanceByGuid(apiKey, "test", "test-shared-data-key", widgetGuid);
assertNull(widgetInstance);
//
// Create the Widget Instance
//
widgetInstance = persistenceManager.newInstance(IWidgetInstance.class);
//
// Set some properties, including preferences
//
widgetInstance.setApiKey(apiKey);
widgetInstance.setWidget(widget);
widgetInstance.setIdKey("test");
widgetInstance.setLang("en");
widgetInstance.setNonce("nonce-test");
widgetInstance.setOpensocialToken("");
widgetInstance.setSharedDataKey("test-shared-data-key");
widgetInstance.setShown(true);
widgetInstance.setUserId("test");
IPreference widgetInstancePreference = persistenceManager.newInstance(IPreference.class);
widgetInstancePreference.setDkey("sharedDataKey");
widgetInstancePreference.setDvalue("test-shared-data-key");
widgetInstancePreference.setReadOnly(true);
widgetInstance.getPreferences().add(widgetInstancePreference);
//
// Save the widget instance
//
persistenceManager.save(widgetInstance);
//
// create a participant
//
IParticipant participant = persistenceManager.newInstance(IParticipant.class);
//participant.setWidget(widget);
participant.setSharedDataKey("test-shared-data-key");
participant.setParticipantId("test");
participant.setParticipantDisplayName("");
participant.setParticipantThumbnailUrl("");
persistenceManager.save(participant);
//
// commit and close persistence manager transaction
//
persistenceManager.commit();
PersistenceManagerFactory.closePersistenceManager();
//
// allocate and begin persistence manager transaction
//
persistenceManager = PersistenceManagerFactory.getPersistenceManager();
persistenceManager.begin();
//
// Get the widget instance created in the previous transaction
//
widgets = persistenceManager.findAll(IWidget.class);
widget = widgets[0];
IWidgetInstance widgetInstance0 = persistenceManager.findWidgetInstanceByGuid(apiKey, "test", "test-shared-data-key", widgetGuid);
assertNotNull(widgetInstance0);
widgetGuid = widget.getIdentifier();
//
// Get the widget instance created in the previous transaction via "widget GUID"
//
IWidgetInstance widgetInstance1 = persistenceManager.findWidgetInstanceByGuid(apiKey, "test", "test-shared-data-key", widgetGuid);
assertNotNull(widgetInstance1);
assertEquals(widgetInstance0, widgetInstance1);
//
// Get the widget instance created in the previous transaction via instance_idkey
//
IWidgetInstance widgetInstance2 = persistenceManager.findWidgetInstanceByIdKey("test");
assertNotNull(widgetInstance2);
assertEquals(widgetInstance0, widgetInstance2);
//
// Get the participant created in the previous transaction by widget instance
//
IParticipant [] participants = new SharedContext(widgetInstance0).getParticipants();
assertNotNull(participants);
assertEquals(1, participants.length);
//
// Get the participant created in the previous transaction by widget instance
//
participant = new SharedContext(widgetInstance0).getViewer(widgetInstance0);
assertNotNull(participant);
//
// delete all the test objects
//
persistenceManager.delete(widgetInstance0);
persistenceManager.delete(participant);
//
// commit and close persistence manager transaction
//
persistenceManager.commit();
PersistenceManagerFactory.closePersistenceManager();
//
// allocate and begin persistence manager transaction
//
persistenceManager = PersistenceManagerFactory.getPersistenceManager();
persistenceManager.begin();
//
// verify test deletes
//
IWidgetInstance [] widgetInstances = persistenceManager.findAll(IWidgetInstance.class);
assertNotNull(widgetInstances);
assertEquals(0, widgetInstances.length);
participants = persistenceManager.findAll(IParticipant.class);
assertNotNull(participants);
// TODO - this fails with the result being 1
// assertEquals(0, participants.length);
//
// rollback and close persistence manager transaction
//
persistenceManager.rollback();
PersistenceManagerFactory.closePersistenceManager();
logger.info("Test run");
}