use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.
the class LifeCycleTest method testElidePatchExtensionUpdate.
@Test
public void testElidePatchExtensionUpdate() throws Exception {
DataStore store = mock(DataStore.class);
DataStoreTransaction tx = mock(DataStoreTransaction.class);
FieldTestModel mockModel = mock(FieldTestModel.class);
Elide elide = getElide(store, dictionary, MOCK_AUDIT_LOGGER);
String body = "[{\"op\": \"replace\",\"path\": \"/testModel/1\",\"value\":{" + "\"type\":\"testModel\",\"id\": \"1\",\"attributes\": {\"field\":\"Foo\"}}}]";
dictionary.setValue(mockModel, "id", "1");
when(store.beginTransaction()).thenReturn(tx);
when(tx.loadObject(any(), any(), isA(RequestScope.class))).thenReturn(mockModel);
String contentType = JSONAPI_CONTENT_TYPE_WITH_JSON_PATCH_EXTENSION;
ElideResponse response = elide.patch(baseUrl, contentType, contentType, "/", body, null, NO_VERSION);
assertEquals(HttpStatus.SC_OK, response.getResponseCode());
assertEquals("[{\"data\":null}]", response.getBody());
verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PRESECURITY));
verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PREFLUSH));
verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PRECOMMIT));
verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(POSTCOMMIT));
verify(mockModel, never()).classCallback(eq(CREATE), any());
verify(mockModel, never()).classCallback(eq(DELETE), any());
verify(mockModel, never()).classAllFieldsCallback(any(), any());
verify(mockModel, never()).classAllFieldsCallback(eq(CREATE), eq(PRECOMMIT));
verify(mockModel, times(1)).attributeCallback(eq(UPDATE), eq(PRESECURITY), any());
verify(mockModel, times(1)).attributeCallback(eq(UPDATE), eq(PREFLUSH), any());
verify(mockModel, times(1)).attributeCallback(eq(UPDATE), eq(PRECOMMIT), any());
verify(mockModel, times(1)).attributeCallback(eq(UPDATE), eq(POSTCOMMIT), any());
verify(mockModel, never()).attributeCallback(eq(CREATE), any(), any());
verify(mockModel, never()).attributeCallback(eq(DELETE), any(), any());
verify(mockModel, never()).relationCallback(eq(CREATE), any(), any());
verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
verify(mockModel, never()).relationCallback(eq(DELETE), any(), any());
verify(tx).preCommit(any());
// Twice because the patch extension request is broken into attributes & relationships separately.
verify(tx, times(2)).loadObject(any(), any(), isA(RequestScope.class));
verify(tx).flush(isA(RequestScope.class));
verify(tx).commit(isA(RequestScope.class));
verify(tx).close();
}
use of com.yahoo.elide.core.datastore.DataStoreTransaction 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.datastore.DataStoreTransaction 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.datastore.DataStoreTransaction 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.datastore.DataStoreTransaction 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());
}
Aggregations