Search in sources :

Example 26 with TestRequestScope

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

the class JsonApiTest method writeSingleIncluded.

@Test
public void writeSingleIncluded() throws JsonProcessingException {
    Parent parent = new Parent();
    Child child = new Child();
    parent.setId(123L);
    child.setId(2);
    parent.setFirstName("bob");
    parent.setChildren(Collections.singleton(child));
    child.setParents(Collections.singleton(parent));
    child.setFriends(new HashSet<>());
    RequestScope userScope = new TestRequestScope(BASE_URL, tx, user, dictionary);
    PersistentResource<Parent> pRec = new PersistentResource<>(parent, userScope.getUUIDFor(parent), userScope);
    JsonApiDocument jsonApiDocument = new JsonApiDocument();
    jsonApiDocument.setData(new Data<>(pRec.toResource()));
    jsonApiDocument.addIncluded(new PersistentResource<>(child, pRec, "children", userScope.getUUIDFor(child), userScope).toResource());
    String expected = "{\"data\":{" + "\"type\":\"parent\"," + "\"id\":\"123\"," + "\"attributes\":{\"firstName\":\"bob\"}," + "\"relationships\":{" + "\"children\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/relationships/children\",\"related\":\"http://localhost:8080/json/parent/123/children\"}," + "\"data\":[{\"type\":\"child\",\"id\":\"2\"}]}," + "\"spouses\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/relationships/spouses\",\"related\":\"http://localhost:8080/json/parent/123/spouses\"}," + "\"data\":[]}}," + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123\"}}," + "\"included\":[{" + "\"type\":\"child\"," + "\"id\":\"2\"," + "\"attributes\":{\"name\":null}," + "\"relationships\":{" + "\"friends\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/children/2/relationships/friends\",\"related\":\"http://localhost:8080/json/parent/123/children/2/friends\"}," + "\"data\":[]}," + "\"parents\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/children/2/relationships/parents\",\"related\":\"http://localhost:8080/json/parent/123/children/2/parents\"}," + "\"data\":[{\"type\":\"parent\",\"id\":\"123\"}]}}," + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/children/2\"}}]}";
    Data<Resource> data = jsonApiDocument.getData();
    String doc = mapper.writeJsonApiDocument(jsonApiDocument);
    assertEquals(data, jsonApiDocument.getData());
    assertEquals(expected, doc);
    checkEquality(jsonApiDocument);
}
Also used : TestRequestScope(com.yahoo.elide.core.TestRequestScope) PersistentResource(com.yahoo.elide.core.PersistentResource) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Parent(example.Parent) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Child(example.Child) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Test(org.junit.jupiter.api.Test)

Example 27 with TestRequestScope

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

the class JsonApiTest method writeSingleNoAttributesNoRel.

@Test
public void writeSingleNoAttributesNoRel() throws JsonProcessingException {
    Parent parent = new Parent();
    parent.setId(123L);
    RequestScope userScope = new TestRequestScope(BASE_URL, tx, user, dictionary);
    JsonApiDocument jsonApiDocument = new JsonApiDocument();
    jsonApiDocument.setData(new Data<>(new PersistentResource<>(parent, userScope.getUUIDFor(parent), userScope).toResource()));
    String expected = "{\"data\":{" + "\"type\":\"parent\"," + "\"id\":\"123\"," + "\"attributes\":{\"firstName\":null}," + "\"relationships\":{" + "\"children\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/relationships/children\",\"related\":\"http://localhost:8080/json/parent/123/children\"}," + "\"data\":[]}," + "\"spouses\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/relationships/spouses\",\"related\":\"http://localhost:8080/json/parent/123/spouses\"}," + "\"data\":[]}}," + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123\"}}}";
    Data<Resource> data = jsonApiDocument.getData();
    String doc = mapper.writeJsonApiDocument(jsonApiDocument);
    assertEquals(data, jsonApiDocument.getData());
    assertEquals(expected, doc);
    checkEquality(jsonApiDocument);
}
Also used : TestRequestScope(com.yahoo.elide.core.TestRequestScope) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Parent(example.Parent) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Test(org.junit.jupiter.api.Test)

Example 28 with TestRequestScope

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

the class JsonApiTest method compareOrder.

@Test
public void compareOrder() {
    Parent parent1 = new Parent();
    parent1.setId(123L);
    Parent parent2 = new Parent();
    parent2.setId(456L);
    RequestScope userScope = new TestRequestScope(BASE_URL, tx, user, dictionary);
    PersistentResource<Parent> pRec1 = new PersistentResource<>(parent1, userScope.getUUIDFor(parent1), userScope);
    PersistentResource<Parent> pRec2 = new PersistentResource<>(parent2, userScope.getUUIDFor(parent2), userScope);
    JsonApiDocument jsonApiDocument1 = new JsonApiDocument();
    jsonApiDocument1.setData(new Data<>(Lists.newArrayList(pRec1.toResource(), pRec2.toResource())));
    JsonApiDocument jsonApiDocument2 = new JsonApiDocument();
    jsonApiDocument2.setData(new Data<>(Lists.newArrayList(pRec2.toResource(), pRec1.toResource())));
    assertEquals(jsonApiDocument1, jsonApiDocument2);
    assertEquals(jsonApiDocument1.hashCode(), jsonApiDocument2.hashCode());
    jsonApiDocument1.getData().sort((a, b) -> Integer.compare(a.hashCode(), b.hashCode()));
    jsonApiDocument2.getData().sort((a, b) -> Integer.compare(b.hashCode(), a.hashCode()));
    assertEquals(jsonApiDocument1, jsonApiDocument2);
    assertEquals(jsonApiDocument1.hashCode(), jsonApiDocument2.hashCode());
}
Also used : TestRequestScope(com.yahoo.elide.core.TestRequestScope) PersistentResource(com.yahoo.elide.core.PersistentResource) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Parent(example.Parent) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Test(org.junit.jupiter.api.Test)

Example 29 with TestRequestScope

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

the class IncludedProcessorTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    includedProcessor = new IncludedProcessor();
    dictionary = TestDictionary.getTestDictionary();
    dictionary.bindEntity(Child.class);
    dictionary.bindEntity(Parent.class);
    dictionary.bindEntity(FunWithPermissions.class);
    reset(mockTransaction);
    testScope = new TestRequestScope(mockTransaction, new TestUser("1"), dictionary);
    // Create objects
    Parent parent1 = newParent(1);
    Parent parent2 = newParent(2);
    Parent parent3 = newParent(3);
    Child child1 = newChild(2);
    Child child2 = newChild(3);
    Child child3 = newChild(4);
    Child child4 = newChild(5);
    FunWithPermissions funWithPermissions = newFunWithPermissions(1);
    // Form relationships
    parent1.setSpouses(new HashSet<>(Collections.singletonList(parent2)));
    parent1.setChildren(new HashSet<>(Collections.singletonList(child1)));
    parent2.setChildren(new HashSet<>(Collections.singletonList(child2)));
    child1.setFriends(new HashSet<>(Collections.singletonList(child2)));
    // Parent with multiple children each with a friend
    parent3.setChildren(new HashSet<>(Arrays.asList(child3, child4)));
    child3.setFriends(new HashSet<>(Collections.singletonList(child1)));
    child4.setFriends(new HashSet<>(Collections.singletonList(child2)));
    // Create Persistent Resource
    parentRecord1 = new PersistentResource<>(parent1, String.valueOf(parent1.getId()), testScope);
    parentRecord2 = new PersistentResource<>(parent2, String.valueOf(parent2.getId()), testScope);
    parentRecord3 = new PersistentResource<>(parent3, String.valueOf(parent3.getId()), testScope);
    childRecord1 = new PersistentResource<>(child1, String.valueOf(child1.getId()), testScope);
    childRecord2 = new PersistentResource<>(child2, String.valueOf(child2.getId()), testScope);
    childRecord3 = new PersistentResource<>(child3, String.valueOf(child3.getId()), testScope);
    childRecord4 = new PersistentResource<>(child4, String.valueOf(child4.getId()), testScope);
    funWithPermissionsRecord = new PersistentResource<>(funWithPermissions, String.valueOf(funWithPermissions.getId()), testScope);
}
Also used : TestRequestScope(com.yahoo.elide.core.TestRequestScope) Parent(example.Parent) FunWithPermissions(example.FunWithPermissions) TestUser(com.yahoo.elide.core.security.TestUser) Child(example.Child) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 30 with TestRequestScope

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

the class UpdateOnCreateTest method updatePermissionOverwrittenForRelationFailureCase.

@Test
public void updatePermissionOverwrittenForRelationFailureCase() {
    com.yahoo.elide.core.RequestScope userTwoScope = new TestRequestScope(tx, userTwo, dictionary);
    UpdateAndCreate updateAndCreateExistingObject = new UpdateAndCreate();
    when(tx.loadObject(any(), eq(1L), any(com.yahoo.elide.core.RequestScope.class))).thenReturn(updateAndCreateExistingObject);
    when(tx.loadObject(any(), eq(2L), any(com.yahoo.elide.core.RequestScope.class))).thenReturn(new Author());
    com.yahoo.elide.core.PersistentResource<UpdateAndCreate> loaded = com.yahoo.elide.core.PersistentResource.loadRecord(EntityProjection.builder().type(UpdateAndCreate.class).build(), "1", userTwoScope);
    com.yahoo.elide.core.PersistentResource<Author> loadedAuthor = com.yahoo.elide.core.PersistentResource.loadRecord(EntityProjection.builder().type(Author.class).build(), "2", userTwoScope);
    assertThrows(ForbiddenAccessException.class, () -> loaded.addRelation("author", loadedAuthor));
}
Also used : TestRequestScope(com.yahoo.elide.core.TestRequestScope) Author(example.Author) UpdateAndCreate(example.UpdateAndCreate) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Aggregations

TestRequestScope (com.yahoo.elide.core.TestRequestScope)48 RequestScope (com.yahoo.elide.core.RequestScope)47 Test (org.junit.jupiter.api.Test)47 Book (example.Book)20 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)20 UpdateAndCreate (example.UpdateAndCreate)19 EntityProjection (com.yahoo.elide.core.request.EntityProjection)18 Publisher (example.Publisher)13 Author (example.Author)11 Editor (example.Editor)9 Collection (java.util.Collection)8 Parent (example.Parent)7 PersistentResource (com.yahoo.elide.core.PersistentResource)6 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)6 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)5 Resource (com.yahoo.elide.jsonapi.models.Resource)5 Child (example.Child)5 Price (example.Price)5 Path (com.yahoo.elide.core.Path)4 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)4