@Test public void testExecuteMultipleRelations() throws Exception { JsonApiDocument jsonApiDocument = new JsonApiDocument(); MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>(); queryParams.put("include", Arrays.asList("children", "spouses")); includedProcessor.execute(jsonApiDocument, parentRecord1, Optional.of(queryParams)); List<Resource> expectedIncluded = Arrays.asList(childRecord1.toResource(), parentRecord2.toResource()); List<Resource> actualIncluded = jsonApiDocument.getIncluded(); Assert.assertEquals( actualIncluded, expectedIncluded, "Included Processor added single requested resource from 'include' query param"); }
@Test public void testExecuteMultipleNestedRelations() throws Exception { JsonApiDocument jsonApiDocument = new JsonApiDocument(); MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>(); queryParams.put("include", Collections.singletonList("children.friends")); includedProcessor.execute(jsonApiDocument, parentRecord3, Optional.of(queryParams)); Set<Resource> expectedIncluded = Sets.newHashSet( childRecord1.toResource(), childRecord2.toResource(), childRecord3.toResource(), childRecord4.toResource()); Set<Resource> actualIncluded = new HashSet<>(jsonApiDocument.getIncluded()); Assert.assertEquals( actualIncluded, expectedIncluded, "Included Processor added multiple nested requested resource collections from 'include' query param"); }
@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")); includedProcessor.execute(jsonApiDocument, parents, Optional.of(queryParams)); List<Resource> expectedIncluded = Arrays.asList(childRecord1.toResource(), childRecord2.toResource()); List<Resource> actualIncluded = jsonApiDocument.getIncluded(); Assert.assertEquals( actualIncluded, expectedIncluded, "Included Processor added requested resource from all records"); }
@Override public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) { JsonApiDocument doc = new JsonApiDocument(); RequestScope requestScope = state.getRequestScope(); ObjectMapper mapper = requestScope.getMapper().getObjectMapper(); Optional<MultivaluedMap<String, String>> queryParams = requestScope.getQueryParams(); Map<String, Relationship> relationships = record.toResource().getRelationships(); Relationship relationship = null; if (relationships != null) { relationship = relationships.get(relationshipName); } // Handle valid relationship if (relationship != null) { // Set data Data<Resource> data = relationship.getData(); doc.setData(data); // Run include processor DocumentProcessor includedProcessor = new IncludedProcessor(); includedProcessor.execute(doc, record, queryParams); return () -> Pair.of(HttpStatus.SC_OK, mapper.convertValue(doc, JsonNode.class)); } // Handle no data for relationship if (relationshipType.isToMany()) { doc.setData(new Data<>(new ArrayList<>())); } else if (relationshipType.isToOne()) { doc.setData(new Data<>((Resource) null)); } else { throw new IllegalStateException("Failed to PATCH a relationship"); } return () -> Pair.of(HttpStatus.SC_OK, mapper.convertValue(doc, JsonNode.class)); }