use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.
the class LifeCycleTest method testAddToCollectionTrigger.
@Test
public void testAddToCollectionTrigger() {
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 modelToAdd1 = mock(PropertyTestModel.class);
PropertyTestModel modelToAdd2 = mock(PropertyTestModel.class);
// First we test adding to a newly created object.
PersistentResource resource = PersistentResource.createObject(ClassType.of(PropertyTestModel.class), scope, Optional.of("1"));
PersistentResource resourceToAdd1 = new PersistentResource(modelToAdd1, scope.getUUIDFor(mockModel), scope);
PersistentResource resourceToAdd2 = new PersistentResource(modelToAdd2, scope.getUUIDFor(mockModel), scope);
resource.updateRelation("models", new HashSet<>(Arrays.asList(resourceToAdd1, resourceToAdd2)));
scope.runQueuedPreSecurityTriggers();
scope.runQueuedPreCommitTriggers();
scope.runQueuedPostCommitTriggers();
verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(POSTCOMMIT), notNull());
// 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);
resource.updateRelation("models", new HashSet<>(Arrays.asList(resourceToAdd1, resourceToAdd2)));
scope.runQueuedPreSecurityTriggers();
scope.runQueuedPreCommitTriggers();
scope.runQueuedPostCommitTriggers();
verify(mockModel, never()).relationCallback(eq(CREATE), any(), any());
verify(mockModel, times(1)).relationCallback(eq(UPDATE), eq(POSTCOMMIT), notNull());
}
use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.
the class LifeCycleTest method testLegacyElidePatchExtensionCreate.
@Test
public void testLegacyElidePatchExtensionCreate() throws Exception {
DataStore store = mock(DataStore.class);
DataStoreTransaction tx = mock(DataStoreTransaction.class);
LegacyTestModel mockModel = mock(LegacyTestModel.class);
Elide elide = getElide(store, dictionary, MOCK_AUDIT_LOGGER);
String body = "[{\"op\": \"add\",\"path\": \"/legacyTestModel\",\"value\":{" + "\"type\":\"legacyTestModel\",\"id\": \"1\",\"attributes\": {\"field\":\"Foo\"}}}]";
when(store.beginTransaction()).thenReturn(tx);
when(tx.createNewObject(eq(ClassType.of(LegacyTestModel.class)), any())).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());
verify(mockModel, times(1)).classCreatePreSecurity();
verify(mockModel, times(1)).classCreatePreCommit();
verify(mockModel, times(1)).classCreatePreCommitAllUpdates();
verify(mockModel, times(1)).classCreatePostCommit();
verify(mockModel, times(3)).classMultiple();
verify(mockModel, never()).classUpdatePreCommit();
verify(mockModel, never()).classUpdatePostCommit();
verify(mockModel, never()).classUpdatePreSecurity();
verify(mockModel, never()).classDeletePreCommit();
verify(mockModel, never()).classDeletePostCommit();
verify(mockModel, never()).classDeletePreSecurity();
verify(mockModel, times(1)).fieldCreatePostCommit();
verify(mockModel, times(1)).fieldCreatePreCommit();
verify(mockModel, times(1)).fieldCreatePreSecurity();
verify(mockModel, times(3)).fieldMultiple();
verify(mockModel, never()).fieldUpdatePreCommit();
verify(mockModel, never()).fieldUpdatePostCommit();
verify(mockModel, never()).fieldUpdatePreSecurity();
verify(tx).preCommit(any());
verify(tx, times(1)).createObject(eq(mockModel), 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 testElideCreate.
@Test
public void testElideCreate() 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 = "{\"data\": {\"type\":\"testModel\",\"id\":\"1\",\"attributes\": {\"field\":\"Foo\"}}}";
when(store.beginTransaction()).thenReturn(tx);
when(tx.createNewObject(eq(ClassType.of(FieldTestModel.class)), any())).thenReturn(mockModel);
ElideResponse response = elide.post(baseUrl, "/testModel", body, null, NO_VERSION);
assertEquals(HttpStatus.SC_CREATED, response.getResponseCode());
verify(mockModel, times(1)).classCallback(eq(CREATE), eq(PRESECURITY));
verify(mockModel, times(1)).classCallback(eq(CREATE), eq(PREFLUSH));
verify(mockModel, times(1)).classCallback(eq(CREATE), eq(PRECOMMIT));
verify(mockModel, times(1)).classCallback(eq(CREATE), eq(POSTCOMMIT));
verify(mockModel, never()).classCallback(eq(UPDATE), any());
verify(mockModel, never()).classCallback(eq(DELETE), any());
verify(mockModel, times(2)).classAllFieldsCallback(any(), any());
verify(mockModel, times(2)).classAllFieldsCallback(eq(CREATE), eq(PRECOMMIT));
verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(PRESECURITY), any());
verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(PREFLUSH), any());
verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(PRECOMMIT), any());
verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(POSTCOMMIT), any());
verify(mockModel, never()).attributeCallback(eq(UPDATE), any(), any());
verify(mockModel, never()).attributeCallback(eq(DELETE), any(), any());
verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(PRESECURITY), any());
verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(PREFLUSH), any());
verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(PRECOMMIT), any());
verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(POSTCOMMIT), any());
verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
verify(mockModel, never()).relationCallback(eq(DELETE), any(), any());
verify(tx).preCommit(any());
verify(tx, times(1)).createObject(eq(mockModel), 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 testElideGet.
@Test
public void testElideGet() 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);
when(store.beginReadTransaction()).thenCallRealMethod();
when(store.beginTransaction()).thenReturn(tx);
when(tx.loadObject(isA(EntityProjection.class), any(), isA(RequestScope.class))).thenReturn(mockModel);
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
ElideResponse response = elide.get(baseUrl, "/testModel/1", queryParams, null, NO_VERSION);
assertEquals(HttpStatus.SC_OK, response.getResponseCode());
verify(mockModel, never()).classAllFieldsCallback(any(), any());
verify(mockModel, never()).classCallback(eq(CREATE), any());
verify(mockModel, never()).classCallback(eq(UPDATE), any());
verify(mockModel, never()).classCallback(eq(DELETE), any());
verify(mockModel, never()).attributeCallback(eq(CREATE), any(), any());
verify(mockModel, never()).attributeCallback(eq(UPDATE), 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());
verify(tx).flush(any());
verify(tx).commit(any());
verify(tx).close();
}
use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.
the class LifeCycleTest method testElideGetSparse.
@Test
public void testElideGetSparse() 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);
when(store.beginReadTransaction()).thenCallRealMethod();
when(store.beginTransaction()).thenReturn(tx);
when(tx.loadObject(isA(EntityProjection.class), any(), isA(RequestScope.class))).thenReturn(mockModel);
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
queryParams.putSingle("fields[testModel]", "field");
ElideResponse response = elide.get(baseUrl, "/testModel/1", queryParams, null, NO_VERSION);
assertEquals(HttpStatus.SC_OK, response.getResponseCode());
verify(mockModel, never()).classAllFieldsCallback(any(), any());
verify(mockModel, never()).classCallback(eq(CREATE), any());
verify(mockModel, never()).classCallback(eq(UPDATE), any());
verify(mockModel, never()).classCallback(eq(DELETE), any());
verify(mockModel, never()).attributeCallback(eq(CREATE), any(), any());
verify(mockModel, never()).attributeCallback(eq(UPDATE), any(), any());
verify(mockModel, never()).attributeCallback(eq(DELETE), any(), any());
verify(mockModel, never()).relationCallback(any(), any(), any());
verify(tx).preCommit(any());
verify(tx).flush(any());
verify(tx).commit(any());
verify(tx).close();
}
Aggregations