Search in sources :

Example 71 with DataStoreTransaction

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);
    }
}
Also used : Book(example.Book) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Author(example.Author) Publisher(example.Publisher) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 72 with DataStoreTransaction

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());
    }
}
Also used : Destination(javax.jms.Destination) ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) Book(example.Book) Author(example.Author) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) JMSProducer(javax.jms.JMSProducer) RequestScope(com.yahoo.elide.core.RequestScope) JMSContext(javax.jms.JMSContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 73 with DataStoreTransaction

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;
}
Also used : DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction)

Example 74 with DataStoreTransaction

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);
}
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 75 with DataStoreTransaction

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());
    }
}
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)139 Test (org.junit.jupiter.api.Test)101 RequestScope (com.yahoo.elide.core.RequestScope)40 DataStore (com.yahoo.elide.core.datastore.DataStore)30 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)28 Elide (com.yahoo.elide.Elide)27 ElideResponse (com.yahoo.elide.ElideResponse)22 EntityProjection (com.yahoo.elide.core.request.EntityProjection)22 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)18 PersistentResource (com.yahoo.elide.core.PersistentResource)17 Item (com.yahoo.elide.datastores.search.models.Item)17 Book (example.Book)13 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 Author (example.Author)8 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)7 FirstBean (com.yahoo.elide.example.beans.FirstBean)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)6