use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.
the class NPlusOneIT method setUp.
@BeforeEach
public void setUp() throws IOException {
reset(logger);
try (DataStoreTransaction tx = dataStore.beginTransaction()) {
Book book1 = new Book();
book1.setTitle("Test Book1");
tx.createObject(book1, null);
Book book2 = new Book();
book2.setTitle("Test Book2");
tx.createObject(book2, null);
Book book3 = new Book();
book3.setTitle("Test Book3");
tx.createObject(book3, null);
Author author1 = new Author();
author1.setName("Bob1");
tx.createObject(author1, null);
Author author2 = new Author();
author2.setName("Bob2");
tx.createObject(author2, null);
Author author3 = new Author();
author3.setName("Bob2");
tx.createObject(author3, null);
Publisher publisher = new Publisher();
tx.createObject(publisher, null);
author1.setBooks(Arrays.asList(book1, book2));
author2.setBooks(Arrays.asList(book1, book2));
author3.setBooks(Arrays.asList(book3));
book1.setAuthors(Arrays.asList(author1, author2));
book2.setAuthors(Arrays.asList(author1, author2));
book3.setAuthors(Arrays.asList(author3));
book1.setPublisher(publisher);
book2.setPublisher(publisher);
publisher.setBooks(new HashSet<>(Arrays.asList(book1, book2)));
tx.commit(null);
}
}
use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.
the class JMSDataStoreTest method testLoadObjects.
@Test
public void testLoadObjects() throws Exception {
Author author1 = new Author();
author1.setId(1);
author1.setName("Jon Doe");
Book book1 = new Book();
book1.setTitle("Enders Game");
book1.setId(1);
book1.setAuthors(Sets.newHashSet(author1));
Book book2 = new Book();
book2.setTitle("Grapes of Wrath");
book2.setId(2);
try (DataStoreTransaction tx = store.beginReadTransaction()) {
RequestScope scope = new RequestScope("/json", "/", NO_VERSION, null, tx, null, null, Collections.EMPTY_MAP, UUID.randomUUID(), new ElideSettingsBuilder(store).withEntityDictionary(dictionary).build());
Iterable<Book> books = tx.loadObjects(EntityProjection.builder().argument(Argument.builder().name("topic").value(TopicType.ADDED).build()).type(Book.class).build(), scope);
JMSContext context = connectionFactory.createContext();
Destination destination = context.createTopic("bookAdded");
JMSProducer producer = context.createProducer();
ObjectMapper mapper = new ObjectMapper();
producer.send(destination, mapper.writeValueAsString(book1));
producer.send(destination, mapper.writeValueAsString(book2));
Iterator<Book> booksIterator = books.iterator();
assertTrue(booksIterator.hasNext());
Book receivedBook = booksIterator.next();
assertEquals("Enders Game", receivedBook.getTitle());
assertEquals(1, receivedBook.getId());
Set<Author> receivedAuthors = Sets.newHashSet((Iterable) tx.getToManyRelation(tx, receivedBook, Relationship.builder().name("authors").projection(EntityProjection.builder().type(Author.class).build()).build(), scope));
assertTrue(receivedAuthors.contains(author1));
assertTrue(booksIterator.hasNext());
receivedBook = booksIterator.next();
assertEquals("Grapes of Wrath", receivedBook.getTitle());
assertEquals(2, receivedBook.getId());
assertFalse(booksIterator.hasNext());
}
}
use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.
the class HashMapDataStoreTest method createNewInheritanceObject.
private <T extends Object> T createNewInheritanceObject(Class<T> type) throws IOException, InstantiationException, IllegalAccessException {
T obj = type.newInstance();
try (DataStoreTransaction t = inMemoryDataStore.beginTransaction()) {
t.createObject(obj, null);
t.commit(null);
}
return obj;
}
use of com.yahoo.elide.core.datastore.DataStoreTransaction 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.core.datastore.DataStoreTransaction in project elide by yahoo.
the class HashMapDataStoreTest method dataStoreTestInheritanceUpdate.
@Test
public void dataStoreTestInheritanceUpdate() 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);
// update Child
try (DataStoreTransaction t = inMemoryDataStore.beginTransaction()) {
child.setNickname("hello");
t.save(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());
FirstChildBean bean = (FirstChildBean) IterableUtils.first(beans);
assertEquals("1", bean.getId());
assertEquals("hello", bean.getNickname());
}
}
Aggregations