Search in sources :

Example 21 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class CanPaginateVisitorTest method testComplexTrueExpression.

@Test
public void testComplexTrueExpression() throws Exception {
    @Entity
    @Include(rootLevel = false)
    class Book {

        @Id
        private long id;

        @ReadPermission(expression = "(Filter Expression Check AND False User Check) OR (Filter Expression Check OR NOT False User Check)")
        private String title;
    }
    EntityDictionary dictionary = TestDictionary.getTestDictionary(checkMappings);
    dictionary.bindEntity(Book.class);
    RequestScope scope = mock(RequestScope.class);
    assertTrue(CanPaginateVisitor.canPaginate(ClassType.of(Book.class), dictionary, scope, new HashSet<>()));
}
Also used : Entity(javax.persistence.Entity) Include(com.yahoo.elide.annotation.Include) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RequestScope(com.yahoo.elide.core.RequestScope) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 22 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class CanPaginateVisitorTest method testFalseUserAndOperationExpression.

@Test
public void testFalseUserAndOperationExpression() throws Exception {
    @Entity
    @Include(rootLevel = false)
    class Book {

        @Id
        private long id;

        @ReadPermission(expression = "False User Check AND In Memory Check")
        private String title;
    }
    EntityDictionary dictionary = TestDictionary.getTestDictionary(checkMappings);
    dictionary.bindEntity(Book.class);
    RequestScope scope = mock(RequestScope.class);
    assertTrue(CanPaginateVisitor.canPaginate(ClassType.of(Book.class), dictionary, scope, new HashSet<>()));
}
Also used : Entity(javax.persistence.Entity) Include(com.yahoo.elide.annotation.Include) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RequestScope(com.yahoo.elide.core.RequestScope) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 23 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class CanPaginateVisitorTest method testMultipleFieldsPagination.

@Test
public void testMultipleFieldsPagination() throws Exception {
    @Entity
    @Include(rootLevel = false)
    @ReadPermission(expression = "In Memory Check")
    class Book {

        @Id
        private long id;

        @ReadPermission(expression = "Filter Expression Check")
        private String title;

        @ReadPermission(expression = "Filter Expression Check")
        private Date publicationDate;
    }
    EntityDictionary dictionary = TestDictionary.getTestDictionary(checkMappings);
    dictionary.bindEntity(Book.class);
    RequestScope scope = mock(RequestScope.class);
    assertTrue(CanPaginateVisitor.canPaginate(ClassType.of(Book.class), dictionary, scope, new HashSet<>()));
}
Also used : Entity(javax.persistence.Entity) Include(com.yahoo.elide.annotation.Include) ReadPermission(com.yahoo.elide.annotation.ReadPermission) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RequestScope(com.yahoo.elide.core.RequestScope) Date(java.util.Date) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 24 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class LifeCycleTest method testPreFlushLifecycleHookException.

@Test
public void testPreFlushLifecycleHookException() {
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel testModel = mock(FieldTestModel.class);
    doThrow(IllegalStateException.class).when(testModel).attributeCallback(eq(UPDATE), eq(PREFLUSH), any(ChangeSpec.class));
    RequestScope scope = buildRequestScope(dictionary, tx);
    PersistentResource resource = new PersistentResource(testModel, "1", scope);
    resource.updateAttribute("field", "New value");
    scope.runQueuedPreSecurityTriggers();
    assertThrows(IllegalStateException.class, () -> scope.runQueuedPreFlushTriggers());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) ChangeSpec(com.yahoo.elide.core.security.ChangeSpec) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Example 25 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class LifeCycleTest method testAddToCollectionTrigger.

@Test
public void testAddToCollectionTrigger() {
    PropertyTestModel mockModel = mock(PropertyTestModel.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    RequestScope scope = buildRequestScope(dictionary, tx);
    when(tx.createNewObject(ClassType.of(PropertyTestModel.class), scope)).thenReturn(mockModel);
    PropertyTestModel modelToAdd1 = mock(PropertyTestModel.class);
    PropertyTestModel modelToAdd2 = mock(PropertyTestModel.class);
    // First we test adding to a newly created object.
    PersistentResource resource = PersistentResource.createObject(ClassType.of(PropertyTestModel.class), scope, Optional.of("1"));
    PersistentResource resourceToAdd1 = new PersistentResource(modelToAdd1, scope.getUUIDFor(mockModel), scope);
    PersistentResource resourceToAdd2 = new PersistentResource(modelToAdd2, scope.getUUIDFor(mockModel), scope);
    resource.updateRelation("models", new HashSet<>(Arrays.asList(resourceToAdd1, resourceToAdd2)));
    scope.runQueuedPreSecurityTriggers();
    scope.runQueuedPreCommitTriggers();
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(POSTCOMMIT), notNull());
    // Build another resource, scope & reset the mock to do a pure update (no create):
    scope = buildRequestScope(dictionary, tx);
    resource = new PersistentResource(mockModel, scope.getUUIDFor(mockModel), scope);
    reset(mockModel);
    resource.updateRelation("models", new HashSet<>(Arrays.asList(resourceToAdd1, resourceToAdd2)));
    scope.runQueuedPreSecurityTriggers();
    scope.runQueuedPreCommitTriggers();
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, never()).relationCallback(eq(CREATE), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(UPDATE), eq(POSTCOMMIT), notNull());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Aggregations

RequestScope (com.yahoo.elide.core.RequestScope)132 Test (org.junit.jupiter.api.Test)99 PersistentResource (com.yahoo.elide.core.PersistentResource)63 TestRequestScope (com.yahoo.elide.core.TestRequestScope)28 Include (com.yahoo.elide.annotation.Include)27 Entity (javax.persistence.Entity)27 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)26 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)23 ReadPermission (com.yahoo.elide.annotation.ReadPermission)22 EntityProjection (com.yahoo.elide.core.request.EntityProjection)22 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)22 Book (example.Book)19 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)17 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)15 HashSet (java.util.HashSet)15 Publisher (example.Publisher)14 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)12 Author (example.Author)10 Editor (example.Editor)10 Collection (java.util.Collection)10