Search in sources :

Example 11 with DataStore

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();
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) ElideResponse(com.yahoo.elide.ElideResponse) DataStore(com.yahoo.elide.core.datastore.DataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Elide(com.yahoo.elide.Elide) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Example 12 with DataStore

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();
}
Also used : FieldTestModel(com.yahoo.elide.core.lifecycle.FieldTestModel) DataStore(com.yahoo.elide.core.datastore.DataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Elide(com.yahoo.elide.Elide) Test(org.junit.jupiter.api.Test)

Example 13 with DataStore

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();
}
Also used : FieldTestModel(com.yahoo.elide.core.lifecycle.FieldTestModel) ElideResponse(com.yahoo.elide.ElideResponse) DataStore(com.yahoo.elide.core.datastore.DataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Elide(com.yahoo.elide.Elide) Test(org.junit.jupiter.api.Test)

Example 14 with DataStore

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());
        }
    }
}
Also used : Function(java.util.function.Function) Type(com.yahoo.elide.core.type.Type) DataStore(com.yahoo.elide.core.datastore.DataStore) EntityBinding(com.yahoo.elide.core.dictionary.EntityBinding) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 15 with DataStore

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);
        }
    }
}
Also used : DataStore(com.yahoo.elide.core.datastore.DataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) IOException(java.io.IOException)

Aggregations

DataStore (com.yahoo.elide.core.datastore.DataStore)38 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)31 Test (org.junit.jupiter.api.Test)27 Elide (com.yahoo.elide.Elide)25 ElideResponse (com.yahoo.elide.ElideResponse)21 RequestScope (com.yahoo.elide.core.RequestScope)19 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)18 EntityProjection (com.yahoo.elide.core.request.EntityProjection)10 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)5 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)5 HashMapDataStore (com.yahoo.elide.core.datastore.inmemory.HashMapDataStore)3 FieldTestModel (com.yahoo.elide.core.lifecycle.FieldTestModel)3 AggregationDataStore (com.yahoo.elide.datastores.aggregation.AggregationDataStore)3 MetaDataStore (com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore)3 JpaDataStore (com.yahoo.elide.datastores.jpa.JpaDataStore)3 NonJtaTransaction (com.yahoo.elide.datastores.jpa.transaction.NonJtaTransaction)3 MultiplexManager (com.yahoo.elide.datastores.multiplex.MultiplexManager)3 NoopBean (com.yahoo.elide.beans.NoopBean)2 TemplateConfigValidator (com.yahoo.elide.datastores.aggregation.validator.TemplateConfigValidator)2 ConfigDataStore (com.yahoo.elide.modelconfig.store.ConfigDataStore)2