use of com.yahoo.elide.core.datastore.DataStore in project elide by yahoo.
the class LifeCycleTest method testElideDelete.
@Test
public void testElideDelete() 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);
dictionary.setValue(mockModel, "id", "1");
when(store.beginTransaction()).thenReturn(tx);
when(tx.loadObject(isA(EntityProjection.class), any(), isA(RequestScope.class))).thenReturn(mockModel);
ElideResponse response = elide.delete(baseUrl, "/testModel/1", "", null, NO_VERSION);
assertEquals(HttpStatus.SC_NO_CONTENT, response.getResponseCode());
verify(mockModel, never()).classAllFieldsCallback(any(), any());
verify(mockModel, never()).classCallback(eq(UPDATE), any());
verify(mockModel, never()).classCallback(eq(CREATE), any());
verify(mockModel, times(1)).classCallback(eq(DELETE), eq(PRESECURITY));
verify(mockModel, times(1)).classCallback(eq(DELETE), eq(PREFLUSH));
verify(mockModel, times(1)).classCallback(eq(DELETE), eq(PRECOMMIT));
verify(mockModel, times(1)).classCallback(eq(DELETE), eq(POSTCOMMIT));
verify(mockModel, never()).attributeCallback(eq(UPDATE), any(), any());
verify(mockModel, never()).attributeCallback(eq(CREATE), any(), any());
verify(mockModel, never()).attributeCallback(eq(DELETE), any(), any());
verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
verify(mockModel, never()).relationCallback(eq(CREATE), any(), any());
verify(mockModel, never()).relationCallback(eq(DELETE), any(), any());
verify(tx).preCommit(any());
verify(tx).delete(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.DataStore in project elide by yahoo.
the class ErrorMapperTest method testElideCreateNoErrorMapper.
@Test
public void testElideCreateNoErrorMapper() throws Exception {
DataStore store = mock(DataStore.class);
DataStoreTransaction tx = mock(DataStoreTransaction.class);
FieldTestModel mockModel = mock(FieldTestModel.class);
Elide elide = getElide(store, dictionary, null);
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);
doThrow(EXPECTED_EXCEPTION).when(tx).preCommit(any());
RuntimeException result = assertThrows(RuntimeException.class, () -> elide.post(baseUrl, "/testModel", body, null, NO_VERSION));
assertEquals(EXPECTED_EXCEPTION, result.getCause());
verify(tx).close();
}
use of com.yahoo.elide.core.datastore.DataStore in project elide by yahoo.
the class ErrorMapperTest method testElideCreateWithErrorMapperMapped.
@Test
public void testElideCreateWithErrorMapperMapped() throws Exception {
DataStore store = mock(DataStore.class);
DataStoreTransaction tx = mock(DataStoreTransaction.class);
FieldTestModel mockModel = mock(FieldTestModel.class);
Elide elide = getElide(store, dictionary, MOCK_ERROR_MAPPER);
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);
doThrow(EXPECTED_EXCEPTION).when(tx).preCommit(any());
when(MOCK_ERROR_MAPPER.map(EXPECTED_EXCEPTION)).thenReturn(MAPPED_EXCEPTION);
ElideResponse response = elide.post(baseUrl, "/testModel", body, null, NO_VERSION);
assertEquals(422, response.getResponseCode());
assertEquals("{\"errors\":[{\"code\":\"SOME_ERROR\"}]}", response.getBody());
verify(tx).close();
}
use of com.yahoo.elide.core.datastore.DataStore in project elide by yahoo.
the class MultiplexManager method populateEntityDictionary.
@Override
public void populateEntityDictionary(EntityDictionary dictionary) {
this.dictionary = dictionary;
for (DataStore dataStore : dataStores) {
EntityDictionary subordinateDictionary = new EntityDictionary(dictionary.getCheckMappings(), dictionary.getRoleChecks(), dictionary.getInjector(), dictionary.getSerdeLookup(), dictionary.getEntitiesToExclude(), dictionary.getScanner());
dataStore.populateEntityDictionary(subordinateDictionary);
for (EntityBinding binding : subordinateDictionary.getBindings(false)) {
// route class to this database manager
this.dataStoreMap.put(binding.entityClass, dataStore);
// bind to multiplex dictionary
dictionary.bindEntity(binding);
}
for (Map.Entry<Type<?>, Function<RequestScope, PermissionExecutor>> entry : subordinateDictionary.getEntityPermissionExecutor().entrySet()) {
dictionary.bindPermissionExecutor(entry.getKey(), entry.getValue());
}
}
}
use of com.yahoo.elide.core.datastore.DataStore in project elide by yahoo.
the class MultiplexWriteTransaction method reverseTransactions.
/**
* Attempt to reverse changes of last commit since not all transactions successfully committed.
* @param restoreList List of database managers to reverse the last commit
* @param cause cause to add any suppressed exceptions
*/
private void reverseTransactions(ArrayList<DataStore> restoreList, Throwable cause, RequestScope requestScope) {
for (DataStore dataStore : restoreList) {
try (DataStoreTransaction transaction = dataStore.beginTransaction()) {
List<Object> list = dirtyObjects.get(dataStore);
for (Object dirtyObject : list == null ? Collections.emptyList() : list) {
Object cloned = clonedObjects.get(dirtyObject);
if (cloned == NEWLY_CREATED_OBJECT) {
transaction.delete(dirtyObject, requestScope);
} else {
transaction.save(cloned, requestScope);
}
}
transaction.commit(requestScope);
} catch (RuntimeException | IOException e) {
cause.addSuppressed(e);
}
}
}
Aggregations