Search in sources :

Example 26 with RequestScope

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

the class LifeCycleTest method testPreCommitLifecycleHookException.

@Test
public void testPreCommitLifecycleHookException() {
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel testModel = mock(FieldTestModel.class);
    doThrow(IllegalStateException.class).when(testModel).attributeCallback(eq(UPDATE), eq(PRECOMMIT), 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.runQueuedPreCommitTriggers());
}
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 27 with RequestScope

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

the class LifeCycleTest method testRemoveFromCollectionTrigger.

@Test
public void testRemoveFromCollectionTrigger() {
    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 childModel1 = mock(PropertyTestModel.class);
    PropertyTestModel childModel2 = mock(PropertyTestModel.class);
    PropertyTestModel childModel3 = mock(PropertyTestModel.class);
    when(childModel1.getId()).thenReturn("2");
    when(childModel2.getId()).thenReturn("3");
    when(childModel3.getId()).thenReturn("4");
    // First we test removing from a newly created object.
    PersistentResource resource = PersistentResource.createObject(ClassType.of(PropertyTestModel.class), scope, Optional.of("1"));
    PersistentResource childResource1 = new PersistentResource(childModel1, "2", scope);
    PersistentResource childResource2 = new PersistentResource(childModel2, "3", scope);
    PersistentResource childResource3 = new PersistentResource(childModel3, "3", scope);
    resource.updateRelation("models", new HashSet<>(Arrays.asList(childResource1, childResource2)));
    scope.runQueuedPreSecurityTriggers();
    scope.runQueuedPreCommitTriggers();
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
    ArgumentCaptor<ChangeSpec> changes = ArgumentCaptor.forClass(ChangeSpec.class);
    verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(POSTCOMMIT), changes.capture());
    changes.getValue().getModified().equals(List.of(childModel1, childModel2));
    changes.getValue().getOriginal().equals(List.of());
    // 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);
    Relationship relationship = Relationship.builder().projection(EntityProjection.builder().type(PropertyTestModel.class).build()).name("models").build();
    when(tx.getToManyRelation(tx, mockModel, relationship, scope)).thenReturn(new DataStoreIterableBuilder<Object>(Arrays.asList(childModel1, childModel2)).build());
    when(mockModel.getModels()).thenReturn(new HashSet<>(Arrays.asList(childModel1, childModel2)));
    resource.updateRelation("models", new HashSet<>(Arrays.asList(childResource1, childResource3)));
    scope.runQueuedPreSecurityTriggers();
    scope.runQueuedPreCommitTriggers();
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, never()).relationCallback(eq(CREATE), any(), any());
    changes = ArgumentCaptor.forClass(ChangeSpec.class);
    verify(mockModel, times(1)).relationCallback(eq(UPDATE), eq(POSTCOMMIT), changes.capture());
    changes.getValue().getModified().equals(List.of(childModel1, childModel3));
    changes.getValue().getOriginal().equals(List.of(childModel1, childModel2));
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) ChangeSpec(com.yahoo.elide.core.security.ChangeSpec) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Relationship(com.yahoo.elide.core.request.Relationship) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Example 28 with RequestScope

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

the class LifeCycleTest method testPostCommitLifecycleHookException.

@Test
public void testPostCommitLifecycleHookException() {
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel testModel = mock(FieldTestModel.class);
    doThrow(IllegalStateException.class).when(testModel).attributeCallback(eq(UPDATE), eq(POSTCOMMIT), any(ChangeSpec.class));
    RequestScope scope = buildRequestScope(dictionary, tx);
    PersistentResource resource = new PersistentResource(testModel, "1", scope);
    resource.updateAttribute("field", "New value");
    scope.runQueuedPreSecurityTriggers();
    scope.runQueuedPreCommitTriggers();
    assertThrows(IllegalStateException.class, () -> scope.runQueuedPostCommitTriggers());
}
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 29 with RequestScope

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

the class LifeCycleTest method testCreate.

@Test
public void testCreate() {
    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 = PersistentResource.createObject(ClassType.of(FieldTestModel.class), scope, Optional.of("1"));
    resource.updateAttribute("field", "should not affect calls since this is create!");
    verify(mockModel, never()).classCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    scope.runQueuedPreSecurityTriggers();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(CREATE), eq(PRESECURITY));
    verify(mockModel, times(1)).attributeCallback(any(), any(), any());
    verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(PRESECURITY), any());
    verify(mockModel, times(1)).relationCallback(any(), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(PRESECURITY), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreFlushTriggers();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(CREATE), eq(PREFLUSH));
    verify(mockModel, times(1)).attributeCallback(any(), any(), any());
    verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(PREFLUSH), any());
    verify(mockModel, times(1)).relationCallback(any(), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(PREFLUSH), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreCommitTriggers();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(CREATE), eq(PRECOMMIT));
    verify(mockModel, times(1)).attributeCallback(any(), any(), any());
    verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(PRECOMMIT), any());
    verify(mockModel, times(1)).relationCallback(any(), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(PRECOMMIT), any());
    verify(mockModel, times(2)).classAllFieldsCallback(any(), any());
    verify(mockModel, times(2)).classAllFieldsCallback(eq(CREATE), eq(PRECOMMIT));
    clearInvocations(mockModel);
    scope.getPermissionExecutor().executeCommitChecks();
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(CREATE), eq(POSTCOMMIT));
    verify(mockModel, times(1)).attributeCallback(any(), any(), any());
    verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(POSTCOMMIT), any());
    verify(mockModel, times(1)).relationCallback(any(), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(POSTCOMMIT), 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 30 with RequestScope

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

the class LifeCycleTest method testRead.

@Test
public void testRead() {
    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.getAttribute(Attribute.builder().type(String.class).name("field").build());
    verify(mockModel, never()).classCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreSecurityTriggers();
    verify(mockModel, never()).classCallback(any(), any());
    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, never()).classCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreCommitTriggers();
    verify(mockModel, never()).classCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, never()).classCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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