IncludedProcessor implements DocumentProcessor { @Override public void execute(JsonApiDocument jsonApiDocument, PersistentResource resource, Optional<MultivaluedMap<String, String>> queryParams) { if (isPresent(queryParams, INCLUDE)) { addIncludedResources(jsonApiDocument, resource, queryParams.get().get(INCLUDE)); } }  @Override void execute(JsonApiDocument jsonApiDocument, PersistentResource resource,
                        Optional<MultivaluedMap<String, String>> queryParams); @Override void execute(JsonApiDocument jsonApiDocument, Set<PersistentResource> resources,
                        Optional<MultivaluedMap<String, String>> queryParams);  }
@Test public void testExecuteSingleRelation() throws Exception { JsonApiDocument jsonApiDocument = new JsonApiDocument(); MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>(); queryParams.put(INCLUDE, Collections.singletonList("children")); includedProcessor.execute(jsonApiDocument, parentRecord1, Optional.of(queryParams)); List<Resource> expectedIncluded = Collections.singletonList(childRecord1.toResource()); List<Resource> actualIncluded = jsonApiDocument.getIncluded(); assertEquals(expectedIncluded, actualIncluded, "Included Processor added single requested resource 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(); assertEquals(expectedIncluded, actualIncluded, "Included Processor added requested resource from all records"); }
@Test public void testExecuteSingleNestedRelation() throws Exception { JsonApiDocument jsonApiDocument = new JsonApiDocument(); MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>(); queryParams.put(INCLUDE, Collections.singletonList("children.friends")); includedProcessor.execute(jsonApiDocument, parentRecord1, Optional.of(queryParams)); List<Resource> expectedIncluded = Arrays.asList(childRecord1.toResource(), childRecord2.toResource()); List<Resource> actualIncluded = jsonApiDocument.getIncluded(); assertEquals(expectedIncluded, actualIncluded, "Included Processor added single nested requested resources from 'include' query param"); }
@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(); assertEquals(expectedIncluded, actualIncluded, "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()); assertEquals(expectedIncluded, actualIncluded, "Included Processor added multiple nested requested resource collections from 'include' query param"); }
@Test public void testIncludeForbiddenRelationship() { JsonApiDocument jsonApiDocument = new JsonApiDocument(); MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>(); queryParams.put(INCLUDE, Collections.singletonList("relation1")); includedProcessor.execute(jsonApiDocument, funWithPermissionsRecord, Optional.of(queryParams)); assertNull(jsonApiDocument.getIncluded(), "Included Processor included forbidden relationship"); }
@Test public void testNoQueryParams() throws Exception { JsonApiDocument jsonApiDocument = new JsonApiDocument(); includedProcessor.execute(jsonApiDocument, parentRecord1, Optional.empty()); assertNull(jsonApiDocument.getIncluded(), "Included Processor adds no resources when not given query params"); }
@Test public void testNoQueryIncludeParams() throws Exception { JsonApiDocument jsonApiDocument = new JsonApiDocument(); MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>(); queryParams.put("unused", Collections.emptyList()); includedProcessor.execute(jsonApiDocument, parentRecord1, Optional.of(queryParams)); assertNull(jsonApiDocument.getIncluded(), "Included Processor adds no resources when not given query params"); }