Search in sources :

Example 91 with RequestScope

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

the class JsonApiTest method compareOrder.

@Test
public void compareOrder() {
    Parent parent1 = new Parent();
    parent1.setId(123L);
    Parent parent2 = new Parent();
    parent2.setId(456L);
    RequestScope userScope = new TestRequestScope(BASE_URL, tx, user, dictionary);
    PersistentResource<Parent> pRec1 = new PersistentResource<>(parent1, userScope.getUUIDFor(parent1), userScope);
    PersistentResource<Parent> pRec2 = new PersistentResource<>(parent2, userScope.getUUIDFor(parent2), userScope);
    JsonApiDocument jsonApiDocument1 = new JsonApiDocument();
    jsonApiDocument1.setData(new Data<>(Lists.newArrayList(pRec1.toResource(), pRec2.toResource())));
    JsonApiDocument jsonApiDocument2 = new JsonApiDocument();
    jsonApiDocument2.setData(new Data<>(Lists.newArrayList(pRec2.toResource(), pRec1.toResource())));
    assertEquals(jsonApiDocument1, jsonApiDocument2);
    assertEquals(jsonApiDocument1.hashCode(), jsonApiDocument2.hashCode());
    jsonApiDocument1.getData().sort((a, b) -> Integer.compare(a.hashCode(), b.hashCode()));
    jsonApiDocument2.getData().sort((a, b) -> Integer.compare(b.hashCode(), a.hashCode()));
    assertEquals(jsonApiDocument1, jsonApiDocument2);
    assertEquals(jsonApiDocument1.hashCode(), jsonApiDocument2.hashCode());
}
Also used : TestRequestScope(com.yahoo.elide.core.TestRequestScope) PersistentResource(com.yahoo.elide.core.PersistentResource) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Parent(example.Parent) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Test(org.junit.jupiter.api.Test)

Example 92 with RequestScope

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

the class LifeCycleTest method testRelationshipUpdate.

@Test
public void testRelationshipUpdate() {
    FieldTestModel mockModel = mock(FieldTestModel.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    RequestScope scope = buildRequestScope(dictionary, tx);
    when(tx.createNewObject(ClassType.of(FieldTestModel.class), scope)).thenReturn(mockModel);
    FieldTestModel modelToAdd = mock(FieldTestModel.class);
    PersistentResource resource = new PersistentResource(mockModel, scope.getUUIDFor(mockModel), scope);
    PersistentResource resourceToAdd = new PersistentResource(modelToAdd, scope.getUUIDFor(mockModel), scope);
    resource.addRelation("models", resourceToAdd);
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PRESECURITY));
    verify(mockModel, times(1)).relationCallback(any(), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(UPDATE), eq(PRESECURITY), notNull());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreSecurityTriggers();
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreFlushTriggers();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PREFLUSH));
    verify(mockModel, times(1)).relationCallback(any(), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(UPDATE), eq(PREFLUSH), notNull());
    clearInvocations(mockModel);
    scope.runQueuedPreCommitTriggers();
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PRECOMMIT));
    verify(mockModel, times(1)).relationCallback(any(), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(UPDATE), eq(PRECOMMIT), notNull());
    clearInvocations(mockModel);
    scope.getPermissionExecutor().executeCommitChecks();
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(POSTCOMMIT));
    verify(mockModel, times(1)).relationCallback(any(), 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)

Example 93 with RequestScope

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

the class LifeCycleTest method testDelete.

@Test
public void testDelete() {
    FieldTestModel mockModel = mock(FieldTestModel.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    RequestScope scope = buildRequestScope(dictionary, tx);
    when(tx.createNewObject(ClassType.of(FieldTestModel.class), scope)).thenReturn(mockModel);
    PersistentResource resource = new PersistentResource(mockModel, "1", scope);
    resource.deleteResource();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(DELETE), eq(PRESECURITY));
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreSecurityTriggers();
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).classCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreFlushTriggers();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(DELETE), eq(PREFLUSH));
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreCommitTriggers();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(DELETE), eq(PRECOMMIT));
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.getPermissionExecutor().executeCommitChecks();
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(DELETE), eq(POSTCOMMIT));
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
}
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)

Example 94 with RequestScope

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

the class LifeCycleTest method testAttributeUpdate.

@Test
public void testAttributeUpdate() {
    FieldTestModel mockModel = mock(FieldTestModel.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    RequestScope scope = buildRequestScope(dictionary, tx);
    when(tx.createNewObject(ClassType.of(FieldTestModel.class), scope)).thenReturn(mockModel);
    PersistentResource resource = new PersistentResource(mockModel, scope.getUUIDFor(mockModel), scope);
    resource.updateAttribute("field", "new value");
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PRESECURITY));
    verify(mockModel, times(1)).attributeCallback(any(), any(), any());
    verify(mockModel, times(1)).attributeCallback(eq(UPDATE), eq(PRESECURITY), notNull());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreSecurityTriggers();
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreFlushTriggers();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PREFLUSH));
    verify(mockModel, times(1)).attributeCallback(any(), any(), any());
    verify(mockModel, times(1)).attributeCallback(eq(UPDATE), eq(PREFLUSH), notNull());
    clearInvocations(mockModel);
    scope.runQueuedPreCommitTriggers();
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PRECOMMIT));
    verify(mockModel, times(1)).attributeCallback(any(), any(), any());
    verify(mockModel, times(1)).attributeCallback(eq(UPDATE), eq(PRECOMMIT), notNull());
    clearInvocations(mockModel);
    scope.getPermissionExecutor().executeCommitChecks();
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(POSTCOMMIT));
    verify(mockModel, times(1)).attributeCallback(any(), any(), any());
    verify(mockModel, times(1)).attributeCallback(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)

Example 95 with RequestScope

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

the class PermissionExecutorTest method newResource.

public <T> PersistentResource<T> newResource(T obj, Class<T> cls, boolean markNew) {
    EntityDictionary dictionary = EntityDictionary.builder().checks(TestCheckMappings.MAPPINGS).build();
    dictionary.bindEntity(cls);
    RequestScope requestScope = new RequestScope(null, null, NO_VERSION, null, null, null, null, null, UUID.randomUUID(), getElideSettings(dictionary));
    PersistentResource resource = new PersistentResource<>(obj, requestScope.getUUIDFor(obj), requestScope);
    if (markNew) {
        requestScope.getNewPersistentResources().add(resource);
    }
    return resource;
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RequestScope(com.yahoo.elide.core.RequestScope)

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