Search in sources :

Example 1 with FunctionalList

use of com.liferay.apio.architect.list.FunctionalList in project com-liferay-apio-architect by liferay.

the class IsAFunctionalListTest method testInvalidFunctionalListUpdatesDescription.

@Test
public void testInvalidFunctionalListUpdatesDescription() {
    FunctionalList<Integer> functionalList = new FunctionalList<>(null, 1);
    Description description = new StringDescription();
    Matcher<FunctionalList<Integer>> functionalListMatcher = aFunctionalListThat(contains(2));
    functionalListMatcher.describeMismatch(functionalList, description);
    String expected = "was a functional list whose item 0: was <1>";
    assertThat(description.toString(), is(expected));
}
Also used : FunctionalList(com.liferay.apio.architect.list.FunctionalList) Description(org.hamcrest.Description) StringDescription(org.hamcrest.StringDescription) StringDescription(org.hamcrest.StringDescription) Test(org.junit.Test)

Example 2 with FunctionalList

use of com.liferay.apio.architect.list.FunctionalList in project com-liferay-apio-architect by liferay.

the class PageWriter method _writeNestedResources.

private <S, U> void _writeNestedResources(RepresentorFunction representorFunction, SingleModel<U> singleModel, JSONObjectBuilder itemJsonObjectBuilder, SingleModel<S> rootSingleModel, FunctionalList<String> embeddedPathElements) {
    Optional<Representor<U, ?>> representorOptional = unsafeCast(representorFunction.apply(singleModel.getResourceName()));
    representorOptional.ifPresent(_representor -> {
        Map<String, Representor<?, ?>> nested = _representor.getNested();
        nested.forEach((key, value) -> {
            Map<String, Function<U, ?>> nestedFunctions = _representor.getNestedFunctions();
            Function<U, ?> nestedMapper = nestedFunctions.get(key);
            Object mappedModel = nestedMapper.apply(singleModel.getModel());
            FunctionalList<String> embeddedNestedPathElements = new FunctionalList<>(embeddedPathElements, key);
            _writeItemEmbeddedModelFields(new SingleModel<>(mappedModel, "", Collections.emptyList()), embeddedNestedPathElements, itemJsonObjectBuilder, __ -> Optional.of(value), rootSingleModel);
        });
    });
}
Also used : ResourceNameFunction(com.liferay.apio.architect.writer.alias.ResourceNameFunction) SingleModelFunction(com.liferay.apio.architect.writer.alias.SingleModelFunction) Function(java.util.function.Function) PathFunction(com.liferay.apio.architect.writer.alias.PathFunction) RepresentorFunction(com.liferay.apio.architect.writer.alias.RepresentorFunction) FunctionalList(com.liferay.apio.architect.list.FunctionalList) JsonObject(com.google.gson.JsonObject) Representor(com.liferay.apio.architect.representor.Representor)

Example 3 with FunctionalList

use of com.liferay.apio.architect.list.FunctionalList 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 4 with FunctionalList

use of com.liferay.apio.architect.list.FunctionalList in project com-liferay-apio-architect by liferay.

the class SingleModelWriter method _writeNestedResources.

private <S> void _writeNestedResources(RepresentorFunction representorFunction, SingleModel<S> singleModel, FunctionalList<String> embeddedPathElements) {
    Optional<Representor<S, ?>> representorOptional = unsafeCast(representorFunction.apply(singleModel.getResourceName()));
    representorOptional.ifPresent(_representor -> {
        Map<String, Representor<?, ?>> nested = _representor.getNested();
        nested.forEach((key, value) -> {
            Map<String, Function<S, ?>> nestedFunctions = _representor.getNestedFunctions();
            Function<S, ?> nestedMapper = nestedFunctions.get(key);
            Object mappedModel = nestedMapper.apply(singleModel.getModel());
            FunctionalList<String> embeddedNestedPathElements = new FunctionalList<>(embeddedPathElements, key);
            writeEmbeddedModelFields(new SingleModel<>(mappedModel, "", Collections.emptyList()), embeddedNestedPathElements, __ -> Optional.of(value));
        });
    });
}
Also used : ResourceNameFunction(com.liferay.apio.architect.writer.alias.ResourceNameFunction) RepresentorFunction(com.liferay.apio.architect.writer.alias.RepresentorFunction) SingleModelFunction(com.liferay.apio.architect.writer.alias.SingleModelFunction) Function(java.util.function.Function) PathFunction(com.liferay.apio.architect.writer.alias.PathFunction) FunctionalList(com.liferay.apio.architect.list.FunctionalList) JsonObject(com.google.gson.JsonObject) Representor(com.liferay.apio.architect.representor.Representor)

Aggregations

FunctionalList (com.liferay.apio.architect.list.FunctionalList)4 JsonObject (com.google.gson.JsonObject)2 Representor (com.liferay.apio.architect.representor.Representor)2 PathFunction (com.liferay.apio.architect.writer.alias.PathFunction)2 RepresentorFunction (com.liferay.apio.architect.writer.alias.RepresentorFunction)2 ResourceNameFunction (com.liferay.apio.architect.writer.alias.ResourceNameFunction)2 SingleModelFunction (com.liferay.apio.architect.writer.alias.SingleModelFunction)2 Function (java.util.function.Function)2 Test (org.junit.Test)2 SingleModel (com.liferay.apio.architect.single.model.SingleModel)1 Path (com.liferay.apio.architect.uri.Path)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 Description (org.hamcrest.Description)1 StringDescription (org.hamcrest.StringDescription)1