public Relationship(@JsonProperty("links") Map<String, String> links, @JsonProperty("data") Data<Resource> data) { this.links = links; this.data = data; if (data != null) { if (data.isToOne()) { Resource resource = data.getSingleValue(); this.idData = new Data<>(resource != null ? resource.toResourceIdentifier() : null); } else { this.idData = new Data<>( data.get().stream() .map(Resource::toResourceIdentifier) .collect(Collectors.toList()) ); } } else { this.idData = null; } }
public Relationship(@JsonProperty("links") Map<String, String> links, @JsonProperty("data") Data<Resource> data) { this.links = links; this.data = data; if (data != null) { if (data.isToOne()) { Resource resource = data.getSingleValue(); this.idData = new Data<>(resource != null ? resource.toResourceIdentifier() : null); } else { this.idData = new Data<>( data.get().stream() .map(Resource::toResourceIdentifier) .collect(Collectors.toList()) ); } } else { this.idData = null; } }
private boolean patch(Data<Resource> data, RequestScope requestScope) { boolean isUpdated; if (relationshipType.isToMany() && data == null) { throw new InvalidEntityBodyException("Expected data but received null"); } if (relationshipType.isToMany()) { Collection<Resource> resources = data.get(); if (resources == null) { return false; } if (!resources.isEmpty()) { isUpdated = record.updateRelation(relationshipName, new Relationship(null, new Data<>(resources)).toPersistentResources(requestScope)); } else { isUpdated = record.clearRelation(relationshipName); } } else if (relationshipType.isToOne()) { if (data != null) { Resource resource = data.getSingleValue(); Relationship relationship = new Relationship(null, new Data<>(resource)); isUpdated = record.updateRelation(relationshipName, relationship.toPersistentResources(requestScope)); } else { isUpdated = record.clearRelation(relationshipName); } } else { throw new IllegalStateException("Bad relationship type"); } return isUpdated; }
@Override public Supplier<Pair<Integer, JsonNode>> handlePatch(StateContext state) { JsonApiDocument jsonApiDocument = state.getJsonApiDocument(); Data<Resource> data = jsonApiDocument.getData(); if (data == null) { throw new InvalidEntityBodyException("Expected data but found null"); } if (!data.isToOne()) { throw new InvalidEntityBodyException("Expected single element but found list"); } Resource resource = data.getSingleValue(); if (!record.matchesId(resource.getId())) { throw new InvalidEntityBodyException("Id in response body does not match requested id to update from path"); } patch(resource, state.getRequestScope()); return constructPatchResponse(record, state); }
@Override public Supplier<Pair<Integer, JsonNode>> handlePatch(StateContext state) { JsonApiDocument jsonApiDocument = state.getJsonApiDocument(); Data<Resource> data = jsonApiDocument.getData(); if (data == null) { throw new InvalidEntityBodyException("Expected data but found null"); } if (!data.isToOne()) { throw new InvalidEntityBodyException("Expected single element but found list"); } Resource resource = data.getSingleValue(); if (!record.matchesId(resource.getId())) { throw new InvalidEntityBodyException("Id in response body does not match requested id to update from path"); } patch(resource, state.getRequestScope()); return constructPatchResponse(record, state); }
private boolean patch(Data<Resource> data, RequestScope requestScope) { boolean isUpdated; if (relationshipType.isToMany() && data == null) { throw new InvalidEntityBodyException("Expected data but received null"); } if (relationshipType.isToMany()) { Collection<Resource> resources = data.get(); if (resources == null) { return false; } if (!resources.isEmpty()) { isUpdated = record.updateRelation(relationshipName, new Relationship(null, new Data<>(resources)).toPersistentResources(requestScope)); } else { isUpdated = record.clearRelation(relationshipName); } } else if (relationshipType.isToOne()) { if (data != null) { Resource resource = data.getSingleValue(); Relationship relationship = new Relationship(null, new Data<>(resource)); isUpdated = record.updateRelation(relationshipName, relationship.toPersistentResources(requestScope)); } else { isUpdated = record.clearRelation(relationshipName); } } else { throw new IllegalStateException("Bad relationship type"); } return isUpdated; }
Resource resource = childJsonApiDocument.getData().getSingleValue(); Collection<ResourceIdentifier> resourceIdentifiers = resource.getRelationships().get("parents").getResourceIdentifierData().get(); ResourceIdentifier rId1 = resourceIdentifiers.iterator().next(); boolean hasIdentifier = false; resource = parentJsonApiDocument.getData().getSingleValue(); resourceIdentifiers = resource.getRelationships().get("children").getResourceIdentifierData().get(); for (ResourceIdentifier resourceIdentifier : resourceIdentifiers) {
@Test(priority = 1) public void testPatchAttrSingle() throws Exception { String request = jsonParser.getJson("/ResourceIT/testPatchAttrSingle.json"); given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .body(request) .patch("/parent/2") .then() .statusCode(HttpStatus.SC_NO_CONTENT) .header(HttpHeaders.CONTENT_LENGTH, (String) null); String actual = given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .get("/parent/2") .then() .statusCode(HttpStatus.SC_OK) .contentType(JSONAPI_CONTENT_TYPE) .extract().response().asString(); JsonApiDocument doc = jsonApiMapper.readJsonApiDocument(actual); Data<Resource> data = doc.getData(); Resource resource = data.getSingleValue(); assertEquals(resource.getAttributes().get("firstName"), "syzygy"); assertEquals(resource.getRelationships().size(), 2); assertEquals(resource.getRelationships().get("children").getData().get().size(), 2); }
Resource resource = data.getSingleValue(); Iterator<Resource> itr = resource.getRelationships().get("children").getData().get().iterator(); String rel1 = itr.next().getId();