Search in sources :

Example 1 with DELETE

use of com.liferay.apio.architect.operation.Method.DELETE in project com-liferay-apio-architect by liferay.

the class MockSingleModelWriter method write.

/**
 * Writes a {@link RootModel}, with the hierarchy of embedded models and
 * multiple fields.
 *
 * @param httpHeaders the request's {@code HttpHeaders}
 * @param singleModelMessageMapper the {@link SingleModelMessageMapper} to
 *        use for writing the JSON object
 */
public static JsonObject write(HttpHeaders httpHeaders, SingleModelMessageMapper<RootModel> singleModelMessageMapper) {
    RequestInfo requestInfo = getRequestInfo(httpHeaders);
    Operation deleteOperation = new Operation(DELETE, "delete-operation");
    Operation putOperation = new Operation(createForm("u", "r"), PUT, "update-operation");
    SingleModel<RootModel> singleModel = new SingleModel<>(() -> "first", "root", asList(deleteOperation, putOperation));
    SingleModelWriter<RootModel> singleModelWriter = SingleModelWriter.create(builder -> builder.singleModel(singleModel).modelMessageMapper(singleModelMessageMapper).pathFunction(MockWriterUtil::identifierToPath).resourceNameFunction(__ -> Optional.of("models")).representorFunction(MockWriterUtil::getRepresentorOptional).requestInfo(requestInfo).singleModelFunction(MockWriterUtil::getSingleModel).build());
    Optional<String> optional = singleModelWriter.write();
    if (!optional.isPresent()) {
        throw new AssertionError("Writer failed to write");
    }
    return new Gson().fromJson(optional.get(), JsonObject.class);
}
Also used : SingleModel(com.liferay.apio.architect.single.model.SingleModel) JsonObject(com.google.gson.JsonObject) MockFormCreator.createForm(com.liferay.apio.architect.test.util.form.MockFormCreator.createForm) RequestInfo(com.liferay.apio.architect.request.RequestInfo) SingleModelMessageMapper(com.liferay.apio.architect.message.json.SingleModelMessageMapper) Operation(com.liferay.apio.architect.operation.Operation) HttpHeaders(javax.ws.rs.core.HttpHeaders) SingleModelWriter(com.liferay.apio.architect.writer.SingleModelWriter) Arrays.asList(java.util.Arrays.asList) Gson(com.google.gson.Gson) RootModel(com.liferay.apio.architect.test.util.model.RootModel) DELETE(com.liferay.apio.architect.operation.Method.DELETE) Optional(java.util.Optional) MockWriterUtil.getRequestInfo(com.liferay.apio.architect.test.util.writer.MockWriterUtil.getRequestInfo) SingleModel(com.liferay.apio.architect.single.model.SingleModel) PUT(com.liferay.apio.architect.operation.Method.PUT) RootModel(com.liferay.apio.architect.test.util.model.RootModel) Gson(com.google.gson.Gson) Operation(com.liferay.apio.architect.operation.Operation) RequestInfo(com.liferay.apio.architect.request.RequestInfo) MockWriterUtil.getRequestInfo(com.liferay.apio.architect.test.util.writer.MockWriterUtil.getRequestInfo)

Example 2 with DELETE

use of com.liferay.apio.architect.operation.Method.DELETE in project com-liferay-apio-architect by liferay.

the class PageEndpointImpl method deleteCollectionItem.

@Override
public Response deleteCollectionItem(String id) throws Exception {
    Try<ItemRoutes<T, S>> itemRoutesTry = Try.fromOptional(_itemRoutesSupplier::get, notFound(_name));
    ThrowableConsumer<S> throwableConsumer = itemRoutesTry.mapOptional(ItemRoutes::getDeleteConsumerOptional, notAllowed(DELETE, _name, id)).map(function -> function.apply(_httpServletRequest)).getUnchecked();
    Path path = new Path(_name, id);
    throwableConsumer.accept(_identifierFunction.apply(path));
    return noContent().build();
}
Also used : Body(com.liferay.apio.architect.form.Body) ItemRoutes(com.liferay.apio.architect.routes.ItemRoutes) ExceptionSupplierUtil.notFound(com.liferay.apio.architect.endpoint.ExceptionSupplierUtil.notFound) NestedCollectionRoutes(com.liferay.apio.architect.routes.NestedCollectionRoutes) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ExceptionSupplierUtil.notAllowed(com.liferay.apio.architect.endpoint.ExceptionSupplierUtil.notAllowed) PageEndpoint(com.liferay.apio.architect.endpoint.PageEndpoint) Representor(com.liferay.apio.architect.representor.Representor) Identifier(com.liferay.apio.architect.identifier.Identifier) ThrowableFunction(com.liferay.apio.architect.function.throwable.ThrowableFunction) HttpServletRequest(javax.servlet.http.HttpServletRequest) Response.noContent(javax.ws.rs.core.Response.noContent) Page(com.liferay.apio.architect.pagination.Page) CollectionRoutes(com.liferay.apio.architect.routes.CollectionRoutes) POST(com.liferay.apio.architect.operation.Method.POST) DELETE(com.liferay.apio.architect.operation.Method.DELETE) RelatedCollection(com.liferay.apio.architect.related.RelatedCollection) SingleModel(com.liferay.apio.architect.single.model.SingleModel) NoSuchElementException(java.util.NoSuchElementException) PUT(com.liferay.apio.architect.operation.Method.PUT) Try(com.liferay.apio.architect.functional.Try) Predicate(java.util.function.Predicate) Path(com.liferay.apio.architect.uri.Path) Response(javax.ws.rs.core.Response) ThrowableConsumer(com.liferay.apio.architect.consumer.throwable.ThrowableConsumer) Optional(java.util.Optional) Path(com.liferay.apio.architect.uri.Path) ItemRoutes(com.liferay.apio.architect.routes.ItemRoutes)

Example 3 with DELETE

use of com.liferay.apio.architect.operation.Method.DELETE in project com-liferay-apio-architect by liferay.

the class ItemRoutesTest method _testItemRoutes.

private void _testItemRoutes(ItemRoutes<String, Long> itemRoutes) throws Exception {
    Optional<ItemRoutes<String, Long>> optional = Optional.of(itemRoutes);
    Map map = optional.flatMap(ItemRoutes::getFormOptional).map(form -> {
        assertThat(form.id, is("u/name"));
        return (Map) form.get(_body);
    }).get();
    Optional<String> valueOptional = _body.getValueOptional("key");
    assertThat(map.get("key"), is(valueOptional.get()));
    SingleModel<String> singleModel = optional.flatMap(ItemRoutes::getItemFunctionOptional).get().apply(null).apply(42L).getUnchecked();
    assertThat(singleModel.getResourceName(), is("name"));
    assertThat(singleModel.getModel(), is("Apio"));
    optional.flatMap(ItemRoutes::getDeleteConsumerOptional).get().apply(null).accept(42L);
    SingleModel<String> updatedSingleModel = optional.flatMap(ItemRoutes::getUpdateItemFunctionOptional).get().apply(null).apply(42L).apply(_body).getUnchecked();
    assertThat(updatedSingleModel.getResourceName(), is("name"));
    assertThat(updatedSingleModel.getModel(), is("Updated"));
    List<Operation> operations = updatedSingleModel.getOperations();
    assertThat(operations, hasSize(2));
    Operation firstOperation = operations.get(0);
    assertThat(firstOperation.getFormOptional(), is(emptyOptional()));
    assertThat(firstOperation.method, is(DELETE));
    assertThat(firstOperation.name, is("name/delete"));
    Operation secondOperation = operations.get(1);
    assertThat(secondOperation.getFormOptional(), is(optionalWithValue()));
    assertThat(secondOperation.method, is(PUT));
    assertThat(secondOperation.name, is("name/update"));
}
Also used : OptionalMatchers.optionalWithValue(com.spotify.hamcrest.optional.OptionalMatchers.optionalWithValue) UpdateItemFunction(com.liferay.apio.architect.alias.routes.UpdateItemFunction) Builder(com.liferay.apio.architect.routes.ItemRoutes.Builder) DeleteItemConsumer(com.liferay.apio.architect.alias.routes.DeleteItemConsumer) GetItemFunction(com.liferay.apio.architect.alias.routes.GetItemFunction) Body(com.liferay.apio.architect.form.Body) Set(java.util.Set) Test(org.junit.Test) FORM_BUILDER_FUNCTION(com.liferay.apio.architect.routes.RoutesTestUtil.FORM_BUILDER_FUNCTION) REQUEST_PROVIDE_FUNCTION(com.liferay.apio.architect.routes.RoutesTestUtil.REQUEST_PROVIDE_FUNCTION) OptionalMatchers.emptyOptional(com.spotify.hamcrest.optional.OptionalMatchers.emptyOptional) Operation(com.liferay.apio.architect.operation.Operation) TreeSet(java.util.TreeSet) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) Map(java.util.Map) ITEM_PERMISSION_FUNCTION(com.liferay.apio.architect.routes.RoutesTestUtil.ITEM_PERMISSION_FUNCTION) Is.is(org.hamcrest.core.Is.is) DELETE(com.liferay.apio.architect.operation.Method.DELETE) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Optional(java.util.Optional) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SingleModel(com.liferay.apio.architect.single.model.SingleModel) PUT(com.liferay.apio.architect.operation.Method.PUT) Operation(com.liferay.apio.architect.operation.Operation) Map(java.util.Map)

Aggregations

DELETE (com.liferay.apio.architect.operation.Method.DELETE)3 PUT (com.liferay.apio.architect.operation.Method.PUT)3 SingleModel (com.liferay.apio.architect.single.model.SingleModel)3 Optional (java.util.Optional)3 Body (com.liferay.apio.architect.form.Body)2 Operation (com.liferay.apio.architect.operation.Operation)2 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 DeleteItemConsumer (com.liferay.apio.architect.alias.routes.DeleteItemConsumer)1 GetItemFunction (com.liferay.apio.architect.alias.routes.GetItemFunction)1 UpdateItemFunction (com.liferay.apio.architect.alias.routes.UpdateItemFunction)1 ThrowableConsumer (com.liferay.apio.architect.consumer.throwable.ThrowableConsumer)1 ExceptionSupplierUtil.notAllowed (com.liferay.apio.architect.endpoint.ExceptionSupplierUtil.notAllowed)1 ExceptionSupplierUtil.notFound (com.liferay.apio.architect.endpoint.ExceptionSupplierUtil.notFound)1 PageEndpoint (com.liferay.apio.architect.endpoint.PageEndpoint)1 ThrowableFunction (com.liferay.apio.architect.function.throwable.ThrowableFunction)1 Try (com.liferay.apio.architect.functional.Try)1 Identifier (com.liferay.apio.architect.identifier.Identifier)1 SingleModelMessageMapper (com.liferay.apio.architect.message.json.SingleModelMessageMapper)1 POST (com.liferay.apio.architect.operation.Method.POST)1