Search in sources :

Example 76 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class EntityManagerLifeCycleHookIT method setUp.

@BeforeEach
public void setUp() throws IOException {
    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);
        tx.commit(null);
    }
}
Also used : Book(example.Book) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 77 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class PersistentResourceTest method testPatchRequestScope.

@Test
public void testPatchRequestScope() {
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    PatchRequestScope parentScope = new PatchRequestScope(null, "/book", NO_VERSION, tx, new TestUser("1"), UUID.randomUUID(), null, Collections.emptyMap(), elideSettings);
    PatchRequestScope scope = new PatchRequestScope(parentScope.getPath(), parentScope.getJsonApiDocument(), parentScope);
    // verify wrap works
    assertEquals(parentScope.getUpdateStatusCode(), scope.getUpdateStatusCode());
    assertEquals(parentScope.getObjectEntityCache(), scope.getObjectEntityCache());
    Parent parent = newParent(7);
    PersistentResource<Parent> parentResource = new PersistentResource<>(parent, "1", scope);
    parentResource.updateAttribute("firstName", "foobar");
    ArgumentCaptor<Attribute> attributeArgument = ArgumentCaptor.forClass(Attribute.class);
    verify(tx, times(1)).setAttribute(eq(parent), attributeArgument.capture(), eq(scope));
    assertEquals(attributeArgument.getValue().getName(), "firstName");
    assertEquals(attributeArgument.getValue().getArguments().iterator().next().getValue(), "foobar");
}
Also used : PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Parent(example.Parent) Attribute(com.yahoo.elide.core.request.Attribute) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) TestUser(com.yahoo.elide.core.security.TestUser) Test(org.junit.jupiter.api.Test)

Example 78 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class PersistentResourceTest method testRelationshipMissingData.

/**
 * Verify that Relationship toMany cannot contain null resources, but toOne can.
 *
 * @throws Exception
 */
@Test
public void testRelationshipMissingData() throws Exception {
    User goodUser = new TestUser("1");
    @SuppressWarnings("resource") DataStoreTransaction tx = mock(DataStoreTransaction.class);
    RequestScope goodScope = new RequestScope(null, null, NO_VERSION, null, tx, goodUser, null, null, UUID.randomUUID(), elideSettings);
    // null resource in toMany relationship is not valid
    List<Resource> idList = new ArrayList<>();
    idList.add(new ResourceIdentifier("child", "3").castToResource());
    idList.add(new ResourceIdentifier("child", "6").castToResource());
    idList.add(null);
    assertThrows(NullPointerException.class, () -> new Relationship(Collections.emptyMap(), new Data<>(idList)));
    // However null toOne relationship is valid
    Relationship toOneRelationship = new Relationship(Collections.emptyMap(), new Data<>((Resource) null));
    assertTrue(toOneRelationship.getData().get().isEmpty());
    assertNull(toOneRelationship.toPersistentResources(goodScope));
    // no Data
    Relationship nullRelationship = new Relationship(Collections.emptyMap(), null);
    assertNull(nullRelationship.getData());
    assertNull(nullRelationship.toPersistentResources(goodScope));
}
Also used : ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) TestUser(com.yahoo.elide.core.security.TestUser) User(com.yahoo.elide.core.security.User) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Resource(com.yahoo.elide.jsonapi.models.Resource) ArrayList(java.util.ArrayList) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Data(com.yahoo.elide.jsonapi.models.Data) TestUser(com.yahoo.elide.core.security.TestUser) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 79 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class TransactionWrapperTest method testPreCommit.

@Test
public void testPreCommit() {
    DataStoreTransaction wrapped = mock(DataStoreTransaction.class);
    DataStoreTransaction wrapper = new TestTransactionWrapper(wrapped);
    wrapper.preCommit(null);
    verify(wrapped, times(1)).preCommit(any());
}
Also used : DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Test(org.junit.jupiter.api.Test)

Example 80 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class TransactionWrapperTest method testGetAttribute.

@Test
public void testGetAttribute() {
    DataStoreTransaction wrapped = mock(DataStoreTransaction.class);
    DataStoreTransaction wrapper = new TestTransactionWrapper(wrapped);
    when(wrapped.getAttribute(any(), isA(Attribute.class), any())).thenReturn(1L);
    Object actual = wrapper.getAttribute(null, Attribute.builder().name("foo").type(String.class).build(), null);
    verify(wrapped, times(1)).getAttribute(any(), isA(Attribute.class), any());
    assertEquals(1L, actual);
}
Also used : Attribute(com.yahoo.elide.core.request.Attribute) 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