private Supplier<Pair<Integer, JsonNode>> handleRequest(StateContext state, BiFunction<Data<Resource>, RequestScope, Boolean> handler) { Data<Resource> data = state.getJsonApiDocument().getData(); handler.apply(data, state.getRequestScope()); // TODO: figure out if we've made modifications that differ from those requested by client return () -> Pair.of(HttpStatus.SC_NO_CONTENT, null); }
private Supplier<Pair<Integer, JsonNode>> handleRequest(StateContext state, BiFunction<Data<Resource>, RequestScope, Boolean> handler) { Data<Resource> data = state.getJsonApiDocument().getData(); handler.apply(data, state.getRequestScope()); // TODO: figure out if we've made modifications that differ from those requested by client return () -> Pair.of(HttpStatus.SC_NO_CONTENT, null); }
/** * Clear all relationships for all resources in document. */ private static void clearAllExceptRelationships(JsonApiDocument doc) { Data<Resource> data = doc.getData(); if (data == null || data.get() == null) { return; } data.get().forEach(JsonApiPatch::clearAllExceptRelationships); }
/** * Clear all relationships for all resources in document. */ private static void clearAllExceptRelationships(JsonApiDocument doc) { Data<Resource> data = doc.getData(); if (data == null || data.get() == null) { return; } data.get().forEach(JsonApiPatch::clearAllExceptRelationships); }
@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); } }
JsonApiMapper mapper = requestScope.getMapper(); Data<Resource> data = doc.getData(); Collection<Resource> resources = data.get();
@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); }
/** * Remove data via patch extension. */ private Supplier<Pair<Integer, JsonNode>> handleRemoveOp(String path, JsonNode patchValue, PatchRequestScope requestScope) { try { JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchValue); String fullPath; if (path.contains("relationships")) { // Reserved keyword for relationships fullPath = path; } else { Data<Resource> data = value.getData(); if (data == null || data.get() == null) { fullPath = path; } else { Collection<Resource> resources = data.get(); String id = getSingleResource(resources).getId(); fullPath = path + "/" + id; } } DeleteVisitor visitor = new DeleteVisitor( new PatchRequestScope(path, value, requestScope)); return visitor.visit(JsonApiParser.parse(fullPath)); } catch (IOException e) { throw new InvalidEntityBodyException("Could not parse patch extension value: " + patchValue); } }
/** * Remove data via patch extension. */ private Supplier<Pair<Integer, JsonNode>> handleRemoveOp(String path, JsonNode patchValue, PatchRequestScope requestScope) { try { JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchValue); String fullPath; if (path.contains("relationships")) { // Reserved keyword for relationships fullPath = path; } else { Data<Resource> data = value.getData(); if (data == null || data.get() == null) { fullPath = path; } else { Collection<Resource> resources = data.get(); String id = getSingleResource(resources).getId(); fullPath = path + "/" + id; } } DeleteVisitor visitor = new DeleteVisitor( new PatchRequestScope(path, value, requestScope)); return visitor.visit(JsonApiParser.parse(fullPath)); } catch (IOException e) { throw new InvalidEntityBodyException("Could not parse patch extension value: " + patchValue); } }
@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); }
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) {
try { JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchValue); Data<Resource> data = value.getData(); if (data == null || data.get() == null) { throw new InvalidEntityBodyException("Expected an entity body but received none.");
@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); }
Data<Resource> data = doc.getData(); Resource resource = data.getSingleValue(); Iterator<Resource> itr = resource.getRelationships().get("children").getData().get().iterator();
try { JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchValue); Data<Resource> data = value.getData(); if (data == null || data.get() == null) { throw new InvalidEntityBodyException("Expected an entity body but received none.");
Data<Resource> list = doc.getData(); Iterator<Resource> itr = list.get().iterator(); String rel1 = itr.next().getId();