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));
}
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);
});
});
}
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"))));
}
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));
});
});
}
Aggregations