Search in sources :

Example 51 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class AggregationDataStoreTransactionTest method loadObjectsBypassCache.

@Test
public void loadObjectsBypassCache() {
    Mockito.reset(queryLogger);
    query = Query.builder().source(playerStatsTable).bypassingCache(true).build();
    NativeQuery myQuery = NativeQuery.builder().fromClause(playerStatsTable.getName()).projectionClause(" ").build();
    QueryResult queryResult = QueryResult.builder().data(DATA).build();
    when(queryEngine.executeQuery(query, qeTransaction)).thenReturn(queryResult);
    when(queryEngine.explain(query)).thenReturn(Arrays.asList(myQuery.toString()));
    AggregationDataStoreTransaction transaction = new MyAggregationDataStoreTransaction(queryEngine, cache, queryLogger);
    EntityProjection entityProjection = EntityProjection.builder().type(PlayerStats.class).build();
    assertEquals(DATA, Lists.newArrayList(transaction.loadObjects(entityProjection, scope)));
    Mockito.verify(queryEngine, never()).getTableVersion(any(), any());
    Mockito.verifyNoInteractions(cache);
    Mockito.verify(queryLogger, times(1)).acceptQuery(Mockito.eq(scope.getRequestId()), any(), any(), any(), any(), any());
    Mockito.verify(queryLogger, times(1)).processQuery(Mockito.eq(scope.getRequestId()), any(), any(), Mockito.eq(false));
    Mockito.verify(queryLogger, times(1)).completeQuery(Mockito.eq(scope.getRequestId()), any());
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) NativeQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery) PlayerStats(example.PlayerStats) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 52 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class AggregationDataStoreTransactionTest method loadObjectsNoTableVersion.

@Test
public void loadObjectsNoTableVersion() {
    Mockito.reset(queryLogger);
    NativeQuery myQuery = NativeQuery.builder().fromClause(playerStatsTable.getName()).projectionClause(" ").build();
    QueryResult queryResult = QueryResult.builder().data(DATA).build();
    when(queryEngine.executeQuery(query, qeTransaction)).thenReturn(queryResult);
    when(queryEngine.explain(query)).thenReturn(Arrays.asList(myQuery.toString()));
    AggregationDataStoreTransaction transaction = new MyAggregationDataStoreTransaction(queryEngine, cache, queryLogger);
    EntityProjection entityProjection = EntityProjection.builder().type(PlayerStats.class).build();
    transaction.loadObjects(entityProjection, scope);
    String cacheKey = ";" + queryKey;
    Mockito.verify(cache).get(cacheKey);
    Mockito.verify(cache).put(cacheKey, queryResult);
    Mockito.verify(queryLogger, times(1)).acceptQuery(Mockito.eq(scope.getRequestId()), any(), any(), any(), any(), any());
    Mockito.verify(queryLogger, times(1)).processQuery(Mockito.eq(scope.getRequestId()), any(), any(), Mockito.eq(false));
    Mockito.verify(queryLogger, times(1)).completeQuery(Mockito.eq(scope.getRequestId()), any());
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) NativeQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery) PlayerStats(example.PlayerStats) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 53 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class JpaDataStoreTransactionTest method testNoDelegationOnLoadRecords.

@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testNoDelegationOnLoadRecords(boolean delegateToInMemory) {
    AbstractJpaTransaction tx = new AbstractJpaTransaction(entityManager, (unused) -> {
    }, DEFAULT_LOGGER, delegateToInMemory) {

        @Override
        public boolean isOpen() {
            return false;
        }

        @Override
        public void begin() {
        }

        @Override
        protected Predicate<Collection<?>> isPersistentCollection() {
            return (unused) -> true;
        }
    };
    EntityProjection projection = EntityProjection.builder().type(Book.class).build();
    DataStoreIterable<Book> result = tx.loadObjects(projection, scope);
    assertFalse(result.needsInMemoryFilter());
    assertFalse(result.needsInMemorySort());
    assertFalse(result.needsInMemoryPagination());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Author(example.Author) ClassType(com.yahoo.elide.core.type.ClassType) PersistentSet(org.hibernate.collection.internal.PersistentSet) ArrayList(java.util.ArrayList) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) TestInstance(org.junit.jupiter.api.TestInstance) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AbstractJpaTransaction(com.yahoo.elide.datastores.jpa.transaction.AbstractJpaTransaction) BeforeAll(org.junit.jupiter.api.BeforeAll) NO_VERSION(com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Relationship(com.yahoo.elide.core.request.Relationship) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) RequestScope(com.yahoo.elide.core.RequestScope) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) DEFAULT_LOGGER(com.yahoo.elide.datastores.jpa.JpaDataStore.DEFAULT_LOGGER) Predicate(java.util.function.Predicate) Collection(java.util.Collection) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Mockito.when(org.mockito.Mockito.when) EntityManager(javax.persistence.EntityManager) Arguments(org.junit.jupiter.params.provider.Arguments) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Query(javax.persistence.Query) Stream(java.util.stream.Stream) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Collection(java.util.Collection) AbstractJpaTransaction(com.yahoo.elide.datastores.jpa.transaction.AbstractJpaTransaction) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 54 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class JpaDataStoreTransactionTest method testGetRelationDelegation.

@ParameterizedTest
@MethodSource("getTestArguments")
public void testGetRelationDelegation(boolean delegateToInMemory, int numberOfAuthors, FilterExpression filter, boolean usesInMemory) throws Exception {
    AbstractJpaTransaction tx = new AbstractJpaTransaction(entityManager, (unused) -> {
    }, DEFAULT_LOGGER, delegateToInMemory, false) {

        @Override
        public boolean isOpen() {
            return false;
        }

        @Override
        public void begin() {
        }

        @Override
        protected Predicate<Collection<?>> isPersistentCollection() {
            return (unused) -> true;
        }
    };
    EntityProjection projection = EntityProjection.builder().type(Author.class).build();
    List<Author> authors = new ArrayList<>();
    Author author1 = mock(Author.class);
    authors.add(author1);
    for (int idx = 1; idx < numberOfAuthors; idx++) {
        authors.add(mock(Author.class));
    }
    when(query.getResultList()).thenReturn(authors);
    DataStoreIterable<Author> loadedAuthors = tx.loadObjects(projection, scope);
    assertFalse(loadedAuthors.needsInMemoryPagination());
    assertFalse(loadedAuthors.needsInMemorySort());
    assertFalse(loadedAuthors.needsInMemoryFilter());
    Relationship relationship = Relationship.builder().name("books").projection(EntityProjection.builder().type(Book.class).filterExpression(filter).build()).build();
    PersistentSet returnCollection = mock(PersistentSet.class);
    when(author1.getBooks()).thenReturn(returnCollection);
    DataStoreIterable<Book> loadedBooks = tx.getToManyRelation(tx, author1, relationship, scope);
    assertEquals(usesInMemory, loadedBooks.needsInMemoryFilter());
    assertEquals(usesInMemory, loadedBooks.needsInMemorySort());
    assertEquals(usesInMemory, loadedBooks.needsInMemoryPagination());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Author(example.Author) ClassType(com.yahoo.elide.core.type.ClassType) PersistentSet(org.hibernate.collection.internal.PersistentSet) ArrayList(java.util.ArrayList) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) TestInstance(org.junit.jupiter.api.TestInstance) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AbstractJpaTransaction(com.yahoo.elide.datastores.jpa.transaction.AbstractJpaTransaction) BeforeAll(org.junit.jupiter.api.BeforeAll) NO_VERSION(com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Relationship(com.yahoo.elide.core.request.Relationship) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) RequestScope(com.yahoo.elide.core.RequestScope) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) DEFAULT_LOGGER(com.yahoo.elide.datastores.jpa.JpaDataStore.DEFAULT_LOGGER) Predicate(java.util.function.Predicate) Collection(java.util.Collection) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Mockito.when(org.mockito.Mockito.when) EntityManager(javax.persistence.EntityManager) Arguments(org.junit.jupiter.params.provider.Arguments) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Query(javax.persistence.Query) Stream(java.util.stream.Stream) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) EntityProjection(com.yahoo.elide.core.request.EntityProjection) ArrayList(java.util.ArrayList) PersistentSet(org.hibernate.collection.internal.PersistentSet) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) Collection(java.util.Collection) Author(example.Author) AbstractJpaTransaction(com.yahoo.elide.datastores.jpa.transaction.AbstractJpaTransaction) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 55 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class SubscriptionDataFetcher method get.

@Override
public Object get(DataFetchingEnvironment environment) throws Exception {
    OperationDefinition.Operation op = environment.getOperationDefinition().getOperation();
    if (op != OperationDefinition.Operation.SUBSCRIPTION) {
        throw new InvalidEntityBodyException(String.format("%s not supported for subscription models.", op));
    }
    /* build environment object, extracts required fields */
    Environment context = new Environment(environment, nonEntityDictionary);
    /* safe enable debugging */
    if (log.isDebugEnabled()) {
        logContext(log, RelationshipOp.FETCH, context);
    }
    if (context.isRoot()) {
        String entityName = context.field.getName();
        String aliasName = context.field.getAlias();
        EntityProjection projection = context.requestScope.getProjectionInfo().getProjection(aliasName, entityName);
        Flowable<PersistentResource> recordPublisher = PersistentResource.loadRecords(projection, new ArrayList<>(), context.requestScope).toFlowable(BackpressureStrategy.BUFFER).onBackpressureBuffer(bufferSize, true, false);
        return recordPublisher.map(SubscriptionNodeContainer::new);
    }
    // as the PersistentResourceFetcher.
    return context.container.processFetch(context);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) PersistentResource(com.yahoo.elide.core.PersistentResource) InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Environment(com.yahoo.elide.graphql.Environment) SubscriptionNodeContainer(com.yahoo.elide.graphql.subscriptions.containers.SubscriptionNodeContainer) OperationDefinition(graphql.language.OperationDefinition)

Aggregations

EntityProjection (com.yahoo.elide.core.request.EntityProjection)108 Test (org.junit.jupiter.api.Test)90 Book (example.Book)41 RequestScope (com.yahoo.elide.core.RequestScope)27 Author (example.Author)27 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)22 Publisher (example.Publisher)22 Relationship (com.yahoo.elide.core.request.Relationship)19 Path (com.yahoo.elide.core.Path)18 TestRequestScope (com.yahoo.elide.core.TestRequestScope)18 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)18 HashMap (java.util.HashMap)18 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)18 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)16 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)15 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)15 Collection (java.util.Collection)15 LinkedHashSet (java.util.LinkedHashSet)14 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)13 Editor (example.Editor)13