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; } }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Data<Resource> that = ((Relationship) o).getData(); if (that == null || data == null) { return that == data; } Collection<ResourceIdentifier> resourceIdentifiers = data.toResourceIdentifiers(); Collection<ResourceIdentifier> theirIdentifiers = that.toResourceIdentifiers(); return resourceIdentifiers.stream().allMatch(theirIdentifiers::contains); }
@Override public void serialize(Data<Resource> data, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { Collection<Resource> list = data.get(); if (data.isToOne()) { if (list == null || list.isEmpty()) { jsonGenerator.writeObject(null); return; } jsonGenerator.writeObject(list.iterator().next()); return; } jsonGenerator.writeObject((list == null) ? Collections.emptyList() : list); } }
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; }
@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); }
@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 Data getData(Set<PersistentResource> collection) { Preconditions.checkNotNull(collection); List<Resource> resources = collection.stream().map(PersistentResource::toResource).collect(Collectors.toList()); return new Data<>(resources); }
/** * Fetch the item if the data is toOne. * * @return T if toOne * @throws IllegalAccessError when the data is not toOne */ public T getSingleValue() { if (isToOne()) { return ((SingleElementSet<T>) values).getValue(); } throw new IllegalAccessError("Data is not toOne"); }
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(); assertEquals(resource.getId(), "6"); boolean hasIdentifier = false; resource = parentJsonApiDocument.getData().getSingleValue(); resourceIdentifiers = resource.getRelationships().get("children").getResourceIdentifierData().get(); for (ResourceIdentifier resourceIdentifier : resourceIdentifiers) { hasIdentifier |= resourceIdentifier.getId().equals("6");
public Set<PersistentResource> toPersistentResources(RequestScope requestScope) throws ForbiddenAccessException, InvalidObjectIdentifierException { Set<PersistentResource> res = new LinkedHashSet<>(); if (data == null) { return null; } Collection<Resource> resources = data.get(); if (resources != null) { for (Resource resource : resources) { try { if (data.isToOne() && resource == null) { continue; } res.add(resource.toPersistentResource(requestScope)); } catch (ForbiddenAccessException e) { //skip resource } } } return res.isEmpty() ? (data.isToOne() ? null : res) : res; }
@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 Data getData(Set<PersistentResource> collection) { Preconditions.checkNotNull(collection); List<Resource> resources = collection.stream().map(PersistentResource::toResource).collect(Collectors.toList()); return new Data<>(resources); }
/** * Fetch the item if the data is toOne. * * @return T if toOne * @throws IllegalAccessError when the data is not toOne */ public T getSingleValue() { if (isToOne()) { return ((SingleElementSet<T>) values).getValue(); } throw new IllegalAccessError("Data is not toOne"); }
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; } }
Resource resource = data.getSingleValue(); Iterator<Resource> itr = resource.getRelationships().get("children").getData().get().iterator(); String rel1 = itr.next().getId(); String rel2 = itr.next().getId();
@Override public void serialize(Data<Resource> data, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { Collection<Resource> list = data.get(); if (data.isToOne()) { if (list == null || list.isEmpty()) { jsonGenerator.writeObject(null); return; } jsonGenerator.writeObject(list.iterator().next()); return; } jsonGenerator.writeObject((list == null) ? Collections.emptyList() : list); } }
@Override public Data<Resource> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { JsonNode node = jsonParser.getCodec().readTree(jsonParser); if (node.isArray()) { List<Resource> resources = new ArrayList<>(); for (JsonNode n : node) { Resource r = MAPPER.convertValue(n, Resource.class); resources.add(r); } return new Data<>(resources); } Resource resource = MAPPER.convertValue(node, Resource.class); return new Data<>(resource); } }