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; } }
@SuppressWarnings("unchecked") public Collection<ResourceIdentifier> toResourceIdentifiers() { return ((Collection<Resource>) get()).stream() .map(object -> object != null ? object.toResourceIdentifier() : null) .collect(Collectors.toList()); } }
@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); }
@SuppressWarnings("unchecked") public Collection<ResourceIdentifier> toResourceIdentifiers() { return ((Collection<Resource>) get()).stream() .map(object -> object != null ? object.toResourceIdentifier() : null) .collect(Collectors.toList()); } }
@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 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 boolean equals(Object obj) { if (!(obj instanceof JsonApiDocument)) { return false; } JsonApiDocument other = (JsonApiDocument) obj; Collection<Resource> resources = data.get(); if ((resources == null || other.getData().get() == null) && resources != other.getData().get()) { return false; } if (resources != null) { if (resources.size() != other.getData().get().size() || !resources.stream().allMatch(other.getData().get()::contains)) { return false; } } // TODO: Verify links and meta? if (other.getIncluded() == null) { return included.isEmpty(); } return included.stream().allMatch(other.getIncluded()::contains); } }
@Override public boolean equals(Object obj) { if (!(obj instanceof JsonApiDocument)) { return false; } JsonApiDocument other = (JsonApiDocument) obj; Collection<Resource> resources = data.get(); if ((resources == null || other.getData().get() == null) && resources != other.getData().get()) { return false; } if (resources != null) { if (resources.size() != other.getData().get().size() || !resources.stream().allMatch(other.getData().get()::contains)) { return false; } } // TODO: Verify links and meta? if (other.getIncluded() == null) { return included.isEmpty(); } return included.stream().allMatch(other.getIncluded()::contains); } }
private boolean post(Data<Resource> data, RequestScope requestScope) { if (data == null) { throw new InvalidEntityBodyException("Expected data but received null"); } Collection<Resource> resources = data.get(); if (resources == null) { return false; } resources.stream().forEachOrdered(resource -> record.addRelation(relationshipName, resource.toPersistentResource(requestScope))); return true; }
private boolean post(Data<Resource> data, RequestScope requestScope) { if (data == null) { throw new InvalidEntityBodyException("Expected data but received null"); } Collection<Resource> resources = data.get(); if (resources == null) { return false; } resources.stream().forEachOrdered(resource -> record.addRelation(relationshipName, resource.toPersistentResource(requestScope))); return true; }
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; }
private boolean delete(Data<Resource> data, RequestScope requestScope) { if (data == null) { throw new InvalidEntityBodyException("Expected data but received null"); } Collection<Resource> resources = data.get(); if (resources == null || resources.isEmpty()) { // As per: http://jsonapi.org/format/#crud-updating-relationship-responses-403 throw new ForbiddenAccessException("Unknown update"); } resources.stream().forEachOrdered(resource -> record.removeRelation(relationshipName, resource.toPersistentResource(requestScope))); return true; } }
private boolean delete(Data<Resource> data, RequestScope requestScope) { if (data == null) { throw new InvalidEntityBodyException("Expected data but received null"); } Collection<Resource> resources = data.get(); if (resources == null || resources.isEmpty()) { // As per: http://jsonapi.org/format/#crud-updating-relationship-responses-403 throw new ForbiddenAccessException("Unknown update"); } resources.stream().forEachOrdered(resource -> record.removeRelation(relationshipName, resource.toPersistentResource(requestScope))); return true; } }
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; }
@Test(dataProvider = "like_queries") public void testSpecialCharacterLikeQuery(String filterParam, int noOfRecords) throws Exception { String actual = given().when().get(String.format("/book?%s", filterParam)).then().statusCode(HttpStatus.SC_OK) .extract().body().asString(); JsonApiDocument doc = jsonApiMapper.readJsonApiDocument(actual); assertEquals(doc.getData().get().size(), noOfRecords); }
@Test(priority = -1) public void testRootCollectionWithNoOperatorFilter() throws Exception { String actual = given().when().get("/parent?filter[parent.id][isnull]").then().statusCode(HttpStatus.SC_OK) .extract().body().asString(); JsonApiDocument doc = jsonApiMapper.readJsonApiDocument(actual); assertEquals(doc.getData().get().size(), 0); }
@Test(priority = -1) public void testRootCollection() throws Exception { String actual = given().when().get("/parent").then().statusCode(HttpStatus.SC_OK) .extract().body().asString(); JsonApiDocument doc = jsonApiMapper.readJsonApiDocument(actual); assertEquals(doc.getData().get().size(), 4); }