Search in sources :

Example 1 with PUT

use of com.liferay.apio.architect.operation.Method.PUT 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 PUT

use of com.liferay.apio.architect.operation.Method.PUT 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)2 PUT (com.liferay.apio.architect.operation.Method.PUT)2 Operation (com.liferay.apio.architect.operation.Operation)2 SingleModel (com.liferay.apio.architect.single.model.SingleModel)2 Optional (java.util.Optional)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 Body (com.liferay.apio.architect.form.Body)1 SingleModelMessageMapper (com.liferay.apio.architect.message.json.SingleModelMessageMapper)1 RequestInfo (com.liferay.apio.architect.request.RequestInfo)1 Builder (com.liferay.apio.architect.routes.ItemRoutes.Builder)1 FORM_BUILDER_FUNCTION (com.liferay.apio.architect.routes.RoutesTestUtil.FORM_BUILDER_FUNCTION)1 ITEM_PERMISSION_FUNCTION (com.liferay.apio.architect.routes.RoutesTestUtil.ITEM_PERMISSION_FUNCTION)1 REQUEST_PROVIDE_FUNCTION (com.liferay.apio.architect.routes.RoutesTestUtil.REQUEST_PROVIDE_FUNCTION)1 MockFormCreator.createForm (com.liferay.apio.architect.test.util.form.MockFormCreator.createForm)1 RootModel (com.liferay.apio.architect.test.util.model.RootModel)1 MockWriterUtil.getRequestInfo (com.liferay.apio.architect.test.util.writer.MockWriterUtil.getRequestInfo)1