Search in sources :

Example 1 with OtherBean

use of com.yahoo.elide.example.other.OtherBean 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");
    }
}
Also used : OtherBean(com.yahoo.elide.example.other.OtherBean) HashMapDataStore(com.yahoo.elide.core.datastore.inmemory.HashMapDataStore) FirstBean(com.yahoo.elide.example.beans.FirstBean) HashMapDataStore(com.yahoo.elide.core.datastore.inmemory.HashMapDataStore) DataStore(com.yahoo.elide.core.datastore.DataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Test(org.junit.jupiter.api.Test)

Aggregations

DataStore (com.yahoo.elide.core.datastore.DataStore)1 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)1 HashMapDataStore (com.yahoo.elide.core.datastore.inmemory.HashMapDataStore)1 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)1 FirstBean (com.yahoo.elide.example.beans.FirstBean)1 OtherBean (com.yahoo.elide.example.other.OtherBean)1 Test (org.junit.jupiter.api.Test)1