use of com.yahoo.elide.jsonapi.extensions.PatchRequestScope in project elide by yahoo.
the class Elide method patch.
/**
* Handle PATCH.
*
* @param baseUrlEndPoint base URL with prefix endpoint
* @param contentType the content type
* @param accept the accept
* @param path the path
* @param jsonApiDocument the json api document
* @param queryParams the query params
* @param requestHeaders the request headers
* @param opaqueUser the opaque user
* @param apiVersion the API version
* @param requestId the request ID
* @return Elide response object
*/
public ElideResponse patch(String baseUrlEndPoint, String contentType, String accept, String path, String jsonApiDocument, MultivaluedMap<String, String> queryParams, Map<String, List<String>> requestHeaders, User opaqueUser, String apiVersion, UUID requestId) {
Handler<DataStoreTransaction, User, HandlerResult> handler;
if (JsonApiPatch.isPatchExtension(contentType) && JsonApiPatch.isPatchExtension(accept)) {
handler = (tx, user) -> {
PatchRequestScope requestScope = new PatchRequestScope(baseUrlEndPoint, path, apiVersion, tx, user, requestId, queryParams, requestHeaders, elideSettings);
try {
Supplier<Pair<Integer, JsonNode>> responder = JsonApiPatch.processJsonPatch(dataStore, path, jsonApiDocument, requestScope);
return new HandlerResult(requestScope, responder);
} catch (RuntimeException e) {
return new HandlerResult(requestScope, e);
}
};
} else {
handler = (tx, user) -> {
JsonApiDocument jsonApiDoc = mapper.readJsonApiDocument(jsonApiDocument);
RequestScope requestScope = new RequestScope(baseUrlEndPoint, path, apiVersion, jsonApiDoc, tx, user, queryParams, requestHeaders, requestId, elideSettings);
requestScope.setEntityProjection(new EntityProjectionMaker(elideSettings.getDictionary(), requestScope).parsePath(path));
BaseVisitor visitor = new PatchVisitor(requestScope);
return visit(path, requestScope, visitor);
};
}
return handleRequest(false, opaqueUser, dataStore::beginTransaction, requestId, handler);
}
use of com.yahoo.elide.jsonapi.extensions.PatchRequestScope in project elide by yahoo.
the class PersistentResourceTest method testPatchRequestScope.
@Test
public void testPatchRequestScope() {
DataStoreTransaction tx = mock(DataStoreTransaction.class);
PatchRequestScope parentScope = new PatchRequestScope(null, "/book", NO_VERSION, tx, new TestUser("1"), UUID.randomUUID(), null, Collections.emptyMap(), elideSettings);
PatchRequestScope scope = new PatchRequestScope(parentScope.getPath(), parentScope.getJsonApiDocument(), parentScope);
// verify wrap works
assertEquals(parentScope.getUpdateStatusCode(), scope.getUpdateStatusCode());
assertEquals(parentScope.getObjectEntityCache(), scope.getObjectEntityCache());
Parent parent = newParent(7);
PersistentResource<Parent> parentResource = new PersistentResource<>(parent, "1", scope);
parentResource.updateAttribute("firstName", "foobar");
ArgumentCaptor<Attribute> attributeArgument = ArgumentCaptor.forClass(Attribute.class);
verify(tx, times(1)).setAttribute(eq(parent), attributeArgument.capture(), eq(scope));
assertEquals(attributeArgument.getValue().getName(), "firstName");
assertEquals(attributeArgument.getValue().getArguments().iterator().next().getValue(), "foobar");
}
Aggregations