Search in sources :

Example 6 with SingleModel

use of com.liferay.apio.architect.single.model.SingleModel 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 SingleModel

use of com.liferay.apio.architect.single.model.SingleModel 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 8 with SingleModel

use of com.liferay.apio.architect.single.model.SingleModel in project com-liferay-apio-architect by liferay.

the class FieldsWriterTest method testWriteEmbeddedRelatedModelsWithEmbeddedPredicate.

@SuppressWarnings("unchecked")
@Test
public void testWriteEmbeddedRelatedModelsWithEmbeddedPredicate() {
    List<String> linkedRelatedModelURLs = new ArrayList<>();
    List<String> embeddedRelatedModelURLs = new ArrayList<>();
    List<FunctionalList<String>> firstEmbeddedPathElementsList = new ArrayList<>();
    List<FunctionalList<String>> secondEmbeddedPathElementsList = new ArrayList<>();
    List<FunctionalList<String>> thirdEmbeddedPathElementsList = new ArrayList<>();
    List<SingleModel> singleModels = new ArrayList<>();
    Function<SingleModel<?>, Optional<Path>> pathFunction = Mockito.mock(Function.class);
    Mockito.when(_requestInfo.getEmbedded()).thenReturn("first.embedded2"::equals);
    Mockito.when(pathFunction.apply(Mockito.any())).thenReturn(Optional.of(new Path("name1", "id1")), Optional.of(new Path("name2", "id2")), Optional.of(new Path("name3", "id3")), Optional.of(new Path("name4", "id4")));
    _fieldsWriter.writeRelatedModels(pathFunction, (singleModel, embeddedPathElements) -> {
        singleModels.add(singleModel);
        firstEmbeddedPathElementsList.add(embeddedPathElements);
    }, (url, embeddedPathElements) -> {
        linkedRelatedModelURLs.add(url);
        secondEmbeddedPathElementsList.add(embeddedPathElements);
    }, (url, embeddedPathElements) -> {
        embeddedRelatedModelURLs.add(url);
        thirdEmbeddedPathElementsList.add(embeddedPathElements);
    });
    assertThat(singleModels, hasSize(equalTo(1)));
    SingleModel singleModel = singleModels.get(0);
    assertThat(singleModel.getModel(), is(instanceOf(FirstEmbeddedModel.class)));
    assertThat(linkedRelatedModelURLs, hasSize(equalTo(3)));
    assertThat(linkedRelatedModelURLs, contains("www.liferay.com/p/name1/id1", "www.liferay.com/p/name3/id3", "www.liferay.com/p/name4/id4"));
    assertThat(embeddedRelatedModelURLs, hasSize(equalTo(1)));
    assertThat(embeddedRelatedModelURLs, contains("www.liferay.com/p/name2/id2"));
    assertThat(firstEmbeddedPathElementsList, hasSize(equalTo(1)));
    assertThat(firstEmbeddedPathElementsList, contains(aFunctionalListThat(contains("first", "embedded2"))));
    assertThat(secondEmbeddedPathElementsList, hasSize(equalTo(3)));
    assertThat(secondEmbeddedPathElementsList, contains(aFunctionalListThat(contains("first", "embedded1")), aFunctionalListThat(contains("first", "linked1")), aFunctionalListThat(contains("first", "linked2"))));
    assertThat(thirdEmbeddedPathElementsList, hasSize(equalTo(1)));
    assertThat(thirdEmbeddedPathElementsList, contains(aFunctionalListThat(contains("first", "embedded2"))));
}
Also used : SingleModel(com.liferay.apio.architect.single.model.SingleModel) Path(com.liferay.apio.architect.uri.Path) FunctionalList(com.liferay.apio.architect.list.FunctionalList) Optional(java.util.Optional) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 9 with SingleModel

use of com.liferay.apio.architect.single.model.SingleModel in project com-liferay-apio-architect by liferay.

the class FieldsWriterTest method testGetSingleModel.

@Test
public void testGetSingleModel() {
    SingleModel<Integer> parentSingleModel = new SingleModel<>(3, "", Collections.emptyList());
    RelatedModel<Integer, String> relatedModel = new RelatedModel<>("key", FirstEmbeddedId.class, String::valueOf);
    Optional<SingleModel<FirstEmbeddedModel>> optional = FieldsWriter.getSingleModel(relatedModel, parentSingleModel, MockWriterUtil::getSingleModel);
    assertThat(optional, is(optionalWithValue()));
    optional.ifPresent(singleModel -> {
        assertThat(singleModel.getResourceName(), is("first"));
        FirstEmbeddedModel firstEmbeddedModel = singleModel.getModel();
        assertThat(firstEmbeddedModel.getId(), is("3"));
    });
}
Also used : SingleModel(com.liferay.apio.architect.single.model.SingleModel) RelatedModel(com.liferay.apio.architect.related.RelatedModel) FirstEmbeddedModel(com.liferay.apio.architect.test.util.model.FirstEmbeddedModel) MockWriterUtil(com.liferay.apio.architect.test.util.writer.MockWriterUtil) Test(org.junit.Test)

Aggregations

SingleModel (com.liferay.apio.architect.single.model.SingleModel)9 Optional (java.util.Optional)8 Operation (com.liferay.apio.architect.operation.Operation)6 Collections (java.util.Collections)5 List (java.util.List)5 Map (java.util.Map)5 Test (org.junit.Test)5 RequestInfo (com.liferay.apio.architect.request.RequestInfo)4 JsonObject (com.google.gson.JsonObject)3 Body (com.liferay.apio.architect.form.Body)3 FunctionalList (com.liferay.apio.architect.list.FunctionalList)3 SingleModelMessageMapper (com.liferay.apio.architect.message.json.SingleModelMessageMapper)3 Page (com.liferay.apio.architect.pagination.Page)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 Unsafe.unsafeCast (com.liferay.apio.architect.unsafe.Unsafe.unsafeCast)3 OptionalMatchers.emptyOptional (com.spotify.hamcrest.optional.OptionalMatchers.emptyOptional)3 OptionalMatchers.optionalWithValue (com.spotify.hamcrest.optional.OptionalMatchers.optionalWithValue)3 Set (java.util.Set)3 TreeSet (java.util.TreeSet)3