Search in sources :

Example 1 with FirstBean

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");
    }
}
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)

Example 2 with FirstBean

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);
}
Also used : FirstBean(com.yahoo.elide.example.beans.FirstBean) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with FirstBean

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));
    }
}
Also used : FirstBean(com.yahoo.elide.example.beans.FirstBean) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Test(org.junit.jupiter.api.Test)

Example 4 with FirstBean

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());
    }
}
Also used : FirstChildBean(com.yahoo.elide.example.beans.FirstChildBean) FirstBean(com.yahoo.elide.example.beans.FirstBean) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Test(org.junit.jupiter.api.Test)

Example 5 with FirstBean

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());
}
Also used : FirstChildBean(com.yahoo.elide.example.beans.FirstChildBean) FirstBean(com.yahoo.elide.example.beans.FirstBean) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Test(org.junit.jupiter.api.Test)

Aggregations

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