Search in sources :

Example 96 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction 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 97 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction 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)

Example 98 with DataStoreTransaction

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

the class VerifyFieldAccessFilterExpressionVisitorTest method setupMocks.

@BeforeEach
public void setupMocks() {
    // this will test with the default interface implementation
    scope = mock(RequestScope.class);
    PermissionExecutor permissionExecutor = mock(PermissionExecutor.class);
    DataStoreTransaction transaction = mock(DataStoreTransaction.class);
    EntityDictionary dictionary = EntityDictionary.builder().build();
    dictionary.bindEntity(Book.class);
    dictionary.bindEntity(Author.class);
    when(scope.getDictionary()).thenReturn(dictionary);
    when(scope.getPermissionExecutor()).thenReturn(permissionExecutor);
    when(scope.getTransaction()).thenReturn(transaction);
    when(permissionExecutor.evaluateFilterJoinUserChecks(any(), any())).thenCallRealMethod();
    when(permissionExecutor.handleFilterJoinReject(any(), any(), any())).thenCallRealMethod();
}
Also used : PermissionExecutor(com.yahoo.elide.core.security.PermissionExecutor) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) RequestScope(com.yahoo.elide.core.RequestScope) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 99 with DataStoreTransaction

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

the class VerifyFieldAccessFilterExpressionVisitorTest method testBypassReadonlyFilterRestriction.

@Test
public void testBypassReadonlyFilterRestriction() throws Exception {
    RSQLFilterDialect dialect = RSQLFilterDialect.builder().dictionary(scope.getDictionary()).build();
    FilterExpression expression = dialect.parseFilterExpression("authors.name==foo", ClassType.of(Book.class), true);
    Book book = new Book();
    PersistentResource<Book> resource = new PersistentResource<>(book, "", scope);
    PermissionExecutor permissionExecutor = scope.getPermissionExecutor();
    DataStoreTransaction tx = scope.getTransaction();
    when(permissionExecutor.evaluateFilterJoinUserChecks(any(), any())).thenReturn(ExpressionResult.PASS);
    VerifyFieldAccessFilterExpressionVisitor visitor = new VerifyFieldAccessFilterExpressionVisitor(resource);
    // restricted HOME field
    assertTrue(expression.accept(visitor));
    verify(permissionExecutor, times(1)).evaluateFilterJoinUserChecks(any(), any());
    verify(permissionExecutor, never()).checkSpecificFieldPermissions(any(), any(), any(), any());
    verify(permissionExecutor, never()).checkUserPermissions(any(), any(), isA(String.class));
    verify(permissionExecutor, never()).handleFilterJoinReject(any(), any(), any());
    verify(tx, never()).getToManyRelation(any(), any(), any(), any());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) Book(example.Book) PermissionExecutor(com.yahoo.elide.core.security.PermissionExecutor) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) Test(org.junit.jupiter.api.Test)

Example 100 with DataStoreTransaction

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

the class LifeCycleTest method testLegacyElideDelete.

@Test
public void testLegacyElideDelete() throws Exception {
    DataStore store = mock(DataStore.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    LegacyTestModel mockModel = mock(LegacyTestModel.class);
    Elide elide = getElide(store, dictionary, MOCK_AUDIT_LOGGER);
    dictionary.setValue(mockModel, "id", "1");
    when(store.beginTransaction()).thenReturn(tx);
    when(tx.loadObject(isA(EntityProjection.class), any(), isA(RequestScope.class))).thenReturn(mockModel);
    ElideResponse response = elide.delete(baseUrl, "/legacyTestModel/1", "", null, NO_VERSION);
    assertEquals(HttpStatus.SC_NO_CONTENT, response.getResponseCode());
    verify(mockModel, never()).classUpdatePostCommit();
    verify(mockModel, never()).classUpdatePreSecurity();
    verify(mockModel, never()).classUpdatePreCommit();
    verify(mockModel, never()).classCreatePreCommitAllUpdates();
    verify(mockModel, never()).classCreatePostCommit();
    verify(mockModel, never()).classCreatePreSecurity();
    verify(mockModel, never()).classCreatePreCommit();
    verify(mockModel, times(1)).classDeletePreSecurity();
    verify(mockModel, times(1)).classDeletePreCommit();
    verify(mockModel, times(1)).classDeletePostCommit();
    verify(mockModel, times(3)).classMultiple();
    verify(mockModel, never()).fieldUpdatePostCommit();
    verify(mockModel, never()).fieldUpdatePreSecurity();
    verify(mockModel, never()).fieldUpdatePreCommit();
    verify(mockModel, never()).fieldCreatePostCommit();
    verify(mockModel, never()).fieldCreatePreSecurity();
    verify(mockModel, never()).fieldCreatePreCommit();
    verify(mockModel, never()).fieldMultiple();
    verify(tx).preCommit(any());
    verify(tx).delete(eq(mockModel), isA(RequestScope.class));
    verify(tx).flush(isA(RequestScope.class));
    verify(tx).commit(isA(RequestScope.class));
    verify(tx).close();
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) ElideResponse(com.yahoo.elide.ElideResponse) DataStore(com.yahoo.elide.core.datastore.DataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Elide(com.yahoo.elide.Elide) RequestScope(com.yahoo.elide.core.RequestScope) 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