use of com.yahoo.elide.jsonapi.models.Relationship in project elide by yahoo.
the class JsonApiTest method readSingle.
@Test
public void readSingle() throws IOException {
String doc = "{\"data\":{\"type\":\"parent\",\"id\":\"123\",\"attributes\":{\"firstName\":\"bob\"},\"relationships\":{\"children\":{\"data\":{\"type\":\"child\",\"id\":\"2\"}}}}}";
JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
Data<Resource> dataObj = jsonApiDocument.getData();
Resource data = dataObj.getSingleValue();
Map<String, Object> attributes = data.getAttributes();
Map<String, Relationship> relations = data.getRelationships();
assertEquals("parent", data.getType());
assertEquals("123", data.getId());
assertEquals("bob", attributes.get("firstName"));
assertEquals("child", relations.get("children").getData().getSingleValue().getType());
assertEquals("2", relations.get("children").getData().getSingleValue().getId());
checkEquality(jsonApiDocument);
}
use of com.yahoo.elide.jsonapi.models.Relationship in project elide by yahoo.
the class JsonApiTest method readSingleWithMeta.
@Test
public void readSingleWithMeta() throws IOException {
String doc = "{\"data\":{\"type\":\"parent\",\"id\":\"123\",\"attributes\":{\"firstName\":\"bob\"},\"relationships\":{\"children\":{\"data\":{\"type\":\"child\",\"id\":\"2\"}}}},\"meta\":{\"additional\":\"info\"}}";
JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
Meta meta = jsonApiDocument.getMeta();
Data<Resource> dataObj = jsonApiDocument.getData();
Resource data = dataObj.getSingleValue();
Map<String, Object> attributes = data.getAttributes();
Map<String, Relationship> relations = data.getRelationships();
assertEquals(meta.getMetaMap().get("additional"), "info");
assertEquals(data.getType(), "parent");
assertEquals(data.getId(), "123");
assertEquals(attributes.get("firstName"), "bob");
assertEquals(relations.get("children").getData().getSingleValue().getType(), "child");
assertEquals(relations.get("children").getData().getSingleValue().getId(), "2");
checkEquality(jsonApiDocument);
}
use of com.yahoo.elide.jsonapi.models.Relationship 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