Search in sources :

Example 16 with DataStore

use of com.yahoo.elide.core.datastore.DataStore in project elide by yahoo.

the class MultiplexManagerTest method partialCommitFailure.

@Test
public void partialCommitFailure() throws IOException {
    final EntityDictionary entityDictionary = EntityDictionary.builder().build();
    final HashMapDataStore ds1 = new HashMapDataStore(DefaultClassScanner.getInstance(), FirstBean.class.getPackage());
    final DataStore ds2 = new TestDataStore(OtherBean.class.getPackage());
    final MultiplexManager multiplexManager = new MultiplexManager(ds1, ds2);
    multiplexManager.populateEntityDictionary(entityDictionary);
    assertEquals(ds1, multiplexManager.getSubManager(ClassType.of(FirstBean.class)));
    assertEquals(ds2, multiplexManager.getSubManager(ClassType.of(OtherBean.class)));
    try (DataStoreTransaction t = ds1.beginTransaction()) {
        assertFalse(t.loadObjects(EntityProjection.builder().type(FirstBean.class).build(), null).iterator().hasNext());
        FirstBean firstBean = FirstBean.class.newInstance();
        firstBean.setName("name");
        t.createObject(firstBean, null);
        // t.save(firstBean);
        assertFalse(t.loadObjects(EntityProjection.builder().type(FirstBean.class).build(), null).iterator().hasNext());
        t.commit(null);
    } catch (InstantiationException | IllegalAccessException e) {
        log.error("", e);
    }
    try (DataStoreTransaction t = multiplexManager.beginTransaction()) {
        FirstBean firstBean = (FirstBean) t.loadObjects(EntityProjection.builder().type(FirstBean.class).build(), null).iterator().next();
        firstBean.setName("update");
        t.save(firstBean, null);
        OtherBean otherBean = OtherBean.class.newInstance();
        t.createObject(otherBean, null);
        // t.save(firstBean);
        assertThrows(TransactionException.class, () -> t.commit(null));
    } catch (InstantiationException | IllegalAccessException e) {
        log.error("", e);
    }
    // verify state
    try (DataStoreTransaction t = ds1.beginTransaction()) {
        Iterable<Object> beans = t.loadObjects(EntityProjection.builder().type(FirstBean.class).build(), null);
        assertNotNull(beans);
        ArrayList<Object> list = Lists.newArrayList(beans.iterator());
        assertEquals(list.size(), 1);
        assertEquals(((FirstBean) list.get(0)).getName(), "name");
    }
}
Also used : OtherBean(com.yahoo.elide.example.other.OtherBean) HashMapDataStore(com.yahoo.elide.core.datastore.inmemory.HashMapDataStore) FirstBean(com.yahoo.elide.example.beans.FirstBean) HashMapDataStore(com.yahoo.elide.core.datastore.inmemory.HashMapDataStore) DataStore(com.yahoo.elide.core.datastore.DataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Test(org.junit.jupiter.api.Test)

Example 17 with DataStore

use of com.yahoo.elide.core.datastore.DataStore in project elide by yahoo.

the class DefaultAsyncAPIDAOTest method setupMocks.

@BeforeEach
public void setupMocks() {
    dataStore = mock(DataStore.class);
    asyncQuery = mock(AsyncQuery.class);
    asyncQueryResult = mock(AsyncQueryResult.class);
    filter = mock(FilterExpression.class);
    tx = mock(DataStoreTransaction.class);
    Map<String, Class<? extends Check>> checkMappings = new HashMap<>();
    EntityDictionary dictionary = EntityDictionary.builder().checks(checkMappings).build();
    dictionary.bindEntity(AsyncQuery.class);
    dictionary.bindEntity(AsyncQueryResult.class);
    ElideSettings elideSettings = new ElideSettingsBuilder(dataStore).withEntityDictionary(dictionary).withJoinFilterDialect(RSQLFilterDialect.builder().dictionary(dictionary).build()).withSubqueryFilterDialect(RSQLFilterDialect.builder().dictionary(dictionary).build()).withISO8601Dates("yyyy-MM-dd'T'HH:mm'Z'", TimeZone.getTimeZone("UTC")).build();
    elide = new Elide(elideSettings);
    when(dataStore.beginTransaction()).thenReturn(tx);
    asyncAPIDAO = new DefaultAsyncAPIDAO(elide.getElideSettings(), dataStore);
}
Also used : HashMap(java.util.HashMap) Check(com.yahoo.elide.core.security.checks.Check) AsyncQueryResult(com.yahoo.elide.async.models.AsyncQueryResult) ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) DataStore(com.yahoo.elide.core.datastore.DataStore) AsyncQuery(com.yahoo.elide.async.models.AsyncQuery) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) ElideSettings(com.yahoo.elide.ElideSettings) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Elide(com.yahoo.elide.Elide) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 18 with DataStore

use of com.yahoo.elide.core.datastore.DataStore in project elide by yahoo.

the class AggregationDataStoreTestHarness method getDataStore.

@Override
public DataStore getDataStore() {
    AggregationDataStore.AggregationDataStoreBuilder aggregationDataStoreBuilder = AggregationDataStore.builder();
    ClassScanner scanner = DefaultClassScanner.getInstance();
    MetaDataStore metaDataStore;
    if (validator != null) {
        metaDataStore = new MetaDataStore(scanner, validator.getElideTableConfig().getTables(), validator.getElideNamespaceConfig().getNamespaceconfigs(), true);
        aggregationDataStoreBuilder.dynamicCompiledClasses(metaDataStore.getDynamicTypes());
    } else {
        metaDataStore = new MetaDataStore(scanner, true);
    }
    AggregationDataStore aggregationDataStore = aggregationDataStoreBuilder.queryEngine(new SQLQueryEngine(metaDataStore, (name) -> connectionDetailsMap.getOrDefault(name, defaultConnectionDetails), new HashSet<>(Arrays.asList(new AggregateBeforeJoinOptimizer(metaDataStore))), new DefaultQueryPlanMerger(metaDataStore), new DefaultQueryValidator(metaDataStore.getMetadataDictionary()))).queryLogger(new Slf4jQueryLogger()).build();
    Consumer<EntityManager> txCancel = em -> em.unwrap(Session.class).cancelQuery();
    DataStore jpaStore = new JpaDataStore(() -> entityManagerFactory.createEntityManager(), em -> new NonJtaTransaction(em, txCancel));
    return new MultiplexManager(jpaStore, metaDataStore, aggregationDataStore);
}
Also used : SQLQueryEngine(com.yahoo.elide.datastores.aggregation.queryengines.sql.SQLQueryEngine) Arrays(java.util.Arrays) SQLQueryEngine(com.yahoo.elide.datastores.aggregation.queryengines.sql.SQLQueryEngine) Session(org.hibernate.Session) MultiplexManager(com.yahoo.elide.datastores.multiplex.MultiplexManager) NonJtaTransaction(com.yahoo.elide.datastores.jpa.transaction.NonJtaTransaction) HashSet(java.util.HashSet) ClassScanner(com.yahoo.elide.core.utils.ClassScanner) AggregateBeforeJoinOptimizer(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.AggregateBeforeJoinOptimizer) Map(java.util.Map) DataSource(javax.sql.DataSource) AggregationDataStore(com.yahoo.elide.datastores.aggregation.AggregationDataStore) DefaultQueryPlanMerger(com.yahoo.elide.datastores.aggregation.query.DefaultQueryPlanMerger) JpaDataStore(com.yahoo.elide.datastores.jpa.JpaDataStore) Slf4jQueryLogger(com.yahoo.elide.datastores.aggregation.core.Slf4jQueryLogger) SQLDialectFactory(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialectFactory) DynamicConfigValidator(com.yahoo.elide.modelconfig.validator.DynamicConfigValidator) DefaultClassScanner(com.yahoo.elide.core.utils.DefaultClassScanner) DefaultQueryValidator(com.yahoo.elide.datastores.aggregation.DefaultQueryValidator) EntityManager(javax.persistence.EntityManager) DataStoreTestHarness(com.yahoo.elide.core.datastore.test.DataStoreTestHarness) ConnectionDetails(com.yahoo.elide.datastores.aggregation.queryengines.sql.ConnectionDetails) Consumer(java.util.function.Consumer) EntityManagerFactory(javax.persistence.EntityManagerFactory) DataStore(com.yahoo.elide.core.datastore.DataStore) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) AllArgsConstructor(lombok.AllArgsConstructor) Collections(java.util.Collections) JpaDataStore(com.yahoo.elide.datastores.jpa.JpaDataStore) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) DefaultQueryPlanMerger(com.yahoo.elide.datastores.aggregation.query.DefaultQueryPlanMerger) NonJtaTransaction(com.yahoo.elide.datastores.jpa.transaction.NonJtaTransaction) MultiplexManager(com.yahoo.elide.datastores.multiplex.MultiplexManager) DefaultQueryValidator(com.yahoo.elide.datastores.aggregation.DefaultQueryValidator) Slf4jQueryLogger(com.yahoo.elide.datastores.aggregation.core.Slf4jQueryLogger) EntityManager(javax.persistence.EntityManager) AggregateBeforeJoinOptimizer(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.AggregateBeforeJoinOptimizer) ClassScanner(com.yahoo.elide.core.utils.ClassScanner) DefaultClassScanner(com.yahoo.elide.core.utils.DefaultClassScanner) AggregationDataStore(com.yahoo.elide.datastores.aggregation.AggregationDataStore) JpaDataStore(com.yahoo.elide.datastores.jpa.JpaDataStore) DataStore(com.yahoo.elide.core.datastore.DataStore) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) AggregationDataStore(com.yahoo.elide.datastores.aggregation.AggregationDataStore) HashSet(java.util.HashSet) Session(org.hibernate.Session)

Example 19 with DataStore

use of com.yahoo.elide.core.datastore.DataStore in project elide by yahoo.

the class ElideStandaloneSettings method getDataStore.

/**
 * Gets the DataStore for elide.
 * @param metaDataStore MetaDataStore object.
 * @param aggregationDataStore AggregationDataStore object.
 * @param entityManagerFactory EntityManagerFactory object.
 * @return DataStore object initialized.
 */
default DataStore getDataStore(MetaDataStore metaDataStore, AggregationDataStore aggregationDataStore, EntityManagerFactory entityManagerFactory) {
    List<DataStore> stores = new ArrayList<>();
    DataStore jpaDataStore = new JpaDataStore(() -> entityManagerFactory.createEntityManager(), em -> new NonJtaTransaction(em, TXCANCEL, DEFAULT_LOGGER, true, true));
    stores.add(jpaDataStore);
    if (getAnalyticProperties().enableDynamicModelConfigAPI()) {
        stores.add(new ConfigDataStore(getAnalyticProperties().getDynamicConfigPath(), new TemplateConfigValidator(getClassScanner(), getAnalyticProperties().getDynamicConfigPath())));
    }
    stores.add(metaDataStore);
    stores.add(aggregationDataStore);
    return new MultiplexManager(stores.toArray(new DataStore[0]));
}
Also used : JpaDataStore(com.yahoo.elide.datastores.jpa.JpaDataStore) TemplateConfigValidator(com.yahoo.elide.datastores.aggregation.validator.TemplateConfigValidator) ConfigDataStore(com.yahoo.elide.modelconfig.store.ConfigDataStore) AggregationDataStore(com.yahoo.elide.datastores.aggregation.AggregationDataStore) DataStore(com.yahoo.elide.core.datastore.DataStore) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) JpaDataStore(com.yahoo.elide.datastores.jpa.JpaDataStore) ConfigDataStore(com.yahoo.elide.modelconfig.store.ConfigDataStore) ArrayList(java.util.ArrayList) NonJtaTransaction(com.yahoo.elide.datastores.jpa.transaction.NonJtaTransaction) MultiplexManager(com.yahoo.elide.datastores.multiplex.MultiplexManager)

Example 20 with DataStore

use of com.yahoo.elide.core.datastore.DataStore in project elide by yahoo.

the class ErrorMapperTest method testElideCreateWithErrorMapperUnmapped.

@Test
public void testElideCreateWithErrorMapperUnmapped() 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());
    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)

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