Search in sources :

Example 11 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class EntityProjectionMakerTest method testRootEntityNoQueryParams.

@Test
public void testRootEntityNoQueryParams() {
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    String path = "/book/1";
    RequestScope scope = new TestRequestScope(dictionary, path, queryParams);
    EntityProjectionMaker maker = new EntityProjectionMaker(dictionary, scope);
    EntityProjection expected = EntityProjection.builder().type(Book.class).attribute(Attribute.builder().name("title").type(String.class).build()).attribute(Attribute.builder().name("awards").type(Collection.class).build()).attribute(Attribute.builder().name("genre").type(String.class).build()).attribute(Attribute.builder().name("language").type(String.class).build()).attribute(Attribute.builder().name("publishDate").type(long.class).build()).attribute(Attribute.builder().name("authorTypes").type(Collection.class).build()).attribute(Attribute.builder().name("price").type(Price.class).build()).relationship("authors", EntityProjection.builder().type(Author.class).build()).relationship("publisher", EntityProjection.builder().type(Publisher.class).build()).relationship("editor", EntityProjection.builder().type(Editor.class).build()).build();
    EntityProjection actual = maker.parsePath(path);
    projectionEquals(expected, actual);
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) TestRequestScope(com.yahoo.elide.core.TestRequestScope) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Collection(java.util.Collection) Author(example.Author) Editor(example.Editor) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Test(org.junit.jupiter.api.Test)

Example 12 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class EntityProjectionMakerTest method testInvalidSparseFieldsNested.

@Test
public void testInvalidSparseFieldsNested() {
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.add("fields[book]", "publisher,title");
    // Invalid Fields: cost
    queryParams.add("fields[publisher]", "name,cost");
    queryParams.add("include", "publisher");
    String path = "/book";
    RequestScope scope = new TestRequestScope(dictionary, path, queryParams);
    EntityProjectionMaker maker = new EntityProjectionMaker(dictionary, scope);
    Exception e = assertThrows(InvalidValueException.class, () -> maker.parsePath(path));
    assertEquals("Invalid value: publisher does not contain the fields: [cost]", e.getMessage());
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) TestRequestScope(com.yahoo.elide.core.TestRequestScope) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) Test(org.junit.jupiter.api.Test)

Example 13 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class EntityProjectionMakerTest method testRootCollectionNoQueryParams.

@Test
public void testRootCollectionNoQueryParams() {
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    String path = "/book";
    RequestScope scope = new TestRequestScope(dictionary, path, queryParams);
    EntityProjectionMaker maker = new EntityProjectionMaker(dictionary, scope);
    EntityProjection expected = EntityProjection.builder().type(Book.class).attribute(Attribute.builder().name("title").type(String.class).build()).attribute(Attribute.builder().name("awards").type(Collection.class).build()).attribute(Attribute.builder().name("genre").type(String.class).build()).attribute(Attribute.builder().name("language").type(String.class).build()).attribute(Attribute.builder().name("publishDate").type(long.class).build()).attribute(Attribute.builder().name("authorTypes").type(Collection.class).build()).attribute(Attribute.builder().name("price").type(Price.class).build()).relationship("authors", EntityProjection.builder().type(Author.class).build()).relationship("publisher", EntityProjection.builder().type(Publisher.class).build()).relationship("editor", EntityProjection.builder().type(Editor.class).build()).pagination(PaginationImpl.getDefaultPagination(ClassType.of(Book.class))).build();
    EntityProjection actual = maker.parsePath(path);
    projectionEquals(expected, actual);
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) TestRequestScope(com.yahoo.elide.core.TestRequestScope) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Price(example.Price) Book(example.Book) Publisher(example.Publisher) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Test(org.junit.jupiter.api.Test)

Example 14 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class EntityProjectionMakerTest method testNestedCollectionWithSingleInclude.

@Test
public void testNestedCollectionWithSingleInclude() {
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.add("include", "books");
    String path = "/author/1/books/3/publisher";
    RequestScope scope = new TestRequestScope(dictionary, path, queryParams);
    EntityProjectionMaker maker = new EntityProjectionMaker(dictionary, scope);
    EntityProjection expected = EntityProjection.builder().type(Author.class).relationship("books", EntityProjection.builder().type(Book.class).relationship("publisher", EntityProjection.builder().type(Publisher.class).attribute(Attribute.builder().name("name").type(String.class).build()).attribute(Attribute.builder().name("updateHookInvoked").type(boolean.class).build()).relationship("books", EntityProjection.builder().attribute(Attribute.builder().name("title").type(String.class).build()).attribute(Attribute.builder().name("awards").type(Collection.class).build()).attribute(Attribute.builder().name("genre").type(String.class).build()).attribute(Attribute.builder().name("language").type(String.class).build()).attribute(Attribute.builder().name("publishDate").type(long.class).build()).attribute(Attribute.builder().name("authorTypes").type(Collection.class).build()).attribute(Attribute.builder().name("price").type(Price.class).build()).relationship("authors", EntityProjection.builder().type(Author.class).build()).relationship("publisher", EntityProjection.builder().type(Publisher.class).build()).relationship("editor", EntityProjection.builder().type(Editor.class).build()).type(Book.class).build()).relationship("editor", EntityProjection.builder().type(Editor.class).build()).pagination(PaginationImpl.getDefaultPagination(ClassType.of(Publisher.class))).build()).build()).build();
    EntityProjection actual = maker.parsePath(path);
    projectionEquals(expected, actual);
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) TestRequestScope(com.yahoo.elide.core.TestRequestScope) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Price(example.Price) Book(example.Book) Publisher(example.Publisher) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Test(org.junit.jupiter.api.Test)

Example 15 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class EntityProjectionMakerTest method testInvalidSparseFields.

@Test
public void testInvalidSparseFields() {
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    // Invalid Fields: bookTitle & bookName
    queryParams.add("fields[book]", "publisher,bookTitle,bookName");
    String path = "/book";
    RequestScope scope = new TestRequestScope(dictionary, path, queryParams);
    EntityProjectionMaker maker = new EntityProjectionMaker(dictionary, scope);
    Exception e = assertThrows(InvalidValueException.class, () -> maker.parsePath(path));
    assertEquals("Invalid value: book does not contain the fields: [bookTitle, bookName]", e.getMessage());
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) TestRequestScope(com.yahoo.elide.core.TestRequestScope) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) Test(org.junit.jupiter.api.Test)

Aggregations

RequestScope (com.yahoo.elide.core.RequestScope)132 Test (org.junit.jupiter.api.Test)99 PersistentResource (com.yahoo.elide.core.PersistentResource)63 TestRequestScope (com.yahoo.elide.core.TestRequestScope)28 Include (com.yahoo.elide.annotation.Include)27 Entity (javax.persistence.Entity)27 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)26 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)23 ReadPermission (com.yahoo.elide.annotation.ReadPermission)22 EntityProjection (com.yahoo.elide.core.request.EntityProjection)22 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)22 Book (example.Book)19 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)17 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)15 HashSet (java.util.HashSet)15 Publisher (example.Publisher)14 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)12 Author (example.Author)10 Editor (example.Editor)10 Collection (java.util.Collection)10