Search in sources :

Example 6 with Operation

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

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

the class PageTest method setUp.

@Before
public void setUp() {
    _pageItems = new PageItems<>(Collections.singleton("apio"), 10);
    Pagination pagination = new Pagination(1, 4);
    _path = new Path("name", "id");
    _operations = Collections.singletonList(new Operation(POST, "operation"));
    _page = new Page<>("name", _pageItems, pagination, _path, _operations);
}
Also used : Path(com.liferay.apio.architect.uri.Path) Operation(com.liferay.apio.architect.operation.Operation) Before(org.junit.Before)

Example 8 with Operation

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

Example 9 with Operation

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

Operation (com.liferay.apio.architect.operation.Operation)9 Optional (java.util.Optional)7 SingleModel (com.liferay.apio.architect.single.model.SingleModel)6 List (java.util.List)6 Collections (java.util.Collections)5 Map (java.util.Map)5 JsonObject (com.google.gson.JsonObject)4 Page (com.liferay.apio.architect.pagination.Page)4 RequestInfo (com.liferay.apio.architect.request.RequestInfo)4 Path (com.liferay.apio.architect.uri.Path)4 Body (com.liferay.apio.architect.form.Body)3 Form (com.liferay.apio.architect.form.Form)3 JSONObjectBuilder (com.liferay.apio.architect.message.json.JSONObjectBuilder)3 POST (com.liferay.apio.architect.operation.Method.POST)3 PageItems (com.liferay.apio.architect.pagination.PageItems)3 Pagination (com.liferay.apio.architect.pagination.Pagination)3 FORM_BUILDER_FUNCTION (com.liferay.apio.architect.routes.RoutesTestUtil.FORM_BUILDER_FUNCTION)3 REQUEST_PROVIDE_FUNCTION (com.liferay.apio.architect.routes.RoutesTestUtil.REQUEST_PROVIDE_FUNCTION)3 WriterUtil.getFieldsWriter (com.liferay.apio.architect.writer.util.WriterUtil.getFieldsWriter)3 OptionalMatchers.emptyOptional (com.spotify.hamcrest.optional.OptionalMatchers.emptyOptional)3