Search in sources :

Example 71 with PersistentResource

use of com.yahoo.elide.core.PersistentResource 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 72 with PersistentResource

use of com.yahoo.elide.core.PersistentResource 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 73 with PersistentResource

use of com.yahoo.elide.core.PersistentResource 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 74 with PersistentResource

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

the class IncludedProcessorTest method testExecuteSingleRelationOnCollection.

@Test
public void testExecuteSingleRelationOnCollection() throws Exception {
    JsonApiDocument jsonApiDocument = new JsonApiDocument();
    Set<PersistentResource> parents = new HashSet<>();
    parents.add(parentRecord1);
    parents.add(parentRecord2);
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.put(INCLUDE, Collections.singletonList("children"));
    testScope.setQueryParams(queryParams);
    includedProcessor.execute(jsonApiDocument, parents, queryParams);
    List<Resource> expectedIncluded = Arrays.asList(childRecord1.toResource(), childRecord2.toResource());
    List<Resource> actualIncluded = jsonApiDocument.getIncluded();
    assertEquals(expectedIncluded, actualIncluded, "Included Processor added requested resource from all records");
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) PersistentResource(com.yahoo.elide.core.PersistentResource) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 75 with PersistentResource

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

the class LifeCycleTest method testRelationshipUpdate.

@Test
public void testRelationshipUpdate() {
    FieldTestModel mockModel = mock(FieldTestModel.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    RequestScope scope = buildRequestScope(dictionary, tx);
    when(tx.createNewObject(ClassType.of(FieldTestModel.class), scope)).thenReturn(mockModel);
    FieldTestModel modelToAdd = mock(FieldTestModel.class);
    PersistentResource resource = new PersistentResource(mockModel, scope.getUUIDFor(mockModel), scope);
    PersistentResource resourceToAdd = new PersistentResource(modelToAdd, scope.getUUIDFor(mockModel), scope);
    resource.addRelation("models", resourceToAdd);
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PRESECURITY));
    verify(mockModel, times(1)).relationCallback(any(), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(UPDATE), eq(PRESECURITY), notNull());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreSecurityTriggers();
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreFlushTriggers();
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PREFLUSH));
    verify(mockModel, times(1)).relationCallback(any(), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(UPDATE), eq(PREFLUSH), notNull());
    clearInvocations(mockModel);
    scope.runQueuedPreCommitTriggers();
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PRECOMMIT));
    verify(mockModel, times(1)).relationCallback(any(), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(UPDATE), eq(PRECOMMIT), notNull());
    clearInvocations(mockModel);
    scope.getPermissionExecutor().executeCommitChecks();
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, times(1)).classCallback(any(), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(POSTCOMMIT));
    verify(mockModel, times(1)).relationCallback(any(), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(UPDATE), eq(POSTCOMMIT), notNull());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Aggregations

PersistentResource (com.yahoo.elide.core.PersistentResource)100 Test (org.junit.jupiter.api.Test)71 RequestScope (com.yahoo.elide.core.RequestScope)60 ReadPermission (com.yahoo.elide.annotation.ReadPermission)18 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)18 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)17 Include (com.yahoo.elide.annotation.Include)16 Entity (javax.persistence.Entity)16 Resource (com.yahoo.elide.jsonapi.models.Resource)13 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)10 NotFilterExpression (com.yahoo.elide.core.filter.expression.NotFilterExpression)10 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)10 PermissionExecutor (com.yahoo.elide.core.security.PermissionExecutor)10 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)10 Book (example.Book)10 LinkedHashSet (java.util.LinkedHashSet)9 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)8 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)8 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)8 RSQLFilterDialect (com.yahoo.elide.core.filter.dialect.RSQLFilterDialect)7