Search in sources :

Example 1 with POST

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

the class CollectionRoutesTest method _testCollectionRoutes.

private void _testCollectionRoutes(CollectionRoutes<String> collectionRoutes) {
    Optional<CollectionRoutes<String>> optional = Optional.of(collectionRoutes);
    Map map = optional.flatMap(CollectionRoutes::getFormOptional).map(form -> {
        assertThat(form.id, is("c/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(CollectionRoutes::getCreateItemFunctionOptional).get().apply(null).apply(_body).getUnchecked();
    assertThat(singleModel.getResourceName(), is("name"));
    assertThat(singleModel.getModel(), is("Apio"));
    Page<String> page = optional.flatMap(CollectionRoutes::getGetPageFunctionOptional).get().apply(null).getUnchecked();
    assertThat(page.getItems(), hasSize(1));
    assertThat(page.getItems(), hasItem("Apio"));
    assertThat(page.getTotalCount(), is(1));
    List<Operation> operations = page.getOperations();
    assertThat(operations, hasSize(1));
    Operation operation = operations.get(0);
    assertThat(operation.getFormOptional(), is(optionalWithValue()));
    assertThat(operation.method, is(POST));
    assertThat(operation.name, is("name/create"));
}
Also used : CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) GetPageFunction(com.liferay.apio.architect.alias.routes.GetPageFunction) Body(com.liferay.apio.architect.form.Body) OptionalMatchers.emptyOptional(com.spotify.hamcrest.optional.OptionalMatchers.emptyOptional) Operation(com.liferay.apio.architect.operation.Operation) TreeSet(java.util.TreeSet) CreateItemFunction(com.liferay.apio.architect.alias.routes.CreateItemFunction) Builder(com.liferay.apio.architect.routes.CollectionRoutes.Builder) Pagination(com.liferay.apio.architect.pagination.Pagination) Page(com.liferay.apio.architect.pagination.Page) Map(java.util.Map) POST(com.liferay.apio.architect.operation.Method.POST) Is.is(org.hamcrest.core.Is.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SingleModel(com.liferay.apio.architect.single.model.SingleModel) OptionalMatchers.optionalWithValue(com.spotify.hamcrest.optional.OptionalMatchers.optionalWithValue) Set(java.util.Set) Test(org.junit.Test) PageItems(com.liferay.apio.architect.pagination.PageItems) FORM_BUILDER_FUNCTION(com.liferay.apio.architect.routes.RoutesTestUtil.FORM_BUILDER_FUNCTION) REQUEST_PROVIDE_FUNCTION(com.liferay.apio.architect.routes.RoutesTestUtil.REQUEST_PROVIDE_FUNCTION) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) List(java.util.List) PAGINATION(com.liferay.apio.architect.routes.RoutesTestUtil.PAGINATION) Matchers.contains(org.hamcrest.Matchers.contains) Optional(java.util.Optional) COLLECTION_PERMISSION_FUNCTION(com.liferay.apio.architect.routes.RoutesTestUtil.COLLECTION_PERMISSION_FUNCTION) Collections(java.util.Collections) Operation(com.liferay.apio.architect.operation.Operation) Map(java.util.Map)

Example 2 with POST

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

the class NestedCollectionRoutesTest method _testNestedCollectionRoutes.

private void _testNestedCollectionRoutes(NestedCollectionRoutes<String, Long> nestedCollectionRoutes) {
    Optional<NestedCollectionRoutes<String, Long>> optional = Optional.of(nestedCollectionRoutes);
    Map map = optional.flatMap(NestedCollectionRoutes::getFormOptional).map(form -> {
        assertThat(form.id, is("c/name/nested"));
        return (Map) form.get(_body);
    }).get();
    Optional<String> valueOptional = _body.getValueOptional("key");
    assertThat(map.get("key"), is(valueOptional.get()));
    SingleModel<String> singleModel = optional.flatMap(NestedCollectionRoutes::getNestedCreateItemFunctionOptional).get().apply(null).apply(42L).apply(_body).getUnchecked();
    assertThat(singleModel.getResourceName(), is("nested"));
    assertThat(singleModel.getModel(), is("Apio"));
    Path path = new Path("name", "42");
    Page<String> page = optional.flatMap(NestedCollectionRoutes::getNestedGetPageFunctionOptional).get().apply(null).apply(path).apply(42L).getUnchecked();
    assertThat(page.getItems(), hasSize(1));
    assertThat(page.getItems(), hasItem("Apio"));
    assertThat(page.getPathOptional(), optionalWithValue(equalTo(path)));
    assertThat(page.getTotalCount(), is(1));
    List<Operation> operations = page.getOperations();
    assertThat(operations, hasSize(1));
    Operation secondOperation = operations.get(0);
    assertThat(secondOperation.getFormOptional(), is(optionalWithValue()));
    assertThat(secondOperation.method, is(POST));
    assertThat(secondOperation.name, is("name/nested/create"));
}
Also used : CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Body(com.liferay.apio.architect.form.Body) OptionalMatchers.emptyOptional(com.spotify.hamcrest.optional.OptionalMatchers.emptyOptional) Operation(com.liferay.apio.architect.operation.Operation) TreeSet(java.util.TreeSet) Pagination(com.liferay.apio.architect.pagination.Pagination) Page(com.liferay.apio.architect.pagination.Page) Map(java.util.Map) POST(com.liferay.apio.architect.operation.Method.POST) Is.is(org.hamcrest.core.Is.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SingleModel(com.liferay.apio.architect.single.model.SingleModel) RoutesTestUtil.getNestedCollectionPermissionFunction(com.liferay.apio.architect.routes.RoutesTestUtil.getNestedCollectionPermissionFunction) OptionalMatchers.optionalWithValue(com.spotify.hamcrest.optional.OptionalMatchers.optionalWithValue) NestedGetPageFunction(com.liferay.apio.architect.alias.routes.NestedGetPageFunction) Set(java.util.Set) Test(org.junit.Test) PageItems(com.liferay.apio.architect.pagination.PageItems) Path(com.liferay.apio.architect.uri.Path) FORM_BUILDER_FUNCTION(com.liferay.apio.architect.routes.RoutesTestUtil.FORM_BUILDER_FUNCTION) REQUEST_PROVIDE_FUNCTION(com.liferay.apio.architect.routes.RoutesTestUtil.REQUEST_PROVIDE_FUNCTION) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) NestedCreateItemFunction(com.liferay.apio.architect.alias.routes.NestedCreateItemFunction) List(java.util.List) PAGINATION(com.liferay.apio.architect.routes.RoutesTestUtil.PAGINATION) Matchers.contains(org.hamcrest.Matchers.contains) Optional(java.util.Optional) Builder(com.liferay.apio.architect.routes.NestedCollectionRoutes.Builder) Collections(java.util.Collections) Path(com.liferay.apio.architect.uri.Path) Operation(com.liferay.apio.architect.operation.Operation) Map(java.util.Map)

Example 3 with POST

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

the class MockPageWriter method write.

/**
 * Writes a Collection of {@link RootModel}, with the hierarchy of embedded
 * models and multiple fields.
 *
 * @param httpHeaders the request's {@code HttpHeaders}
 * @param pageMessageMapper the {@link PageMessageMapper} to use for writing
 *        the JSON object
 */
public static JsonObject write(HttpHeaders httpHeaders, PageMessageMapper<RootModel> pageMessageMapper) {
    RequestInfo requestInfo = getRequestInfo(httpHeaders);
    Collection<RootModel> items = Arrays.asList(() -> "1", () -> "2", () -> "3");
    PageItems<RootModel> pageItems = new PageItems<>(items, 9);
    Pagination pagination = new Pagination(3, 2);
    Path path = new Path("name", "id");
    List<Operation> operations = Collections.singletonList(new Operation(createForm("c", "p"), POST, "create-operation"));
    Page<RootModel> page = new Page<>("root", pageItems, pagination, path, operations);
    PageWriter<RootModel> pageWriter = PageWriter.create(builder -> builder.page(page).pageMessageMapper(pageMessageMapper).pathFunction(MockWriterUtil::identifierToPath).resourceNameFunction(__ -> Optional.of("models")).representorFunction(MockWriterUtil::getRepresentorOptional).requestInfo(requestInfo).singleModelFunction(MockWriterUtil::getSingleModel).build());
    return new Gson().fromJson(pageWriter.write(), JsonObject.class);
}
Also used : Path(com.liferay.apio.architect.uri.Path) JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) MockFormCreator.createForm(com.liferay.apio.architect.test.util.form.MockFormCreator.createForm) RequestInfo(com.liferay.apio.architect.request.RequestInfo) Collection(java.util.Collection) PageItems(com.liferay.apio.architect.pagination.PageItems) Path(com.liferay.apio.architect.uri.Path) Operation(com.liferay.apio.architect.operation.Operation) PageMessageMapper(com.liferay.apio.architect.message.json.PageMessageMapper) List(java.util.List) PageWriter(com.liferay.apio.architect.writer.PageWriter) HttpHeaders(javax.ws.rs.core.HttpHeaders) Pagination(com.liferay.apio.architect.pagination.Pagination) Gson(com.google.gson.Gson) RootModel(com.liferay.apio.architect.test.util.model.RootModel) Page(com.liferay.apio.architect.pagination.Page) POST(com.liferay.apio.architect.operation.Method.POST) Optional(java.util.Optional) MockWriterUtil.getRequestInfo(com.liferay.apio.architect.test.util.writer.MockWriterUtil.getRequestInfo) Collections(java.util.Collections) RootModel(com.liferay.apio.architect.test.util.model.RootModel) Gson(com.google.gson.Gson) Page(com.liferay.apio.architect.pagination.Page) 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) Pagination(com.liferay.apio.architect.pagination.Pagination) PageItems(com.liferay.apio.architect.pagination.PageItems)

Aggregations

POST (com.liferay.apio.architect.operation.Method.POST)3 Operation (com.liferay.apio.architect.operation.Operation)3 Page (com.liferay.apio.architect.pagination.Page)3 PageItems (com.liferay.apio.architect.pagination.PageItems)3 Pagination (com.liferay.apio.architect.pagination.Pagination)3 Collections (java.util.Collections)3 List (java.util.List)3 Optional (java.util.Optional)3 Body (com.liferay.apio.architect.form.Body)2 FORM_BUILDER_FUNCTION (com.liferay.apio.architect.routes.RoutesTestUtil.FORM_BUILDER_FUNCTION)2 PAGINATION (com.liferay.apio.architect.routes.RoutesTestUtil.PAGINATION)2 REQUEST_PROVIDE_FUNCTION (com.liferay.apio.architect.routes.RoutesTestUtil.REQUEST_PROVIDE_FUNCTION)2 SingleModel (com.liferay.apio.architect.single.model.SingleModel)2 Path (com.liferay.apio.architect.uri.Path)2 OptionalMatchers.emptyOptional (com.spotify.hamcrest.optional.OptionalMatchers.emptyOptional)2 OptionalMatchers.optionalWithValue (com.spotify.hamcrest.optional.OptionalMatchers.optionalWithValue)2 Map (java.util.Map)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 CoreMatchers.hasItem (org.hamcrest.CoreMatchers.hasItem)2