use of com.yahoo.elide.example.beans.FirstBean in project elide by yahoo.
the class MultiplexManagerTest method partialCommitFailure.
@Test
public void partialCommitFailure() throws IOException {
final EntityDictionary entityDictionary = EntityDictionary.builder().build();
final HashMapDataStore ds1 = new HashMapDataStore(DefaultClassScanner.getInstance(), FirstBean.class.getPackage());
final DataStore ds2 = new TestDataStore(OtherBean.class.getPackage());
final MultiplexManager multiplexManager = new MultiplexManager(ds1, ds2);
multiplexManager.populateEntityDictionary(entityDictionary);
assertEquals(ds1, multiplexManager.getSubManager(ClassType.of(FirstBean.class)));
assertEquals(ds2, multiplexManager.getSubManager(ClassType.of(OtherBean.class)));
try (DataStoreTransaction t = ds1.beginTransaction()) {
assertFalse(t.loadObjects(EntityProjection.builder().type(FirstBean.class).build(), null).iterator().hasNext());
FirstBean firstBean = FirstBean.class.newInstance();
firstBean.setName("name");
t.createObject(firstBean, null);
// t.save(firstBean);
assertFalse(t.loadObjects(EntityProjection.builder().type(FirstBean.class).build(), null).iterator().hasNext());
t.commit(null);
} catch (InstantiationException | IllegalAccessException e) {
log.error("", e);
}
try (DataStoreTransaction t = multiplexManager.beginTransaction()) {
FirstBean firstBean = (FirstBean) t.loadObjects(EntityProjection.builder().type(FirstBean.class).build(), null).iterator().next();
firstBean.setName("update");
t.save(firstBean, null);
OtherBean otherBean = OtherBean.class.newInstance();
t.createObject(otherBean, null);
// t.save(firstBean);
assertThrows(TransactionException.class, () -> t.commit(null));
} catch (InstantiationException | IllegalAccessException e) {
log.error("", e);
}
// verify state
try (DataStoreTransaction t = ds1.beginTransaction()) {
Iterable<Object> beans = t.loadObjects(EntityProjection.builder().type(FirstBean.class).build(), null);
assertNotNull(beans);
ArrayList<Object> list = Lists.newArrayList(beans.iterator());
assertEquals(list.size(), 1);
assertEquals(((FirstBean) list.get(0)).getName(), "name");
}
}
use of com.yahoo.elide.example.beans.FirstBean in project elide by yahoo.
the class HashMapDataStoreTest method testCanGenerateIdsAfterDataCommitted.
@Test
public void testCanGenerateIdsAfterDataCommitted() throws Exception {
// given an object with a non-generated ID has been created
FirstBean object = new FirstBean();
object.id = "1";
object.name = "number one";
try (DataStoreTransaction t = inMemoryDataStore.beginTransaction()) {
t.createObject(object, null);
t.save(object, null);
t.commit(null);
}
// when an object without ID is created, that works
FirstBean object2 = new FirstBean();
object2.id = null;
object2.name = "number two";
try (DataStoreTransaction t = inMemoryDataStore.beginTransaction()) {
t.createObject(object2, null);
t.save(object2, null);
t.commit(null);
}
// and a meaningful ID is assigned
Set<String> names = new HashSet<>();
try (DataStoreTransaction t = inMemoryDataStore.beginTransaction()) {
for (Object objBean : t.loadObjects(EntityProjection.builder().type(FirstBean.class).build(), null)) {
FirstBean bean = (FirstBean) objBean;
names.add(bean.name);
assertFalse(bean.id == null);
}
}
assertEquals(ImmutableSet.of("number one", "number two"), names);
}
use of com.yahoo.elide.example.beans.FirstBean in project elide by yahoo.
the class HashMapDataStoreTest method testValidCommit.
@Test
public void testValidCommit() throws Exception {
FirstBean object = new FirstBean();
object.id = "0";
object.name = "Test";
try (DataStoreTransaction t = inMemoryDataStore.beginTransaction()) {
assertFalse(t.loadObjects(EntityProjection.builder().type(FirstBean.class).build(), null).iterator().hasNext());
t.createObject(object, null);
assertFalse(t.loadObjects(EntityProjection.builder().type(FirstBean.class).build(), null).iterator().hasNext());
t.commit(null);
}
try (DataStoreTransaction t = inMemoryDataStore.beginTransaction()) {
Iterable<Object> beans = t.loadObjects(EntityProjection.builder().type(FirstBean.class).build(), null);
assertNotNull(beans);
assertTrue(beans.iterator().hasNext());
FirstBean bean = (FirstBean) IterableUtils.first(beans);
assertTrue(!"0".equals(bean.id) && "Test".equals(bean.name));
}
}
use of com.yahoo.elide.example.beans.FirstBean in project elide by yahoo.
the class HashMapDataStoreTest method dataStoreTestInheritanceDelete.
@Test
public void dataStoreTestInheritanceDelete() throws IOException, InstantiationException, IllegalAccessException {
Map<String, Object> entry = inMemoryDataStore.get(ClassType.of(FirstBean.class));
assertEquals(0, entry.size());
FirstChildBean child = createNewInheritanceObject(FirstChildBean.class);
createNewInheritanceObject(FirstBean.class);
// Delete Child
try (DataStoreTransaction t = inMemoryDataStore.beginTransaction()) {
t.delete(child, null);
t.commit(null);
}
// Only 1 parent entry should remain.
try (DataStoreTransaction t = inMemoryDataStore.beginTransaction()) {
Iterable<Object> beans = t.loadObjects(EntityProjection.builder().type(FirstBean.class).build(), null);
assertNotNull(beans);
assertTrue(beans.iterator().hasNext());
FirstBean bean = (FirstBean) IterableUtils.first(beans);
assertEquals("2", bean.getId());
}
}
use of com.yahoo.elide.example.beans.FirstBean in project elide by yahoo.
the class HashMapDataStoreTest method dataStoreTestInheritance.
@Test
public void dataStoreTestInheritance() throws IOException, InstantiationException, IllegalAccessException {
Map<String, Object> entry = inMemoryDataStore.get(ClassType.of(FirstBean.class));
assertEquals(0, entry.size());
FirstChildBean child = createNewInheritanceObject(FirstChildBean.class);
// Adding Child object, adds a parent entry.
try (DataStoreTransaction t = inMemoryDataStore.beginTransaction()) {
Iterable<Object> beans = t.loadObjects(EntityProjection.builder().type(FirstBean.class).build(), null);
assertNotNull(beans);
assertTrue(beans.iterator().hasNext());
FirstBean bean = (FirstBean) IterableUtils.first(beans);
assertEquals("1", bean.getId());
}
assertEquals("1", child.getId());
assertNotNull(entry);
assertEquals(1, entry.size());
// New Parent avoids id collision.
FirstBean parent = createNewInheritanceObject(FirstBean.class);
assertEquals("2", parent.getId());
// New Child avoids id collision
FirstChildBean child1 = createNewInheritanceObject(FirstChildBean.class);
assertEquals("3", child1.getId());
}
Aggregations