use of com.yahoo.elide.jsonapi.models.ResourceIdentifier in project elide by yahoo.
the class PersistentResourceTest method testRelationshipMissingData.
/**
* Verify that Relationship toMany cannot contain null resources, but toOne can.
*
* @throws Exception
*/
@Test
public void testRelationshipMissingData() throws Exception {
User goodUser = new TestUser("1");
@SuppressWarnings("resource") DataStoreTransaction tx = mock(DataStoreTransaction.class);
RequestScope goodScope = new RequestScope(null, null, NO_VERSION, null, tx, goodUser, null, null, UUID.randomUUID(), elideSettings);
// null resource in toMany relationship is not valid
List<Resource> idList = new ArrayList<>();
idList.add(new ResourceIdentifier("child", "3").castToResource());
idList.add(new ResourceIdentifier("child", "6").castToResource());
idList.add(null);
assertThrows(NullPointerException.class, () -> new Relationship(Collections.emptyMap(), new Data<>(idList)));
// However null toOne relationship is valid
Relationship toOneRelationship = new Relationship(Collections.emptyMap(), new Data<>((Resource) null));
assertTrue(toOneRelationship.getData().get().isEmpty());
assertNull(toOneRelationship.toPersistentResources(goodScope));
// no Data
Relationship nullRelationship = new Relationship(Collections.emptyMap(), null);
assertNull(nullRelationship.getData());
assertNull(nullRelationship.toPersistentResources(goodScope));
}
use of com.yahoo.elide.jsonapi.models.ResourceIdentifier in project elide by yahoo.
the class PersistentResourceTest method testTransferPermissionSuccessOnUpdateSingularRelationship.
@Test
public void testTransferPermissionSuccessOnUpdateSingularRelationship() {
example.User userModel = new example.User();
userModel.setId(1);
NoShareEntity noShare = new NoShareEntity();
/* The noshare already exists so no exception should be thrown */
userModel.setNoShare(noShare);
List<Resource> idList = new ArrayList<>();
idList.add(new ResourceIdentifier("noshare", "1").castToResource());
Relationship ids = new Relationship(null, new Data<>(idList));
when(tx.getToOneRelation(any(), eq(userModel), any(), any())).thenReturn(noShare);
when(tx.loadObject(any(), eq(1L), any())).thenReturn(noShare);
RequestScope goodScope = buildRequestScope(tx, goodUser);
PersistentResource<example.User> userResource = new PersistentResource<>(userModel, goodScope.getUUIDFor(userModel), goodScope);
boolean returnVal = userResource.updateRelation("noShare", ids.toPersistentResources(goodScope));
assertFalse(returnVal);
assertEquals(noShare, userModel.getNoShare());
}
use of com.yahoo.elide.jsonapi.models.ResourceIdentifier in project elide by yahoo.
the class JsonApiTest method readListIncluded.
@Test
public void readListIncluded() throws IOException {
String doc = "{\"data\":[{\"type\":\"parent\",\"id\":\"123\",\"attributes\":{\"firstName\":\"bob\"},\"relationships\":{\"children\":{\"links\":{\"self\":\"/parent/123/relationships/child\",\"related\":\"/parent/123/child\"},\"data\":{\"type\":\"child\",\"id\":\"2\"}}}}],\"included\":[{\"type\":\"child\",\"id\":\"2\",\"relationships\":{\"parents\":{\"links\":{\"self\":\"/parent/123/relationships/child\",\"related\":\"/parent/123/child\"},\"data\":{\"type\":\"parent\",\"id\":\"123\"}}}}]}";
JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
Data<Resource> list = jsonApiDocument.getData();
Resource data = list.get().iterator().next();
Map<String, Object> attributes = data.getAttributes();
List<Resource> included = jsonApiDocument.getIncluded();
Resource includedChild = IterableUtils.first(included);
ResourceIdentifier parent = includedChild.getRelationships().get("parents").getResourceIdentifierData().getSingleValue();
assertEquals("parent", data.getType());
assertEquals("123", data.getId());
assertEquals("bob", attributes.get("firstName"));
assertEquals("child", includedChild.getType());
assertEquals("2", includedChild.getId());
assertEquals("123", parent.getId());
checkEquality(jsonApiDocument);
}
use of com.yahoo.elide.jsonapi.models.ResourceIdentifier in project elide by yahoo.
the class PersistentResource method getRelationshipsWithRelationshipFunction.
/**
* Get relationship mappings.
*
* @param relationshipFunction a function to load the value of a relationship. Takes a string of the relationship
* name and returns the relationship's value.
* @return Relationship mapping
*/
protected Map<String, Relationship> getRelationshipsWithRelationshipFunction(final Function<String, Observable<PersistentResource>> relationshipFunction) {
final Map<String, Relationship> relationshipMap = new LinkedHashMap<>();
final Set<String> relationshipFields = filterFields(dictionary.getRelationships(obj));
for (String field : relationshipFields) {
TreeMap<String, Resource> orderedById = new TreeMap<>(lengthFirstComparator);
for (PersistentResource relationship : relationshipFunction.apply(field).toList().blockingGet()) {
orderedById.put(relationship.getId(), new ResourceIdentifier(relationship.getTypeName(), relationship.getId()).castToResource());
}
Observable<Resource> resources = Observable.fromIterable(orderedById.values());
Data<Resource> data;
RelationshipType relationshipType = getRelationshipType(field);
if (relationshipType.isToOne()) {
data = new Data<>(firstOrNullIfEmpty(resources));
} else {
data = new Data<>(resources);
}
Map<String, String> links = null;
if (requestScope.getElideSettings().isEnableJsonLinks()) {
links = requestScope.getElideSettings().getJsonApiLinks().getRelationshipLinks(this, field);
}
relationshipMap.put(field, new Relationship(links, data));
}
return relationshipMap;
}
Aggregations