Search in sources :

Example 6 with PersistentResource

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

use of com.yahoo.elide.core.PersistentResource 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 8 with PersistentResource

use of com.yahoo.elide.core.PersistentResource 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 9 with PersistentResource

use of com.yahoo.elide.core.PersistentResource 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 10 with PersistentResource

use of com.yahoo.elide.core.PersistentResource 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

PersistentResource (com.yahoo.elide.core.PersistentResource)100 Test (org.junit.jupiter.api.Test)71 RequestScope (com.yahoo.elide.core.RequestScope)60 ReadPermission (com.yahoo.elide.annotation.ReadPermission)18 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)18 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)17 Include (com.yahoo.elide.annotation.Include)16 Entity (javax.persistence.Entity)16 Resource (com.yahoo.elide.jsonapi.models.Resource)13 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)10 NotFilterExpression (com.yahoo.elide.core.filter.expression.NotFilterExpression)10 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)10 PermissionExecutor (com.yahoo.elide.core.security.PermissionExecutor)10 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)10 Book (example.Book)10 LinkedHashSet (java.util.LinkedHashSet)9 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)8 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)8 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)8 RSQLFilterDialect (com.yahoo.elide.core.filter.dialect.RSQLFilterDialect)7