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());
}
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));
}
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());
}
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());
}
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());
}
Aggregations